Hey guys whats up. So I have 4 vectors:
vector<Vertex3> m_vertexCoordinates; // a list of all the vertices in the model
vector<int> m_triangleIndices; // list storing the order that vertices are drawn
vector<Vertex3> m_textureCoordinates; // a list of all the textures in the model
vector<int> m_textureIndices; // list storing the order that textures are drawn
Simple enough. I can draw my models without error by doing:
int count = 0;
for each (int i in m_triangleIndices)
{
float s = m_textureCoordinates[m_textureIndices[count]].x();
float t = m_textureCoordinates[m_textureIndices[count++]].y();
glTexCoord2f(s,t);
glVertex3fv(m_vertexCoordinates[i]);
}
I now want to adopt the same thing using VBOs. I have two methods, one to create my buffer, the other to draw it:
void createOpenGLVertexBufferObject()
{
// Deal with ARRAY_BUFFER
glGenBuffers(1, &mBufferVertList);
glBindBuffer(GL_ARRAY_BUFFER, mBufferVertList);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*m_vertexCoordinates.size()*3+sizeof(GLfloat)*m_textureCoordinates.size()*2, NULL, GL_STATIC_DRAW);
GLfloat *vertBuff = (GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY);
int count = 0;
for (vector<Vertex3>::iterator v = m_vertexCoordinates.begin(); v < m_vertexCoordinates.end(); v++)
{
vertBuff[count++] = v->x();
vertBuff[count++] = v->y();
vertBuff[count++] = v->z();
}
int index = 0;
for (int i = 0; i < m_textureCoordinates.size(); i++)
{
int tex_ind = m_textureIndices[index++];
float s = m_textureCoordinates[tex_ind].x();
float t = m_textureCoordinates[tex_ind].y();
vertBuff[count++] = s;
vertBuff[count++] = t;
}
glUnmapBuffer(GL_ARRAY_BUFFER);
// Deal with ELEMENT_ARRAY_BUFFER
glGenBuffers(1, &mBufferTriList);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferTriList);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*m_triangleIndices.size(), NULL, GL_STATIC_DRAW);
GLuint *triBuff = (GLuint *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
for (int i = 0; i < m_triangleIndices.size(); i++)
triBuff[i] = m_triangleIndices[i];
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}
void drawOpenGLVertexBufferObject()
{
glBindBuffer(GL_ARRAY_BUFFER, mBufferVertList);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferTriList);
// enable vertex coordinates
glEnableClientState(GL_VERTEX_ARRAY);
// enable textures
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, (float *)NULL);
glTexCoordPointer(2, GL_FLOAT, 0, (float *)NULL + m_vertexCoordinates.size()*3);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_material);
glColor3d(1,1,1);
glDrawElements(GL_TRIANGLES, m_triangleIndices.size(), GL_UNSIGNED_INT, 0);
}
The problem is, when I do this, the textures are not being drawn correctly. The models are correct, the textures seem to be drawn in the wrong order but I cant see the reason why. Anybody got a clue?