VBO与Vertex Array相对,VBO中的顶点法线问题

时间:2012-03-11 13:30:04

标签: opengl vbo normals

我不知道这里有什么问题,一切都正常,除了法线。 当我使用顶点数组时,模型看起来很完美但是当我切换到VBO时,由于顶点法线,模型看起来更糟。我花了很多时间来解决它,但不知道出了什么问题。 VBO一代似乎很完美。但还是不知道。 任何想法?

#define BUFFER_OFFSET(i) ((char *)NULL + (i))


void InitVBO(){

glGenBuffers(1, &vboNormID); 
glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, VertNormals);
glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));

glGenBuffers(1, &vboVertID); 
glBindBuffer(GL_ARRAY_BUFFER, vboVertID); 
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, p_VERTICES);
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));


glGenBuffers(1, &indexVBOID); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLFace)*nb_Faces, p_indices);

//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, p_indices, GL_DYNAMIC_DRAW);}

遵循渲染代码。 VBO +顶点数组 顶点阵列工作得很完美。我可以看到具有顶点法线的模型的完美形状,但是对于VBO,顶点法线存在一些问题。我想我在BUFFER_OFFSET(12)做错了。

void RenderTringularModel(GLvoid){
if(VertNormals && !MESH_SMOOTH)
{
    glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
    glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);

    glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);

    glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));
    glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));

    glDrawElements(GL_TRIANGLES, nb_Faces*3, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glPopClientAttrib();

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

}
else
{
    //glShadeModel (GL_FLAT);
    glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), p_VERTICES);
    glNormalPointer(GL_FLOAT,  sizeof(GLpoint), VertNormals);
    glDrawElements(GL_TRIANGLES, 3*nb_Faces, GL_UNSIGNED_INT, p_indices);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glPopClientAttrib();
}

}

更新1: 我想你在谈论InitVBO ..那时我错了。这个怎么样......但是它也没有用......实际上问题就是..在很多方面我试图在InitVBO()中绑定顶点法线..但每次结果都是一样的。

    glGenBuffers(1, &vboVertID); 
glBindBuffer(GL_ARRAY_BUFFER, vboVertID); 
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, p_VERTICES);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, sizeof(GLpoint)*nb_Vertices, VertNormals);  
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));
glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));

更新2: 好的...我没有绑定顶点法线(vboNormID)。我正在丢弃它。现在好吗?现在它应该工作..实际上我已经用这种方式完成了所有事情..将顶点法线与vboVertID和vboNormID等绑定等等。但并非每个算法都有效..通过分析此代码给我任何建议。

    void RenderTringularModel(GLvoid){
if(VertNormals && !MESH_SMOOTH)
{
    glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);

    glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);

    glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));
    glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));

    glDrawElements(GL_TRIANGLES, nb_Faces*3, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

更新3:

我不知道是谁告诉我使用BUFFER_OFFSET()...只是废话..我现在已修复它,现在工作正常没有这个愚蠢的BUFFER_OFFSET().. 现在固定代码非常简单如下:

void InitVBO(){
glGenBuffers(1, &vboVertID); 
glBindBuffer(GL_ARRAY_BUFFER, vboVertID); 
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, p_VERTICES, GL_DYNAMIC_DRAW);

glGenBuffers(1, &vboNormID);
glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, VertNormals, GL_DYNAMIC_DRAW);

glGenBuffers(1, &indexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, p_indices, GL_DYNAMIC_DRAW);}


void RenderTringularModel(GLpoint *P, GLpoint *Vn, GLFace *T, int nbF){
glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glNormalPointer(GL_FLOAT, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glDrawElements(GL_TRIANGLES, nb_Faces*3, GL_UNSIGNED_INT, 0);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glPopClientAttrib();}

现在,伙计们指导我如何实时更新这些缓冲区。我的意思是说我需要以实时,法线,顶点,三角形更新整个数据。 例如:在细分三角形之后,我需要用顶点和法线更新三角形缓冲区..另一件事是,在模型的切割模拟中,我需要更新顶点位置和法线,所以我需要更新那个时候的缓冲区..所以告诉我该怎么做这个问题?我怎么能弥补呢?我需要删除缓冲区并重新生成吗?如果是,那怎么样?

0 个答案:

没有答案