1

既然我正在使用索引,我很难让我的 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 的渲染:

图1

使用 VBO 渲染:

图2

4

1 回答 1

2
glNormalPointer(GL_FLOAT, sizeof(MyVertex), (void*)(NumOfV*sizeof(float)));//Number of vertices times size of float

您的偏移量在这里是错误的。您正在提供交错的顶点数据,因此前三个浮点数是您的位置,接下来的三个浮点数是法线。因此,法线的基本偏移量是 3 个浮点数字节(跳过该位置)。是整个模型NumOfV中的顶点数,而不是数据结构中位置的浮点数。MyVertex

最好使用offsetof完成:

glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), (void*)offsetof(MyVertex, x));
glNormalPointer(GL_FLOAT, sizeof(MyVertex), (void*)offsetof(MyVertex, nx));
于 2012-11-16T22:24:27.827 回答