I am having a little trouble getting my VBO to render normals correctly now that I am using indexing. I am pretty sure it is something wrong with my offset for the normal pointer, but the math seems to add up to me.
How I store the data:
struct MyVertex
{
float x, y, z; //Vertex
float nx, ny, nz; //Normal
};
Below is how I set up the VBO:
GLuint VertexVBOID, IndexVBOID;
glGenBuffers(1, &VertexVBOID);
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertex)*NumOfV, &ModelV[0].x, GL_STATIC_DRAW);
glGenBuffers(1, &IndexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*NumOfF*3, &ModelI[0], GL_STATIC_DRAW);
My render code:
if(UsingVBO == false)
{
glRotatef(Timer.getElapsedTime().asSeconds() * 10, 0, 1, 0);
glBegin(GL_TRIANGLES);
for(int i = 0; i < NumOfF*3; i+=3)
{
glNormal3f(ModelV[ModelI[i]].nx, ModelV[ModelI[i]].ny, ModelV[ModelI[i]].nz);
glVertex3f(ModelV[ModelI[i]].x, ModelV[ModelI[i]].y, ModelV[ModelI[i]].z);
glNormal3f(ModelV[ModelI[i+1]].nx, ModelV[ModelI[i+1]].ny, ModelV[ModelI[i+1]].nz);
glVertex3f(ModelV[ModelI[i+1]].x, ModelV[ModelI[i+1]].y, ModelV[ModelI[i+1]].z);
glNormal3f(ModelV[ModelI[i+2]].nx, ModelV[ModelI[i+2]].ny, ModelV[ModelI[i+2]].nz);
glVertex3f(ModelV[ModelI[i+2]].x, ModelV[ModelI[i+2]].y, ModelV[ModelI[i+2]].z);
}
glEnd();
}
else
{
glRotatef(Timer.getElapsedTime().asSeconds() * 10, 0, 1, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), 0);
glNormalPointer(GL_FLOAT, sizeof(MyVertex), (void*)(NumOfV*sizeof(float)));//Number of vertices times size of float
glDrawElements(GL_TRIANGLES, NumOfF*3, GL_UNSIGNED_INT, 0);//number of indices
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
Rendering without VBO:

Rendering with VBO:

Your offset is wrong here. You’re providing interleaved vertex data, so that the first three floats are your positions and the next three floats are the normals. Your base offset for normals is therefore 3 floats worth of bytes (to skip the position).
NumOfVis the number of vertices in your entire model, not the number of floats for positions in yourMyVertexdata structure.This is best done with the
offsetofmacro: