1 |
#include <iostream> |
2 |
#include <fstream> |
3 |
#include <string> |
4 |
#include <vector> |
5 |
#include "txtprt_import.h" |
6 |
#include "entity.h" |
7 |
#include "util.h" |
8 |
|
9 |
using namespace std; |
10 |
|
11 |
// FIXME: In case of failed loading a memory leak can happen |
12 |
|
13 |
geometry * |
14 |
txtprt_parser::read (string filename) |
15 |
{ |
16 |
double tmpn; |
17 |
string tmps; |
18 |
int i, o; |
19 |
|
20 |
geometry_container *objs = new geometry_container; |
21 |
|
22 |
ifs.open (filename.c_str (), ifstream::in); |
23 |
|
24 |
while (ifs.good ()) |
25 |
{ |
26 |
char c[1024]; |
27 |
ifs.getline (c, 1024); |
28 |
line_data += (string) c + "\n"; |
29 |
} |
30 |
|
31 |
ifs.close (); |
32 |
|
33 |
double nobj; |
34 |
|
35 |
match_section ('O', nobj); |
36 |
|
37 |
for (o = 0; o < nobj; o++) |
38 |
{ |
39 |
double c1, c2, c3, c4, l0, l1, l2; |
40 |
match_section ('M', tmps); |
41 |
match_section ('L', tmps); |
42 |
match_number (l0); |
43 |
match_space (); |
44 |
match_number (l1); |
45 |
match_space (); |
46 |
match_number (l2); |
47 |
match_nl (); |
48 |
match_section ('T', tmps); |
49 |
|
50 |
match_number (c1); |
51 |
match_space (); |
52 |
match_number (c2); |
53 |
match_space (); |
54 |
match_number (c3); |
55 |
match_space (); |
56 |
match_number (c4); |
57 |
match_nl (); |
58 |
//m->diffuse = colour (c1, c2, c3, c4); |
59 |
match_number (c1); |
60 |
match_space (); |
61 |
match_number (c2); |
62 |
match_space (); |
63 |
match_number (c3); |
64 |
match_space (); |
65 |
match_number (c4); |
66 |
match_nl (); |
67 |
//m->specular = colour (c1, c2, c3, c4); |
68 |
match_number (c1); |
69 |
match_space (); |
70 |
match_number (c2); |
71 |
match_space (); |
72 |
match_number (c3); |
73 |
match_space (); |
74 |
match_number (c4); |
75 |
match_nl (); |
76 |
//m->emission = colour (c1, c2, c3, c4); |
77 |
match_number (c1); |
78 |
match_nl (); |
79 |
//m->shininess = (GLfloat) c1; |
80 |
|
81 |
match_section ('V', tmpn); |
82 |
if (tmpn <= 0) |
83 |
throw txtprt_i_exception ("No Vertices?!"); |
84 |
|
85 |
vector < vertex_t2f_n3f_v3f > verts; |
86 |
for (i = 0; i < tmpn; i++) |
87 |
{ |
88 |
double x, y, z; |
89 |
match_number (x); |
90 |
match_space (); |
91 |
match_number (y); |
92 |
match_space (); |
93 |
match_number (z); |
94 |
match_nl (); |
95 |
verts.push_back (vertex_t2f_n3f_v3f (point (l0 + x, l1 + y, l2 + z), vec3 (0, 0, 1))); |
96 |
// FIXME: propably vertex colours ? |
97 |
} |
98 |
|
99 |
match_section ('N', tmpn); |
100 |
|
101 |
if (tmpn > verts.size ()) |
102 |
{ |
103 |
throw txtprt_i_exception ("Too many vertex normals!!"); |
104 |
} |
105 |
|
106 |
for (i = 0; i < tmpn; i++) |
107 |
{ |
108 |
double x, y, z; |
109 |
match_number (x); |
110 |
match_space (); |
111 |
match_number (y); |
112 |
match_space (); |
113 |
match_number (z); |
114 |
match_nl (); |
115 |
verts[i].n = vec3 (x, y, z); |
116 |
} |
117 |
|
118 |
match_section ('A', tmpn); |
119 |
|
120 |
vector<vertex_t2f_n3f_v3f> vd; |
121 |
|
122 |
for (i = 0; i < tmpn; i++) |
123 |
{ |
124 |
double a, b, c; |
125 |
match_number (a); |
126 |
|
127 |
if (verts.size () < a) |
128 |
throw txtprt_i_exception ("Vertex a out of range!"); |
129 |
|
130 |
match_space (); |
131 |
match_number (b); |
132 |
|
133 |
if (verts.size () < b) |
134 |
throw txtprt_i_exception ("Vertex b out of range!"); |
135 |
|
136 |
match_space (); |
137 |
match_number (c); |
138 |
|
139 |
if (verts.size () < c) |
140 |
throw txtprt_i_exception ("Vertex c out of range!"); |
141 |
|
142 |
match_nl (); |
143 |
double smooth_fl; |
144 |
match_number (smooth_fl); |
145 |
match_nl (); |
146 |
double u, v; |
147 |
match_number (u); |
148 |
match_space (); |
149 |
match_number (v); |
150 |
match_nl (); |
151 |
verts[(int) a].t = tex2 (1 - u, 1 - v); |
152 |
match_number (u); |
153 |
match_space (); |
154 |
match_number (v); |
155 |
match_nl (); |
156 |
verts[(int) b].t = tex2 (1 - u, 1 - v); |
157 |
match_number (u); |
158 |
match_space (); |
159 |
match_number (v); |
160 |
match_nl (); |
161 |
verts[(int) c].t = tex2 (1 - u, 1 - v); |
162 |
|
163 |
if (!smooth_fl) |
164 |
{ |
165 |
point ap = verts[(int) a].v; |
166 |
point bp = verts[(int) b].v; |
167 |
point cp = verts[(int) c].v; |
168 |
vec3 nv = normalize (cross (vec3 (cp.x - bp.x, cp.y - bp.y, cp.z - bp.z), |
169 |
vec3 (ap.x - bp.x, ap.y - bp.y, ap.z - bp.z))); |
170 |
|
171 |
verts[(int) a].n = nv; |
172 |
verts[(int) b].n = nv; |
173 |
verts[(int) c].n = nv; |
174 |
} |
175 |
|
176 |
vd.push_back (verts[(int) a]); |
177 |
vd.push_back (verts[(int) b]); |
178 |
vd.push_back (verts[(int) c]); |
179 |
} |
180 |
|
181 |
geometry_triangles *tri = new geometry_triangles; |
182 |
tri->m = testmat; |
183 |
tri->set (vd); |
184 |
objs->add (tri); |
185 |
} |
186 |
|
187 |
return objs; |
188 |
} |
189 |
|