本文介绍了OpenGL的/ GLSL - 使用缓冲区对象为均匀阵列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的(片段)着色器包含12个结构的均匀数组:

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

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

在我的节目,我有每个包含一个光源数据12缓冲区对象。 (他们需要单独的缓冲器。)

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

我怎么能这些缓冲区绑定到着色器内各自的位置?

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

这些运行而无需调用一个错误,但位置肯定是不对的(4294967295)。 (该阵列所使用的着色器里面,​​所以我不认为它是被优化掉了)

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)

推荐答案

作为的说:

As glGetUniformLocation docs say:

名称必须是一个积极匀变速
      在程序的名称不是一个结构,
      结构的阵列
或一个载体或一个的子组件
      矩阵。

...

统一变量是结构或数组
      结构可致电查询
      glGetUniformLocation 每个字段
      结构

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

所以,你只能查询一次场。
像这样的:

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

希望它帮助。

您可以通过使用 和的。这将是更喜欢的DirectX不变的缓冲区。所需的硬件/驱动程序支持:要么OpenglGL 3.1芯或扩展。

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.

这篇关于OpenGL的/ GLSL - 使用缓冲区对象为均匀阵列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:58