fill out draw()

This commit is contained in:
Josh Holtrop 2011-04-25 14:48:47 -04:00
parent e07ed46297
commit a12c4ec42d
2 changed files with 25 additions and 7 deletions

View File

@ -450,7 +450,7 @@ bool WFObj::buildVBO()
{ {
map<VertexRef, int> flat_vertices; map<VertexRef, int> flat_vertices;
int vid = 0, texture_ref_count = 0, n_vrefs = 0; int vid = 0, texture_ref_count = 0, n_vrefs = 0;
bool do_textures = m_loadtexture != NULL m_do_textures = m_loadtexture != NULL
&& m_vertices[VERTEX_TEXTURE].size() > 0; && m_vertices[VERTEX_TEXTURE].size() > 0;
for (map<string, vector<Face> >::iterator it = m_faces.begin(); for (map<string, vector<Face> >::iterator it = m_faces.begin();
it != m_faces.end(); it != m_faces.end();
@ -486,18 +486,18 @@ bool WFObj::buildVBO()
} }
} }
if (texture_ref_count == 0) if (texture_ref_count == 0)
do_textures = false; m_do_textures = false;
size_t n_floats_per_vref = m_n_floats_per_vref =
3 + /* vertex coordinates */ 3 + /* vertex coordinates */
3 + /* normal coordinates */ 3 + /* normal coordinates */
(do_textures ? 2 : 0); /* texture coordinates */ (m_do_textures ? 2 : 0); /* texture coordinates */
size_t n_data_elements = n_floats_per_vref * flat_vertices.size(); size_t n_data_elements = m_n_floats_per_vref * flat_vertices.size();
GLfloat * data = new GLfloat[n_data_elements]; GLfloat * data = new GLfloat[n_data_elements];
for (map<VertexRef, int>::iterator it = flat_vertices.begin(); for (map<VertexRef, int>::iterator it = flat_vertices.begin();
it != flat_vertices.end(); it != flat_vertices.end();
it++) it++)
{ {
int base = n_floats_per_vref * it->second; int base = m_n_floats_per_vref * it->second;
VertexRef vr = it->first; VertexRef vr = it->first;
data[base + 0] = m_vertices[VERTEX][vr.vertex - 1][0]; data[base + 0] = m_vertices[VERTEX][vr.vertex - 1][0];
data[base + 1] = m_vertices[VERTEX][vr.vertex - 1][1]; data[base + 1] = m_vertices[VERTEX][vr.vertex - 1][1];
@ -505,7 +505,7 @@ bool WFObj::buildVBO()
data[base + 3] = m_vertices[VERTEX_NORMAL][vr.normal - 1][0]; data[base + 3] = m_vertices[VERTEX_NORMAL][vr.normal - 1][0];
data[base + 4] = m_vertices[VERTEX_NORMAL][vr.normal - 1][1]; data[base + 4] = m_vertices[VERTEX_NORMAL][vr.normal - 1][1];
data[base + 5] = m_vertices[VERTEX_NORMAL][vr.normal - 1][2]; data[base + 5] = m_vertices[VERTEX_NORMAL][vr.normal - 1][2];
if (do_textures && vr.texture > 0) if (m_do_textures && vr.texture > 0)
{ {
data[base + 6] = m_vertices[VERTEX_TEXTURE][vr.texture - 1][0]; data[base + 6] = m_vertices[VERTEX_TEXTURE][vr.texture - 1][0];
data[base + 7] = m_vertices[VERTEX_TEXTURE][vr.texture - 1][1]; data[base + 7] = m_vertices[VERTEX_TEXTURE][vr.texture - 1][1];
@ -551,14 +551,30 @@ void WFObj::draw()
return; return;
glBindBuffer(GL_ARRAY_BUFFER, m_data_vbo); glBindBuffer(GL_ARRAY_BUFFER, m_data_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_vbo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_vbo);
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
size_t stride = sizeof(GLfloat) * m_n_floats_per_vref;
glVertexPointer(3, GL_FLOAT, stride, NULL);
glNormalPointer(GL_FLOAT, stride, (GLvoid *) (sizeof(GLfloat) * 3));
if (m_do_textures)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, stride,
(GLvoid *) (sizeof(GLfloat) * 6));
}
for (map<string, vector<Face> >::iterator it = m_faces.begin(); for (map<string, vector<Face> >::iterator it = m_faces.begin();
it != m_faces.end(); it != m_faces.end();
it++) it++)
{ {
glPushAttrib(GL_LIGHTING_BIT); glPushAttrib(GL_LIGHTING_BIT);
renderMaterial(m_materials[it->first]); renderMaterial(m_materials[it->first]);
glDrawElements(GL_TRIANGLES, m_face_indices[it->first].second,
GL_UNSIGNED_SHORT,
(GLvoid *) (sizeof(GLfloat) * m_face_indices[it->first].first));
glPopAttrib(); glPopAttrib();
} }
glPopClientAttrib();
} }
void WFObj::renderMaterial(const WFObj::Material & m) void WFObj::renderMaterial(const WFObj::Material & m)

View File

@ -115,6 +115,8 @@ protected:
std::string m_current_material_name; std::string m_current_material_name;
bool m_valid; bool m_valid;
GLuint m_data_vbo, m_index_vbo; GLuint m_data_vbo, m_index_vbo;
bool m_do_textures;
size_t m_n_floats_per_vref;
}; };
#endif #endif