How do I specify a const from outside the shader?

I want to change the number of lights I loop through, so I was hoping there was a way I could do it by just changing a uniform variable. Something like this, but I get errors. Sorry, I am new to shaders.

uniform int activeLights;
void main()
{
const int nLights = int(activeLights);

for( int i=0;i<nLights;i++ )
{
}

}

I guess my card(nv Gf5500FX) doesn’t support using a uniform variable as the iteration max in a for loop. Every shader I’ve tried using that has borked on it.

So, I thought about trying something like this…

    	if (i < activeLights);
  	{  
  	gl_FragColor +=tColor;
  	}  	

I thought this would work because it should only add the color to the fragment if i<activeLights, but it adds the values anyway.

This is the whole of the fragment shader that I’m working with.

  uniform int activeLights;
uniform sampler2D Texture;
varying vec3 normal, lightDir[2], halfVector[2];
void main()
{
vec4 diffuseLight, specularLight;
vec4 tColor;
float shine[2];
shine[0] = 50.0;
shine[1] = 500.0;
float lightIntensity;
vec3 n = normalize(normal);

for( int i=0;i<2;i++ )
{
	lightIntensity = max(dot(n, normalize(lightDir[i])), 0.0);
	diffuseLight = lightIntensity * gl_LightSource[i].diffuse;
	specularLight =  pow(max(dot(n, normalize(halfVector[i])), 0.0), shine[i]) * gl_LightSource[i].specular;
	tColor = texture2D( Texture, gl_TexCoord[0].xy ) * lightIntensity;
	tColor *=  (diffuseLight * specularLight + gl_LightSource[i].ambient);
  	  	
  	if (i < activeLights);
  	{  
  	gl_FragColor +=tColor;
  	}  	
}

}

Can someone please explain to me why it still adds tColor to gl_FragColor? I know that there are probably some things in that code that are very non-standard, but I’m very new to this. Thank you! :wink:

Can someone please explain to me why it still adds tColor to gl_FragColor?
This is why:

if (i < activeLights);

Remove ; at the end of line.

Ahhh… thank you very much! I was under the assumption that every single line had to end with a semicolon. :wink:

Originally posted by Dr_Davenstein:
Ahhh… thank you very much! I was under the assumption that every single line had to end with a semicolon. :wink:
why didn’t you type

for( int i=0;i<2;i++ );

instead of

for( int i=0;i<2;i++ )

then :wink: ?

That is why I prefer Kernighan/Ritchie style :

if (i < activeLights) {
   gl_FragColor +=tColor;
}

Originally posted by ZbuffeR:
[b] That is why I prefer Kernighan/Ritchie style :

if (i < activeLights) {
   gl_FragColor +=tColor;
}

[/b]
in my editor, it would look slightly different:

if(i < activeLights) {
        gl_FragColor += tColor; }

can i have that style named after me :stuck_out_tongue: ?

This looks like to be the Pico style:

http://en.wikipedia.org/wiki/Indent_style
http://fr.wikipedia.org/wiki/Style_d’indentation

at least in the french version…

Originally posted by Dr_Davenstein:
[b]

const int nLights = int(activeLights);

[/b]
This is illegal. You can’t assign a non-const integer to a const int.

Originally posted by Humus:
[b]

const int nLights = int(activeLights);

[/b]
This is illegal. You can’t assign a non-const integer to a const int. [/b]


Well, the reason I did that, was because of this error:
(16) : error C5043: profile requires index expression to be compile-time constant

I’m actually using the TyphoonLabs Shader Designer to learn with, and some of their example shaders have indexing variables passed to the shader as uniform integers. For some reason, my card doesn’t allow this. I couldn’t find anything in the specs about it, but I gave up looking pretty quickly. I just decided to try and find a way around it. :wink:

Well, your card doesn’t support dynamic branching, so you’ll have to use static loop count. If you need to support varying number of loops you’ll have to create several instances of the shader with each number of loops you plan to use. Just passing a #define value when you compile should work.

Thanks everyone! :slight_smile:

const int nLights = int(activeLights);

[/b]
This is illegal. You can’t assign a non-const integer to a const int.</font><hr /></blockquote><font size=“2” face=“Verdana, Arial”>I have to say that const is somewhat confusing in GLSL. In CPP, it means that the variable is constant in the scope, but it can still have different values, every time you run it. In GLSL, it is a compile time constant. I believe that CG makes this explicit by requiring you to say static const.

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