Lights Sources Not Working

Started by DarkerMinecraft, December 09, 2019, 18:57:47

Previous topic - Next topic

DarkerMinecraft

I am trying to create more than one lights sources in the shaders but I am getting no light now... I have set max lights to 4 in the shaders. Here is the Vertex Shader
#version 400 core

in vec3 position;
in vec3 normal;

in vec2 textureCoords;

out vec2 pass_textureCoords;

out vec3 surfaceNormal;
out vec3 toLightVector[4];
out vec3 toCameraVector;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

uniform vec3 lightPosition[4];

uniform float fakeLighting;
uniform float numberOfRows;

uniform vec2 offset;

void main(void) {

	vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
	
	gl_Position = projectionMatrix * viewMatrix * worldPosition;
	pass_textureCoords = (textureCoords/numberOfRows) + offset;
	
	vec3 actualNormal = normal;
	
	if(fakeLighting > 0.5) {
		actualNormal = vec3(0.0, 1.0, 0.0);
	} 
	
	surfaceNormal = (transformationMatrix * vec4(actualNormal, 0.0)).xyz;
	for(int i=0; i<=4; i++) {
		toLightVector[i] = lightPosition[i] - worldPosition.xyz;
	}
	
	toCameraVector = (inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 0.0)).xyz - worldPosition.xyz;

}

Here is the Fragment Shader
#version 400 core

in vec2 pass_textureCoords;

in vec3 surfaceNormal;
in vec3 toLightVector[4];
in vec3 toCameraVector;

out vec4 out_Color;

uniform sampler2D textureSampler;

uniform vec3 lightColor[4];

uniform float shineDamper;
uniform float reflectivity;

void main(void) {
	
	vec3 unitNormal = normalize(surfaceNormal);
	vec3 unitVectorToCamera = normalize(toCameraVector);
	
	vec3 totalDiffuse = vec3(0.0);
	vec3 totalSpecular = vec3(0.0);
	
	for(int i=0; i<4; i++) {
		vec3 unitLightVector = normalize(toLightVector[i]);
		float nDot1 = dot(unitNormal, unitLightVector);
		float brightness = max(nDot1, 0.0);
		vec3 lightDirection = -unitLightVector;
		vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
		float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
		specularFactor = max(specularFactor, 0.0);
		float dampedFactor = pow(specularFactor, shineDamper);
		totalDiffuse += brightness * lightColor[i];
		totalSpecular += dampedFactor * reflectivity * lightColor[i];
	}
	totalDiffuse = max(totalDiffuse, 0.2);
	
	vec4 textureColor = texture(textureSampler, pass_textureCoords); 
	
	if(textureColor.a < 0.5) {
		discard;
	}
	
	out_Color = vec4(totalDiffuse, 1.0) * textureColor + vec4(totalSpecular, 1.0);

}

KaiHH

In standard OpenGL forward rendering, people usually compute lighting in view space. This spares you from bringing your camera direction and vertex positions into world space, which is wrong by the way.
You want `(inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 1.0)).xyz` and not `(inverse(viewMatrix) * vec4(0.0, 0.0, 0.0, 0.0)).xyz`. The latter is always the zero vector.

DarkerMinecraft

I change the toCameraVector but I am getting the exact same thing. It did work with one light source