简体   繁体   English

在 VAO 中使用多个 VBO

[英]Using multiple VBO in a VAO

I try to use 2 VBO inside a VAO and I end up with a crash (far beyond my app).我尝试在 VAO 中使用 2 VBO,但最终崩溃了(远远超出了我的应用程序)。

The idea is to make a first VBO (and optionnally an IBO) to stucture the geometry.这个想法是制作第一个 VBO(以及可选的 IBO)来构造几何体。

This worked well, until I get the idea to add a second VBO for the model matrix as a vertex attribute instead of an uniform.这很有效,直到我想到为 model 矩阵添加第二个 VBO 作为顶点属性而不是统一。

So, when I declare my mesh I do as follow (reduced code):因此,当我声明我的网格时,我会按照以下步骤操作(简化代码):

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);

Until there, everything is fine, all I have to do is to call glBindVertexArray() and a glDrawXXX() command, I have something perfect on screen.直到那里,一切都很好,我所要做的就是调用 glBindVertexArray() 和 glDrawXXX() 命令,我在屏幕上有完美的东西。

So, I decided to remove the modelMatrix uniform from the shader to use a mat4 attribute, I could have choose an UBO instead but I want to extend the idea to instancing rendering by providing several matrices.因此,我决定从着色器中删除 modelMatrix uniform 以使用 mat4 属性,我本可以选择 UBO,但我想通过提供多个矩阵将想法扩展到实例化渲染。

So, I tested with one model matrix in a VBO and just before the rendering, I do as follow (the VBO is built the same way before, I just put 16 floats for an identity matrix):因此,我在 VBO 中使用一个 model 矩阵进行了测试,就在渲染之前,我按照以下步骤进行操作(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);

And the result is a beautiful crash, I don't have any clue because the crash occurs within the driver where I can't have a debug output.结果是一次漂亮的崩溃,我没有任何线索,因为崩溃发生在我无法调试 output 的驱动程序中。

Is my idea a complete garbage?我的想法完全是垃圾吗? Or there is something I missed?或者我错过了什么?

I found the error glVertexAttribDivisor() is part of the old ways (like glVertexAttribPointer(), ...), I switched to glVertexBindingDivisor()/glVertexArrayBindingDivisor() and now there is no crash at all.我发现错误 glVertexAttribDivisor() 是旧方法的一部分(如 glVertexAttribPointer(), ...),我切换到 glVertexBindingDivisor()/glVertexArrayBindingDivisor() 现在根本没有崩溃。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM