既然我正在使用索引,我很难让我的 VBO 正确渲染法线。我很确定我的普通指针的偏移量有问题,但数学似乎加起来了。
我如何存储数据:
struct MyVertex
{
float x, y, z; //Vertex
float nx, ny, nz; //Normal
};
以下是我设置 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);
我的渲染代码:
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);
}
没有 VBO 的渲染:
使用 VBO 渲染: