I would like to leave OpenGL's lights and make my own. I would like my shaders to allow for a variable number of lights.
Can we declare an array of uniforms in GLSL shaders? If so, how would we set the values of those uniforms?
To initialize an array you can use a constructor. Like in object orientation GLSL has a constructor for arrays. The syntax is as follows. int a[4] = int[](4, 2, 0, 5, 1); float a[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1); int[] c = int[3](1, 2, 3); int[] d = int[](5, 7, 3, 4, 5, 6);
A uniform is a global Shader variable declared with the "uniform" storage qualifier. These act as parameters that the user of a shader program can pass to that program. Their values are stored in a program object.
GLSL does not support anonymous structures (ie: structs without a type name), and structs must have at least one member declaration. Structs cannot be defined within another struct, but one struct can use another previously defined struct as a member.
According to the OpenGL wiki, a uniform is “a global GLSL variable declared with the 'uniform' storage qualifier.” To be a little more specific: your shader executes on the GPU, which is physically distinct from the rest of the computer, separated by a bus.
Yes this is possible. You declare uniform arrays similar to how you'd do it in C, e.g.
uniform float v[10];
Then you can set their values using glUniform{1,2,3,4}{f,i}v
GLfloat v[10] = {...}; glUniform1fv(glGetUniformLocation(program, "v"), 10, v);
Yes it is possible to declare an array of uniforms in GLSL shaders. Just google "glsl uniform array" for some examples (edit: or see datenwolf's example). There are however limitations on how many uniforms can be sent to different graphics cards (at least on older ones, I'm not sure about current ones (although I imagine there still would be)).
If you do decide to go down the route of uniforms, i would suggest using uniform buffers. According to http://www.opengl.org/wiki/Uniform_Buffer_Object, "Switching between uniform buffer bindings is typically faster than switching dozens of uniforms in a program".
If you have large numbers of lights and parameters, you could also send the data as float buffers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With