【问题标题】:Opengl - instanced attributesOpengl - 实例化属性
【发布时间】:2015-05-30 17:45:49
【问题描述】:

我使用 oglplus - 它是 OpenGL 的 c++ 包装器。

我在为我的粒子渲染器定义实例数据时遇到问题 - 位置工作正常,但当我想从同一个 VBO 实例化一堆整数时出现问题。

我将跳过一些实现细节,以免使这个问题变得更复杂。假设我在描述操作之前绑定了 VAO 和 VBO。

我有一个结构数组(称为“粒子”),我像这样上传:

glBufferData(GL_ARRAY_BUFFER, sizeof(Particle) * numInstances, newData, GL_DYNAMIC_DRAW);

结构体的定义:

struct Particle
{
    float3 position;        
    //some more attributes, 9 floats in total
    //(...)
    int fluidID;        
};

我使用辅助函数来定义 OpenGL 属性,如下所示:

void addInstancedAttrib(const InstancedAttribDescriptor& attribDesc, GLSLProgram& program, int offset=0)
        {
            //binding and some implementation details
            //(...)

            oglplus::VertexArrayAttrib attrib(program, attribDesc.getName().c_str());
            attrib.Pointer(attribDesc.getPerVertVals(), attribDesc.getType(), false, sizeof(Particle), (void*)offset);

            attrib.Divisor(1);
            attrib.Enable();
        }

我像这样为位置和流体添加属性:

            InstancedAttribDescriptor posDesc(3, "InstanceTranslation", oglplus::DataType::Float);
            this->instancedData.addInstancedAttrib(posDesc, this->program);

            InstancedAttribDescriptor fluidDesc(1, "FluidID", oglplus::DataType::Int);
            this->instancedData.addInstancedAttrib(fluidDesc, this->program, (int)offsetof(Particle,fluidID));

顶点着色器代码:

uniform vec3 FluidColors[2];

in vec3 InstanceTranslation;
in vec3 VertexPosition;
in vec3 n;

in int FluidID;

out float lightIntensity;
out vec3 sphereColor;

void main()
{   

    //some typical MVP transformations
    //(...)
    sphereColor = FluidColors[FluidID];
    gl_Position = projection * vertexPosEye;

}

整个代码产生这个输出:

如您所见,粒子按照我想要的方式排列,这意味着“InstanceTranslation”属性设置正确。左边的粒子组的 FluidID 值为 0,右边的粒子组等于 1。第二组粒子具有正确的位置,但在 FluidColors 数组中的索引不正确。

我知道的:

  • 我设置 FluidColors 制服的方式没有问题。如果我像这样在着色器中硬编码颜色选择:

    sphereColor = FluidID == 0? FluidColors[0] : FluidColors1;

我明白了:

  • OpenGL 从 glGetError 返回 GL_NO_ERROR,所以我提供的枚举/值没有问题
  • offsetof 宏没有问题。我尝试使用硬编码值,但它们也不起作用。
  • 这不是 GLint 的兼容性问题,我使用简单的 32 位 Ints(使用 sizeof(int) 检查)
  • 我需要使用 FluidID 作为索引到颜色数组的实例属性,否则,如果我要将粒子组的颜色设置为简单的 vec3 制服,我必须批处理相同的粒子类型(使用同一个 FluidID) 首先放在一起,这意味着对它们进行排序,操作成本太高。

【问题讨论】:

    标签: c++ opengl geometry-instancing


    【解决方案1】:

    对我来说,这似乎是您如何设置fluidID 属性指针的问题。由于您在着色器中使用int 类型,您必须使用glVertexAttribIPointer() 来设置属性指针。您使用普通glVertexAttribPointer() 函数设置的属性仅适用于基于浮点的属性类型。它们接受整数输入,但是当着色器访问它们时,数据将被转换为浮点数。

    在 oglplus 中,如果要使用整数属性,显然必须使用 VertexArrayAttrib::IPointer() 而不是 VertexArrayAttrib::Pointer()。

    【讨论】:

    • 我非常感谢你,它成功了。我浪费了半天的时间试图让这个工作,可能永远不会自己想出答案。在我读过的一篇教程/文章中还没有看到这个东西。再次感谢您。
    猜你喜欢
    • 2014-11-07
    • 2021-02-11
    • 1970-01-01
    • 2011-10-22
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    相关资源
    最近更新 更多