additions in scene, shader, material, modules, towards the new module format

This commit is contained in:
anselme 2015-11-28 14:43:03 +01:00
parent e95212468f
commit 09afb90dd4
21 changed files with 245 additions and 74 deletions

View File

@ -27,9 +27,10 @@ set(LIB_SRC_LIST
parametricmesh.cpp parametricmesh.cpp
texture.cpp texture.cpp
scene.cpp scene.cpp
entityloader.cpp
deferredmodule.cpp deferredmodule.cpp
forwardmodule.cpp
shadersource.cpp shadersource.cpp
light.cpp
) )
set(LIBRARY_NAME ${PROJECT_NAME}) set(LIBRARY_NAME ${PROJECT_NAME})

View File

@ -13,13 +13,9 @@ class PhongEntity;
class DeferredModule : public Module class DeferredModule : public Module
{ {
public: public:
DeferredModule(Lights::Light* myDirLight, Lights* myPointLights); DeferredModule();
//void addEntity(PhongEntity* myEntity); virtual void renderGL(Camera* myCamera, Scene* scene);
//void clearEntities();
virtual void renderGL(Camera* myCamera) = 0;
virtual bool requiresModernOpenGL() {return true;}
private: private:
/*Lights::Light* dirLight; /*Lights::Light* dirLight;
Lights* pointLights; Lights* pointLights;

38
forwardmodule.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "forwardmodule.h"
#include "scene.h"
#include "phongentity.h"
#include "mesh.h"
#include "material.h"
const char* const ForwardModule::flagStr[ForwardModule::NB_FLAGS] =
{
"NORMAL_MAP",
"DIFFUSE_TEXTURE",
"SPECULAR_TEXTURE",
"ALPHA_MASK"
};
void ForwardModule::renderGL(Camera* myCamera, Scene* scene)
{
}
// modern opengl methods
void ForwardModule::setShaderSource(ShaderSource* source)
{
shaderSources = source;
}
void ForwardModule::compileShaders(Scene* scene)
{
for(SceneIterator<PhongEntity*>* EntityIt = scene->getGeometry();
EntityIt->isValid(); EntityIt->next())
{
Mesh* m = EntityIt->getItem()->getMesh();
for(Mesh::Group &g : m->indiceGroups)
{
}
}
}

41
forwardmodule.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef FORWARDMODULE_H
#define FORWARDMODULE_H
#include "module.h"
#include <vector>
#include <cstddef>
#include <glew/glew.h>
#include "shadersource.h"
class Scene;
class PhongEntity;
class ForwardModule : public Module
{
public:
ForwardModule() : shaderSources(NULL) {}
virtual void renderGL(Camera* myCamera, Scene* scene);
virtual bool requiresModernOpenGL() {return false;}
// modern opengl methods
void setShaderSource(ShaderSource* source);
void compileShaders(Scene* scene);
private:
enum {
NORMAL_MAP,
DIFFUSE_TEXTURE,
SPECULAR_TEXTURE,
ALPHA_MASK,
NB_FLAGS
};
static const char* const flagStr[NB_FLAGS];
ShaderSource* shaderSources;
Shader* shaders[1 << NB_FLAGS];
};
#endif // FORWARDMODULE_H

25
light.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "light.h"
Light::Light()
{
initPointLight();
}
void Light::initDirectionnalLight(glm::vec3 dir, glm::vec3 lightColor, bool isShadowCaster)
{
initSpotLight(glm::vec3(0), dir, -1, lightColor, isShadowCaster);
}
void Light::initPointLight(glm::vec3 pos, glm::vec3 lightColor, bool isShadowCaster)
{
initSpotLight(pos, glm::vec3(0), 360, lightColor, isShadowCaster);
}
void Light::initSpotLight(glm::vec3 pos, glm::vec3 dir, float spotAngle, glm::vec3 lightColor, bool isShadowCaster)
{
position = pos;
direction = dir;
angle = spotAngle;
color = lightColor;
shadowCaster = isShadowCaster;
}

25
light.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef LIGHT_H
#define LIGHT_H
#include <glm/vec3.hpp>
class Light
{
private:
// standard attributes
glm::vec3 position;
glm::vec3 direction;
float angle;
glm::vec3 color;
// shadowmap attributes
bool shadowCaster;
// Shadowmap fbo
public:
Light();
void initDirectionnalLight(glm::vec3 dir = glm::vec3(1, 0, 0), glm::vec3 lightColor = glm::vec3(1), bool isShadowCaster = false);
void initPointLight(glm::vec3 pos = glm::vec3(0), glm::vec3 lightColor = glm::vec3(1), bool isShadowCaster = false);
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), bool isShadowCaster = false);
};
#endif // LIGHT_H

View File

@ -1,5 +1,5 @@
#ifndef LIGHT_H #ifndef LIGHTS_H
#define LIGHT_H #define LIGHTS_H
#include <glew/glew.h> #include <glew/glew.h>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
@ -19,10 +19,12 @@ public:
{ {
glm::vec3 position; glm::vec3 position;
glm::vec3 color; glm::vec3 color;
bool shadowCaster;
// Shadowmap fbo
} Light; } Light;
private: private:
std::vector<Light> lights; std::vector<Light> lights;
}; };
#endif // LIGHT_H #endif // LIGHTS_H

View File

@ -4,6 +4,14 @@
#include "shader.h" #include "shader.h"
#include "glm/fwd.hpp" #include "glm/fwd.hpp"
enum {
// Geometry Flags
NORMAL_MAP = 1 << 0,
DIFFUSE_TEXTURE = 1 << 1,
SPECULAR_TEXTURE = 1 << 2,
ALPHA_MASK = 1 << 3,
};
class Material class Material
{ {
public: public:
@ -11,6 +19,7 @@ public:
Shader* getShader() {return shader;} Shader* getShader() {return shader;}
virtual void bindAttributes() = 0; virtual void bindAttributes() = 0;
virtual unsigned int getFlags() = 0;
protected: protected:
Shader* shader; Shader* shader;

View File

@ -2,11 +2,12 @@
#define MODULE #define MODULE
class Camera; class Camera;
class Scene;
class Module class Module
{ {
public: public:
virtual void renderGL(Camera* myCamera) = 0; virtual void renderGL(Camera* myCamera, Scene* scene) = 0;
virtual bool requiresModernOpenGL() {return true;} virtual bool requiresModernOpenGL() {return true;}
}; };

View File

@ -12,7 +12,7 @@ Modules :
- forward (compatible crappy) - forward (compatible crappy)
IN : IN :
for sources for sources
for geometry for geometrie phong
OUT : OUT :
image éclairée image éclairée
@ -25,13 +25,13 @@ image éclairée
- gbuffer - gbuffer
IN : IN :
for géométrie for geometrie phong
OUT : OUT :
gbuffer gbuffer
- shadowmap - shadowmap
IN : IN :
for géométrie for géométrie phong
source source
OUT : OUT :
shadowmap shadowmap

View File

@ -44,12 +44,35 @@ void PhongMaterial::bindAttributes()
} }
} }
unsigned int PhongMaterial::getFlags()
{
unsigned int flags = 0;
if(normal_map != NULL)
flags |= NORMAL_MAP;
if(diffuse_texture != NULL)
flags |= DIFFUSE_TEXTURE;
if(specular_texture != NULL)
flags |= SPECULAR_TEXTURE;
return flags;
}
void PhongMaterial::setTexture(Texture* myTexture) void PhongMaterial::setTexture(Texture* myTexture)
{
setDiffuseTexture(myTexture);
}
void PhongMaterial::setDiffuseTexture(Texture* myTexture)
{ {
diffuse_texture = myTexture; diffuse_texture = myTexture;
updateShader(); updateShader();
} }
void PhongMaterial::setSpecularTexture(Texture* myTexture)
{
specular_texture = myTexture;
updateShader();
}
void PhongMaterial::setNormalMap(Texture* myNormalMap) void PhongMaterial::setNormalMap(Texture* myNormalMap)
{ {
normal_map = myNormalMap; normal_map = myNormalMap;

View File

@ -14,6 +14,7 @@ public:
glm::vec3 specular; glm::vec3 specular;
float shininess; float shininess;
Texture* diffuse_texture; Texture* diffuse_texture;
Texture* specular_texture;
Texture* normal_map; Texture* normal_map;
PhongMaterial() : PhongMaterial() :
@ -22,6 +23,7 @@ public:
specular(0.5f), specular(0.5f),
shininess(10), shininess(10),
diffuse_texture(NULL), diffuse_texture(NULL),
specular_texture(NULL),
normal_map(NULL) normal_map(NULL)
{ {
updateShader(); updateShader();
@ -32,14 +34,20 @@ public:
specular(myKs), specular(myKs),
shininess(myNs), shininess(myNs),
diffuse_texture(NULL), diffuse_texture(NULL),
specular_texture(NULL),
normal_map(NULL) normal_map(NULL)
{ {
updateShader(); updateShader();
} }
virtual void bindAttributes(); virtual void bindAttributes();
virtual unsigned int getFlags();
void crappyBindAttributes(); void crappyBindAttributes();
// deprecated, you should use setDiffuseTexture instead
void setTexture(Texture* myTexture); void setTexture(Texture* myTexture);
void setDiffuseTexture(Texture* myTexture);
void setSpecularTexture(Texture* myTexture);
void setNormalMap(Texture* myNormalMap); void setNormalMap(Texture* myNormalMap);
void updateShader(); void updateShader();

View File

@ -34,7 +34,7 @@ void PhongModule::clearEntities()
entities.clear(); entities.clear();
} }
void PhongModule::renderGL(Camera* myCamera) void PhongModule::renderGL(Camera* myCamera, Scene* scene)
{ {
if(!SparrowRenderer::isModernOpenGLAvailable()) if(!SparrowRenderer::isModernOpenGLAvailable())
{ {

View File

@ -25,7 +25,7 @@ public:
void addEntity(PhongEntity* myEntity); void addEntity(PhongEntity* myEntity);
void clearEntities(); void clearEntities();
virtual void renderGL(Camera* myCamera); virtual void renderGL(Camera* myCamera, Scene* scene = NULL);
virtual bool requiresModernOpenGL() {return false;} virtual bool requiresModernOpenGL() {return false;}
// modern opengl methods // modern opengl methods

83
scene.h
View File

@ -3,74 +3,55 @@
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include "camera.h" #include "camera.h"
#include <vector>
// Scene Node flags : #include "light.h"
#include "phongentity.h"
#define CAMERA_NODE 1
#define DIRECTIONNAL_LIGHT_NODE 2
#define POINT_LIGHT_NODE 4
#define PHONG_NODE 8
#define PHONG_DIFFUSE_TEXTURED_NODE 16
#define PHONG_SPECULAR_TEXTURED_NODE 32
#define PHONG_NORMAL_TEXTURED_NODE 64
#define PARTICLES_NODE 128
// up to 24 more nodes can be added here...
// Scene interface : // Scene interface :
template <class T>
class SceneIterator
{
public:
virtual SceneIterator& operator++() = 0;
virtual T& operator*() = 0;
virtual bool isValid() = 0;
void next() {if(isValid()) operator++();}
T& getItem() {return operator*();}
};
class Scene class Scene
{ {
// the scene is supposed to contain Scene Nodes
public: public:
// this should draw all nodes matching the specified flags. virtual SceneIterator<Light*>* getLights() = 0;
virtual draw(unsigned int flags) = 0; virtual SceneIterator<PhongEntity*>* getGeometry() = 0;
}; };
// Some basic implementations : // Some basic implementations :
class Mesh; template <class T>
class ArraySceneIterator : public SceneIterator<T>
class LeafNode : public SceneNode
{ {
glm::mat4 transform; std::vector<T> &vec;
std::vector<LeafNode*> children; int id;
public: public:
virtual void drawChildren(glm::mat4 m, unsigned int flags) ArraySceneIterator(std::vector<T> &myVec, int myId=0) : vec(myVec), id(myId) {}
{ virtual SceneIterator<T>& operator++() {++id; return *this;}
for(LeafNode* node : children) virtual T& operator*() {return vec[id];}
node->drawChildren(m*transform, flags); virtual bool isValid() {return id < vec.size();}
}
}; };
class MeshNode : public LeafNode class ArrayScene : public Scene
{ {
Mesh* mesh; std::vector<Light*> lights;
std::vector<PhongEntity*> entities;
public: public:
virtual void drawChildren(glm::mat4 m, unsigned int flags) void clearScene() {lights.clear(); entities.clear();}
{ void addEntity(PhongEntity* myEntity) {entities.push_back(myEntity);}
// TODO : draw VAOs matching the flags void addLight(Light* myLight) {lights.push_back(myLight);}
for(LeafNode* node : children)
node->drawChildren(m*transform, flags);
}
};
class TreeScene virtual SceneIterator<Light*>* getLights() {return new ArraySceneIterator<Light*>(lights);}
{ virtual SceneIterator<PhongEntity*>* getGeometry() {return new ArraySceneIterator<PhongEntity*>(entities);}
LeafNode* root;
public:
virtual draw(unsigned int flags)
{
root->drawChildren(glm::mat4(), flags);
}
};
class CameraNode : public LeafNode
{
public:
virtual glm::mat4 getProjectionMatrix() = 0;
virtual glm::mat4 getViewMatrix() = 0;
virtual void resize(int width, int height) = 0;
}; };
#endif // SCENE_H #endif // SCENE_H

View File

@ -18,10 +18,13 @@ ShaderSource::~ShaderSource()
delete(sources[i]); delete(sources[i]);
} }
void ShaderSource::addSource(const char *source, SourceType type) void ShaderSource::setSource(const char *source, SourceType type)
{ {
if(sources[type] != NULL) if(sources[type] != NULL)
delete(sources[type]); delete(sources[type]);
if(source == NULL)
sources[type] = NULL;
else
sources[type] = new std::string(source); sources[type] = new std::string(source);
} }

View File

@ -19,7 +19,7 @@ public:
ShaderSource(); ShaderSource();
~ShaderSource(); ~ShaderSource();
void addSource(const char *source, SourceType type); void setSource(const char *source, SourceType type);
Shader* compile(int nbDefines = 0, const char** defines = NULL); Shader* compile(int nbDefines = 0, const char** defines = NULL);

View File

@ -50,7 +50,7 @@ SkyboxModule::~SkyboxModule()
} }
} }
void SkyboxModule::renderGL(Camera* myCamera) void SkyboxModule::renderGL(Camera* myCamera, Scene* scene)
{ {
glm::mat4 viewMatrix = glm::mat4(glm::mat3(myCamera->getViewMatrix())); glm::mat4 viewMatrix = glm::mat4(glm::mat3(myCamera->getViewMatrix()));
glm::mat4 projectionMatrix = myCamera->getProjectionMatrix(); glm::mat4 projectionMatrix = myCamera->getProjectionMatrix();

View File

@ -28,7 +28,7 @@ class SkyboxModule : public Module
public: public:
SkyboxModule(Texture* myCubeMap); SkyboxModule(Texture* myCubeMap);
~SkyboxModule(); ~SkyboxModule();
virtual void renderGL(Camera* myCamera); virtual void renderGL(Camera* myCamera, Scene* scene = NULL);
virtual bool requiresModernOpenGL() {return false;} virtual bool requiresModernOpenGL() {return false;}
}; };

View File

@ -62,7 +62,7 @@ void SparrowRenderer::renderGL()
for(ModuleNode &m : modules) for(ModuleNode &m : modules)
{ {
if(m.isEnabled) if(m.isEnabled)
m.module->renderGL(getCamera()); m.module->renderGL(getCamera(), scene);
} }
} }
@ -96,4 +96,16 @@ Camera* SparrowRenderer::getCamera()
return camera; return camera;
} }
// scene methods
void SparrowRenderer::setScene(Scene* myScene)
{
scene = myScene;
}
Scene* SparrowRenderer::getScene()
{
return scene;
}

View File

@ -6,6 +6,7 @@
class Camera; class Camera;
class Module; class Module;
class Scene;
class SparrowRenderer class SparrowRenderer
{ {
@ -25,6 +26,10 @@ public:
void setCamera(Camera* myCamera); void setCamera(Camera* myCamera);
Camera* getCamera(); Camera* getCamera();
// scene methods
void setScene(Scene* myScene);
Scene* getScene();
protected: protected:
typedef struct s_moduleNode{ typedef struct s_moduleNode{
Module* module; Module* module;
@ -35,6 +40,7 @@ protected:
} ModuleNode; } ModuleNode;
Camera* camera; Camera* camera;
Scene* scene;
std::list<ModuleNode> modules; std::list<ModuleNode> modules;
static bool modernOpenglAvailable; static bool modernOpenglAvailable;