Sign in to follow this  

Diffuse Lighting, GLSL.

Recommended Posts

Slushy    168

Hello, im back again with another question. So I've been working on diffuse lighting for a couple of days now and I can't seem to get it working properly. I get the feeling that there is something messed up with my direction/position Vector of the light. But I can't seem to figure out what. Either way, here are my shaders:


#version 330

uniform mat4 perspectiveMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

layout(location = 0) in vec3 in_Position;
layout(location = 1) in vec2 in_TextureCoord;
layout(location = 2) in vec3 in_Normal;

out vec2 frag_TextureCoord;
out vec3 normal;

void main(void){
	gl_Position = perspectiveMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
	normal = mat3(modelMatrix) * in_Normal;
	normal = normalize(normal);
	frag_TextureCoord = in_TextureCoord;


#version 330

uniform sampler2D texture_diffuse;

uniform vec3 lightPosition;
uniform vec3 lightColor;
uniform float lightPower;

in vec2 frag_TextureCoord;
in vec3 normal;

out vec3 out_Color;

void main(void){
	float lightIntensity;
	vec3 lightDir;
	lightDir = -lightPosition;
	lightIntensity = clamp(dot(normal, lightDir), 0.0f, 1.0f);
	out_Color = clamp((lightColor * lightIntensity), 0.0f, 1.0f);
	out_Color = out_Color * texture(texture_diffuse, frag_TextureCoord).rgb;

My position/direction vector that I send to my fragment shader in the uniform is: 0, -1, 0

and what I get is this:


Share this post

Link to post
Share on other sites
Slushy    168

I solved it, it's kinda embarassing. I had the offset wrong in my interleaved vbo. It gathered the wrong normals and that messed it up.

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