0

我尝试在 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, attribIndex, bindingIndex);

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

glVertexArrayVertexBuffer(vao, 0/*bindingindex*/, vbo, 0, 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(), 0, 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, attribIndex, size, GL_FLOAT, GL_FALSE, 0);

    glVertexArrayAttribBinding(theObjectVAOBuiltBefore, attribIndex, bindingIndex);

    glEnableVertexArrayAttrib(theObjectVAOBuiltBefore, attribIndex);

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

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

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

我的想法完全是垃圾吗?还是我错过了什么?

4

1 回答 1

1

我发现错误 glVertexAttribDivisor() 是旧方法的一部分(如 glVertexAttribPointer(),...),我切换到 glVertexBindingDivisor()/glVertexArrayBindingDivisor() 现在根本没有崩溃。

答案在那里:https ://www.khronos.org/opengl/wiki/Vertex_Specification#Separate_attribute_format

于 2020-12-24T04:36:18.097 回答