Shader now respects distance between light source and fragment.
This commit is contained in:
parent
ae949e814f
commit
589e0df6ea
@ -2,6 +2,7 @@
|
||||
|
||||
in vec3 vNormal;
|
||||
in vec2 vTexCoord;
|
||||
in vec4 fragPosition;
|
||||
|
||||
out vec4 oColor;
|
||||
|
||||
@ -11,13 +12,15 @@ uniform vec3 lightSources[128];
|
||||
uniform vec3 ambientColor;
|
||||
uniform float ambientFactor;
|
||||
uniform vec3 lightColors[128];
|
||||
uniform float diffuseFactor;
|
||||
uniform float lightIntensities[128];
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 ambientColor = ambientFactor * ambientColor;
|
||||
vec3 diffuseColor = vec3(0.0, 0.0, 0.0);
|
||||
for(int i = 0; i<lightCount; i++) {
|
||||
float distance = distance(lightSources[i], vec3(fragPosition));
|
||||
float diffuseFactor = (1.0*lightIntensities[i])/(distance);
|
||||
diffuseColor += dot(normalize(vNormal), normalize(lightSources[i]))
|
||||
*diffuseFactor*lightColors[i];
|
||||
}
|
||||
|
@ -9,9 +9,11 @@ in vec2 aTexCoord;
|
||||
|
||||
out vec3 vNormal;
|
||||
out vec2 vTexCoord;
|
||||
out vec4 fragPosition;
|
||||
|
||||
void main()
|
||||
{
|
||||
fragPosition = uViewMatrix * vec4(aPosition, 1.0);
|
||||
vNormal = inverse(transpose(mat3(uViewMatrix))) * aNormal;
|
||||
vTexCoord = aTexCoord;
|
||||
gl_Position = uProjectionMatrix * uViewMatrix * vec4(aPosition, 1.0);
|
||||
|
10
graphics.cc
10
graphics.cc
@ -23,7 +23,7 @@ void initCustomResources()
|
||||
// TODO look up if this is really necessary, since this looks really stupid.
|
||||
Model model = Model("Bunny.obj");
|
||||
|
||||
// look up all shader files starting with 'HelloWorld' and build a ShaderProgram from it:
|
||||
// look up all shader files starting with 'phong' and build a ShaderProgram from it:
|
||||
shader = Shader("phong", model);
|
||||
|
||||
// load Level
|
||||
@ -52,7 +52,6 @@ void draw( float runTime )
|
||||
shader.getReference()->setUniform("ambientColor", level.getAmbientLight());
|
||||
shader.getReference()->setUniform("ambientFactor", 1.0f);
|
||||
if (level.getLights().size() > 0) {
|
||||
shader.getReference()->setUniform("diffuseFactor", 0.7f);
|
||||
shader.getReference()->setUniform("lightCount", (int) level.getLights().size());
|
||||
|
||||
// TODO look into doing this less often
|
||||
@ -70,6 +69,13 @@ void draw( float runTime )
|
||||
}
|
||||
glUniform3fv(shader.getReference()->getUniformLocation("lightColors"),
|
||||
sizeof(lightColours), (GLfloat*) lightColours);
|
||||
// Build light attenuation array
|
||||
float lightIntensities[level.getLights().size()];
|
||||
for(int i = 0; i<level.getLights().size(); i++) {
|
||||
lightIntensities[i] = level.getLights()[i].getIntensity();
|
||||
}
|
||||
glUniform1fv(shader.getReference()->getUniformLocation("lightIntensities"),
|
||||
sizeof(lightIntensities), (GLfloat*) lightIntensities);
|
||||
}
|
||||
|
||||
// render the level(currently only a bunny):
|
||||
|
Loading…
Reference in New Issue
Block a user