How to pass uniform array of struct to shader via C++ code

jpm picture jpm · May 11, 2014 · Viewed 19.2k times · Source

for eg. in FragmentShader:-

struct LightSource
{
        int Type;
        vec3 Position;
        vec3 Attenuation;
        vec3 Direction;
        vec3 Color;
};

uniform LightSource Light[4];

main(){
        //somecode
}

Now how can i send values for Light[4].

Answer

user3256930 picture user3256930 · May 11, 2014

You will need to get the location of each field of the struct for each array element and send the value separately. See the OpenGL wiki page for reference: https://www.khronos.org/opengl/wiki/Uniform_(GLSL)#Uniform_management.

For example to set the value of Light[0].Type you would do the following:

GLuint loc = glGetUniformLocation(shader_program_id, "Light[0].Type");
glUniform1i(loc, value);