OpenGL / GLSL -使用缓冲对象来实现一致的数组值。

[英]OpenGL / GLSL - Using buffer objects for uniform array values


My (fragment) shader has a uniform array containing 12 structs:

我的(片段)着色器有一个包含12个结构体的统一数组:

struct LightSource
{
    vec3 position;
    vec4 color;
    float dist;
};
uniform LightSource lightSources[12];

In my program I have 12 buffer objects that each contain the data for one light source. (They need to be seperate buffers.)

在我的程序中,我有12个缓冲对象,每个对象都包含一个光源的数据。(它们需要是分离的缓冲区。)

How can I bind these buffers to their respective position inside the shader?

我如何将这些缓冲区绑定到它们在着色器中的各自位置?

I'm not even sure how to retrieve the location of the array.

我甚至不知道如何检索数组的位置。

glGetUniformLocation(program,"lightSources");
glGetUniformLocation(program,"lightSources[0]");

These run without invoking an error, but the location is definitely wrong(4294967295). (The array is being used inside the shader, so I don't think it's being optimized out)

这些运行没有调用错误,但是位置肯定是错误的(4294967295)。(这个数组在材质中使用,所以我不认为它被优化出来了)

1 个解决方案

#1


4  

As glGetUniformLocation docs say:

作为glGetUniformLocation医生说:

name must be an active uniform variable name in program that is not a structure, an array of structures, or a subcomponent of a vector or a matrix.

在程序中,名称必须是活动的统一变量名,而不是结构、结构数组或向量或矩阵的子组件。

...

Uniform variables that are structures or arrays of structures may be queried by calling glGetUniformLocation for each field within the structure.

可以通过调用结构内每个字段的glGetUniformLocation查询结构或结构数组的一致变量。

So, you can only query one field at a time. Like this:

因此,一次只能查询一个字段。是这样的:

glGetUniformLocation(program,"lightSources[0].position")
glGetUniformLocation(program,"lightSources[0].color")
glGetUniformLocation(program,"lightSources[0].dist")

Hope it helps.

希望它可以帮助。

Edit:

You can make your life easier (at a cost of old hardware/drivers compatibility) by using Interface Blocks, Uniform Buffer Objects and glGetUniformBlockIndex. This will be more like DirectX constant buffers. Required hardware/drivers support for that: either OpenglGL 3.1 core or ARB_uniform_buffer_object extension.

通过使用接口块、统一的缓冲区对象和glGetUniformBlockIndex,您可以使您的生活更轻松(以牺牲旧的硬件/驱动兼容性为代价)。这更像是DirectX常量缓冲区。为此所需的硬件/驱动程序支持:OpenglGL 3.1 core或ARB_uniform_buffer_object扩展。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/12/27/7def5a27cb5f2ecb872aa30ca49e0201.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告