First attempt at Phong shading. Build broken.

This commit is contained in:
Faerbit 2014-10-31 10:38:57 +01:00
parent 911c8ebfcc
commit c505553439
3 changed files with 31 additions and 5 deletions

View File

@ -6,11 +6,18 @@ in vec2 vTexCoord;
out vec4 oColor; out vec4 oColor;
uniform sampler2D uTexture; uniform sampler2D uTexture;
uniform vec3 lightSources[128];
uniform vec3 ambientIntensity;
uniform float ambientFactor;
uniform vec3 diffuseIntensity;
uniform float diffuseFactor;
void main() void main()
{ {
vec3 color = texture(uTexture, vTexCoord).rgb; vec3 ambientColor = ambientFactor * ambientIntensity;
color *= dot(normalize(vNormal),vec3(0,0,1)); vec3 diffuseColor = dot(normalize(vNormal),vec3(0,0,1))*diffuseFactor*diffuseIntensity;
vec3 finalColor = diffuseColor + ambientColor;
oColor = vec4( color, 1.0 ); vec3 texture = texture(uTexture, vTexCoord).rgb;
oColor = vec4(finalColor*texture, 1.0 );
} }

View File

@ -48,7 +48,24 @@ void draw( float runTime )
shader.getReference()->setUniform( "uViewMatrix", viewMatrix ); shader.getReference()->setUniform( "uViewMatrix", viewMatrix );
shader.getReference()->setUniform( "uProjectionMatrix", buildFrustum(75.0, 0.1, 100.0, (float)g_windowSize.x/(float)g_windowSize.y) ); shader.getReference()->setUniform( "uProjectionMatrix", buildFrustum(75.0, 0.1, 100.0, (float)g_windowSize.x/(float)g_windowSize.y) );
// render the bunny: //set lighting parameters
shader.getReference()->setUniform("ambientIntensity", level.getAmbientLight());
shader.getReference()->setUniform("ambientFactor", 1.0f);
shader.getReference()->setUniform("diffuseIntensity", glm::vec3(1.0f, 1.0f, 1.0f));
shader.getReference()->setUniform("diffuseFactor", 1.0f);
// TODO look into doing this less often
// Build light position array
glm::vec3 lights[level.getLights().size()];
for(int i = 0; i<level.getLights().size(); i++) {
lights[i] = level.getLights()[i].getPosition();
}
shader.getReference()->setUniform("lightSources", lights);
//glUniform1fv(glGetUniformLocation(shader.getReference(), "lightSources", 128, lights);
// render the level(currently only a bunny):
level.render(); level.render();
} }

View File

@ -21,6 +21,8 @@ void Level::load(Shader shader) {
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 0.0f), shader); glm::vec3(0.0f, 0.0f, 0.0f), shader);
objects.push_back(object); objects.push_back(object);
//set lighting parameters
ambientLight = glm::vec3(0.1f, 0.1f, 0.1f);
} }
void Level::render() { void Level::render() {