OpenGL Diffuse shader problem

Hey, as always, I'll jump right into it!

I'm writing a 3D game engine, and the diffiuse lightning seems to have some issues.

It would be hard to describe the problem, so here are two small clips of it happening!

https://youtu.be/N2x1nxwAnF0
https://youtu.be/7xnANGHcEjU


I know for sure that my normals are correct!

Here I have the fragment shader part that calculates the diffuse component
1
2
3
4
vec3 norm = normalize(normal);
vec3 lightDirection = normalize(lightPos - fragPos);
float diff = max(dot(norm, lightDirection), 0.0);
vec3 diffuse = diff * lightColor;


What am I missing?

Thanks in advance!
Last edited on
The position of the fragment is relative to window space, which is why the lighting effects are changing as you move. Compute the normals and the light position in world space (i.e., model-view transformed) in the vertex shader and give the results to the fragment shader to light each fragment per the cosine law.

Also, don't forget to clamp diffuse in [0, 1].

Edit: I mean to say that something is in the wrong coordinate space. (I'm very rusty.)
Last edited on
Well i adapted the code like you suggested, and it seems to work better, but sadly not perfectly!

http://i.imgur.com/YBNFmE4.png

I'll just do the kiddo' thingy and copy paste my shaders in here! They're not that complex.

Vertex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#version 330

layout(location = 0) in vec3 _vertex;
layout(location = 1) in vec3 _normal;
layout(location = 2) in vec4 _vertexColor;

out vec3 normal;
out vec3 fragPos;
out vec4 vertexColor;
out vec3 worldSpacePosition;
  
uniform mat4 projection;
uniform mat4 view;
uniform mat4 transformation;
  
void main(){
  gl_Position = (projection * view * transformation * vec4(_vertex, 1));
  
  vec3 position = transformation[3].xyz;

  normal = _normal;
  fragPos = vec3(gl_Position);
  vertexColor = _vertexColor;
  worldSpacePosition = position;
 }


Fragment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#version 330

in vec3 normal;
in vec3 fragPos;
in vec4 vertexColor;
in vec3 worldSpacePosition;

out vec4 color;

uniform vec3 lightPos;
uniform vec3 lightColor;

void main(){

//AMBIENT
	float ambientStrenght = 0.1f;
	vec3 ambient = ambientStrenght * lightColor;

//DIFFUSE
	vec3 norm = normalize(normal);
	vec3 lightDirection = normalize(lightPos - worldSpacePosition);
	float diff = max(dot(norm, lightDirection), 0.0);
	vec3 diffuse = clamp(diff * lightColor, 0, 1);

	color = vec4((ambient + diffuse), 1) * vertexColor;
}



Am i right that when I pass something from the vertex shader to the fragement shader, it gets interpolated?

EDIT: I should mention that every differend colored rectangles / chunks a single VBO is
Last edited on
Topic archived. No new replies allowed.