Search code examples
c++arraysopenglglsl

OpenGL / GLSL - Using buffer objects for uniform array values


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

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

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)


Solution

  • As glGetUniformLocation docs say:

    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.

    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.