only one directionnal light is allowed now, phongEntity now handles the mesh's opengl initialization

This commit is contained in:
Anselme 2015-08-06 14:25:17 +02:00
parent d4df0cd606
commit fc9eb93c75
7 changed files with 38 additions and 34 deletions

View File

@ -11,17 +11,18 @@ class Shader;
class Lights class Lights
{ {
private: public:
void addLight(const glm::vec3 &myPosition = glm::vec3(0), const glm::vec3 myColor = glm::vec3(1));
void bind(GLuint lightsLocation, GLuint nbLocation, Shader* shader);
typedef struct typedef struct
{ {
glm::vec3 position; glm::vec3 position;
glm::vec3 color; glm::vec3 color;
} Light; } Light;
private:
std::vector<Light> lights; std::vector<Light> lights;
public:
void addLight(const glm::vec3 &myPosition = glm::vec3(0), const glm::vec3 myColor = glm::vec3(1));
void bind(GLuint lightsLocation, GLuint nbLocation, Shader* shader);
}; };
#endif // LIGHT_H #endif // LIGHT_H

View File

@ -5,8 +5,7 @@ uniform vec3 materialKd;
uniform vec3 materialKs; uniform vec3 materialKs;
uniform float materialNs; uniform float materialNs;
uniform int nbDirLights; uniform vec3 dirLight[2];
uniform vec3 dirLights[8];
uniform int nbPointLights; uniform int nbPointLights;
uniform vec3 pointLights[8]; uniform vec3 pointLights[8];
@ -17,8 +16,8 @@ uniform sampler2D baseTexture;
in vec3 varNormal; in vec3 varNormal;
in vec2 varTexCoord; in vec2 varTexCoord;
in vec3 lightDirInView[4]; in vec3 lightDirInView[5];
in vec3 halfVecInView[4]; in vec3 halfVecInView[5];
// resultat // resultat
layout(location = 0)out vec4 outColor; layout(location = 0)out vec4 outColor;
@ -40,11 +39,9 @@ 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; int i;
vec3 kd = vec3(texture2D(baseTexture, varTexCoord)); vec3 kd = vec3(texture2D(baseTexture, varTexCoord));
vec3 light = 0.1*kd; vec3 light = 0.1*kd + computeLight(kd, materialKs, materialNs, dirLight[1], varNormal, lightDirInView[0], halfVecInView[0]);
for(i=0; i<nbDirLights && i<4; ++i) for(i=1; i<nbPointLights+1; ++i)
light += computeLight(kd, materialKs, materialNs, dirLights[i*2+1], varNormal, lightDirInView[i], halfVecInView[i]); light += computeLight(kd, materialKs, materialNs, pointLights[i*2 -1], varNormal, lightDirInView[i], halfVecInView[i]);
for(i=0; i<nbPointLights && i+nbDirLights<4; ++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);
} }

View File

@ -6,8 +6,7 @@ uniform mat4 MVP;
uniform mat4 normalMatrix; uniform mat4 normalMatrix;
uniform mat4 viewMatrix; uniform mat4 viewMatrix;
uniform int nbDirLights; uniform vec3 dirLight[2];
uniform vec3 dirLights[8];
uniform int nbPointLights; uniform int nbPointLights;
uniform vec3 pointLights[8]; uniform vec3 pointLights[8];
@ -15,8 +14,8 @@ 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[4]; out vec3 lightDirInView[5];
out vec3 halfVecInView[4]; out vec3 halfVecInView[5];
out vec3 varNormal; out vec3 varNormal;
out vec2 varTexCoord; out vec2 varTexCoord;
@ -35,10 +34,12 @@ void computePointLightingVectorsInView(in vec3 posInView, in vec3 lightPosition,
void main(void) { void main(void) {
int i; int i;
for(i=0; i<nbDirLights && i<4; ++i) computeDirectionnalLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), dirLight[0], lightDirInView[0], halfVecInView[0]);
computeDirectionnalLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), dirLights[i*2], lightDirInView[i], halfVecInView[i]); for(i=1; i<nbPointLights+1; ++i)
for(i=0; i<nbPointLights && i+nbDirLights<4; ++i) computePointLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)),
computePointLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), pointLights[i*2], lightDirInView[nbDirLights+i], halfVecInView[nbDirLights+i]); pointLights[i*2 - 2],
lightDirInView[i],
halfVecInView[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)));

