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 |
|
|
entity* txtprt_parser::read (string filename) { |
14 |
|
|
double tmpn; |
15 |
|
|
string tmps; |
16 |
|
|
int i; |
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 |
|
|
match_section ('O', tmps); |
32 |
|
|
match_section ('M', tmps); |
33 |
root |
1.3 |
match_section ('T', tmps); |
34 |
|
|
|
35 |
|
|
material m; |
36 |
|
|
m.diffuse = colour (0, 0, 0, 0); |
37 |
|
|
double c1, c2, c3, c4; |
38 |
|
|
match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl (); |
39 |
|
|
m.diffuse = colour (c1, c2, c3, c4); |
40 |
|
|
match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl (); |
41 |
|
|
m.specular = colour (c1, c2, c3, c4); |
42 |
|
|
match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl (); |
43 |
|
|
m.emission = colour (c1, c2, c3, c4); |
44 |
|
|
|
45 |
root |
1.1 |
match_section ('V', tmpn); |
46 |
|
|
if (tmpn <= 0) |
47 |
|
|
throw txtprt_i_exception ("No Vertices?!"); |
48 |
|
|
|
49 |
|
|
vector<vertex2d> verts; |
50 |
|
|
for (i = 0; i < tmpn; i++) { |
51 |
|
|
double x, y, z; |
52 |
|
|
match_number (x); |
53 |
|
|
match_space (); |
54 |
|
|
match_number (y); |
55 |
|
|
match_space (); |
56 |
|
|
match_number (z); |
57 |
|
|
match_nl (); |
58 |
root |
1.3 |
verts.push_back (vertex2d (point (x, y, z), vec3 (0, 0, 1))); |
59 |
root |
1.1 |
// FIXME: propably vertex coloUrs ? |
60 |
|
|
} |
61 |
|
|
match_section ('N', tmpn); |
62 |
|
|
if (tmpn > verts.size()) { throw txtprt_i_exception ("Too many vertex normals!!"); } |
63 |
|
|
for (i = 0; i < tmpn; i++) { |
64 |
|
|
double x, y, z; |
65 |
|
|
match_number (x); |
66 |
|
|
match_space (); |
67 |
|
|
match_number (y); |
68 |
|
|
match_space (); |
69 |
|
|
match_number (z); |
70 |
|
|
match_nl (); |
71 |
|
|
verts[i].n = vec3 (x, y, z); |
72 |
|
|
} |
73 |
|
|
char tmpc; |
74 |
|
|
match_asection (tmpc, tmpn); |
75 |
|
|
if (tmpc == 'U') { |
76 |
|
|
for (i = 0; i < tmpn; i++) { |
77 |
|
|
double u, v; |
78 |
|
|
match_number (u); |
79 |
|
|
match_space (); |
80 |
|
|
match_number (v); |
81 |
|
|
match_nl (); |
82 |
|
|
verts[i].t = texc (u,v); |
83 |
|
|
} |
84 |
|
|
match_section ('A', tmpn); |
85 |
|
|
} |
86 |
|
|
for (i = 0; i < tmpn; i++) { |
87 |
|
|
double a, b, c; |
88 |
|
|
match_number (a); |
89 |
|
|
if (verts.size () < a) |
90 |
root |
1.2 |
throw txtprt_i_exception ("Vertex a out of range!"); |
91 |
root |
1.1 |
match_space (); |
92 |
|
|
match_number (b); |
93 |
|
|
if (verts.size () < b) |
94 |
root |
1.2 |
throw txtprt_i_exception ("Vertex b out of range!"); |
95 |
root |
1.1 |
match_space (); |
96 |
|
|
match_number (c); |
97 |
|
|
if (verts.size () < c) |
98 |
root |
1.2 |
throw txtprt_i_exception ("Vertex c out of range!"); |
99 |
root |
1.1 |
match_nl (); |
100 |
|
|
entity_triangles *tri = new entity_triangles; |
101 |
|
|
tri->push_back (verts[(int)a]); |
102 |
|
|
tri->push_back (verts[(int)b]); |
103 |
|
|
tri->push_back (verts[(int)c]); |
104 |
root |
1.3 |
tri->m = m; |
105 |
root |
1.1 |
ec->add (tri); |
106 |
|
|
} |
107 |
|
|
return e; |
108 |
|
|
} |