worked a little on scene refactoring
This commit is contained in:
parent
c3b5a356fe
commit
371707bdd4
@ -48,30 +48,29 @@ void ForwardModule::lightPass(Camera* myCamera, Scene* scene, Light* light)
|
|||||||
{
|
{
|
||||||
std::size_t j;
|
std::size_t j;
|
||||||
for(j=0; j<lightFlagList.size(); ++j)
|
for(j=0; j<lightFlagList.size(); ++j)
|
||||||
if(lightFlagList[j] == Light::getFlags(light))
|
if(lightFlagList[j] == light->getFlags())
|
||||||
break;
|
break;
|
||||||
if(j == lightFlagList.size())
|
if(j == lightFlagList.size())
|
||||||
continue; // WARNING : missing shader for the light
|
{
|
||||||
|
fprintf(stderr, "WARNING : missing shader for the light");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
Shader* shader = shaders[i*lightFlagList.size() + j];
|
Shader* shader = shaders[i*lightFlagList.size() + j];
|
||||||
shader->bind();
|
shader->bind();
|
||||||
|
|
||||||
// bind light attributes
|
// bind light attributes
|
||||||
if(light == NULL)
|
|
||||||
{
|
|
||||||
// ambient light
|
|
||||||
shader->bindVec3(shader->getLocation("lightColor"), glm::vec3(0.1f)); // add attribute, and setter for ambient lighting
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
switch(light->getType())
|
switch(light->getType())
|
||||||
{
|
{
|
||||||
|
case Light::AMBIENT:
|
||||||
|
shader->bindVec3(shader->getLocation("lightColor"), light->getColor()); // add attribute, and setter for ambient lighting
|
||||||
|
break;
|
||||||
case Light::DIRECTIONNAL:
|
case Light::DIRECTIONNAL:
|
||||||
shader->bindVec3(shader->getLocation("dirLight"), -light->getDir());
|
shader->bindVec3(shader->getLocation("dirLight"), -light->getDir());
|
||||||
shader->bindVec3(shader->getLocation("lightColor"), light->getColor());
|
shader->bindVec3(shader->getLocation("lightColor"), light->getColor());
|
||||||
if(light->isShadowCaster())
|
if(light->isShadowCaster())
|
||||||
{
|
{
|
||||||
light->getShadowMap()->bind(NB_FLAGS); // NB_FLAGS has the value of the first available slot after the phong material texture slots
|
light->getShadowMap()->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"), NB_FLAGS);
|
shader->bindInteger(shader->getLocation("shadowMap"), PhongMaterial::NB_PHONG_SLOTS);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Light::POINT:
|
case Light::POINT:
|
||||||
@ -84,7 +83,6 @@ void ForwardModule::lightPass(Camera* myCamera, Scene* scene, Light* light)
|
|||||||
// TODO add cutoff and attenuation
|
// TODO add cutoff and attenuation
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
unsigned int id = 2;
|
unsigned int id = 2;
|
||||||
for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
|
for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
|
||||||
geometryIt->isValid(); geometryIt->next())
|
geometryIt->isValid(); geometryIt->next())
|
||||||
@ -184,20 +182,6 @@ void ForwardModule::compileShaders(Scene* scene)
|
|||||||
{
|
{
|
||||||
std::vector<const char*> defines;
|
std::vector<const char*> defines;
|
||||||
|
|
||||||
// set geometry defines
|
|
||||||
if(i & NORMAL_MAP_FLAG)
|
|
||||||
defines.push_back(flagStr[NORMAL_MAP]);
|
|
||||||
if(i & AMBIENT_TEXTURE_FLAG)
|
|
||||||
defines.push_back(flagStr[AMBIENT_TEXTURE]);
|
|
||||||
if(i & DIFFUSE_TEXTURE_FLAG)
|
|
||||||
defines.push_back(flagStr[DIFFUSE_TEXTURE]);
|
|
||||||
if(i & SPECULAR_TEXTURE_FLAG)
|
|
||||||
defines.push_back(flagStr[SPECULAR_TEXTURE]);
|
|
||||||
if(i & ALPHA_MASK_FLAG)
|
|
||||||
defines.push_back(flagStr[ALPHA_MASK]);
|
|
||||||
if(i & INSTANCING_FLAG)
|
|
||||||
defines.push_back(flagStr[INSTANCING]);
|
|
||||||
|
|
||||||
std::size_t boundary = defines.size();
|
std::size_t boundary = defines.size();
|
||||||
for(int j : lightFlagList)
|
for(int j : lightFlagList)
|
||||||
{
|
{
|
||||||
|
@ -28,6 +28,16 @@ Light::Light()
|
|||||||
initPointLight();
|
initPointLight();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Light::initAmbientLight(glm::vec3 lightColor = glm::vec3(0.1f))
|
||||||
|
{
|
||||||
|
type = AMBIENT;
|
||||||
|
position = glm::vec3(0);
|
||||||
|
color = lightColor;
|
||||||
|
shadowCaster = false;
|
||||||
|
isDir = false;
|
||||||
|
viewMatrix = glm::mat4();
|
||||||
|
}
|
||||||
|
|
||||||
void Light::initDirectionnalLight(glm::vec3 dir, glm::vec3 lightColor)
|
void Light::initDirectionnalLight(glm::vec3 dir, glm::vec3 lightColor)
|
||||||
{
|
{
|
||||||
type = DIRECTIONNAL;
|
type = DIRECTIONNAL;
|
||||||
@ -87,8 +97,8 @@ void Light::initShadowMap(int resWidth, int resHeight, glm::vec3 dim)
|
|||||||
shadowMap->initColorAttachments();
|
shadowMap->initColorAttachments();
|
||||||
|
|
||||||
ShaderSource source;
|
ShaderSource source;
|
||||||
source.setSource(vertSource, ShaderSource::VERTEX);
|
source.setSource(shadowVertSource, ShaderSource::VERTEX);
|
||||||
source.setSource(fragSource, ShaderSource::FRAGMENT);
|
source.setSource(shadowFragSource, ShaderSource::FRAGMENT);
|
||||||
std::vector<const char*> defines;
|
std::vector<const char*> defines;
|
||||||
shaders[0] = source.compile(defines.size(), defines.data());
|
shaders[0] = source.compile(defines.size(), defines.data());
|
||||||
defines.push_back("ALPHA_MASK");
|
defines.push_back("ALPHA_MASK");
|
||||||
@ -145,32 +155,30 @@ void Light::setPosition(glm::vec3 new_pos)
|
|||||||
viewMatrix = glm::lookAt(position, position+direction, glm::vec3(0, 1, 0));
|
viewMatrix = glm::lookAt(position, position+direction, glm::vec3(0, 1, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Light::getFlags(Light* l)
|
unsigned int Light::getFlags()
|
||||||
{
|
{
|
||||||
if(l == NULL)
|
|
||||||
return 1 << AMBIENT_FLAG;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
unsigned int flags = 0;
|
unsigned int flags = 0;
|
||||||
switch(l->getType())
|
switch(l->getType())
|
||||||
{
|
{
|
||||||
|
case AMBIENT :
|
||||||
|
flags = 1 << AMBIENT_FLAG;
|
||||||
|
break;
|
||||||
case DIRECTIONNAL :
|
case DIRECTIONNAL :
|
||||||
flags |= 1 << DIRECTIONNAL_FLAG;
|
flags = 1 << DIRECTIONNAL_FLAG;
|
||||||
break;
|
break;
|
||||||
case POINT :
|
case POINT :
|
||||||
flags |= 1 << POINT_FLAG;
|
flags = 1 << POINT_FLAG;
|
||||||
break;
|
break;
|
||||||
case SPOT :
|
case SPOT :
|
||||||
flags |= 1 << SPOT_FLAG;
|
flags = 1 << SPOT_FLAG;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(l->isShadowCaster())
|
if(l->isShadowCaster())
|
||||||
flags |= 1 << SHADOWMAP_FLAG;
|
flags |= 1 << SHADOWMAP_FLAG;
|
||||||
return flags;
|
return flags;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* Light::vertSource =
|
const char* Light::shadowVertSource =
|
||||||
"layout(location = 0)in vec3 inPosition;\n\
|
"layout(location = 0)in vec3 inPosition;\n\
|
||||||
#ifdef ALPHA_MASK\n\
|
#ifdef ALPHA_MASK\n\
|
||||||
layout(location = 2)in vec2 inTexCoord;\n\
|
layout(location = 2)in vec2 inTexCoord;\n\
|
||||||
@ -185,7 +193,7 @@ const char* Light::vertSource =
|
|||||||
gl_Position = MVP * vec4(inPosition, 1.0);\n\
|
gl_Position = MVP * vec4(inPosition, 1.0);\n\
|
||||||
}\n";
|
}\n";
|
||||||
|
|
||||||
const char* Light::fragSource =
|
const char* Light::shadowFragSource =
|
||||||
"#ifdef ALPHA_MASK\n\
|
"#ifdef ALPHA_MASK\n\
|
||||||
uniform sampler2D alphaMask;\n\
|
uniform sampler2D alphaMask;\n\
|
||||||
in vec2 varTexCoord;\n\
|
in vec2 varTexCoord;\n\
|
||||||
|
11
src/light.h
11
src/light.h
@ -15,6 +15,7 @@ class Light : public Camera
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum LightType {
|
enum LightType {
|
||||||
|
AMBIENT,
|
||||||
DIRECTIONNAL,
|
DIRECTIONNAL,
|
||||||
POINT,
|
POINT,
|
||||||
SPOT
|
SPOT
|
||||||
@ -32,6 +33,7 @@ public:
|
|||||||
static const glm::mat4 biasMatrix;
|
static const glm::mat4 biasMatrix;
|
||||||
|
|
||||||
Light();
|
Light();
|
||||||
|
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));
|
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));
|
||||||
@ -58,7 +60,10 @@ public:
|
|||||||
// does nothing, just required for inheriting Camera
|
// does nothing, just required for inheriting Camera
|
||||||
void resize(int width, int height) {}
|
void resize(int width, int height) {}
|
||||||
|
|
||||||
static unsigned int getFlags(Light* l);
|
/**
|
||||||
|
* @brief getFlags returns the flags that defines the specificities of the light
|
||||||
|
*/
|
||||||
|
unsigned int getFlags();
|
||||||
private:
|
private:
|
||||||
// standard attributes
|
// standard attributes
|
||||||
LightType type;
|
LightType type;
|
||||||
@ -79,8 +84,8 @@ private:
|
|||||||
Shader* shaders[2];
|
Shader* shaders[2];
|
||||||
glm::mat4 viewMatrix;
|
glm::mat4 viewMatrix;
|
||||||
glm::mat4 projectionMatrix;
|
glm::mat4 projectionMatrix;
|
||||||
static const char* vertSource;
|
static const char* shadowVertSource;
|
||||||
static const char* fragSource;
|
static const char* shadowFragSource;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LIGHT_H
|
#endif // LIGHT_H
|
||||||
|
@ -16,6 +16,7 @@ private:
|
|||||||
NORMALS_SLOT,
|
NORMALS_SLOT,
|
||||||
SPECULAR_SLOT,
|
SPECULAR_SLOT,
|
||||||
ALPHA_SLOT,
|
ALPHA_SLOT,
|
||||||
|
NB_PHONG_SLOTS
|
||||||
};
|
};
|
||||||
|
|
||||||
glm::vec3 ambient;
|
glm::vec3 ambient;
|
||||||
|
@ -31,52 +31,45 @@ std::vector<unsigned int> Scene::getLightTypes()
|
|||||||
return types;
|
return types;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayScene::~ArrayScene()
|
BasicScene::~BasicScene()
|
||||||
{
|
{
|
||||||
clearScene();
|
clearScene();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArrayScene::addMesh(GeometryNode* node)
|
void BasicScene::addMesh(GeometryNode* gn)
|
||||||
{
|
{
|
||||||
Node n;
|
geometry->push_back(gn);
|
||||||
n.geometry = node;
|
|
||||||
unsigned int flags = node->mesh->getFlags();
|
|
||||||
if(nodes.count(flags) > 0)
|
|
||||||
nodes[m->getFlags()]->push_back(n);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::vector<Node> *vec = new std::vector<Node>();
|
|
||||||
vec->push_back(n);
|
|
||||||
nodes[m->getFlags()] = vec;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArrayScene::addMesh(Mesh* m, glm::mat4 transform)
|
GeometryNode* BasicScene::addMesh(Mesh* m, glm::mat4 transform)
|
||||||
{
|
{
|
||||||
GeometryNode *gn = new GeometryNode(m, transform);
|
GeometryNode *gn = new GeometryNode(m, transform);
|
||||||
allocations.push_back(gn);
|
allocations.push_back(gn);
|
||||||
addMesh(gn);
|
addMesh(gn);
|
||||||
|
return gn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArrayScene::clearScene()
|
void BasicScene::addLight(Light* myLight)
|
||||||
|
{
|
||||||
|
lights.push_back(myLight);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BasicScene::clearScene()
|
||||||
{
|
{
|
||||||
for(auto it : nodes)
|
|
||||||
delete it;
|
|
||||||
for(GeometryNode *gn : allocations)
|
for(GeometryNode *gn : allocations)
|
||||||
delete gn;
|
delete gn;
|
||||||
allocations.clear();
|
allocations.clear();
|
||||||
nodes.clear();
|
geometry.clear();
|
||||||
|
lights.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
SceneIterator<Light*> ArrayScene::getLights(unsigned int flags)
|
SceneIterator<Light*>* BasicScene::getLights()
|
||||||
{
|
{
|
||||||
if(nodes.count(flags))
|
return new ArrayIterator<Light*>(lights);
|
||||||
return ArrayIterator<Light*>(*(nodes[flags]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SceneIterator<GeometryNode*> ArrayScene::getGeometry(unsigned int flags)
|
SceneIterator<GeometryNode*>* BasicScene::getGeometry()
|
||||||
{
|
{
|
||||||
if(nodes.count(flags))
|
return new ArrayIterator<GeometryNode*>(geometry);
|
||||||
return ArrayIterator<GeometryNode*>(*(nodes[flags]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
29
src/scene.h
29
src/scene.h
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include <glm/mat4x4.hpp>
|
#include <glm/mat4x4.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <unordered_map>
|
|
||||||
#include "light.h"
|
#include "light.h"
|
||||||
|
|
||||||
class Camera;
|
class Camera;
|
||||||
@ -41,8 +40,8 @@ public:
|
|||||||
|
|
||||||
Pipeline* getPipeline() {return m_pipeline;}
|
Pipeline* getPipeline() {return m_pipeline;}
|
||||||
|
|
||||||
virtual SceneIterator<Light*> getLights(unsigned int flags) = 0;
|
virtual SceneIterator<Light*>* getLights() = 0;
|
||||||
virtual SceneIterator<GeometryNode*> getGeometry(unsigned int flags) = 0;
|
virtual SceneIterator<GeometryNode*>* getGeometry() = 0;
|
||||||
|
|
||||||
virtual void getMeshTypes(std::vector<unsigned int> &meshTypes);
|
virtual void getMeshTypes(std::vector<unsigned int> &meshTypes);
|
||||||
virtual void getLightTypes(std::vector<unsigned int> &lightTypes);
|
virtual void getLightTypes(std::vector<unsigned int> &lightTypes);
|
||||||
@ -62,31 +61,25 @@ public:
|
|||||||
virtual bool isValid() {return id < vec.size();}
|
virtual bool isValid() {return id < vec.size();}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ArrayScene : public Scene
|
class BasicScene : public Scene
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
union Node
|
std::vector<Light*> lights; // fast node access for rendering
|
||||||
{
|
std::vector<GeometryNode*> geometry;
|
||||||
Light* light;
|
|
||||||
GeometryNode* geometry;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::unordered_map<std::vector<Node>*> nodes;
|
|
||||||
|
|
||||||
std::vector<GeometryNode*> allocations;
|
std::vector<GeometryNode*> allocations;
|
||||||
public:
|
public:
|
||||||
ArrayScene() : Scene() {}
|
BasicScene() : Scene() {}
|
||||||
~ArrayScene();
|
~BasicScene();
|
||||||
|
|
||||||
void setPipeline(Pipeline *pipeline) {m_pipeline = pipeline;}
|
void setPipeline(Pipeline *pipeline) {m_pipeline = pipeline;}
|
||||||
void clearScene();
|
void clearScene();
|
||||||
void addMesh(GeometryNode* node);
|
void addMesh(GeometryNode* node);
|
||||||
void addMesh(Mesh* m, glm::mat4 transform);
|
GeometryNode* addMesh(Mesh* m, glm::mat4 transform);
|
||||||
void addLight(Light* myLight) {lights.push_back(myLight);}
|
void addLight(Light* myLight);
|
||||||
Mesh* getMesh(int id) {return geometry[id]->mesh;}
|
Mesh* getMesh(int id) {return geometry[id]->mesh;}
|
||||||
|
|
||||||
SceneIterator<Light*> getLights(unsigned int flags);
|
SceneIterator<Light*>* getLights();
|
||||||
SceneIterator<GeometryNode*> getGeometry(unsigned int flags);
|
SceneIterator<GeometryNode*>* getGeometry();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SCENE_H
|
#endif // SCENE_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user