Sign in to follow this  
Jethro_T

GLSL Directional Light

Recommended Posts

Jethro_T    194
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[i].position));
float normalDotLightDirection = max(dot(eyeNormal, lightDirection), 0.0);

frontColor += material.ambient * directionalLights[i].ambient;
frontColor += normalDotLightDirection * material.diffuse * directionalLights[i].diffuse;

if (normalDotLightDirection > 0.0) {
vec3 reflection = -2.0 * eyeNormal * normalDotLightDirection + lightDirection;
frontColor += material.specular * directionalLights[i].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.

Share this post


Link to post
Share on other sites
bluntman    255
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this