diff --git a/CMakeLists.txt b/CMakeLists.txt index c00b695..3c16fc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,9 +27,10 @@ set(LIB_SRC_LIST parametricmesh.cpp texture.cpp scene.cpp - entityloader.cpp deferredmodule.cpp + forwardmodule.cpp shadersource.cpp + light.cpp ) set(LIBRARY_NAME ${PROJECT_NAME}) diff --git a/deferredmodule.h b/deferredmodule.h index 8900b44..4cf32fc 100644 --- a/deferredmodule.h +++ b/deferredmodule.h @@ -13,13 +13,9 @@ class PhongEntity; class DeferredModule : public Module { public: - DeferredModule(Lights::Light* myDirLight, Lights* myPointLights); + DeferredModule(); - //void addEntity(PhongEntity* myEntity); - //void clearEntities(); - - virtual void renderGL(Camera* myCamera) = 0; - virtual bool requiresModernOpenGL() {return true;} + virtual void renderGL(Camera* myCamera, Scene* scene); private: /*Lights::Light* dirLight; Lights* pointLights; diff --git a/forwardmodule.cpp b/forwardmodule.cpp new file mode 100644 index 0000000..7dcdc45 --- /dev/null +++ b/forwardmodule.cpp @@ -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* EntityIt = scene->getGeometry(); + EntityIt->isValid(); EntityIt->next()) + { + Mesh* m = EntityIt->getItem()->getMesh(); + for(Mesh::Group &g : m->indiceGroups) + { + + } + } +} diff --git a/forwardmodule.h b/forwardmodule.h new file mode 100644 index 0000000..1055af4 --- /dev/null +++ b/forwardmodule.h @@ -0,0 +1,41 @@ +#ifndef FORWARDMODULE_H +#define FORWARDMODULE_H + +#include "module.h" +#include +#include +#include +#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 diff --git a/light.cpp b/light.cpp new file mode 100644 index 0000000..fb0669a --- /dev/null +++ b/light.cpp @@ -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; +} diff --git a/light.h b/light.h new file mode 100644 index 0000000..2bd9ec8 --- /dev/null +++ b/light.h @@ -0,0 +1,25 @@ +#ifndef LIGHT_H +#define LIGHT_H + +#include + +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 diff --git a/lights.h b/lights.h index f3af153..8dbe27a 100644 --- a/lights.h +++ b/lights.h @@ -1,5 +1,5 @@ -#ifndef LIGHT_H -#define LIGHT_H +#ifndef LIGHTS_H +#define LIGHTS_H #include #include @@ -19,10 +19,12 @@ public: { glm::vec3 position; glm::vec3 color; + bool shadowCaster; + // Shadowmap fbo } Light; private: std::vector lights; }; -#endif // LIGHT_H +#endif // LIGHTS_H diff --git a/material.h b/material.h index 7833dbf..552af6d 100644 --- a/material.h +++ b/material.h @@ -4,6 +4,14 @@ #include "shader.h" #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 { public: @@ -11,6 +19,7 @@ public: Shader* getShader() {return shader;} virtual void bindAttributes() = 0; + virtual unsigned int getFlags() = 0; protected: Shader* shader; diff --git a/module.h b/module.h index 9faf600..00ed72a 100644 --- a/module.h +++ b/module.h @@ -2,11 +2,12 @@ #define MODULE class Camera; +class Scene; class Module { public: - virtual void renderGL(Camera* myCamera) = 0; + virtual void renderGL(Camera* myCamera, Scene* scene) = 0; virtual bool requiresModernOpenGL() {return true;} }; diff --git a/modules.todo b/modules.todo index d37df98..28d621b 100644 --- a/modules.todo +++ b/modules.todo @@ -12,7 +12,7 @@ Modules : - forward (compatible crappy) IN : for sources -for geometry +for geometrie phong OUT : image éclairée @@ -25,13 +25,13 @@ image éclairée - gbuffer IN : -for géométrie +for geometrie phong OUT : gbuffer - shadowmap IN : -for géométrie +for géométrie phong source OUT : shadowmap diff --git a/phongmaterial.cpp b/phongmaterial.cpp index 4c86993..aea9401 100644 --- a/phongmaterial.cpp +++ b/phongmaterial.cpp @@ -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) +{ + setDiffuseTexture(myTexture); +} + +void PhongMaterial::setDiffuseTexture(Texture* myTexture) { diffuse_texture = myTexture; updateShader(); } +void PhongMaterial::setSpecularTexture(Texture* myTexture) +{ + specular_texture = myTexture; + updateShader(); +} + void PhongMaterial::setNormalMap(Texture* myNormalMap) { normal_map = myNormalMap; diff --git a/phongmaterial.h b/phongmaterial.h index 2b76a65..0916eb7 100644 --- a/phongmaterial.h +++ b/phongmaterial.h @@ -14,6 +14,7 @@ public: glm::vec3 specular; float shininess; Texture* diffuse_texture; + Texture* specular_texture; Texture* normal_map; PhongMaterial() : @@ -21,7 +22,8 @@ public: diffuse(0.5f), specular(0.5f), shininess(10), - diffuse_texture(NULL), + diffuse_texture(NULL), + specular_texture(NULL), normal_map(NULL) { updateShader(); @@ -32,14 +34,20 @@ public: specular(myKs), shininess(myNs), diffuse_texture(NULL), + specular_texture(NULL), normal_map(NULL) { updateShader(); } virtual void bindAttributes(); + virtual unsigned int getFlags(); void crappyBindAttributes(); + // deprecated, you should use setDiffuseTexture instead void setTexture(Texture* myTexture); + + void setDiffuseTexture(Texture* myTexture); + void setSpecularTexture(Texture* myTexture); void setNormalMap(Texture* myNormalMap); void updateShader(); diff --git a/phongmodule.cpp b/phongmodule.cpp index b8b7ad3..713642d 100644 --- a/phongmodule.cpp +++ b/phongmodule.cpp @@ -34,7 +34,7 @@ void PhongModule::clearEntities() entities.clear(); } -void PhongModule::renderGL(Camera* myCamera) +void PhongModule::renderGL(Camera* myCamera, Scene* scene) { if(!SparrowRenderer::isModernOpenGLAvailable()) { diff --git a/phongmodule.h b/phongmodule.h index d3d8462..44e53e3 100644 --- a/phongmodule.h +++ b/phongmodule.h @@ -25,7 +25,7 @@ public: void addEntity(PhongEntity* myEntity); void clearEntities(); - virtual void renderGL(Camera* myCamera); + virtual void renderGL(Camera* myCamera, Scene* scene = NULL); virtual bool requiresModernOpenGL() {return false;} // modern opengl methods diff --git a/scene.h b/scene.h index b589a02..c8e7d5d 100644 --- a/scene.h +++ b/scene.h @@ -3,74 +3,55 @@ #include #include "camera.h" - -// Scene Node flags : - -#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... +#include +#include "light.h" +#include "phongentity.h" // Scene interface : +template +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 { - // the scene is supposed to contain Scene Nodes public: - // this should draw all nodes matching the specified flags. - virtual draw(unsigned int flags) = 0; + virtual SceneIterator* getLights() = 0; + virtual SceneIterator* getGeometry() = 0; }; // Some basic implementations : -class Mesh; - -class LeafNode : public SceneNode +template +class ArraySceneIterator : public SceneIterator { - glm::mat4 transform; - std::vector children; + std::vector &vec; + int id; public: - virtual void drawChildren(glm::mat4 m, unsigned int flags) - { - for(LeafNode* node : children) - node->drawChildren(m*transform, flags); - } + ArraySceneIterator(std::vector &myVec, int myId=0) : vec(myVec), id(myId) {} + virtual SceneIterator& operator++() {++id; return *this;} + virtual T& operator*() {return vec[id];} + virtual bool isValid() {return id < vec.size();} }; -class MeshNode : public LeafNode +class ArrayScene : public Scene { - Mesh* mesh; + std::vector lights; + std::vector entities; public: - virtual void drawChildren(glm::mat4 m, unsigned int flags) - { - // TODO : draw VAOs matching the flags - for(LeafNode* node : children) - node->drawChildren(m*transform, flags); - } -}; + void clearScene() {lights.clear(); entities.clear();} + void addEntity(PhongEntity* myEntity) {entities.push_back(myEntity);} + void addLight(Light* myLight) {lights.push_back(myLight);} -class TreeScene -{ - 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; + virtual SceneIterator* getLights() {return new ArraySceneIterator(lights);} + virtual SceneIterator* getGeometry() {return new ArraySceneIterator(entities);} }; #endif // SCENE_H diff --git a/shadersource.cpp b/shadersource.cpp index 5ec0e80..66d6133 100644 --- a/shadersource.cpp +++ b/shadersource.cpp @@ -18,11 +18,14 @@ ShaderSource::~ShaderSource() delete(sources[i]); } -void ShaderSource::addSource(const char *source, SourceType type) +void ShaderSource::setSource(const char *source, SourceType type) { if(sources[type] != NULL) delete(sources[type]); - sources[type] = new std::string(source); + if(source == NULL) + sources[type] = NULL; + else + sources[type] = new std::string(source); } Shader* ShaderSource::compile(int nbDefines, const char** defines) diff --git a/shadersource.h b/shadersource.h index 78512be..5c2872f 100644 --- a/shadersource.h +++ b/shadersource.h @@ -19,7 +19,7 @@ public: 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); diff --git a/skyboxmodule.cpp b/skyboxmodule.cpp index 2671ac4..97ef1f6 100644 --- a/skyboxmodule.cpp +++ b/skyboxmodule.cpp @@ -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 projectionMatrix = myCamera->getProjectionMatrix(); diff --git a/skyboxmodule.h b/skyboxmodule.h index 2a911a9..c75de24 100644 --- a/skyboxmodule.h +++ b/skyboxmodule.h @@ -28,7 +28,7 @@ class SkyboxModule : public Module public: SkyboxModule(Texture* myCubeMap); ~SkyboxModule(); - virtual void renderGL(Camera* myCamera); + virtual void renderGL(Camera* myCamera, Scene* scene = NULL); virtual bool requiresModernOpenGL() {return false;} }; diff --git a/sparrowrenderer.cpp b/sparrowrenderer.cpp index 6a32225..b8f5c24 100644 --- a/sparrowrenderer.cpp +++ b/sparrowrenderer.cpp @@ -62,7 +62,7 @@ void SparrowRenderer::renderGL() for(ModuleNode &m : modules) { if(m.isEnabled) - m.module->renderGL(getCamera()); + m.module->renderGL(getCamera(), scene); } } @@ -96,4 +96,16 @@ Camera* SparrowRenderer::getCamera() return camera; } +// scene methods + +void SparrowRenderer::setScene(Scene* myScene) +{ + scene = myScene; +} + +Scene* SparrowRenderer::getScene() +{ + return scene; +} + diff --git a/sparrowrenderer.h b/sparrowrenderer.h index 53f6ca4..af48922 100644 --- a/sparrowrenderer.h +++ b/sparrowrenderer.h @@ -6,6 +6,7 @@ class Camera; class Module; +class Scene; class SparrowRenderer { @@ -25,6 +26,10 @@ public: void setCamera(Camera* myCamera); Camera* getCamera(); + // scene methods + void setScene(Scene* myScene); + Scene* getScene(); + protected: typedef struct s_moduleNode{ Module* module; @@ -35,6 +40,7 @@ protected: } ModuleNode; Camera* camera; + Scene* scene; std::list modules; static bool modernOpenglAvailable;