From 476f4cde662a6f95e21b6568133e841144ebb3aa Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 19 Apr 2011 16:16:41 -0400 Subject: [PATCH] remove render(), gut processInputLine(), add m_faces map --- WFObj.cc | 198 +++++-------------------------------------------------- WFObj.h | 10 ++- 2 files changed, 24 insertions(+), 184 deletions(-) diff --git a/WFObj.cc b/WFObj.cc index efdcd62..6c9ec2e 100644 --- a/WFObj.cc +++ b/WFObj.cc @@ -124,7 +124,8 @@ bool WFObj::load(const char *fname) bool WFObj::load(const WFObj::Buffer &buff) { string buildup; - for (size_t idx = 0; idx < buff.size;) + size_t idx = 0; + while (idx < buff.size) { string line = getLine(buff, idx, &idx) string input = trimString(line); @@ -174,193 +175,34 @@ string WFObj::getLine(const Buff & buff, size_t idx, size_t *update_idx) void WFObj::processInputLine(const std::string & input) { string line = input; - vector lineParts; + vector tokens; for (;;) { string token = stripFirstToken(line); if (token == "") break; - lineParts.push_back(token); + tokens.push_back(token); } - if (lineParts.size() > 0) - m_data.push_back(lineParts); -} + if (tokens.size() == 0) + return; -GLuint WFObj::render(bool doTextureInfo, bool enableBlending) -{ - checkGLError(); - GLuint list = glGenLists(1); - glNewList(list, GL_COMPILE); - int len = m_data.size(); - vector vertices[VERTEX_TYPES]; - int numVertsLast = 0; - bool inFace = false; - bool inMaterial = false; - string currentMaterialName; - WFMtl material(this); - for (int i = 0; i < len; i++) + string type = tokens[0]; + if (type == "v") + m_vertices[VERTEX].push_back(readVertex(tokens)); + else if (type == "vt") + m_vertices[VERTEX_TEXTURE].push_back(readVertex(tokens)); + else if (type == "vn") + m_vertices[VERTEX_NORMAL].push_back(readVertex(tokens)); + else if (type == "f") { - string type = m_data[i][0]; - if (type == "v") - { - Vertex v = readVertex(m_data[i]); - updateAABB(v.getData()); - vertices[VERTEX].push_back(v); - } - else if (type == "vt") - vertices[VERTEX_TEXTURE].push_back(readVertex(m_data[i])); - else if (type == "vn") - vertices[VERTEX_NORMAL].push_back(readVertex(m_data[i])); - else if (type == "f") - { - int numVerts = m_data[i].size() - 1; - if (inFace && (numVerts != numVertsLast || numVertsLast > 4)) - { -#ifdef DEBUGGL - cout << "glEnd()" << endl; -#endif - glEnd(); - inFace = false; - } - if (!inFace) - { - if (numVerts == 3) - { -#ifdef DEBUGGL - cout << "glBegin(GL_TRIANGLES)" << endl; -#endif - glBegin(GL_TRIANGLES); - } - else if (numVerts == 4) - { -#ifdef DEBUGGL - cout << "glBegin(GL_QUADS)" << endl; -#endif - glBegin(GL_QUADS); - } - else - { -#ifdef DEBUGGL - cout << "glBegin(GL_POLYGON)" << endl; -#endif - glBegin(GL_POLYGON); - } - inFace = true; - } - for (int v = 1; v <= numVerts; v++) - { - int vertexIndices[3] = {0, 0, 0}; - parseVertexIndices(m_data[i][v], vertexIndices); - for (int j = 0; j < 3; j++) - { - if (vertexIndices[j] < 0) - vertexIndices[j] += vertices[j].size() + 1; - } - bool valid = true; - for (int j = 0; j < 3; j++) - { - if (vertexIndices[j] > (int) vertices[j].size()) - { - valid = false; - break; - } - } - if (vertexIndices[VERTEX] <= 0) - valid = false; - if (!valid) - continue; - if (vertexIndices[VERTEX_NORMAL] != 0) - { - /* a normal is present */ -#ifdef DEBUGGL - cout << " glNormal3f(" << - vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1] - [0] << ", " << - vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1] - [1] << ", " << - vertices[VERTEX_NORMAL][vertexIndices[VERTEX_NORMAL]-1] - [2] << ")" << endl; -#endif - glNormal3fv(vertices[VERTEX_NORMAL] - [vertexIndices[VERTEX_NORMAL]-1].getData()); - } - if (vertexIndices[VERTEX_TEXTURE] != 0) - { - /* a texture coordinate is present */ -#ifdef DEBUGGL - cout << " glTexCoord2f(" << - vertices[VERTEX_TEXTURE][vertexIndices[VERTEX_TEXTURE]-1] - [0] << ", " << - vertices[VERTEX_TEXTURE][vertexIndices[VERTEX_TEXTURE]-1] - [1] << ')' << endl; -#endif - glTexCoord2fv(vertices[VERTEX_TEXTURE] - [vertexIndices[VERTEX_TEXTURE]-1].getData()); - } -#ifdef DEBUGGL - cout << " glVertex3f(" << - vertices[VERTEX][vertexIndices[VERTEX]-1][0] << ", " << - vertices[VERTEX][vertexIndices[VERTEX]-1][1] << ", " << - vertices[VERTEX][vertexIndices[VERTEX]-1][2] << ")" << - " [" << vertexIndices[VERTEX] << "]" << endl; -#endif - glVertex3fv(vertices[VERTEX][vertexIndices[VERTEX]-1].getData()); - } - - numVertsLast = numVerts; - } - else if (type == "usemtl") - { - if (inFace) - { -#ifdef DEBUGGL - cout << "glEnd()" << endl; -#endif - glEnd(); - inFace = false; - } - if (inMaterial) - { - material.renderEnd(currentMaterialName, doTextureInfo, - enableBlending); - inMaterial = false; - } - if (m_data[i].size() >= 2) - { - currentMaterialName = m_data[i][1]; - material.renderBegin(currentMaterialName, doTextureInfo, - enableBlending); - inMaterial = true; - } - } - else if (type == "mtllib") - { - if (m_data[i].size() >= 2) - { - FileLoader::Path path( - basePath(m_path.fullPath) + m_data[i][1], - m_data[i][1]); - material.load(path); - } - } } - if (inFace) + else if (type == "usemtl") { -#ifdef DEBUGGL - cout << "glEnd()" << endl; -#endif - glEnd(); - inFace = false; } - if (inMaterial) + else if (type == "mtllib") { - material.renderEnd(currentMaterialName, doTextureInfo, enableBlending); - inMaterial = false; } - glEndList(); - checkGLError(); - return list; } void WFObj::updateAABB(const float * const vertex) @@ -400,14 +242,6 @@ WFObj::Vertex WFObj::readVertex(const vector & parts) return v; } -void WFObj::parseVertexIndices(const string & vtxref, int * ret) -{ - vector parts = splitString(vtxref, '/'); - int num = parts.size(); - for (int i = 0; i < num && i < 3; i++) - sscanf(parts[i].c_str(), "%d", ret + i); -} - diff --git a/WFObj.h b/WFObj.h index a0e654c..377a234 100644 --- a/WFObj.h +++ b/WFObj.h @@ -57,7 +57,6 @@ public: /* methods */ bool load(const char *fname); bool load(const Buffer &buff); - GLuint render(int flags); const float * const getAABB() { return m_aabb; } protected: @@ -81,21 +80,28 @@ protected: int normal; }; + class Face + { + public: + VertexRef vertices[3]; + }; + /* methods */ void clear(); void processInputLine(const std::string & input); Vertex readVertex(const std::vector & parts); - void parseVertexIndices(const std::string & vtxref, int * ret); void updateAABB(const float * const vertex); static Buffer loadfile(const char *path); std::string getLine(const Buff & buff, size_t idx, size_t *update_idx); /* variables */ std::vector m_vertices[VERTEX_TYPES]; + std::map< std::string, std::vector< Face > > m_faces; FileLoader::Path m_path; float m_aabb[6]; loadfile_t m_loadfile; loadtexture_t m_loadtexture; + std::string m_current_material_name; }; #endif