Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: How do I specify a const from outside the shader?

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2006
    Location
    United States
    Posts
    23

    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.

    Code :
    uniform int activeLights;
    void main()
    {
    const int nLights = int(activeLights);
     
    for( int i=0;i<nLights;i++ )
    {
    }
     
    }

  2. #2
    Junior Member Newbie
    Join Date
    Mar 2006
    Location
    United States
    Posts
    23

    Re: How do I specify a const from outside the shader?

    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...
    Code :
        	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.
    Code :
      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!

  3. #3
    Senior Member OpenGL Pro k_szczech's Avatar
    Join Date
    Feb 2006
    Location
    Poland
    Posts
    1,119

    Re: How do I specify a const from outside the shader?

    Can someone please explain to me why it still adds tColor to gl_FragColor?
    This is why:
    Code :
    if (i < activeLights);
    Remove ; at the end of line.

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2006
    Location
    United States
    Posts
    23

    Re: How do I specify a const from outside the shader?

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

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    664

    Re: How do I specify a const from outside the shader?

    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

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

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

  6. #6
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: How do I specify a const from outside the shader?

    That is why I prefer Kernighan/Ritchie style :

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

  7. #7
    Advanced Member Frequent Contributor
    Join Date
    Aug 2004
    Location
    munich, germany
    Posts
    664

    Re: How do I specify a const from outside the shader?

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

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

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

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,749

    Re: How do I specify a const from outside the shader?

    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...

  9. #9
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,444

    Re: How do I specify a const from outside the shader?

    Originally posted by Dr_Davenstein:
    Code :
    const int nLights = int(activeLights);
    This is illegal. You can't assign a non-const integer to a const int.

  10. #10
    Junior Member Newbie
    Join Date
    Mar 2006
    Location
    United States
    Posts
    23

    Re: How do I specify a const from outside the shader?

    Originally posted by Humus:
    Code :
    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:
    Code :
    (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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •