ninja commit on Light refactoring

This commit is contained in:
Anselme FRANÇOIS 2016-06-13 07:58:39 +02:00
parent 1acf555854
commit 637195391d
10 changed files with 129 additions and 108 deletions

View File

@ -44,14 +44,27 @@ in vec4 posInLightSpace;
in vec2 varTexCoord; in vec2 varTexCoord;
#ifndef AMBIENT_LIGHT // switch on light type
#if defined POINT_LIGHT
in vec3 lightDirInView; in vec3 lightDirInView;
in vec3 halfVecInView; in vec3 halfVecInView;
#endif
#ifdef POINT_LIGHT
in vec3 lightDistInView; in vec3 lightDistInView;
uniform float attenuation; uniform float attenuation;
#elif defined DIRECTIONNAL_LIGHT
in vec3 lightDirInView;
in vec3 halfVecInView;
#elif defined SPOT_LIGHT
in vec3 lightDirInView;
in vec3 halfVecInView;
in vec3 lightDistInView;
uniform float attenuation;
uniform float cutoff;
#endif #endif
layout(location = 0)out vec4 outColor; layout(location = 0)out vec4 outColor;

View File

@ -7,9 +7,9 @@ out float fragmentdepth;
void main() void main()
{ {
#ifdef ALPHA_MASK #ifdef ALPHA_MASK
if(texture(alphaMask, varTexCoord).r < 0.5) if(texture(alphaMask, varTexCoord).r < 0.5)
discard; discard;
#endif #endif
fragmentdepth = gl_FragCoord.z; fragmentdepth = gl_FragCoord.z;
} }

View File

@ -1,10 +1,12 @@
#version 330 core // inspired from
// http://learnopengl.com/#!Advanced-Lighting/Shadows/Point-Shadows
layout(triangles) in; layout(triangles) in;
layout(triangle_strip, max_vertices = 18) out; layout(triangle_strip, max_vertices = 18) out;
uniform mat4 view[6]; uniform mat4 MVP[6];
uniform mat4 proj;
out vec4 FragPos;
// debug parameter // debug parameter
flat out int fcolor_idx; flat out int fcolor_idx;
@ -14,7 +16,8 @@ void main(void){
for (int i = 0; i < gl_in.length(); ++i){ for (int i = 0; i < gl_in.length(); ++i){
gl_Layer = layerId; gl_Layer = layerId;
fcolor_idx = layerId; fcolor_idx = layerId;
gl_Position = proj * view[layerId] * gl_in[i].gl_Position; FragPos = gl_in[i].gl_Position;
gl_Position = MVP[layerId] * FragPos;
EmitVertex(); EmitVertex();
} }
EndPrimitive(); EndPrimitive();

View File

@ -5,12 +5,19 @@ layout(location = 2) in vec2 inTexCoord;
out vec2 varTexCoord; out vec2 varTexCoord;
#endif #endif
#ifndef POINT_LIGHT
uniform mat4 MVP; uniform mat4 MVP;
#endif
void main() void main()
{ {
#ifdef ALPHA_MASK #ifdef ALPHA_MASK
varTexCoord = inTexCoord.xy; varTexCoord = inTexCoord.xy;
#endif #endif
gl_Position = MVP * vec4(inPosition, 1.0);
} #ifdef POINT_LIGHT
gl_Position = vec4(inPosition, 1.0);
#else
gl_Position = MVP * vec4(inPosition, 1.0);
#endif
}

View File

@ -1,35 +0,0 @@
#include "deferredmodule.h"
#include "texture.h"
GBuffer::GBuffer(int width, int height) : FrameBuffer()
{
Texture* tex;
// - Normal buffer
tex = new Texture(GL_RGB, GL_RGB16F, width, height, GL_FLOAT);
tex->setFiltering(GL_NEAREST);
addTexture(tex, GL_COLOR_ATTACHMENT0);
// - Color + Specular exponent buffer
tex = new Texture(GL_RGBA, GL_RGBA, width, height);
tex->setFiltering(GL_NEAREST);
addTexture(tex, GL_COLOR_ATTACHMENT1);
// - Specular color + objectId buffer
tex = new Texture(GL_RGBA, GL_RGBA, width, height);
tex->setFiltering(GL_NEAREST);
addTexture(tex, GL_COLOR_ATTACHMENT2);
// - depth buffer
tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, width, height, GL_FLOAT);
tex->setFiltering(GL_NEAREST);
addTexture(tex, GL_DEPTH_ATTACHMENT);
initColorAttachments();
}
GBuffer::~GBuffer()
{
for(Texture* t : m_textures)
delete(t);
}

