OpenGL - 错误地创建多个VBO

时间:2012-08-18 00:27:38

标签: opengl vbo

我遇到的问题是,一旦我为一个关卡中的主几何创建一个VBO,当我为对象创建第二个时,对象VBO就不起作用了。我假设我有一些缓冲区绑定错误但我不能为我的生活找到它。我先创建VBO级别然后再创建VBO对象,再次,当我禁用VBO级别的创建时,对象VBO会正确创建。

这是我的代码。

在level.h中

// Zone VBO
GLuint zoneVBO;
GLuint zoneVAO;
GLuint zoneIBO;

// Object VBO
GLuint objVBO;
GLuint objVAO;
GLuint objIBO;

创建区域(几何)VBO - 就像一个注释,创建在两个VBO中都能正常工作,因此数据移动不是问题,我想是一个绑定错误:

void WLD::createZoneVBO()
{
    // Get the size for our VBO
    int numVert = 0;
    int numPoly = 0;
    int VBOsize = 0;
    int IBOsize = 0;

    // Get the count of vertices and polygons
    for(int i = 0; i < zoneFragMap[0x36]; i++)
    {
        numVert += zmeshes[i].numVert;
        numPoly += zmeshes[i].numPoly;
        VBOsize += zmeshes[i].numVert * sizeof(Vertex);
        IBOsize += zmeshes[i].numPoly * 3 * sizeof(GLuint);
    }

    // Create the IBO and VBO data
    GLuint* iboData = new GLuint[zonePolyProcessed * 3];
    Vertex* vboData = new Vertex[zoneVertProcessed];

    int iboPos = 0;
    int vboPos = 0;

    // Create the VBO and IBO
    for(int i = 0; i < zoneFragMap[0x36]; i++)
    {
        // Copy the data to the IBO
        memcpy(&iboData[iboPos], zmeshes[i].indices, zmeshes[i].numPoly * 3 * sizeof(GLuint));//sizeof(*zmeshes[i].indices));

        // Advance the position
        iboPos += zmeshes[i].numPoly * 3;

        // Copy the data to the VBO
        memcpy(&vboData[vboPos], zmeshes[i].vertices, zmeshes[i].numVert * sizeof(Vertex));//sizeof(*zmeshes[i].vertices));

        // Advance the position
        vboPos += zmeshes[i].numVert;
    }

    //Create VBO for the triangle
    glGenBuffers(1, &zoneVBO);
    glBindBuffer(GL_ARRAY_BUFFER, zoneVBO);
    glBufferData(GL_ARRAY_BUFFER, zoneVertProcessed * sizeof(Vertex), vboData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    //Create the IBO for the triangle
    //16 bit indices
    //We could have actually made one big IBO for both the quad and triangle.
    glGenBuffers(1, &zoneIBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, zoneIBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, zonePolyProcessed * 3 * sizeof(GLuint), iboData, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    // Create the VAO
    glGenVertexArraysAPPLE(1, &zoneVAO);
    glBindVertexArrayAPPLE(zoneVAO);
    glBindBuffer(GL_ARRAY_BUFFER, zoneVAO);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(opaque["position"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));
    glVertexAttribPointer(opaque["normal"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 3));
    glVertexAttribPointer(opaque["texcoord"], 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 6));
    glVertexAttribPointer(opaque["color"], 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 8));
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArrayAPPLE(0);

    delete[] iboData;
    delete[] vboData;
}

对象 - 再次,对象几何体被完美地加载,但仅当尚未加载区域几何体时:

void WLD::createObjectVBO()
{
    // Get the size for our VBO
    int numVert = 0;
    int numPoly = 0;
    int VBOsize = 0;
    int IBOsize = 0;

    // Get the count of vertices and polygons
    for(int i = 0; i < objFragMap[0x36]; i++)
    {
        numVert += objMeshes[i].numVert;
        numPoly += objMeshes[i].numPoly;
        VBOsize += objMeshes[i].numVert * sizeof(Vertex);
        IBOsize += objMeshes[i].numPoly * 3 * sizeof(GLuint);
    }

    // Create the IBO and VBO data
    GLuint* iboData = new GLuint[objPolyProcessed * 3];
    Vertex* vboData = new Vertex[objVertProcessed];

    int iboPos = 0;
    int vboPos = 0;

    // Create the VBO and IBO
    for(int i = 0; i < objFragMap[0x36]; i++)
    {
        // Copy the data to the IBO
        memcpy(&iboData[iboPos], objMeshes[i].indices, objMeshes[i].numPoly * 3 * sizeof(GLuint));//sizeof(*zmeshes[i].indices));

        // Advance the position
        iboPos += objMeshes[i].numPoly * 3;

        // Copy the data to the VBO
        memcpy(&vboData[vboPos], objMeshes[i].vertices, objMeshes[i].numVert * sizeof(Vertex));//sizeof(*zmeshes[i].vertices));

        // Advance the position
        vboPos += objMeshes[i].numVert;
    }

    //Create VBO for the triangle
    glGenBuffers(1, &objVBO);
    glBindBuffer(GL_ARRAY_BUFFER, objVBO);
    glBufferData(GL_ARRAY_BUFFER, objVertProcessed * sizeof(Vertex), vboData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    //Create the IBO for the triangle
    //16 bit indices
    //We could have actually made one big IBO for both the quad and triangle.
    glGenBuffers(1, &objIBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, objIBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, objPolyProcessed * 3 * sizeof(GLuint), iboData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // Create the VAO
    glGenVertexArraysAPPLE(1, &objVAO);
    glBindVertexArrayAPPLE(objVAO);
    glBindBuffer(GL_ARRAY_BUFFER, objVAO);
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);
    glEnableVertexAttribArray(3);
    glVertexAttribPointer(opaque["position"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));
    glVertexAttribPointer(opaque["normal"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 3));
    glVertexAttribPointer(opaque["texcoord"], 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 6));
    glVertexAttribPointer(opaque["color"], 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(float) * 8));
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArrayAPPLE(0);
}

渲染这两个VBO:

        opaque.Use();
        glBindVertexArrayAPPLE(getVAO(2));
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, zoneIBO);
        glBindBuffer(GL_ARRAY_BUFFER, zoneVBO);
        glUniformMatrix4fv(opaque("camera"), 1, GL_FALSE, glm::value_ptr(cameraMat));
        renderGeometry(&cameraMat, 0, curRegion);
        glBindVertexArrayAPPLE(0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        glBindVertexArrayAPPLE(objVAO);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, objIBO);
        glBindBuffer(GL_ARRAY_BUFFER, objVBO);
        glUniformMatrix4fv(opaque("camera"), 1, GL_FALSE, glm::value_ptr(cameraMat));
        renderObjects(&cameraMat);
        glBindVertexArrayAPPLE(0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        opaque.UnUse();

总而言之,我认为这是创建VBO的一个问题。我认为我做错了什么,并且当它受到限制时保持一定的约束力。

1 个答案:

答案 0 :(得分:2)

// Create the VAO
glGenVertexArraysAPPLE(1, &zoneVAO);
glBindVertexArrayAPPLE(zoneVAO);
glBindBuffer(GL_ARRAY_BUFFER, zoneVAO);  //<-------
glEnableVertexAttribArray(0);

在我突出显示的行中,看起来应该是zoneVBO,而不是zoneVAO

你应该用glGetError检查错误,它可能已经发现了这个错误。

在调用renderObjects之前,不需要绑定缓冲区。如果你有顶点指针存储在VAO中,那么你不需要绑定缓冲区,因为顶点指针已经指向正确的位置。

相关问题