0

In the program that I'm coding, I have to generate multiple lights sources and pass is to the shader. As this number is defined outside the shader, I though to pass it through an uniform int and use this to declare the arrays size.

uniform int numLights;
uniform vec4 ambientColor;
uniform vec4 specularColor;
uniform vec3 diffuseColor[numLights];
uniform vec3 vLightPosition[numLights];

However, I'm getting a lot of errors now:

enter image description here

Can't I do that? The thing is that I was thinking of the possibility of changing the number of predefined lights and to forget to update the shader, for example. My attempt was to prevent that to happen.

5
  • possible duplicate of (OpenGL 3.1 - 4.2) Dynamic Uniform Arrays? Commented May 26, 2015 at 17:55
  • Really don't know how do it through this question that you showed, @Axalo. :S Commented May 26, 2015 at 17:58
  • uniform vec3 diffuseColor[numLights]; numLights must be a constant not a uniform. Commented May 26, 2015 at 18:00
  • Post errors as text, not as a screenshot. Commented May 26, 2015 at 18:19
  • I couldn't, @genpfault. The VS doesn't let me to copy the text. Commented May 26, 2015 at 18:34

1 Answer 1

1

GLSL follows the C rule there, that array sizes must be known at compile time.

GLSL has a preprocessor but no means to supply preprocessor values other than as source code. So you'll need to generate the suitable #define statements as strings and supply them to glShaderSource prior to supplying the source for that shader — the calls to glShaderSource accumulate all text passed to them prior to compilation so that's the same as prefixing the extra strings to your source file.

You'll then need to recompile each time you change the number of lights, not just supply a uniform.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.