Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Diffuse Lighting

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member Newbie
    Join Date
    Mar 2011

    Diffuse Lighting

    My goal is to have a directional light source steming from the upper left corner of the screen regardless of the scene's transformations. I'm obviously missing something since the lighting is doesn't always seam to stem from the appropriate location when I rotate the scene.

    // Application Side:
    Code :
    float light[4] = {-1.0, 1.0, -1.0, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, light);

    // Vertex Shader
    Code :
    #version 120
    varying vec2 multiTexCoord;
    void main () {
      multiTexCoord = gl_MultiTexCoord0.xy;
      gl_Position = ftransform();

    // Fragment Shader
    Code :
    #version 120
    uniform sampler2D grndTexture;
    uniform sampler2D normalTexture;
    uniform sampler2D colorTexture;
    varying vec2 multiTexCoord;
    void main () {
      vec2 terCord = vec2 (multiTexCoord.x * 127.0 / 20.0, multiTexCoord.y * 127.0 / 20.0);
      vec3 n = normalize(texture2D(normalTexture, multiTexCoord).xyz);
      vec3 t = texture2D(grndTexture, terCord).xyz;
      vec3 l = normalize(gl_LightSource[0];
      vec3 c = texture2D(colorTexture, multiTexCoord).xyz;
      float diffuse = min(dot(n, l), 0.8);\n"
      vec3 final = c * t * diffuse;
      gl_FragColor = vec4 (final.r, final.g, final.b, 1.0);

    Can anyone point out where my brain's malfunctioning here?

  2. #2
    Intern Contributor
    Join Date
    Mar 2014
    San Jose, CA
    Since you're using ftransform() in your vertex shader, you probably have a modelview transformation set up? You'll have to apply the corresponding normal transformation to your normals.

    Your light position also looks somewhat suspicious. Since the z-coordinate is negative, I think it would light the backside of your geometry.

    The overall lighting effect would probably not be very pronounced, since you cap the diffuse value between 0.8 and 1.0.

Posting Permissions

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