Lighting engine isn't working

So I’ve got this shader program that shades a vbo the size of the screen for every light, and entity

How it works:


for(Light light : lights){
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);
			
	_lighting.bind();
			
	glUniform3f(glGetUniformLocation(_lighting.programID, "ambientLight"), ambientLight.x, ambientLight.y, ambientLight.z);
	glUniform2f(glGetUniformLocation(_lighting.programID, "lightLocation"), light.lightLocation.x, Display.getHeight() - light.lightLocation.y);
	glUniform3f(glGetUniformLocation(_lighting.programID, "lightColor"), light.lightColor.x, light.lightColor.y, light.lightColor.z);
						
	glPushMatrix();
		glTranslatef(Display.getWidth() / 2, Display.getHeight() / 2, 0); //Divided because of a scaling issue
				
		_screen.bind();
			glDrawArrays(GL_TRIANGLES, 0, 6); //Size of the screen
		_screen.unbind();
	glPopMatrix();
			
	for(WorldEntity entity : worldEntities){
		entity.onRender(this);
	}
			
	glDisable(GL_BLEND);
}

Fragment shader:

varying position;

uniform vec2 lightLocation;
uniform vec3 lightColor;
uniform vec3 ambientLight;

uniform sampler2D diffuse;
uniform sampler2D normal;
uniform sampler2D specular;

void main() {
	vec4 diff = texture2D(diffuse, gl_TexCoord[0].st);	
	vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).xy * 2.0 - 1.0);
	vec4 spec = texture2D(spec, gl_TexCoord[2].st);
	
	vec4 ambient = vec4(ambientLight.x, ambientLight.y, ambientLight.z, 1.0);
	vec4 diffuse = diff;
	vec4 specular = spec;
	
	vec3 vector = lightLocation.xy - position.xy;
	float distance = sqrt((vector.x * vector.x) + (vector.y * vector.y));
	
	vec4 final = ambient + diffuse + specular;
	final.xy *= max(0.0, dot(normalize(vector), bump)) / distance;
	
	gl_FragColor = final;
}

Vertex shader:

varying position;

void main()
{
	gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
	position = gl_Position;
	
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[1] = gl_MultiTexCoord1;
	gl_TexCoord[2] = gl_MultiTexCoord2;
}

Test entity rendering code:

glPushMatrix();
	glUniform1f(glGetUniformLocation(world.getLightingProgram().programID, "diffuse"),  0);
	glUniform1f(glGetUniformLocation(world.getLightingProgram().programID, "normal"),   1);
	glUniform1f(glGetUniformLocation(world.getLightingProgram().programID, "specular"), 2);
			
	glActiveTexture(GL_TEXTURE0);
	glEnable(GL_TEXTURE_2D);
	testBatch.bind();
			
	glActiveTexture(GL_TEXTURE1);
	glEnable(GL_TEXTURE_2D);
	testNormal.bind();
			
			glActiveTexture(GL_TEXTURE2);
			glEnable(GL_TEXTURE_2D);
			testSpecular.bind();
				
		testBatch.bind();
		 glDrawArrays(GL_TRIANGLES, 0, 6);
		testBatch.unbind();
			
	testTexture.unbind();
	testNormal.unbind();
	testSpecular.unbind();
glPopMatrix();

Result:

Well first of all, check for any errors the shader might be throwing. I don’t see any code in there doing that.
Once you’ve done that you’ll most likely find some errors. Fix them, then come back.
If there’s no errors then, well, we’ll figure out what’s going on.

Check this out:
http://www.opengl.org/wiki/OpenGL_Shading_Language#Error_Checking

The log says;


Errors in fragment shader:
 0(1) : error C0000: syntax error, unexpected identifier, expecting "::" at token "position"
0(13) : error C1060: incompatible types in initialization
0(13) : error C1056: invalid initialization

Errors in vertex shader:
0(1) : error C0000: syntax error, unexpected identifier, expecting "::" at token "position"

Errors with link: 
0(1) : error C0000: syntax error, unexpected identifier, expecting "::" at token "position"


...
varying position;
...

There’s your problem. ‘position’ isn’t given a type.

Alright, fixed that.

Errors in fragment shader: 

0(13) : error C1060: incompatible types in initialization
0(13) : error C1056: invalid initialization
0(20) : error C1060: incompatible types in initialization
0(20) : error C1056: invalid initialization

New fragment shader:

varying vec2 position;

uniform vec2 lightLocation;
uniform vec3 lightColor;
uniform vec3 ambientLight;

uniform sampler2D diffuse;
uniform sampler2D normal;
uniform sampler2D specular;

void main() {
	vec4 diff = texture2D(diffuse, gl_TexCoord[0].st);	
	vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).xy * 2.0 - 1.0);
	vec4 spec = texture2D(spec, gl_TexCoord[2].st);
	
	vec4 ambient = vec4(ambientLight.x, ambientLight.y, ambientLight.z, 1.0);
	vec4 diffuse = diff;
	vec4 specular = spec;
	
	vec3 vector = lightLocation.xy - position.xy;
	float distance = sqrt((vector.x * vector.x) + (vector.y * vector.y));
	
	vec4 final = ambient + diffuse + specular;
	final.xy *= max(0.0, dot(normalize(vector), bump)) / distance;
	
	gl_FragColor = final;
}

New vertex shader

varying vec2 position;

void main()
{
	gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
	position = gl_Position;
	
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[1] = gl_MultiTexCoord1;
	gl_TexCoord[2] = gl_MultiTexCoord2;
}

	vec3 vector = lightLocation.xy - position.xy;

should be


	vec2 vector = lightLocation.xy - position.xy;

Okay, fixed that, and anouther warning from the normalize method because it can’t take a vec2, fixed that.

Errors in fragment shader:
 0(13) : error C1060: incompatible types in initialization
0(13) : error C1056: invalid initialization

varying vec2 position;

uniform vec2 lightLocation;
uniform vec3 lightColor;
uniform vec3 ambientLight;

uniform sampler2D diffuse;
uniform sampler2D normal;
uniform sampler2D specular;

void main() {
	vec4 diff = texture2D(diffuse, gl_TexCoord[0].st);	
	vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).xy * 2.0 - 1.0);
	vec4 spec = texture2D(spec, gl_TexCoord[2].st);
	
	vec4 ambient = vec4(ambientLight.x, ambientLight.y, ambientLight.z, 1.0);
	vec4 diffuse = diff;
	vec4 specular = spec;
	
	vec2 vector = lightLocation.xy - position.xy;
	float distance = sqrt((vector.x * vector.x) + (vector.y * vector.y));
	
	vec4 final = ambient + diffuse + specular;
	final.xy *= max(0.0, dot(normalize(vec3(vector, 0)), bump)) / distance;
	
	gl_FragColor = final;
}

This line:


vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).xy * 2.0 - 1.0);

You are trying to make a vec3 from a vec2.

What you probably want is:


vec3 bump = normalize(texture2D(normal, gl_TexCoord[1].st).rgb * 2.0 - 1.0);

(Using rgb instead of xyz. Using xyz would be equivalent, but is easier to understand we’re dealing with a texture)

I compiled, now I have to fix some graphical errors. Thanks guys!