【问题标题】:Understanding opengl instanced arrays理解 opengl 实例化数组
【发布时间】:2012-09-17 08:00:18
【问题描述】:

给定

std::vector<GLuint> cubeIndices;
struct FaceGroup {
    unsigned int face_index;
    unsigned int start_index;
    size_t length;
    // comparison operators omitted
};
std::set<FaceGroup>::iterator i;
GLuint attribIndex;

我通过从start_indexstart_index + length 循环遍历cubeIndices 中的每个索引来渲染集合中的每个FaceGroup,如下所示:

for (unsigned ix = 0; ix < i->length; ++ix) {
    glDisableVertexAttribArray (attribIndex);
    glVertexAttribI1ui (attribIndex, cubeIndices [i->start_index + ix]);
    glDrawElements (GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, (void*) (i->face_index * sizeof (GLuint)));
}

... 这给了我正确的结果。现在我想使用实例化数组渲染同样的东西。我的推理告诉我,下面的代码等价于上面的循环:

glEnableVertexAttribArray (attribIndex);
glVertexAttribDivisorARB (attribIndex, 1);
glVertexAttribPointer (attribIndex, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof (GLuint), &cubeIndices [i->start_index]);
glDrawElementsInstancedARB (GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, (void*) (i->face_index * sizeof (GLuint)), i->length);

但似乎只渲染每组人脸中的第一个(*试探性分析,我可能错了)。我做错了什么?

【问题讨论】:

    标签: opengl vertex vertex-array vertex-attributes


    【解决方案1】:
    glVertexAttribI1ui (attribIndex, cubeIndices [i->start_index + ix]);
    glVertexAttribPointer (attribIndex
    

    这些不做同样的事情。

    奇怪的是你知道glVertexAttribI*这样的深奥函数,却不知道创建整数数组时需要使用glVertexAttribIPointer。如果您使用glVertexAttribPointer,您正在传输浮点值;提供的任何整数值都将被转换为浮点数(这就是为什么glVertexAttribPointer 有一个参数告诉它是否被规范化)。

    所以你应该使用glVertexAttribIPointer。除非您将着色器更改为使用float 而不是uint 作为attribIndex 的输入。

    【讨论】:

    • 谢谢!!!它现在可以工作了... :) 在我的辩护中,我只发现了 glVertexAttribI1ui,因为我最初尝试了 glVertexAttrib1i,但它不存在,然后我转到 glVertexAttrib 手册页,在那里我找到了 @ 987654333@ 函数。
    猜你喜欢
    • 2011-10-22
    • 2016-12-25
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    相关资源
    最近更新 更多