View File

@ -1,29 +0,0 @@
#ifndef DEFERREDMODULE_H
#define DEFERREDMODULE_H
#include "framebuffer.h"
#include "module.h"
class Shader;
class PhongEntity;
// TODO : everything
class GBuffer : public FrameBuffer
{
public:
GBuffer(int width, int height);
~GBuffer();
};
class DeferredModule : public Module
{
public:
DeferredModule();
virtual void renderGL(Camera* myCamera, Scene* scene);
private:
};
#endif // DEFERREDMODULE_H

View File

@ -5,6 +5,7 @@
#include "light.h" #include "light.h"
#include "phongmaterial.h" #include "phongmaterial.h"
#include "texture.h" #include "texture.h"
#include "camera.h"
#include <glm/ext.hpp> #include <glm/ext.hpp>
void ForwardModule::renderGL(Camera* myCamera, Scene* scene) void ForwardModule::renderGL(Camera* myCamera, Scene* scene)
@ -43,7 +44,7 @@ void ForwardModule::renderGL(Camera* myCamera, Scene* scene)
shader->bindVec3(shader->getLocation("lightColor"), light->getColor()); shader->bindVec3(shader->getLocation("lightColor"), light->getColor());
if(light->isShadowCaster()) if(light->isShadowCaster())
{ {
light->getShadowMap()->bind(PhongMaterial::NB_PHONG_SLOTS); // NB_PHONG_SLOTS has the value of the first available slot after the phong material texture slots light->getShadowMapTexture()->bind(PhongMaterial::NB_PHONG_SLOTS); // NB_PHONG_SLOTS has the value of the first available slot after the phong material texture slots
shader->bindInteger(shader->getLocation("shadowMap"), PhongMaterial::NB_PHONG_SLOTS); shader->bindInteger(shader->getLocation("shadowMap"), PhongMaterial::NB_PHONG_SLOTS);
} }
break; break;
@ -54,7 +55,8 @@ void ForwardModule::renderGL(Camera* myCamera, Scene* scene)
break; break;
case Light::SPOT: case Light::SPOT:
shader->bindVec3(shader->getLocation("lightColor"), light->getColor()); shader->bindVec3(shader->getLocation("lightColor"), light->getColor());
// TODO add cutoff and attenuation shader->bindFloat(shader->getLocation("attenuation"), light->getAttenuation());
shader->bindFloat(shader->getLocation("cutoff"), light->getCutoffAngle());
break; break;
} }
unsigned int id = 2; unsigned int id = 2;
@ -70,10 +72,7 @@ void ForwardModule::renderGL(Camera* myCamera, Scene* scene)
glm::mat4 mvp = myCamera->getProjectionMatrix() * modelViewMatrix; glm::mat4 mvp = myCamera->getProjectionMatrix() * modelViewMatrix;
glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix)); glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix));
if(light != NULL && light->isShadowCaster()) if(light != NULL && light->isShadowCaster())
{ light->bindShadowMap(Shader *shader);
glm::mat4 lightMVP = Light::biasMatrix * light->getProjectionMatrix() * light->getViewMatrix() * node->modelMatrix;
shader->bindMat4(shader->getLocation("lightMVP"), lightMVP);
}
shader->bindMat4(shader->getLocation("viewMatrix"), myCamera->getViewMatrix()); shader->bindMat4(shader->getLocation("viewMatrix"), myCamera->getViewMatrix());
shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix); shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix);
shader->bindMat3(shader->getLocation("normalMatrix"), glm::mat3(normalMatrix)); shader->bindMat3(shader->getLocation("normalMatrix"), glm::mat3(normalMatrix));

View File

