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 |
entity* txtprt_parser::read (string filename) { |
14 |
double tmpn; |
15 |
string tmps; |
16 |
int i, o; |
17 |
|
18 |
entity *e = new entity; |
19 |
entity_container *ec = new entity_container; |
20 |
e->set (ec); |
21 |
|
22 |
ifs.open (filename.c_str(), ifstream::in); |
23 |
while (ifs.good ()) { |
24 |
char c[1024]; |
25 |
ifs.getline (c, 1024); |
26 |
line_data += (string) c + "\n"; |
27 |
} |
28 |
ifs.close(); |
29 |
|
30 |
double objs; |
31 |
|
32 |
match_section ('O', objs); |
33 |
|
34 |
for (o = 0; o < objs; o++) { |
35 |
double c1, c2, c3, c4, l0, l1, l2; |
36 |
match_section ('M', tmps); |
37 |
match_section ('L', tmps); |
38 |
match_number (l0); match_space (); match_number (l1); match_space (); match_number (l2); match_nl (); |
39 |
match_section ('T', tmps); |
40 |
|
41 |
material m; |
42 |
match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl (); |
43 |
m.diffuse = colour (c1, c2, c3, c4); |
44 |
match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl (); |
45 |
m.specular = colour (c1, c2, c3, c4); |
46 |
match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl (); |
47 |
m.emission = colour (c1, c2, c3, c4); |
48 |
match_number (c1); match_nl (); |
49 |
m.shininess = (GLfloat) c1; |
50 |
|
51 |
match_section ('V', tmpn); |
52 |
if (tmpn <= 0) |
53 |
throw txtprt_i_exception ("No Vertices?!"); |
54 |
|
55 |
vector<vertex2d> verts; |
56 |
for (i = 0; i < tmpn; i++) { |
57 |
double x, y, z; |
58 |
match_number (x); |
59 |
match_space (); |
60 |
match_number (y); |
61 |
match_space (); |
62 |
match_number (z); |
63 |
match_nl (); |
64 |
verts.push_back (vertex2d (point (l0 + x, l1 + y, l2 + z), vec3 (0, 0, 1))); |
65 |
// FIXME: propably vertex coloUrs ? |
66 |
} |
67 |
match_section ('N', tmpn); |
68 |
if (tmpn > verts.size()) { throw txtprt_i_exception ("Too many vertex normals!!"); } |
69 |
for (i = 0; i < tmpn; i++) { |
70 |
double x, y, z; |
71 |
match_number (x); |
72 |
match_space (); |
73 |
match_number (y); |
74 |
match_space (); |
75 |
match_number (z); |
76 |
match_nl (); |
77 |
verts[i].n = vec3 (x, y, z); |
78 |
} |
79 |
char tmpc; |
80 |
match_asection (tmpc, tmpn); |
81 |
if (tmpc == 'U') { |
82 |
for (i = 0; i < tmpn; i++) { |
83 |
double u, v; |
84 |
match_number (u); |
85 |
match_space (); |
86 |
match_number (v); |
87 |
match_nl (); |
88 |
verts[i].t = texc (u,v); |
89 |
} |
90 |
match_section ('A', tmpn); |
91 |
} |
92 |
vector<vertex2d> vd; |
93 |
for (i = 0; i < tmpn; i++) { |
94 |
double a, b, c; |
95 |
match_number (a); |
96 |
if (verts.size () < a) |
97 |
throw txtprt_i_exception ("Vertex a out of range!"); |
98 |
match_space (); |
99 |
match_number (b); |
100 |
if (verts.size () < b) |
101 |
throw txtprt_i_exception ("Vertex b out of range!"); |
102 |
match_space (); |
103 |
match_number (c); |
104 |
if (verts.size () < c) |
105 |
throw txtprt_i_exception ("Vertex c out of range!"); |
106 |
match_nl (); |
107 |
double smooth_fl; |
108 |
match_number (smooth_fl); match_nl (); |
109 |
if (!smooth_fl) { |
110 |
point ap = verts[(int)a].p; |
111 |
point bp = verts[(int)b].p; |
112 |
point cp = verts[(int)c].p; |
113 |
vec3 nv = normalize (cross (vec3 (cp.x - bp.x, cp.y - bp.y, cp.z - bp.z), |
114 |
vec3 (ap.x - bp.x, ap.y - bp.y, ap.z - bp.z))); |
115 |
|
116 |
verts[(int)a].n = nv; |
117 |
verts[(int)b].n = nv; |
118 |
verts[(int)c].n = nv; |
119 |
} |
120 |
vd.push_back (verts[(int)a]); |
121 |
vd.push_back (verts[(int)b]); |
122 |
vd.push_back (verts[(int)c]); |
123 |
} |
124 |
|
125 |
entity_triangles *tri = new entity_triangles; |
126 |
tri->m = m; |
127 |
tri->set (vd); |
128 |
ec->add (tri); |
129 |
} |
130 |
return e; |
131 |
} |