better blinn-phong, directionnal light at sun position
This commit is contained in:
parent
3678b8d392
commit
2d4e2b9727
@ -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)
|
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());
|
||||||
}
|
}
|
||||||
|
4
lights.h
4
lights.h
@ -5,7 +5,7 @@
|
|||||||
#include <glew/glew.h>
|
#include <glew/glew.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#define MAX_LIGHTS 16
|
#define MAX_LIGHTS 8
|
||||||
|
|
||||||
class Shader;
|
class Shader;
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ private:
|
|||||||
std::vector<Light> lights;
|
std::vector<Light> lights;
|
||||||
public:
|
public:
|
||||||
void addLight(const glm::vec3 &myPosition = glm::vec3(0), const glm::vec3 myColor = glm::vec3(1));
|
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
|
#endif // LIGHT_H
|
||||||
|
@ -58,7 +58,7 @@ Scene* MyGLWidget::buildScene()
|
|||||||
|
|
||||||
scene->addEntity(myGrid, mat);
|
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;
|
return scene;
|
||||||
}
|
}
|
||||||
|
17
phong.frag
17
phong.frag
@ -5,8 +5,10 @@ uniform vec3 materialKd;
|
|||||||
uniform vec3 materialKs;
|
uniform vec3 materialKs;
|
||||||
uniform float materialNs;
|
uniform float materialNs;
|
||||||
|
|
||||||
uniform vec3 dirLights[32];
|
uniform int nbDirLights;
|
||||||
uniform vec3 pointLights[32];
|
uniform vec3 dirLights[16];
|
||||||
|
uniform int nbPointLights;
|
||||||
|
uniform vec3 pointLights[16];
|
||||||
|
|
||||||
// texture
|
// texture
|
||||||
uniform sampler2D baseTexture;
|
uniform sampler2D baseTexture;
|
||||||
@ -15,8 +17,8 @@ uniform sampler2D baseTexture;
|
|||||||
in vec3 varNormal;
|
in vec3 varNormal;
|
||||||
in vec2 varTexCoord;
|
in vec2 varTexCoord;
|
||||||
|
|
||||||
in vec3 lightDirInView[16];
|
in vec3 lightDirInView[8];
|
||||||
in vec3 halfVecInView[16];
|
in vec3 halfVecInView[8];
|
||||||
|
|
||||||
// resultat
|
// resultat
|
||||||
layout(location = 0)out vec4 outColor;
|
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) {
|
void main(void) {
|
||||||
|
int i;
|
||||||
vec3 kd = vec3(texture2D(baseTexture, varTexCoord));
|
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; i<nbDirLights && i<8; ++i)
|
||||||
|
light += computeLight(kd, materialKs, materialNs, dirLights[i*2+1], varNormal, lightDirInView[i], halfVecInView[i]);
|
||||||
|
for(i=0; i<nbPointLights && i+nbDirLights<8; ++i)
|
||||||
|
light += computeLight(kd, materialKs, materialNs, pointLights[i*2+1], varNormal, lightDirInView[nbDirLights+i], halfVecInView[nbDirLights+i]);
|
||||||
|
|
||||||
outColor = vec4(light, 1);
|
outColor = vec4(light, 1);
|
||||||
}
|
}
|
||||||
|
24
phong.vert
24
phong.vert
@ -6,27 +6,39 @@ uniform mat4 MVP;
|
|||||||
uniform mat4 normalMatrix;
|
uniform mat4 normalMatrix;
|
||||||
uniform mat4 viewMatrix;
|
uniform mat4 viewMatrix;
|
||||||
|
|
||||||
uniform vec3 dirLights[32];
|
uniform int nbDirLights;
|
||||||
uniform vec3 pointLights[32];
|
uniform vec3 dirLights[16];
|
||||||
|
uniform int nbPointLights;
|
||||||
|
uniform vec3 pointLights[16];
|
||||||
|
|
||||||
layout(location = 0)in vec3 inPosition;
|
layout(location = 0)in vec3 inPosition;
|
||||||
layout(location = 1)in vec3 inNormal;
|
layout(location = 1)in vec3 inNormal;
|
||||||
layout(location = 2)in vec4 inTexCoord;
|
layout(location = 2)in vec4 inTexCoord;
|
||||||
|
|
||||||
out vec3 lightDirInView[16];
|
out vec3 lightDirInView[8];
|
||||||
out vec3 halfVecInView[16];
|
out vec3 halfVecInView[8];
|
||||||
|
|
||||||
out vec3 varNormal;
|
out vec3 varNormal;
|
||||||
out vec2 varTexCoord;
|
out vec2 varTexCoord;
|
||||||
|
|
||||||
void computeLightingVectorsInView(in vec3 posInView, in vec3 lightPosition, out vec3 lightDir, out vec3 halfVec){
|
void computeDirectionnalLightingVectorsInView(in vec3 posInView, in vec3 lightDirInWorld, out vec3 lightDir, out vec3 halfVec){
|
||||||
|
lightDir = mat3(viewMatrix)*lightDirInWorld;
|
||||||
|
halfVec = normalize(normalize(lightDir) - normalize(posInView));
|
||||||
|
lightDir = normalize(lightDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
void computePointLightingVectorsInView(in vec3 posInView, in vec3 lightPosition, out vec3 lightDir, out vec3 halfVec){
|
||||||
lightDir = vec3(viewMatrix*vec4(lightPosition, 1.0)) - posInView;
|
lightDir = vec3(viewMatrix*vec4(lightPosition, 1.0)) - posInView;
|
||||||
halfVec = normalize(lightDir - posInView);
|
halfVec = normalize(lightDir - posInView);
|
||||||
lightDir = normalize(lightDir);
|
lightDir = normalize(lightDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main(void) {
|
void main(void) {
|
||||||
computeLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), dirLights[0], lightDirInView[0], halfVecInView[0]);
|
int i;
|
||||||
|
for(i=0; i<nbDirLights && i<8; ++i)
|
||||||
|
computeDirectionnalLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), dirLights[i*2], lightDirInView[i], halfVecInView[i]);
|
||||||
|
for(i=0; i<nbPointLights && i+nbDirLights<8; ++i)
|
||||||
|
computePointLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), pointLights[i*2], lightDirInView[nbDirLights+i], halfVecInView[nbDirLights+i]);
|
||||||
|
|
||||||
// normales corrigees (en fonction de la vue)
|
// normales corrigees (en fonction de la vue)
|
||||||
varNormal = normalize(vec3(normalMatrix*vec4(inNormal,0)));
|
varNormal = normalize(vec3(normalMatrix*vec4(inNormal,0)));
|
||||||
|
@ -11,7 +11,7 @@ void PhongMaterial::bindAttributes()
|
|||||||
if(tex != NULL)
|
if(tex != NULL)
|
||||||
{
|
{
|
||||||
tex->bind(TEX_ID);
|
tex->bind(TEX_ID);
|
||||||
shader->bindTexture(shader->getLocation("baseTexture"), TEX_ID);
|
shader->bindInteger(shader->getLocation("baseTexture"), TEX_ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,8 +25,8 @@ void Scene::drawAll()
|
|||||||
shader->bind();
|
shader->bind();
|
||||||
if(e->getMaterial()->requireLights())
|
if(e->getMaterial()->requireLights())
|
||||||
{
|
{
|
||||||
directionnalLights.bind(shader->getLocation("dirLights"), shader);
|
directionnalLights.bind(shader->getLocation("dirLights"), shader->getLocation("nbDirLights"), shader);
|
||||||
pointLights.bind(shader->getLocation("pointLights"), shader);
|
pointLights.bind(shader->getLocation("pointLights"), shader->getLocation("nbPointLights"), shader);
|
||||||
}
|
}
|
||||||
e->draw(viewMatrix, projectionMatrix);
|
e->draw(viewMatrix, projectionMatrix);
|
||||||
}
|
}
|
||||||
|
@ -132,7 +132,7 @@ void Shader::bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements)
|
|||||||
glAssert(glUniform3fv(location, nb_elements, (GLfloat*)vec));
|
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));
|
glAssert(glUniform1i(location, tex_id));
|
||||||
}
|
}
|
||||||
|
2
shader.h
2
shader.h
@ -25,7 +25,7 @@ public:
|
|||||||
void bindMatrix(GLuint location, glm::mat4 mat);
|
void bindMatrix(GLuint location, glm::mat4 mat);
|
||||||
void bindVec3(GLuint location, glm::vec3 vec);
|
void bindVec3(GLuint location, glm::vec3 vec);
|
||||||
void bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements);
|
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
|
#endif // SHADER_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user