PDA

View Full Version : Diffuse Lighting



Aumnayan
04-25-2014, 02:19 PM
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:

float light[4] = {-1.0, 1.0, -1.0, 0};
glLightfv(GL_LIGHT0, GL_POSITION, light);

// Vertex Shader

#version 120
varying vec2 multiTexCoord;
void main () {
multiTexCoord = gl_MultiTexCoord0.xy;
gl_Position = ftransform();
}

// Fragment Shader

#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].position.xyz);
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?

reto.koradi
04-26-2014, 09:49 AM
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.