#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, o; entity *e = new entity; entity_container *ec = new entity_container; e->set (ec); ifs.open (filename.c_str(), ifstream::in); while (ifs.good ()) { char c[1024]; ifs.getline (c, 1024); line_data += (string) c + "\n"; } ifs.close(); double objs; match_section ('O', objs); for (o = 0; o < objs; o++) { double c1, c2, c3, c4, l0, l1, l2; match_section ('M', tmps); match_section ('L', tmps); match_number (l0); match_space (); match_number (l1); match_space (); match_number (l2); match_nl (); match_section ('T', tmps); material m; 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_number (c1); match_nl (); m.shininess = (GLfloat) c1; 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 (l0 + x, l1 + y, l2 + 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); } match_section ('A', tmpn); vector vd; 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 (); double smooth_fl; match_number (smooth_fl); match_nl (); double u, v; match_number (u); match_space (); match_number (v); match_nl (); verts[(int)a].t = texc (1 - u,1 - v); match_number (u); match_space (); match_number (v); match_nl (); verts[(int)b].t = texc (1 - u,1 - v); match_number (u); match_space (); match_number (v); match_nl (); verts[(int)c].t = texc (1 - u,1 - v); if (!smooth_fl) { point ap = verts[(int)a].p; point bp = verts[(int)b].p; point cp = verts[(int)c].p; vec3 nv = normalize (cross (vec3 (cp.x - bp.x, cp.y - bp.y, cp.z - bp.z), vec3 (ap.x - bp.x, ap.y - bp.y, ap.z - bp.z))); verts[(int)a].n = nv; verts[(int)b].n = nv; verts[(int)c].n = nv; } vd.push_back (verts[(int)a]); vd.push_back (verts[(int)b]); vd.push_back (verts[(int)c]); } entity_triangles *tri = new entity_triangles; tri->m = m; tri->set (vd); ec->add (tri); } return e; }