Results 1 to 2 of 2

Thread: Turning off the light in shader

  1. #1
    Newbie Newbie
    Join Date
    May 2014

    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_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
    Why don't you just set/reset some uniform variable and according to its state choose rendering path.

Similar Threads

  1. Light shader displaying nothing
    By Criss_pl in forum OpenGL: GLSL
    Replies: 1
    Last Post: 03-18-2016, 10:18 AM
  2. Specular light shader
    By paradoxresolved in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-20-2014, 06:57 PM
  3. light shader cg and opengl
    By giugio in forum OpenGL: GLSL
    Replies: 5
    Last Post: 04-07-2010, 06:23 PM
  4. Shader Light Array
    By imported_boiler in forum OpenGL: GLSL
    Replies: 3
    Last Post: 12-05-2005, 09:43 AM
  5. simple light shader
    By okapota in forum OpenGL: Advanced Coding
    Replies: 8
    Last Post: 07-01-2002, 12:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean