Advertisement Jump to content
Sign in to follow this  

GLSL Directional Light

This topic is 2782 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
#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;

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
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
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!