@ -84,7 +84,7 @@ void Light::initShadowMap(int resolution, glm::vec3 dim)
case DIRECTIONNAL: case DIRECTIONNAL:
// 3 orthogonal matrices (cascaded shadowmaps) // 3 orthogonal matrices (cascaded shadowmaps)
// 3 square textures // 3 square textures
m_viewMatrix = glm::lookAt(m_position, m_position-m_direction, glm::vec3(0, 1, 0)); m_viewMatrix.push_back(glm::lookAt(m_position, m_position-m_direction, glm::vec3(0, 1, 0)));
m_projectionMatrix = glm::ortho(-dim.x/2, dim.x/2, -dim.y/2, dim.y/2, -dim.z/2, dim.z/2); m_projectionMatrix = glm::ortho(-dim.x/2, dim.x/2, -dim.y/2, dim.y/2, -dim.z/2, dim.z/2);
tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, resolution, resolution, GL_FLOAT, GL_TEXTURE_2D); tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, resolution, resolution, GL_FLOAT, GL_TEXTURE_2D);
break; break;
@ -96,11 +96,24 @@ void Light::initShadowMap(int resolution, glm::vec3 dim)
m_viewMatrix = glm::lookAt(m_position, m_position-glm::vec3(0, 0, 1), glm::vec3(0, 1, 0)); m_viewMatrix = glm::lookAt(m_position, m_position-glm::vec3(0, 0, 1), glm::vec3(0, 1, 0));
m_projectionMatrix = glm::perspective(90.f, 1.f, 0.1f, 100.f); m_projectionMatrix = glm::perspective(90.f, 1.f, 0.1f, 100.f);
tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, resolution, resolution, GL_FLOAT, GL_TEXTURE_CUBE_MAP); tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, resolution, resolution, GL_FLOAT, GL_TEXTURE_CUBE_MAP);
m_viewMatrices.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(1.0,0.0,0.0), glm::vec3(0.0,-1.0,0.0));
m_viewMatrices.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(-1.0,0.0,0.0), glm::vec3(0.0,-1.0,0.0));
m_viewMatrices.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(0.0,1.0,0.0), glm::vec3(0.0,0.0,1.0));
m_viewMatrices.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(0.0,-1.0,0.0), glm::vec3(0.0,0.0,-1.0));
m_viewMatrices.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(0.0,0.0,1.0), glm::vec3(0.0,-1.0,0.0));
m_viewMatrices.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(0.0,0.0,-1.0), glm::vec3(0.0,-1.0,0.0));
break; break;
case SPOT: case SPOT:
// 1 projection matrix // 1 projection matrix
// 1 square texture // 1 square texture
m_viewMatrix = glm::lookAt(m_position, m_position+m_direction, glm::vec3(0, 1, 0)); m_viewMatrix.push_back(glm::lookAt(m_position, m_position+m_direction, glm::vec3(0, 1, 0)));
m_projectionMatrix = glm::perspective(m_cutOffAngle, 1.f, 0.1f, 100.f); m_projectionMatrix = glm::perspective(m_cutOffAngle, 1.f, 0.1f, 100.f);
tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, resolution, resolution, GL_FLOAT, GL_TEXTURE_2D); tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, resolution, resolution, GL_FLOAT, GL_TEXTURE_2D);
break; break;
@ -142,7 +155,7 @@ void Light::generateShadowMap(Scene* scene)
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
//glCullFace(GL_FRONT); // glCullFace(GL_FRONT);
for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry(); for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
geometryIt->isValid(); geometryIt->next()) geometryIt->isValid(); geometryIt->next())
{ {
@ -157,10 +170,16 @@ void Light::generateShadowMap(Scene* scene)
node->mesh->draw(m_shaders[hasAlpha], false, hasAlpha, false); node->mesh->draw(m_shaders[hasAlpha], false, hasAlpha, false);
} }
} }
//glCullFace(GL_BACK); // glCullFace(GL_BACK);
} }
Texture* Light::getShadowMap() void Light::bindShadowMap(Shader *shader)
{
glm::mat4 lightMVP = Light::biasMatrix * m_projectionMatrix * light->getViewMatrix() * node->modelMatrix;
shader->bindMat4(shader->getLocation("lightMVP"), lightMVP);
}
Texture* Light::getShadowMapTexture()
{ {
return m_shadowMap->getTexture(0); return m_shadowMap->getTexture(0);
} }

View File

