9

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?

4

2 回答 2

3

索引必须是常数。这就是您的解决方法有效的原因。

所以不可能这么写

for(int i=0;i<10;++i)
{
  result += uLightsPos[i];
}
于 2014-10-21T07:15:25.283 回答
0

在循环内部,我这样做了:

for(int i=0;i<n;i++) {
    int j = i;
    and use myArray[j];
}

它编译...所以你可以在你的代码中尝试。

于 2020-07-17T21:30:57.160 回答