Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLSL for-loop array index

Tags:

intel

opengl

glsl

I'm having some trouble using variable indices in GLSL. The folowing GLSL code is working fine on NVidia cards. But its not working 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 simply crashes on glUseProgram

How can I fix this?

Edit:

uLightCount and uLightsPos are uniforms:

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

Edit 2:

I have found a strange workaround:

#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 is working?

like image 315
Bastl Avatar asked Apr 16 '13 14:04

Bastl


1 Answers

The index must be constant. That's why your workaround works.

So it's not possible to write that

for(int i=0;i<10;++i)
{
  result += uLightsPos[i];
}
like image 157
Dave Avatar answered Nov 10 '22 11:11

Dave