fill indices VBO

This commit is contained in:
Josh Holtrop 2011-04-24 23:16:13 -04:00
parent 02a3f85fc1
commit fea2100741

View File

@ -172,9 +172,8 @@ bool WFObj::load(const WFObj::Buffer &buff)
updateAABB(); updateAABB();
buildVBO(); m_valid = buildVBO();
m_valid = true; return m_valid;
return true;
} }
string WFObj::getLine(const Buffer & buff, size_t idx, size_t *update_idx) string WFObj::getLine(const Buffer & buff, size_t idx, size_t *update_idx)
@ -447,15 +446,13 @@ bool WFObj::loadfile(const char *path, Buffer & buff)
bool WFObj::buildVBO() bool WFObj::buildVBO()
{ {
map<VertexRef, int> flat_vertices; map<VertexRef, int> flat_vertices;
int vid = 0, texture_ref_count = 0; int vid = 0, texture_ref_count = 0, n_vrefs = 0;
bool do_textures = m_loadtexture != NULL bool 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();
it++) it++)
{ {
int first = vid;
int num = 0;
for (vector<Face>::iterator fit = it->second.begin(); for (vector<Face>::iterator fit = it->second.begin();
fit != it->second.end(); fit != it->second.end();
fit++) fit++)
@ -480,11 +477,10 @@ bool WFObj::buildVBO()
if (flat_vertices.find(vf) == flat_vertices.end()) if (flat_vertices.find(vf) == flat_vertices.end())
{ {
flat_vertices[vf] = vid++; flat_vertices[vf] = vid++;
num++;
} }
n_vrefs++;
} }
} }
m_face_indices[it->first] = make_pair(first, num);
} }
if (texture_ref_count == 0) if (texture_ref_count == 0)
do_textures = false; do_textures = false;
@ -512,12 +508,37 @@ bool WFObj::buildVBO()
data[base + 7] = m_vertices[VERTEX_TEXTURE][vr.texture - 1][1]; data[base + 7] = m_vertices[VERTEX_TEXTURE][vr.texture - 1][1];
} }
} }
GLshort * indices = new GLshort[n_vrefs];
vid = 0;
for (map<string, vector<Face> >::iterator it = m_faces.begin();
it != m_faces.end();
it++)
{
int first = vid;
int num = 0;
for (vector<Face>::iterator fit = it->second.begin();
fit != it->second.end();
fit++)
{
for (int i = 0; i < 3; i++)
{
VertexRef vf = fit->vertices[i];
indices[vid] = flat_vertices[vf];
num++;
vid++;
}
}
m_face_indices[it->first] = make_pair(first, num);
}
glGenBuffers(1, &m_data_vbo); glGenBuffers(1, &m_data_vbo);
glGenBuffers(1, &m_index_vbo); glGenBuffers(1, &m_index_vbo);
/* move data from client side to GL */ /* move data from client side to GL */
glBufferData(m_data_vbo, sizeof(GLfloat) * n_data_elements, data, glBufferData(m_data_vbo, sizeof(GLfloat) * n_data_elements, data,
GL_STATIC_DRAW); GL_STATIC_DRAW);
glBufferData(m_index_vbo, sizeof(GLshort) * n_vrefs, indices,
GL_STATIC_DRAW);
delete[] data; delete[] data;
delete[] indices;
return true; return true;
} }