| 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 |
root |
1.11 |
geometry * |
| 14 |
|
|
txtprt_parser::read (string filename) |
| 15 |
|
|
{ |
| 16 |
|
|
double tmpn; |
| 17 |
|
|
string tmps; |
| 18 |
|
|
int i, o; |
| 19 |
root |
1.1 |
|
| 20 |
root |
1.11 |
geometry_container *objs = new geometry_container; |
| 21 |
|
|
|
| 22 |
|
|
ifs.open (filename.c_str (), ifstream::in); |
| 23 |
|
|
|
| 24 |
|
|
while (ifs.good ()) |
| 25 |
|
|
{ |
| 26 |
root |
1.1 |
char c[1024]; |
| 27 |
|
|
ifs.getline (c, 1024); |
| 28 |
|
|
line_data += (string) c + "\n"; |
| 29 |
root |
1.11 |
} |
| 30 |
root |
1.1 |
|
| 31 |
root |
1.11 |
ifs.close (); |
| 32 |
root |
1.6 |
|
| 33 |
root |
1.11 |
double nobj; |
| 34 |
root |
1.6 |
|
| 35 |
root |
1.11 |
match_section ('O', nobj); |
| 36 |
|
|
|
| 37 |
|
|
for (o = 0; o < nobj; o++) |
| 38 |
|
|
{ |
| 39 |
root |
1.6 |
double c1, c2, c3, c4, l0, l1, l2; |
| 40 |
|
|
match_section ('M', tmps); |
| 41 |
|
|
match_section ('L', tmps); |
| 42 |
root |
1.11 |
match_number (l0); |
| 43 |
|
|
match_space (); |
| 44 |
|
|
match_number (l1); |
| 45 |
|
|
match_space (); |
| 46 |
|
|
match_number (l2); |
| 47 |
|
|
match_nl (); |
| 48 |
root |
1.6 |
match_section ('T', tmps); |
| 49 |
|
|
|
| 50 |
root |
1.12 |
simple_material *m = new simple_material; |
| 51 |
|
|
|
| 52 |
root |
1.11 |
match_number (c1); |
| 53 |
|
|
match_space (); |
| 54 |
|
|
match_number (c2); |
| 55 |
|
|
match_space (); |
| 56 |
|
|
match_number (c3); |
| 57 |
|
|
match_space (); |
| 58 |
|
|
match_number (c4); |
| 59 |
|
|
match_nl (); |
| 60 |
root |
1.12 |
m->diffuse = colour (c1, c2, c3, c4); |
| 61 |
root |
1.11 |
match_number (c1); |
| 62 |
|
|
match_space (); |
| 63 |
|
|
match_number (c2); |
| 64 |
|
|
match_space (); |
| 65 |
|
|
match_number (c3); |
| 66 |
|
|
match_space (); |
| 67 |
|
|
match_number (c4); |
| 68 |
|
|
match_nl (); |
| 69 |
root |
1.12 |
m->specular = colour (c1, c2, c3, c4); |
| 70 |
root |
1.11 |
match_number (c1); |
| 71 |
|
|
match_space (); |
| 72 |
|
|
match_number (c2); |
| 73 |
|
|
match_space (); |
| 74 |
|
|
match_number (c3); |
| 75 |
|
|
match_space (); |
| 76 |
|
|
match_number (c4); |
| 77 |
|
|
match_nl (); |
| 78 |
root |
1.12 |
m->emission = colour (c1, c2, c3, c4); |
| 79 |
root |
1.11 |
match_number (c1); |
| 80 |
|
|
match_nl (); |
| 81 |
root |
1.12 |
m->shininess = (GLfloat) c1; |
| 82 |
root |
1.11 |
|
| 83 |
root |
1.6 |
match_section ('V', tmpn); |
| 84 |
root |
1.11 |
if (tmpn <= 0) |
| 85 |
|
|
throw txtprt_i_exception ("No Vertices?!"); |
| 86 |
root |
1.6 |
|
| 87 |
root |
1.11 |
vector < vertex2d > verts; |
| 88 |
|
|
for (i = 0; i < tmpn; i++) |
| 89 |
|
|
{ |
| 90 |
|
|
double x, y, z; |
| 91 |
|
|
match_number (x); |
| 92 |
|
|
match_space (); |
| 93 |
|
|
match_number (y); |
| 94 |
|
|
match_space (); |
| 95 |
|
|
match_number (z); |
| 96 |
|
|
match_nl (); |
| 97 |
|
|
verts. |
| 98 |
|
|
push_back (vertex2d |
| 99 |
|
|
(point (l0 + x, l1 + y, l2 + z), vec3 (0, 0, 1))); |
| 100 |
|
|
// FIXME: propably vertex coloUrs ? |
| 101 |
|
|
} |
| 102 |
root |
1.6 |
match_section ('N', tmpn); |
| 103 |
root |
1.11 |
if (tmpn > verts.size ()) |
| 104 |
|
|
{ |
| 105 |
|
|
throw txtprt_i_exception ("Too many vertex normals!!"); |
| 106 |
|
|
} |
| 107 |
|
|
for (i = 0; i < tmpn; i++) |
| 108 |
|
|
{ |
| 109 |
|
|
double x, y, z; |
| 110 |
|
|
match_number (x); |
| 111 |
|
|
match_space (); |
| 112 |
|
|
match_number (y); |
| 113 |
|
|
match_space (); |
| 114 |
|
|
match_number (z); |
| 115 |
|
|
match_nl (); |
| 116 |
|
|
verts[i].n = vec3 (x, y, z); |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
match_section ('A', tmpn); |
| 120 |
|
|
|
| 121 |
|
|
vector < vertex2d > vd; |
| 122 |
|
|
for (i = 0; i < tmpn; i++) |
| 123 |
|
|
{ |
| 124 |
|
|
double a, b, c; |
| 125 |
|
|
match_number (a); |
| 126 |
|
|
if (verts.size () < a) |
| 127 |
|
|
throw txtprt_i_exception ("Vertex a out of range!"); |
| 128 |
|
|
match_space (); |
| 129 |
|
|
match_number (b); |
| 130 |
|
|
if (verts.size () < b) |
| 131 |
|
|
throw txtprt_i_exception ("Vertex b out of range!"); |
| 132 |
|
|
match_space (); |
| 133 |
|
|
match_number (c); |
| 134 |
|
|
if (verts.size () < c) |
| 135 |
|
|
throw txtprt_i_exception ("Vertex c out of range!"); |
| 136 |
|
|
match_nl (); |
| 137 |
|
|
double smooth_fl; |
| 138 |
|
|
match_number (smooth_fl); |
| 139 |
|
|
match_nl (); |
| 140 |
|
|
double u, v; |
| 141 |
|
|
match_number (u); |
| 142 |
|
|
match_space (); |
| 143 |
|
|
match_number (v); |
| 144 |
|
|
match_nl (); |
| 145 |
|
|
verts[(int) a].t = texc (1 - u, 1 - v); |
| 146 |
|
|
match_number (u); |
| 147 |
|
|
match_space (); |
| 148 |
|
|
match_number (v); |
| 149 |
|
|
match_nl (); |
| 150 |
|
|
verts[(int) b].t = texc (1 - u, 1 - v); |
| 151 |
|
|
match_number (u); |
| 152 |
|
|
match_space (); |
| 153 |
|
|
match_number (v); |
| 154 |
|
|
match_nl (); |
| 155 |
|
|
verts[(int) c].t = texc (1 - u, 1 - v); |
| 156 |
|
|
|
| 157 |
|
|
if (!smooth_fl) |
| 158 |
|
|
{ |
| 159 |
|
|
point ap = verts[(int) a].p; |
| 160 |
|
|
point bp = verts[(int) b].p; |
| 161 |
|
|
point cp = verts[(int) c].p; |
| 162 |
|
|
vec3 nv = normalize (cross (vec3 (cp.x - bp.x, cp.y - bp.y, cp.z - bp.z), |
| 163 |
|
|
vec3 (ap.x - bp.x, ap.y - bp.y, ap.z - bp.z))); |
| 164 |
|
|
|
| 165 |
|
|
verts[(int) a].n = nv; |
| 166 |
|
|
verts[(int) b].n = nv; |
| 167 |
|
|
verts[(int) c].n = nv; |
| 168 |
|
|
} |
| 169 |
|
|
|
| 170 |
|
|
vd.push_back (verts[(int) a]); |
| 171 |
|
|
vd.push_back (verts[(int) b]); |
| 172 |
|
|
vd.push_back (verts[(int) c]); |
| 173 |
|
|
} |
| 174 |
|
|
|
| 175 |
|
|
geometry_triangles *tri = new geometry_triangles; |
| 176 |
root |
1.9 |
tri->m = m; |
| 177 |
|
|
tri->set (vd); |
| 178 |
root |
1.11 |
objs->add (tri); |
| 179 |
|
|
} |
| 180 |
|
|
|
| 181 |
|
|
return objs; |
| 182 |
root |
1.1 |
} |
| 183 |
root |
1.11 |
|