| 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 |
root |
1.6 |
int i, o; |
| 17 |
root |
1.1 |
|
| 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 |
root |
1.6 |
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 |
|
|
entity_triangles *tri = new entity_triangles; |
| 93 |
|
|
ec->add (tri); |
| 94 |
|
|
tri->m = m; |
| 95 |
root |
1.1 |
for (i = 0; i < tmpn; i++) { |
| 96 |
root |
1.6 |
double a, b, c; |
| 97 |
|
|
match_number (a); |
| 98 |
|
|
if (verts.size () < a) |
| 99 |
|
|
throw txtprt_i_exception ("Vertex a out of range!"); |
| 100 |
|
|
match_space (); |
| 101 |
|
|
match_number (b); |
| 102 |
|
|
if (verts.size () < b) |
| 103 |
|
|
throw txtprt_i_exception ("Vertex b out of range!"); |
| 104 |
root |
1.1 |
match_space (); |
| 105 |
root |
1.6 |
match_number (c); |
| 106 |
|
|
if (verts.size () < c) |
| 107 |
|
|
throw txtprt_i_exception ("Vertex c out of range!"); |
| 108 |
root |
1.1 |
match_nl (); |
| 109 |
root |
1.8 |
double smooth_fl; |
| 110 |
|
|
match_number (smooth_fl); match_nl (); |
| 111 |
|
|
if (!smooth_fl) { |
| 112 |
|
|
point ap = verts[(int)a].p; |
| 113 |
|
|
point bp = verts[(int)b].p; |
| 114 |
|
|
point cp = verts[(int)c].p; |
| 115 |
|
|
vec3 nv = cross ( vec3 (cp.x - bp.x, cp.y - bp.y, cp.z - bp.z), |
| 116 |
|
|
vec3 (ap.x - bp.x, ap.y - bp.y, ap.z - bp.z)); |
| 117 |
|
|
|
| 118 |
|
|
verts[(int)a].n = nv; |
| 119 |
|
|
verts[(int)b].n = nv; |
| 120 |
|
|
verts[(int)c].n = nv; |
| 121 |
|
|
} |
| 122 |
root |
1.6 |
tri->push_back (verts[(int)a]); |
| 123 |
|
|
tri->push_back (verts[(int)b]); |
| 124 |
|
|
tri->push_back (verts[(int)c]); |
| 125 |
root |
1.1 |
} |
| 126 |
|
|
} |
| 127 |
|
|
return e; |
| 128 |
|
|
} |