GLSL array index for loop

I'm having trouble using index variables in GLSL. The following GLSL code works fine on NVidia cards. But it does not work on my Intel HD 4000:

for(int i=0;i<int(uLightCount);++i) { vec3 lightPos = uLightsPos[i]; .... } 

There is no shader compiler error. The program just crashes to glUseProgram

How can i fix this?

Edit:

uLightCount and uLightsPos are uniform:

 #define MAX_LIGHTS 10 uniform float uLightCount; uniform vec3 uLightsPos[MAX_LIGHTS]; 

Edit 2:

I found a weird solution:

 #define i0 0 #define i1 1 #define i2 2 ... for(int i=0;i<int(uLightCount);++i) { vec3 lightPos; if (i==i0) lightPos = uLightsPos[i0]; if (i==i1) lightPos = uLightsPos[i1]; .... } 

Any idea why this works?

+7
source share
1 answer

The index must be constant. That is why your workaround is working.

Thus, it is impossible to write that

 for(int i=0;i<10;++i) { result += uLightsPos[i]; } 
+1
source

All Articles