remove render(), gut processInputLine(), add m_faces map

This commit is contained in:
Josh Holtrop 2011-04-19 16:16:41 -04:00
parent 3f78016e26
commit 476f4cde66
2 changed files with 24 additions and 184 deletions

198
WFObj.cc
View File

@ -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<string> lineParts;
vector<string> 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<Vertex> 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<string> & parts)
return v;
}
void WFObj::parseVertexIndices(const string & vtxref, int * ret)
{
vector<string> parts = splitString(vtxref, '/');
int num = parts.size();
for (int i = 0; i < num && i < 3; i++)
sscanf(parts[i].c_str(), "%d", ret + i);
}

10
WFObj.h
View File

@ -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<std::string> & 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<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