From 2d4e2b972762a6bd02297336083043e99645a8db Mon Sep 17 00:00:00 2001 From: Anselme Date: Wed, 1 Jul 2015 21:41:22 +0200 Subject: [PATCH] better blinn-phong, directionnal light at sun position --- lights.cpp | 5 +++-- lights.h | 4 ++-- myglwidget.cpp | 2 +- phong.frag | 17 ++++++++++++----- phong.vert | 24 ++++++++++++++++++------ phongmaterial.cpp | 2 +- scene.cpp | 4 ++-- shader.cpp | 2 +- shader.h | 2 +- 9 files changed, 41 insertions(+), 21 deletions(-) diff --git a/lights.cpp b/lights.cpp index c7d004e..695b524 100644 --- a/lights.cpp +++ b/lights.cpp @@ -12,8 +12,9 @@ void Lights::addLight(const glm::vec3 &myPosition, const glm::vec3 myColor) } } -void Lights::bind(GLuint location, Shader* shader) +void Lights::bind(GLuint lightsLocation, GLuint nbLocation, Shader* shader) { if(lights.size() > 0) - shader->bindVec3Array(location, (glm::vec3*)lights.data(), lights.size()*2); + shader->bindVec3Array(lightsLocation, (glm::vec3*)lights.data(), lights.size()*2); + shader->bindInteger(nbLocation, (GLuint)lights.size()); } diff --git a/lights.h b/lights.h index cd266c8..8d035ef 100644 --- a/lights.h +++ b/lights.h @@ -5,7 +5,7 @@ #include #include -#define MAX_LIGHTS 16 +#define MAX_LIGHTS 8 class Shader; @@ -21,7 +21,7 @@ private: std::vector lights; public: void addLight(const glm::vec3 &myPosition = glm::vec3(0), const glm::vec3 myColor = glm::vec3(1)); - void bind(GLuint location, Shader* shader); + void bind(GLuint lightsLocation, GLuint nbLocation, Shader* shader); }; #endif // LIGHT_H diff --git a/myglwidget.cpp b/myglwidget.cpp index f192864..0baeb6e 100644 --- a/myglwidget.cpp +++ b/myglwidget.cpp @@ -58,7 +58,7 @@ Scene* MyGLWidget::buildScene() scene->addEntity(myGrid, mat); - scene->addDirectionnalLight(glm::vec3(2, 3, 4), glm::vec3(0.6f, 0.5f, 0.4f)); + scene->addDirectionnalLight(glm::vec3(6, 4, -4), glm::vec3(0.7f, 0.6f, 0.4f)); // sun return scene; } diff --git a/phong.frag b/phong.frag index f4c1ce3..af1d944 100644 --- a/phong.frag +++ b/phong.frag @@ -5,8 +5,10 @@ uniform vec3 materialKd; uniform vec3 materialKs; uniform float materialNs; -uniform vec3 dirLights[32]; -uniform vec3 pointLights[32]; +uniform int nbDirLights; +uniform vec3 dirLights[16]; +uniform int nbPointLights; +uniform vec3 pointLights[16]; // texture uniform sampler2D baseTexture; @@ -15,8 +17,8 @@ uniform sampler2D baseTexture; in vec3 varNormal; in vec2 varTexCoord; -in vec3 lightDirInView[16]; -in vec3 halfVecInView[16]; +in vec3 lightDirInView[8]; +in vec3 halfVecInView[8]; // resultat layout(location = 0)out vec4 outColor; @@ -36,8 +38,13 @@ vec3 computeLight(in vec3 kd, in vec3 ks, in float ns, in vec3 color, in vec3 no } void main(void) { + int i; vec3 kd = vec3(texture2D(baseTexture, varTexCoord)); - vec3 light = 0.1f*kd + computeLight(kd, materialKs, materialNs, dirLights[1], varNormal, lightDirInView[0], halfVecInView[0]); + vec3 light = 0.1f*kd; + for(i=0; ibind(TEX_ID); - shader->bindTexture(shader->getLocation("baseTexture"), TEX_ID); + shader->bindInteger(shader->getLocation("baseTexture"), TEX_ID); } } diff --git a/scene.cpp b/scene.cpp index 2831c86..7ec04cc 100644 --- a/scene.cpp +++ b/scene.cpp @@ -25,8 +25,8 @@ void Scene::drawAll() shader->bind(); if(e->getMaterial()->requireLights()) { - directionnalLights.bind(shader->getLocation("dirLights"), shader); - pointLights.bind(shader->getLocation("pointLights"), shader); + directionnalLights.bind(shader->getLocation("dirLights"), shader->getLocation("nbDirLights"), shader); + pointLights.bind(shader->getLocation("pointLights"), shader->getLocation("nbPointLights"), shader); } e->draw(viewMatrix, projectionMatrix); } diff --git a/shader.cpp b/shader.cpp index 9acb2ff..539c9c5 100644 --- a/shader.cpp +++ b/shader.cpp @@ -132,7 +132,7 @@ void Shader::bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements) glAssert(glUniform3fv(location, nb_elements, (GLfloat*)vec)); } -void Shader::bindTexture(GLuint location, GLuint tex_id) +void Shader::bindInteger(GLuint location, GLuint tex_id) { glAssert(glUniform1i(location, tex_id)); } diff --git a/shader.h b/shader.h index 01046e0..389e785 100644 --- a/shader.h +++ b/shader.h @@ -25,7 +25,7 @@ public: void bindMatrix(GLuint location, glm::mat4 mat); void bindVec3(GLuint location, glm::vec3 vec); void bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements); - void bindTexture(GLuint location, GLuint tex_id); + void bindInteger(GLuint location, GLuint tex_id); }; #endif // SHADER_H