1 |
root |
1.1 |
#include <iostream> |
2 |
|
|
#include <fstream> |
3 |
|
|
#include <string> |
4 |
|
|
#include <vector> |
5 |
|
|
#include "txtprt_import.h" |
6 |
|
|
#include "entity.h" |
7 |
root |
1.3 |
#include "util.h" |
8 |
root |
1.1 |
|
9 |
|
|
using namespace std; |
10 |
|
|
|
11 |
|
|
// FIXME: In case of failed loading a memory leak can happen |
12 |
|
|
|
13 |
root |
1.11 |
geometry * |
14 |
|
|
txtprt_parser::read (string filename) |
15 |
|
|
{ |
16 |
|
|
double tmpn; |
17 |
|
|
string tmps; |
18 |
|
|
int i, o; |
19 |
root |
1.1 |
|
20 |
root |
1.11 |
geometry_container *objs = new geometry_container; |
21 |
|
|
|
22 |
|
|
ifs.open (filename.c_str (), ifstream::in); |
23 |
|
|
|
24 |
|
|
while (ifs.good ()) |
25 |
|
|
{ |
26 |
root |
1.1 |
char c[1024]; |
27 |
|
|
ifs.getline (c, 1024); |
28 |
|
|
line_data += (string) c + "\n"; |
29 |
root |
1.11 |
} |
30 |
root |
1.1 |
|
31 |
root |
1.11 |
ifs.close (); |
32 |
root |
1.6 |
|
33 |
root |
1.11 |
double nobj; |
34 |
root |
1.6 |
|
35 |
root |
1.11 |
match_section ('O', nobj); |
36 |
|
|
|
37 |
|
|
for (o = 0; o < nobj; o++) |
38 |
|
|
{ |
39 |
root |
1.6 |
double c1, c2, c3, c4, l0, l1, l2; |
40 |
|
|
match_section ('M', tmps); |
41 |
|
|
match_section ('L', tmps); |
42 |
root |
1.11 |
match_number (l0); |
43 |
|
|
match_space (); |
44 |
|
|
match_number (l1); |
45 |
|
|
match_space (); |
46 |
|
|
match_number (l2); |
47 |
|
|
match_nl (); |
48 |
root |
1.6 |
match_section ('T', tmps); |
49 |
|
|
|
50 |
root |
1.11 |
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 |
root |
1.16 |
//m->diffuse = colour (c1, c2, c3, c4); |
59 |
root |
1.11 |
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 |
root |
1.16 |
//m->specular = colour (c1, c2, c3, c4); |
68 |
root |
1.11 |
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 |
root |
1.16 |
//m->emission = colour (c1, c2, c3, c4); |
77 |
root |
1.11 |
match_number (c1); |
78 |
|
|
match_nl (); |
79 |
root |
1.16 |
//m->shininess = (GLfloat) c1; |
80 |
root |
1.11 |
|
81 |
root |
1.6 |
match_section ('V', tmpn); |
82 |
root |
1.11 |
if (tmpn <= 0) |
83 |
|
|
throw txtprt_i_exception ("No Vertices?!"); |
84 |
root |
1.6 |
|
85 |
root |
1.13 |
vector < vertex_t2f_n3f_v3f > verts; |
86 |
root |
1.11 |
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 |
root |
1.15 |
verts.push_back (vertex_t2f_n3f_v3f (point (l0 + x, l1 + y, l2 + z), vec3 (0, 0, 1))); |
96 |
|
|
// FIXME: propably vertex colours ? |
97 |
root |
1.11 |
} |
98 |
root |
1.15 |
|
99 |
root |
1.6 |
match_section ('N', tmpn); |
100 |
root |
1.15 |
|
101 |
root |
1.11 |
if (tmpn > verts.size ()) |
102 |
|
|
{ |
103 |
|
|
throw txtprt_i_exception ("Too many vertex normals!!"); |
104 |
|
|
} |
105 |
root |
1.15 |
|
106 |
root |
1.11 |
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 |
root |
1.15 |
vector<vertex_t2f_n3f_v3f> vd; |
121 |
|
|
|
122 |
root |
1.11 |
for (i = 0; i < tmpn; i++) |
123 |
|
|
{ |
124 |
|
|
double a, b, c; |
125 |
|
|
match_number (a); |
126 |
root |
1.15 |
|
127 |
root |
1.11 |
if (verts.size () < a) |
128 |
|
|
throw txtprt_i_exception ("Vertex a out of range!"); |
129 |
root |
1.15 |
|
130 |
root |
1.11 |
match_space (); |
131 |
|
|
match_number (b); |
132 |
root |
1.15 |
|
133 |
root |
1.11 |
if (verts.size () < b) |
134 |
|
|
throw txtprt_i_exception ("Vertex b out of range!"); |
135 |
root |
1.15 |
|
136 |
root |
1.11 |
match_space (); |
137 |
|
|
match_number (c); |
138 |
root |
1.15 |
|
139 |
root |
1.11 |
if (verts.size () < c) |
140 |
|
|
throw txtprt_i_exception ("Vertex c out of range!"); |
141 |
root |
1.15 |
|
142 |
root |
1.11 |
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 |
root |
1.13 |
verts[(int) a].t = tex2 (1 - u, 1 - v); |
152 |
root |
1.11 |
match_number (u); |
153 |
|
|
match_space (); |
154 |
|
|
match_number (v); |
155 |
|
|
match_nl (); |
156 |
root |
1.13 |
verts[(int) b].t = tex2 (1 - u, 1 - v); |
157 |
root |
1.11 |
match_number (u); |
158 |
|
|
match_space (); |
159 |
|
|
match_number (v); |
160 |
|
|
match_nl (); |
161 |
root |
1.13 |
verts[(int) c].t = tex2 (1 - u, 1 - v); |
162 |
root |
1.11 |
|
163 |
|
|
if (!smooth_fl) |
164 |
|
|
{ |
165 |
root |
1.14 |
point ap = verts[(int) a].v; |
166 |
|
|
point bp = verts[(int) b].v; |
167 |
|
|
point cp = verts[(int) c].v; |
168 |
root |
1.11 |
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 |
root |
1.17 |
tri->m = testmat; |
183 |
root |
1.9 |
tri->set (vd); |
184 |
root |
1.11 |
objs->add (tri); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
return objs; |
188 |
root |
1.1 |
} |
189 |
root |
1.11 |
|