Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Turning off the light in shader

  1. #1
    Newbie Newbie
    Join Date
    May 2014
    Posts
    1

    Turning off the light in shader

    This is my fragment shader:

    Code :
    varying vec3 n;
    varying vec3 position;
    varying vec2 tex_coord;
     
    uniform sampler2D wood_tex;
    uniform int light2;
    void main()
    {
        vec3 light = normalize((gl_LightSource[2].position - position).xyz);
        vec3 h = normalize(normalize(- position) + light);  
     
        vec4 amb = gl_LightSource[2].ambient * gl_FrontMaterial.ambient;
        vec4 diff = gl_LightSource[2].diffuse * gl_FrontMaterial.diffuse * max(0.0, dot(n, light));
        vec4 spec = gl_LightSource[2].specular * gl_FrontMaterial.specular * pow(max(0.0, dot(h, n)), gl_FrontMaterial.shininess);
     
        vec4 wood = texture2D(wood_tex, tex_coord);
        vec4 color = vec4(0.0,0.0,0.0,0.0);
     
        color = (amb + diff) * wood + spec;
     
        gl_FragColor = color;
    }

    Vertex shader:

    Code :
    varying vec3 n;
    varying vec3 position;
    varying vec2 tex_coord;
     
    void main()
    {
        n = normalize(gl_NormalMatrix * gl_Normal);
        position = (gl_ModelViewMatrix * gl_Vertex).xyz;
     
        tex_coord = gl_MultiTexCoord0.st;
     
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    }

    I use these shaders to texture object in my scene. The problem is that when the scene is loaded the object is already illuminated even though the light is turned off. I would like to ask how can I make the shader illuminate the object only when the light is turned on and when I turn the light off the shader will also stop illuminating the object.

  2. #2
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,144
    Why don't you just set/reset some uniform variable and according to its state choose rendering path.

Posting Permissions

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