GLSL Directional Light

Started by
0 comments, last by bluntman 12 years, 10 months ago
I'm trying to write a vertex shader to perform directional lighting. The ambient component of my lighting works but I don't get any lighting at all from the diffuse or specular components.

This is my vertex shader:
[source]
#version 130

struct Material {
vec4 emission, ambient, diffuse, specular;
float shininess;
};


struct DirectionalLight {
vec4 position, ambient, diffuse, specular;
};


uniform mat4 modelViewProjectionMatrix, modelViewMatrix;

uniform vec4 globalAmbient;

uniform Material material;

uniform int numDirectionalLights;
uniform DirectionalLight directionalLights[8];


in vec4 in_vertexA, in_normalA, in_texCoord;


out vec4 frag_texCoord, frag_frontColor;


void main() {
vec4 frontColor = globalAmbient * material.ambient;

vec3 eyeNormal = normalize(vec3(modelViewMatrix * in_normalA));
vec3 eyeVertex = vec3(modelViewMatrix * in_vertexA);

for (int i = 0; i < numDirectionalLights; ++i) {
vec3 lightDirection = normalize(vec3(modelViewMatrix * directionalLights.position));
float normalDotLightDirection = max(dot(eyeNormal, lightDirection), 0.0);

frontColor += material.ambient * directionalLights.ambient;
frontColor += normalDotLightDirection * material.diffuse * directionalLights.diffuse;

if (normalDotLightDirection > 0.0) {
vec3 reflection = -2.0 * eyeNormal * normalDotLightDirection + lightDirection;
frontColor += material.specular * directionalLights.specular * pow(dot(reflection, eyeVertex), material.shininess);
}
}

frag_texCoord = in_texCoord;
frag_frontColor = frontColor;
gl_Position = modelViewProjectionMatrix * in_vertexA;
}
[/source]

I'm not using a normal matrix now but I don't think that matters if I'm not scaling. Does anyone see any obvious problems with this shader? I've checked my normals and they all seem to be right.
Advertisement
Can't see anything immediately wrong (assuming the various matrix*normal calculations have .w component set to 0, to prevent translation). As usual, de-construct your shader, outputting each stage as colour until you find where the values aren't correct. Start by making sure your material and light parameters are getting into the shader correctly but just outputting their colours to all pixels.

This topic is closed to new replies.

Advertisement