Problem when using uniform variable as a loop count in fragment shader

I’ve some problem about my fragment shader.

i have a function to calculate lighting define as


void diffuseHemiSphere(int i,vec3 normal,vec3 lightVec,float attenuation){

	vec4 SkyColor = gl_LightSource[i].diffuse;
	vec4 GroundColor = vec4(0.0,0.0,0.0,0.0);
	float costheta = dot(normal, lightVec);
	float a = 0.5 + 0.5 * costheta;
	diffuseTerm += (a*SkyColor + (1.0-a)*GroundColor)*gl_FrontMaterial.diffuse*attenuation;

when i do something like this it work

for(int i=0;i<1;i++){

but when i use uniform variable as a loop count it does’nt work


for(int i=0;i<lightCount;i++){

“lightCount” is a uniform variable define in fragment shader,and I have successfully use it as
a if/else condition.

I have tried it on my both Geforce 7600gt/6100.
using the latest nvidia’s driver

Can someone explain it to me why it did’nt work.



What is “didn’t work”? It won’t compile?

Post the complete shader. Nobody can the if it’s a CG or GLSL shader.
With GLSL it should work. For CG is a profile needet that supports dynamic branching (minimum fp40)

That functionality is not supported in hardware. gl_LightSource[i] is a indexed lookup into constants, which is only supported in the vertex shader in this generation hardware. If you have a static loop it can be unrolled and made into static constant lookups. If you absolutely need to make it dynamic, you’ll need to store indexed data into a 1D texture and sample that instead.

Thank for your answer.

I will try to create a shader based on number of lights in the scene instead of making a dynamic one.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.