PDA

View Full Version : Uniforms inside a GLSL structure



sam_thedancer
05-26-2013, 03:13 PM
Greetings:
I would like to set up a light properties structure in the vertex shader - something like


struct Light
{
uniform vec4 ambientColors;
etc.
};

and then specify a light in the shader


Light light0;

Finally, I want to assign light properties from within the app program with


glGetUniformLocation(programId, "light0.ambientColors");
etc.

But this doesn't work. It seems the uniform declaration of a structure member isn't acceptable. Is this the case or am I missing something?

Thanks,
Sam

Alfonse Reinheart
05-26-2013, 03:23 PM
Remember: a struct doesn't define variables; it is just a prototype.



struct Foo
{
int bar;
};

Foo stuff;


Using C++ lingo `Foo::bar` is a non-static member of the class `Foo`. It therefore does not name a variable. However, `stuff.bar` is a variable. It's the use of a prototype that creates variables.

Uniforms are first and foremost variables. Therefore, a uniform cannot be inside of a prototype definition.

However, a uniform's type can be a struct:



struct Foo
{
int bar;
};

uniform Foo stuff;


`stuff.bar` is now a uniform variable. It has a specific uniform location. See the OpenGL Wiki for further details (http://www.opengl.org/wiki/Uniform_%28GLSL%29).

sam_thedancer
05-27-2013, 02:29 AM
Thank you, Alfonse. That clears it up totally.