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 |
|
|
|
8 |
|
|
using namespace std; |
9 |
|
|
|
10 |
|
|
// FIXME: In case of failed loading a memory leak can happen |
11 |
|
|
|
12 |
|
|
entity* txtprt_parser::read (string filename) { |
13 |
|
|
double tmpn; |
14 |
|
|
string tmps; |
15 |
|
|
int i; |
16 |
|
|
|
17 |
|
|
entity *e = new entity; |
18 |
|
|
entity_container *ec = new entity_container; |
19 |
|
|
e->set (ec); |
20 |
|
|
e->sec.x = e->sec.y = e->sec.z = 0; |
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 |
|
|
cout << "GOT" << line_data << endl; |
29 |
|
|
ifs.close(); |
30 |
|
|
|
31 |
|
|
match_section ('O', tmps); |
32 |
|
|
match_section ('M', tmps); |
33 |
|
|
match_section ('V', tmpn); |
34 |
|
|
if (tmpn <= 0) |
35 |
|
|
throw txtprt_i_exception ("No Vertices?!"); |
36 |
|
|
|
37 |
|
|
vector<vertex2d> verts; |
38 |
|
|
for (i = 0; i < tmpn; i++) { |
39 |
|
|
double x, y, z; |
40 |
|
|
match_number (x); |
41 |
|
|
match_space (); |
42 |
|
|
match_number (y); |
43 |
|
|
match_space (); |
44 |
|
|
match_number (z); |
45 |
|
|
match_nl (); |
46 |
|
|
verts.push_back (vertex2d (colour (1, 1, 1), point (x, y, z), vec3 (0, 0, 1))); |
47 |
|
|
// FIXME: propably vertex coloUrs ? |
48 |
|
|
} |
49 |
|
|
match_section ('N', tmpn); |
50 |
|
|
if (tmpn > verts.size()) { throw txtprt_i_exception ("Too many vertex normals!!"); } |
51 |
|
|
for (i = 0; i < tmpn; i++) { |
52 |
|
|
double x, y, z; |
53 |
|
|
match_number (x); |
54 |
|
|
match_space (); |
55 |
|
|
match_number (y); |
56 |
|
|
match_space (); |
57 |
|
|
match_number (z); |
58 |
|
|
match_nl (); |
59 |
|
|
verts[i].n = vec3 (x, y, z); |
60 |
|
|
} |
61 |
|
|
char tmpc; |
62 |
|
|
match_asection (tmpc, tmpn); |
63 |
|
|
if (tmpc == 'U') { |
64 |
|
|
for (i = 0; i < tmpn; i++) { |
65 |
|
|
double u, v; |
66 |
|
|
match_number (u); |
67 |
|
|
match_space (); |
68 |
|
|
match_number (v); |
69 |
|
|
match_nl (); |
70 |
|
|
verts[i].t = texc (u,v); |
71 |
|
|
} |
72 |
|
|
match_section ('A', tmpn); |
73 |
|
|
} |
74 |
|
|
for (i = 0; i < tmpn; i++) { |
75 |
|
|
double a, b, c; |
76 |
|
|
match_number (a); |
77 |
|
|
if (verts.size () < a) |
78 |
root |
1.2 |
throw txtprt_i_exception ("Vertex a out of range!"); |
79 |
root |
1.1 |
match_space (); |
80 |
|
|
match_number (b); |
81 |
|
|
if (verts.size () < b) |
82 |
root |
1.2 |
throw txtprt_i_exception ("Vertex b out of range!"); |
83 |
root |
1.1 |
match_space (); |
84 |
|
|
match_number (c); |
85 |
|
|
if (verts.size () < c) |
86 |
root |
1.2 |
throw txtprt_i_exception ("Vertex c out of range!"); |
87 |
root |
1.1 |
match_nl (); |
88 |
|
|
entity_triangles *tri = new entity_triangles; |
89 |
|
|
tri->push_back (verts[(int)a]); |
90 |
|
|
tri->push_back (verts[(int)b]); |
91 |
|
|
tri->push_back (verts[(int)c]); |
92 |
|
|
ec->add (tri); |
93 |
|
|
} |
94 |
|
|
return e; |
95 |
|
|
} |