Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGL多个VBO仅渲染一个_C++_Opengl_Vbo - Fatal编程技术网

C++ OpenGL多个VBO仅渲染一个

C++ OpenGL多个VBO仅渲染一个,c++,opengl,vbo,C++,Opengl,Vbo,我有一个网格类,如下所示 Mesh_PTI::Mesh_PTI(bool dynamic) : m_dynamic(dynamic), m_drawCount(0) { glGenVertexArrays(1, m_vertexArrays); glBindVertexArray(m_vertexArrays[0]); glGenBuffers(NUM_BUFFERS, m_buffers); glBindVertexArray(0); } Mesh_PTI

我有一个网格类,如下所示

Mesh_PTI::Mesh_PTI(bool dynamic) : m_dynamic(dynamic), m_drawCount(0)
{
    glGenVertexArrays(1, m_vertexArrays);
    glBindVertexArray(m_vertexArrays[0]);

    glGenBuffers(NUM_BUFFERS, m_buffers);

    glBindVertexArray(0);
}

Mesh_PTI::Mesh_PTI(glm::vec3 positions[], glm::vec2 texCoords[], unsigned short indices[], unsigned short numVertices, unsigned int numIndices, bool dynamic) :
    m_dynamic(dynamic)
{
    glGenVertexArrays(1, m_vertexArrays);
    glBindVertexArray(m_vertexArrays[0]);

    glGenBuffers(NUM_BUFFERS, m_buffers);

    createBuffers(positions, texCoords, indices, numVertices, numIndices, false);

    glBindVertexArray(0);

    m_drawCount = numIndices;
}

Mesh_PTI::~Mesh_PTI()
{
    glDeleteBuffers(NUM_BUFFERS, m_buffers);
    glDeleteVertexArrays(1, m_vertexArrays);
}

void Mesh_PTI::draw()
{
    if(m_drawCount > 0)
    {
        glBindVertexArray(m_vertexArrays[0]);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_buffers[INDEX_VB]);
        glDrawElements(GL_TRIANGLES, m_drawCount, GL_UNSIGNED_SHORT, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }
}

void Mesh_PTI::setData(glm::vec3 positions[], glm::vec2 texCoords[], unsigned short indices[], unsigned short numVertices, unsigned int numIndices)
{
    glBindVertexArray(m_vertexArrays[0]);
    createBuffers(positions, texCoords, indices, numVertices, numIndices, false);
    glBindVertexArray(0);
    m_drawCount = numIndices;
}

void Mesh_PTI::createBuffers(glm::vec3 positions[], glm::vec2 texCoords[], unsigned short indices[], unsigned short numVertices, unsigned int numIndices, bool dynamic)
{
    glBindBuffer(GL_ARRAY_BUFFER, m_buffers[POSITION_VB]);
    glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(positions[0]), positions, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

    glBindBuffer(GL_ARRAY_BUFFER, m_buffers[TEXCOORD_VB]);
    glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(texCoords[0]), texCoords, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_buffers[INDEX_VB]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(indices[0]), indices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 1, GL_SHORT, GL_FALSE, 0, NULL);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
我需要更新顶点数据。如果我删除网格并使用构造函数加载顶点数据,一切正常

如果我使用第一个构造函数初始化它,并使用
setData
函数加载顶点数据,则该类的多个实例仅呈现调用了
setData
的最后一个实例


我做错了什么?

glGenBuffers只在调用方法时返回可用的缓冲区名称,而不保留这些名称。因此,下次调用glGenBuffers时,如果不将任何内容绑定到glGenBuffers调用的第一个缓冲区,您将得到相同的名称,因为它们还没有被使用。稍后调用glBindBuffers时,您会发现所有实例的VBO都使用相同的名称,因此它们会相互覆盖

您还尝试将元素数组缓冲区绑定为顶点属性,而顶点属性不会 有任何意义,因为索引是作为GLDraweElements的一部分使用的(除非您使用 出于某种原因,请在着色器中删除它们)


另一个相关的注意事项是:在每次使用VAO绘制之前,您不需要绑定索引缓冲区,因为索引缓冲区是图形的一部分。指定的顶点数据是使用glVertexPointer*函数绑定的,因此顶点->属性绑定及其相应的VBO也是状态的一部分,但GL_数组_缓冲区不是。是否在调用setData之间调用draw?
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_SHORT, GL_FALSE, 0, NULL);
//                          ^~~~~ but your indices are GL_UNSIGNED_SHORT