在 VAO 中使用多个 VBO

问题描述

我尝试在 VAO 中使用 2 VBO,但最终出现崩溃(远远超出我的应用)。

我们的想法是制作第一个 VBO(以及可选的 IBO)来构建几何结构。

这很有效,直到我想到为模型矩阵添加第二个 VBO 作为顶点属性而不是统一属性

所以,当我声明我的网格时,我会做如下(减少代码):

gluint vao = 0;

glCreateVertexArrays(1,&vao);

glBindVertexArray(vao);

gluint vbo = 0;

glCreateBuffers(1,&vbo);

glNamedBufferStorage(vbo,...); // Fill the right data ...

for ( ... my attributes ) // Position,normal,texcoords ...
{
    glVertexArrayAttribFormat(vao,attribIndex,size,GL_FLOAT,GL_FALSE,relativeOffset);

    glVertexArrayAttribBinding(vao,bindingIndex);

    glEnabLevertexArrayAttrib(vao,attribIndex);
} -> this gives me the "stride" parameter for below.

glVertexArrayVertexBuffer(vao,0/*bindingindex*/,vbo,stride/*Size of one element in vbo in bytes*/);

gluint ibo = 0;

glCreateBuffers(1,&ibo);

glNamedBufferStorage(ibo,...); // Fill the right data ...

glVertexArrayElementBuffer(vao,ibo);

在那之前,一切都很好,我所要做的就是调用 glBindVertexArray() 和 glDrawXXX() 命令,我在屏幕上有一些完美的东西。

因此,我决定从着色器中移除 modelMatrix 统一值以使用 mat4 属性, 我本来可以选择 UBO,但我想通过提供多个矩阵将这个想法扩展到实例化渲染。

因此,我在 VBO 中使用一个模型矩阵进行了测试,并在渲染之前进行了如下操作(VBO 之前的构建方式相同,我只为单位矩阵放置了 16 个浮点数):

glBindVertexArray(theObjectVAOBuiltBefore);

const auto bindingIndex = static_cast< gluint >(1); // Here next binding point for the VBO,I guess...
const auto stride = static_cast< GLsizei >(16 * sizeof(GLfloat)); // The stride is the size in bytes of a matrix

glVertexArrayVertexBuffer(theObjectVAOBuiltBefore,bindingIndex,m_vertexBufferObject.identifier(),stride); // I add the new VBO to the currentle VAO which have already a VBO (bindingindex 0) and an IBO

// Then I describe my new VBO as a matrix of 4 vec4.
const auto size = static_cast< GLint >(4);

for ( auto columnIndex = 0U; columnIndex < 4U; columnIndex++ )
{
    const auto attribIndex = static_cast< unsigned int >(VertexAttributes::Type::ModelMatrix) + columnIndex;

    glVertexArrayAttribFormat(theObjectVAOBuiltBefore,0);

    glVertexArrayAttribBinding(theObjectVAOBuiltBefore,bindingIndex);

    glEnabLevertexArrayAttrib(theObjectVAOBuiltBefore,attribIndex);

    glVertexAttribDivisor(attribIndex,1); // Here I want this attribute per instance.
}

glDrawElementsInstanced(GL_TRIANGLES,count,GL_UNSIGNED_INT,nullptr,1);

结果是一个漂亮的崩溃,我没有任何线索,因为崩溃发生在驱动程序中,我无法获得调试输出

我的想法完全是垃圾吗?还是我遗漏了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)