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擴展。


注意!

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



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