#include #include #include #include #include "txtprt_import.h" #include "entity.h" #include "util.h" using namespace std; // FIXME: In case of failed loading a memory leak can happen entity* txtprt_parser::read (string filename) { double tmpn; string tmps; int i; entity *e = new entity; entity_container *ec = new entity_container; e->set (ec); e->sec.x = e->sec.y = e->sec.z = 0; ifs.open (filename.c_str(), ifstream::in); while (ifs.good ()) { char c[1024]; ifs.getline (c, 1024); line_data += (string) c + "\n"; } ifs.close(); match_section ('O', tmps); match_section ('M', tmps); match_section ('T', tmps); material m; m.diffuse = colour (0, 0, 0, 0); double c1, c2, c3, c4; match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl (); m.diffuse = colour (c1, c2, c3, c4); match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl (); m.specular = colour (c1, c2, c3, c4); match_number (c1); match_space (); match_number (c2); match_space (); match_number (c3); match_space (); match_number (c4); match_nl (); m.emission = colour (c1, c2, c3, c4); match_section ('V', tmpn); if (tmpn <= 0) throw txtprt_i_exception ("No Vertices?!"); vector verts; for (i = 0; i < tmpn; i++) { double x, y, z; match_number (x); match_space (); match_number (y); match_space (); match_number (z); match_nl (); verts.push_back (vertex2d (point (x, y, z), vec3 (0, 0, 1))); // FIXME: propably vertex coloUrs ? } match_section ('N', tmpn); if (tmpn > verts.size()) { throw txtprt_i_exception ("Too many vertex normals!!"); } for (i = 0; i < tmpn; i++) { double x, y, z; match_number (x); match_space (); match_number (y); match_space (); match_number (z); match_nl (); verts[i].n = vec3 (x, y, z); } char tmpc; match_asection (tmpc, tmpn); if (tmpc == 'U') { for (i = 0; i < tmpn; i++) { double u, v; match_number (u); match_space (); match_number (v); match_nl (); verts[i].t = texc (u,v); } match_section ('A', tmpn); } for (i = 0; i < tmpn; i++) { double a, b, c; match_number (a); if (verts.size () < a) throw txtprt_i_exception ("Vertex a out of range!"); match_space (); match_number (b); if (verts.size () < b) throw txtprt_i_exception ("Vertex b out of range!"); match_space (); match_number (c); if (verts.size () < c) throw txtprt_i_exception ("Vertex c out of range!"); match_nl (); entity_triangles *tri = new entity_triangles; tri->push_back (verts[(int)a]); tri->push_back (verts[(int)b]); tri->push_back (verts[(int)c]); tri->m = m; ec->add (tri); } return e; }