VBO照明和索引

时间:2012-11-16 21:28:38

标签: c++ opengl lighting

我在使用索引时让我的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渲染:

Pic1

使用VBO渲染:

Pic2

1 个答案:

答案 0 :(得分:2)

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

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

最好使用the offsetof macro

glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), (void*)offsetof(MyVertex, x));
glNormalPointer(GL_FLOAT, sizeof(MyVertex), (void*)offsetof(MyVertex, nx));