View File

@ -10,6 +10,11 @@
PhongEntity::PhongEntity(Mesh* myMesh) : mesh(myMesh) {} PhongEntity::PhongEntity(Mesh* myMesh) : mesh(myMesh) {}
PhongEntity::~PhongEntity()
{
delete[] vbo;
}
void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix) void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
{ {
glm::mat4 modelViewMatrix = viewMatrix * modelMatrix; glm::mat4 modelViewMatrix = viewMatrix * modelMatrix;
@ -47,6 +52,7 @@ void PhongEntity::initGL(bool isDynamic)
nb_buffers += mesh->indiceGroups.size(); nb_buffers += mesh->indiceGroups.size();
// create VBOs // create VBOs
vbo = new GLuint[nb_buffers]();
glAssert(glGenBuffers(nb_buffers, vbo)); glAssert(glGenBuffers(nb_buffers, vbo));
for(const Mesh::Group &g : mesh->indiceGroups) for(const Mesh::Group &g : mesh->indiceGroups)

View File

@ -13,7 +13,7 @@ class Shader;
class PhongEntity : public Entity class PhongEntity : public Entity
{ {
private: protected:
enum { enum {
// required buffers // required buffers
POSITION_BUFFER, POSITION_BUFFER,
@ -34,6 +34,7 @@ public:
glm::mat4 modelMatrix; glm::mat4 modelMatrix;
PhongEntity(Mesh* myMesh); PhongEntity(Mesh* myMesh);
~PhongEntity();
virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix); virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix);

View File

@ -5,13 +5,12 @@
#include "phongentity.h" #include "phongentity.h"
#include "camera.h" #include "camera.h"
PhongModule::PhongModule(Lights* myDirLights, Lights* myPointLights) : PhongModule::PhongModule(Lights::Light* myDirLight, Lights* myPointLights) :
dirLights(myDirLights), dirLight(myDirLight),
pointLights(myPointLights), pointLights(myPointLights),
shader(ResourceBase::getShader("phong")) shader(ResourceBase::getShader("phong"))
{ {
nbDirLightsLocation = shader->getLocation("nbDirLights"); dirLightLocation = shader->getLocation("dirLight");
dirLightsLocation = shader->getLocation("dirLights");
nbPointLightsLocation = shader->getLocation("nbPointLights"); nbPointLightsLocation = shader->getLocation("nbPointLights");
pointLightsLocation = shader->getLocation("pointLights"); pointLightsLocation = shader->getLocation("pointLights");
} }
@ -24,7 +23,7 @@ void PhongModule::addEntity(PhongEntity* myEntity)
void PhongModule::renderGL(Camera* myCamera) void PhongModule::renderGL(Camera* myCamera)
{ {
shader->bind(); shader->bind();
dirLights->bind(dirLightsLocation, nbDirLightsLocation, shader); shader->bindVec3Array(dirLightLocation, (glm::vec3*)dirLight, 2);
pointLights->bind(pointLightsLocation, nbPointLightsLocation, shader); pointLights->bind(pointLightsLocation, nbPointLightsLocation, shader);
for(PhongEntity* e : entities) for(PhongEntity* e : entities)
e->draw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix()); e->draw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix());

View File

@ -5,18 +5,17 @@
#include <vector> #include <vector>
#include <cstddef> #include <cstddef>
#include <glew/glew.h> #include <glew/glew.h>
#include "lights.h"
class Shader; class Shader;
class PhongEntity; class PhongEntity;
class Lights;
class PhongModule : public Module class PhongModule : public Module
{ {
private: private:
Lights* dirLights; Lights::Light* dirLight;
Lights* pointLights; Lights* pointLights;
GLuint nbDirLightsLocation; GLuint dirLightLocation;
GLuint dirLightsLocation;
GLuint nbPointLightsLocation; GLuint nbPointLightsLocation;
GLuint pointLightsLocation; GLuint pointLightsLocation;
@ -24,7 +23,7 @@ private:
std::vector<PhongEntity*> entities; std::vector<PhongEntity*> entities;
public: public:
PhongModule(Lights* myDirLights, Lights* myPointLights); PhongModule(Lights::Light* myDirLight, Lights* myPointLights);
void addEntity(PhongEntity* myEntity); void addEntity(PhongEntity* myEntity);