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
{
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
{
glm::vec3 position;
glm::vec3 color;
} Light;
private:
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

View File

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

View File

@ -6,8 +6,7 @@ uniform mat4 MVP;
uniform mat4 normalMatrix;
uniform mat4 viewMatrix;
uniform int nbDirLights;
uniform vec3 dirLights[8];
uniform vec3 dirLight[2];
uniform int nbPointLights;
uniform vec3 pointLights[8];
@ -15,8 +14,8 @@ layout(location = 0)in vec3 inPosition;
layout(location = 1)in vec3 inNormal;
layout(location = 2)in vec4 inTexCoord;
out vec3 lightDirInView[4];
out vec3 halfVecInView[4];
out vec3 lightDirInView[5];
out vec3 halfVecInView[5];
out vec3 varNormal;
out vec2 varTexCoord;
@ -35,15 +34,17 @@ void computePointLightingVectorsInView(in vec3 posInView, in vec3 lightPosition,
void main(void) {
int i;
for(i=0; i<nbDirLights && i<4; ++i)
computeDirectionnalLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), dirLights[i*2], lightDirInView[i], halfVecInView[i]);
for(i=0; i<nbPointLights && i+nbDirLights<4; ++i)
computePointLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), pointLights[i*2], lightDirInView[nbDirLights+i], halfVecInView[nbDirLights+i]);
computeDirectionnalLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)), dirLight[0], lightDirInView[0], halfVecInView[0]);
for(i=1; i<nbPointLights+1; ++i)
computePointLightingVectorsInView(vec3(modelViewMatrix*vec4(inPosition, 1.0)),
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)));
// coordonnees de texture
// coordonnees de texture
varTexCoord = inTexCoord.xy;
// position du vertex

View File

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

View File

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

View File

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

View File

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