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