PDA

View Full Version : How do I specify a const from outside the shader?



Dr_Davenstein
11-16-2006, 07:23 PM
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++ )
{
}

}

Dr_Davenstein
11-17-2006, 12:29 AM
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! ;)

k_szczech
11-17-2006, 03:15 AM
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.

Dr_Davenstein
11-17-2006, 04:17 AM
Ahhh... thank you very much! I was under the assumption that every single line had to end with a semicolon. ;)

RigidBody
11-17-2006, 04:26 AM
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. ;) why didn't you type


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


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

ZbuffeR
11-17-2006, 06:27 AM
That is why I prefer Kernighan/Ritchie style :


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

RigidBody
11-17-2006, 06:42 AM
Originally posted by ZbuffeR:
That is why I prefer Kernighan/Ritchie style :


if (i < activeLights) {
gl_FragColor +=tColor;
}in my editor, it would look slightly different:


if(i < activeLights) {
gl_FragColor += tColor; }can i have that style named after me :p ?

jide
11-17-2006, 09:20 AM
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...

Humus
11-17-2006, 09:28 AM
Originally posted by Dr_Davenstein:

const int nLights = int(activeLights);This is illegal. You can't assign a non-const integer to a const int.

Dr_Davenstein
11-17-2006, 05:57 PM
Originally posted by Humus:

const int nLights = int(activeLights);This is illegal. You can't assign a non-const integer to a const int. [/B]</font><hr /></blockquote><font size="2" face="Verdana, Arial">Well, the reason I did that, was because of this error:

(16) : error C5043: profile requires index expression to be compile-time constantI'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. ;)

Humus
11-18-2006, 06:18 PM
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.

Dr_Davenstein
11-21-2006, 06:25 PM
Thanks everyone! :)

andras
11-24-2006, 09:41 AM
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.