Jump to content
  • Advertisement
Sign in to follow this  

Diffuse Lighting, GLSL.

This topic is 2087 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

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

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!