@ -4,14 +4,14 @@
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <string> #include <string>
#include "camera.h" #include <vector>
class FrameBuffer; class FrameBuffer;
class Shader; class Shader;
class Scene; class Scene;
class Texture; class Texture;
class Light : public Camera class Light
{ {
public: public:
enum LightType { enum LightType {
@ -32,33 +32,77 @@ public:
static const char* flagStr[]; static const char* flagStr[];
static const glm::mat4 biasMatrix; static const glm::mat4 biasMatrix;
virtual LightType getType() = 0;
glm::vec3 getColor() { return m_color; }
virtual bool isShadowCaster() { return false; }
virtual void bindAttributes(Shader *shader) = 0;
virtual unsigned int getFlags() = 0;
protected:
glm::vec3 m_color;
};
class AmbientLight : public Light
{
public:
AmbientLight(glm::vec3 lightColor = glm::vec3(0.1f)) : m_color(lightColor) {}
virtual LightType getType() { return AMBIENT; }
virtual void bindAttributes(Shader *shader)
{ shader->bindVec3(shader->getLocation("lightColor"), m_color); }
virtual unsigned int getFlags() { return 1 << AMBIENT_FLAG; }
};
class ShadowableLight : public Light
{
public:
// enables shadowmapping on this light
virtual void initShadowMap(int resolution, glm::vec3 dim = glm::vec3(1)) = 0;
// disables shadowmapping on this light
virtual void destroyShadowMap() = 0;
// returns true is shadowmapping is enabled on this light
virtual bool isShadowCaster() { return m_shadowCaster; }
// updates the shadowmap if shadowmapping is enabled
virtual void updateShadowMap(Scene* scene) = 0;
protected:
bool m_shadowCaster;
};
class Light
{
public:
Light(); Light();
void initAmbientLight(glm::vec3 lightColor = glm::vec3(0.1f)); void initAmbientLight(glm::vec3 lightColor = glm::vec3(0.1f));
void initDirectionnalLight(glm::vec3 dir = glm::vec3(0, -1, 0), glm::vec3 lightColor = glm::vec3(1)); void initDirectionnalLight(glm::vec3 dir = glm::vec3(0, -1, 0), glm::vec3 lightColor = glm::vec3(1));
void initPointLight(glm::vec3 pos = glm::vec3(0), glm::vec3 lightColor = glm::vec3(1), float att = 1); void initPointLight(glm::vec3 pos = glm::vec3(0), glm::vec3 lightColor = glm::vec3(1), float att = 1);
void initSpotLight(glm::vec3 pos = glm::vec3(0), glm::vec3 dir = glm::vec3(1, 0, 0), float spotAngle = 360, glm::vec3 lightColor = glm::vec3(1), float att = 1); void initSpotLight(glm::vec3 pos = glm::vec3(0), glm::vec3 dir = glm::vec3(1, 0, 0), float spotAngle = 360, glm::vec3 lightColor = glm::vec3(1), float att = 1);
LightType getType() {return m_type;}
glm::vec3 getDir() {return m_direction;} glm::vec3 getDir() { return m_direction; }
glm::vec3 getPos() {return m_position;} glm::vec3 getPos() { return m_position; }
glm::vec3 getColor() {return m_color;} glm::vec3 getColor() { return m_color; }
float getAttenuation() {return m_attenuation;} float getAttenuation() { return m_attenuation; }
float getCutoffAngle() { return m_cutOffAngle; }
bool isShadowCaster() {return m_shadowCaster;} bool isShadowCaster() {return m_shadowCaster;}
void initShadowMap(int resolution, glm::vec3 dim = glm::vec3(1)); void initShadowMap(int resolution, glm::vec3 dim = glm::vec3(1));
void generateShadowMap(Scene* scene); void generateShadowMap(Scene* scene);
Texture* getShadowMap(); void bindShadowMap(Shader *shader);
Texture* getShadowMapTexture();
void setAttenuation(float a) {m_attenuation = a;} void setAttenuation(float a) { m_attenuation = a; }
void setCutoffAngle(float c) { m_cutOffAngle = c; }
void setPosition(glm::vec3 new_pos); void setPosition(glm::vec3 new_pos);
void setColor(glm::vec3 new_color) {m_color = new_color;} void setColor(glm::vec3 new_color) {m_color = new_color;}
// camera inheritance
glm::mat4 getProjectionMatrix() {return m_projectionMatrix;}
glm::mat4 getViewMatrix() {return m_viewMatrix;}
// does nothing, just required for inheriting Camera
void resize(int width, int height) {}
/** /**
* @brief getFlags returns the flags that defines the specificities of the light * @brief getFlags returns the flags that defines the specificities of the light
*/ */
@ -78,7 +122,7 @@ private:
int m_shadowMapResolution; int m_shadowMapResolution;
FrameBuffer* m_shadowMap; FrameBuffer* m_shadowMap;
Shader* m_shaders[2]; Shader* m_shaders[2];
glm::mat4 m_viewMatrix; std::vector<glm::mat4> m_viewMatrices;
glm::mat4 m_projectionMatrix; glm::mat4 m_projectionMatrix;
}; };

View File

@ -8,7 +8,7 @@
void PhongMaterial::bindAttributes(Shader* myShader) void PhongMaterial::bindAttributes(Shader* myShader)
{ {
// TODO store the attributes location (in the shader class maybe) // TODO store the attributes location
myShader->bindFloat(myShader->getLocation("materialNs"), shininess); myShader->bindFloat(myShader->getLocation("materialNs"), shininess);
if(normal_map != NULL) if(normal_map != NULL)