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();