Got phong shading working.

This commit is contained in:
Fabian Klemp 2014-10-31 12:56:09 +01:00
parent c505553439
commit 769514af88
3 changed files with 20 additions and 12 deletions

View File

@ -6,6 +6,7 @@ in vec2 vTexCoord;
out vec4 oColor; out vec4 oColor;
uniform sampler2D uTexture; uniform sampler2D uTexture;
uniform int lightCount;
uniform vec3 lightSources[128]; uniform vec3 lightSources[128];
uniform vec3 ambientIntensity; uniform vec3 ambientIntensity;
uniform float ambientFactor; uniform float ambientFactor;
@ -15,7 +16,10 @@ uniform float diffuseFactor;
void main() void main()
{ {
vec3 ambientColor = ambientFactor * ambientIntensity; vec3 ambientColor = ambientFactor * ambientIntensity;
vec3 diffuseColor = dot(normalize(vNormal),vec3(0,0,1))*diffuseFactor*diffuseIntensity; vec3 diffuseColor = vec3(0.0, 0.0, 0.0);
for(int i = 0; i<lightCount; i++) {
diffuseColor += dot(normalize(vNormal), normalize(lightSources[i]))*diffuseFactor*diffuseIntensity;
}
vec3 finalColor = diffuseColor + ambientColor; vec3 finalColor = diffuseColor + ambientColor;
vec3 texture = texture(uTexture, vTexCoord).rgb; vec3 texture = texture(uTexture, vTexCoord).rgb;

View File

@ -51,19 +51,19 @@ void draw( float runTime )
//set lighting parameters //set lighting parameters
shader.getReference()->setUniform("ambientIntensity", level.getAmbientLight()); shader.getReference()->setUniform("ambientIntensity", level.getAmbientLight());
shader.getReference()->setUniform("ambientFactor", 1.0f); shader.getReference()->setUniform("ambientFactor", 1.0f);
shader.getReference()->setUniform("diffuseIntensity", glm::vec3(1.0f, 1.0f, 1.0f)); if (level.getLights().size() > 0) {
shader.getReference()->setUniform("diffuseFactor", 1.0f); shader.getReference()->setUniform("diffuseIntensity", glm::vec3(1.0f, 1.0f, 1.0f));
shader.getReference()->setUniform("diffuseFactor", 1.0f);
shader.getReference()->setUniform("lightCount", (int) level.getLights().size());
// TODO look into doing this less often
// TODO look into doing this less often // Build light position array
// Build light position array glm::vec3 lights[level.getLights().size()];
glm::vec3 lights[level.getLights().size()]; for(int i = 0; i<level.getLights().size(); i++) {
for(int i = 0; i<level.getLights().size(); i++) { lights[i] = level.getLights()[i].getPosition();
lights[i] = level.getLights()[i].getPosition(); }
glUniform3fv(shader.getReference()->getUniformLocation("lightSources"), sizeof(lights), (GLfloat*) lights);
} }
shader.getReference()->setUniform("lightSources", lights);
//glUniform1fv(glGetUniformLocation(shader.getReference(), "lightSources", 128, lights);
// render the level(currently only a bunny): // render the level(currently only a bunny):
level.render(); level.render();

View File

@ -23,6 +23,10 @@ void Level::load(Shader shader) {
objects.push_back(object); objects.push_back(object);
//set lighting parameters //set lighting parameters
ambientLight = glm::vec3(0.1f, 0.1f, 0.1f); ambientLight = glm::vec3(0.1f, 0.1f, 0.1f);
Light light = Light(glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), 1.0f);
lights.push_back(light);
Light light2 = Light(glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), 1.0f);
lights.push_back(light2);
} }
void Level::render() { void Level::render() {