diff --git a/asciientity.h b/asciientity.h index a3b4963..48a1bd0 100644 --- a/asciientity.h +++ b/asciientity.h @@ -6,7 +6,7 @@ #include #include #include "material.h" -#include "resourcebase.h" +#include "asciimodule.h" class Texture; @@ -26,7 +26,7 @@ public: backColor(myBackColor), fontColor(myFontColor) { - shader = ResourceBase::getShader("ascii"); + shader = ASCIIModule::getShader(); } virtual void bindAttributes(); @@ -34,16 +34,17 @@ public: class ASCIIEntity : public Entity { - GLuint rows; - GLuint columns; - char* textBuffer; - glm::vec2 position; glm::vec2 size; glm::mat4 modelView; ASCIIMaterial* mat; + GLuint rows; + GLuint columns; + + char* textBuffer; + void updateModelView(); public: diff --git a/asciimodule.cpp b/asciimodule.cpp index 9fbf52a..77b2898 100644 --- a/asciimodule.cpp +++ b/asciimodule.cpp @@ -1,5 +1,7 @@ #include "asciimodule.h" +Shader* ASCIIModule::shader; + ASCIIModule::ASCIIModule() { @@ -9,3 +11,13 @@ void ASCIIModule::renderGL(Camera* myCamera) { } + +Shader* ASCIIModule::getShader() +{ + return shader; +} + +void ASCIIModule::setShader(Shader* myShader) +{ + shader = myShader; +} diff --git a/asciimodule.h b/asciimodule.h index 083d62b..f99bd26 100644 --- a/asciimodule.h +++ b/asciimodule.h @@ -9,14 +9,16 @@ class Shader; class ASCIIModule : public Module { - Shader* shader; + static Shader* shader; std::vector entities; - public: ASCIIModule(); void addEntity(ASCIIEntity* myEntity); virtual void renderGL(Camera* myCamera); + + static Shader* getShader(); + static void setShader(Shader* myShader); }; #endif // ASCIIMODULE_H diff --git a/camera.h b/camera.h index c6492d5..06e03c0 100644 --- a/camera.h +++ b/camera.h @@ -10,7 +10,7 @@ public: virtual glm::mat4 getProjectionMatrix() = 0; virtual glm::mat4 getViewMatrix() = 0; - virtual void resize(int width, int height) = 0; + virtual void resize(int width, int height) = 0; }; #endif // CAMERA_H diff --git a/phongcolor.frag b/phongcolor.frag new file mode 100644 index 0000000..3f1b872 --- /dev/null +++ b/phongcolor.frag @@ -0,0 +1,45 @@ +#version 330 core + +// material +uniform vec3 materialKd; +uniform vec3 materialKs; +uniform float materialNs; + +uniform vec3 dirLight[2]; +uniform int nbPointLights; +uniform vec3 pointLights[8]; + +// fragment +in vec3 varNormal; +in vec2 varTexCoord; + +in vec3 lightDirInView[5]; +in vec3 halfVecInView[5]; + +// resultat +layout(location = 0)out vec4 outColor; + +// -------------------- + +vec3 computeLight(in vec3 kd, in vec3 ks, in float ns, in vec3 color, in vec3 normal, in vec3 lightDir, in vec3 halfVec){ + float diffuse = 0; + float specular = 0; + + diffuse = dot(normal, lightDir); + diffuse = diffuse < 0 ? 0 : diffuse; + specular = dot(halfVec, normal); + specular = specular < 0 ? 0 : specular; + + return color*diffuse*(kd+ks*pow(specular, ns)); +} + +void main(void) { + int i; + vec3 kd = materialKd; + vec3 light = 0.1*kd + computeLight(kd, materialKs, materialNs, dirLight[1], varNormal, lightDirInView[0], halfVecInView[0]); + for(i=1; ibindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix); shader->bindMatrix(shader->getLocation("MVP"), mvp); + shader->bindVec3Array(shader->getLocation("dirLight"), (glm::vec3*)dirLight, 2); + pointLights->bind(shader->getLocation("pointLights"), shader->getLocation("nbPointLights"), shader); drawGroup(i); } } diff --git a/phongentity.h b/phongentity.h index 9acf5f2..3b4f533 100644 --- a/phongentity.h +++ b/phongentity.h @@ -4,14 +4,13 @@ #include #include #include +#include "lights.h" class Mesh; class Material; class Shader; -#include "entity.h" - -class PhongEntity : public Entity +class PhongEntity { protected: enum { @@ -36,7 +35,7 @@ public: PhongEntity(Mesh* myMesh); ~PhongEntity(); - virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix); + void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix, Lights::Light* dirLight, Lights* pointLights); void initGL(bool isDynamic = false); void destroyGL(); diff --git a/phongmaterial.cpp b/phongmaterial.cpp index d71a2ba..4a83f9b 100644 --- a/phongmaterial.cpp +++ b/phongmaterial.cpp @@ -1,22 +1,29 @@ #include "phongmaterial.h" #include "texture.h" +#include "phongmodule.h" #define TEX_ID 0 +void PhongMaterial::updateShader() +{ + shader = PhongModule::getShader(diffuse_texture == NULL ? PhongModule::PHONG_COLOR : PhongModule::PHONG_TEXTURE); +} + void PhongMaterial::bindAttributes() { shader->bind(); shader->bindVec3(shader->getLocation("materialKd"), kd); shader->bindVec3(shader->getLocation("materialKs"), ks); shader->bindFloat(shader->getLocation("materialNs"), ns); - if(tex != NULL) + if(diffuse_texture != NULL) { - tex->bind(TEX_ID); + diffuse_texture->bind(TEX_ID); shader->bindInteger(shader->getLocation("baseTexture"), TEX_ID); } } void PhongMaterial::setTexture(Texture* myTexture) { - tex = myTexture; + diffuse_texture = myTexture; + updateShader(); } diff --git a/phongmaterial.h b/phongmaterial.h index c82928c..56133c7 100644 --- a/phongmaterial.h +++ b/phongmaterial.h @@ -3,7 +3,6 @@ #include "material.h" #include "glm/vec3.hpp" -#include "resourcebase.h" class Texture; @@ -13,19 +12,22 @@ public: glm::vec3 kd; glm::vec3 ks; float ns; - Texture* tex; + Texture* diffuse_texture; + Texture* normal_map; - PhongMaterial() : kd(0.5f), ks(0.5f), ns(10), tex(NULL) + PhongMaterial() : kd(0.5f), ks(0.5f), ns(10), diffuse_texture(NULL) { - shader = ResourceBase::getShader("phong"); + updateShader(); } - PhongMaterial(glm::vec3 myKd, glm::vec3 myKs, float myNs) : kd(myKd), ks(myKs), ns(myNs), tex(NULL) + PhongMaterial(glm::vec3 myKd, glm::vec3 myKs, float myNs) : kd(myKd), ks(myKs), ns(myNs), diffuse_texture(NULL) { - shader = ResourceBase::getShader("phong"); + updateShader(); } virtual void bindAttributes(); void setTexture(Texture* myTexture); + + void updateShader(); }; #endif // PHONGMATERIAL_H diff --git a/phongmodule.cpp b/phongmodule.cpp index 04fb2b4..f54a856 100644 --- a/phongmodule.cpp +++ b/phongmodule.cpp @@ -1,18 +1,17 @@ #include "phongmodule.h" #include "lights.h" #include "shader.h" -#include "resourcebase.h" #include "phongentity.h" #include "camera.h" +#include "mesh.h" + +Shader* PhongModule::shaders[NB_SHADERS] = {0, 0}; PhongModule::PhongModule(Lights::Light* myDirLight, Lights* myPointLights) : dirLight(myDirLight), - pointLights(myPointLights), - shader(ResourceBase::getShader("phong")) + pointLights(myPointLights) { - dirLightLocation = shader->getLocation("dirLight"); - nbPointLightsLocation = shader->getLocation("nbPointLights"); - pointLightsLocation = shader->getLocation("pointLights"); + } void PhongModule::addEntity(PhongEntity* myEntity) @@ -27,9 +26,16 @@ void PhongModule::clearEntities() void PhongModule::renderGL(Camera* myCamera) { - shader->bind(); - shader->bindVec3Array(dirLightLocation, (glm::vec3*)dirLight, 2); - pointLights->bind(pointLightsLocation, nbPointLightsLocation, shader); for(PhongEntity* e : entities) - e->draw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix()); + e->draw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix(), dirLight, pointLights); +} + +void PhongModule::setShader(ShaderType slot, Shader* myShader) +{ + shaders[slot] = myShader; +} + +Shader* PhongModule::getShader(ShaderType slot) +{ + return shaders[slot]; } diff --git a/phongmodule.h b/phongmodule.h index 7c5d028..4919368 100644 --- a/phongmodule.h +++ b/phongmodule.h @@ -12,23 +12,27 @@ class PhongEntity; class PhongModule : public Module { -private: - Lights::Light* dirLight; - Lights* pointLights; - GLuint dirLightLocation; - GLuint nbPointLightsLocation; - GLuint pointLightsLocation; - - Shader* shader; - std::vector entities; - public: + enum ShaderType {PHONG_COLOR, PHONG_TEXTURE, NB_SHADERS}; + PhongModule(Lights::Light* myDirLight, Lights* myPointLights); void addEntity(PhongEntity* myEntity); void clearEntities(); void virtual renderGL(Camera* myCamera); + + static void setShader(ShaderType slot, Shader* myShader); + static Shader* getShader(ShaderType slot); +private: + Lights::Light* dirLight; + Lights* pointLights; + GLuint dirLightLocation; + GLuint nbPointLightsLocation; + GLuint pointLightsLocation; + std::vector entities; + + static Shader* shaders[NB_SHADERS]; }; #endif // PHONGMODULE_H diff --git a/phong.frag b/phongtexture.frag similarity index 100% rename from phong.frag rename to phongtexture.frag diff --git a/resourcebase.cpp b/resourcebase.cpp deleted file mode 100644 index 063fb07..0000000 --- a/resourcebase.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include "resourcebase.h" - -ResourceBase::DataBase ResourceBase::textures; -ResourceBase::DataBase ResourceBase::meshes; -ResourceBase::DataBase ResourceBase::materials; -ResourceBase::DataBase ResourceBase::shaders; -ResourceBase::DataBase ResourceBase::entities; -ResourceBase::DataBase ResourceBase::lights; - -void ResourceBase::setTexture(const std::string &textureName, Texture* myTexture) -{ - textures.add(textureName, myTexture); -} - -void ResourceBase::setMesh(const std::string &meshName, Mesh* myMesh ) -{ - meshes.add(meshName, myMesh); -} - -void ResourceBase::setMaterial(const std::string &materialName, Material* myMaterial) -{ - materials.add(materialName, myMaterial); -} - -void ResourceBase::setShader(const std::string &shaderName, Shader* myShader) -{ - shaders.add(shaderName, myShader); -} - -void ResourceBase::setEntity(const std::string &entityName, PhongEntity* myEntity) -{ - entities.add(entityName, myEntity); -} - -void ResourceBase::setLights(const std::string &lightsName, Lights* myLights) -{ - lights.add(lightsName, myLights); -} - - -Texture* ResourceBase::getTexture(const std::string &textureName) -{ - return textures.get(textureName); -} - -Mesh* ResourceBase::getMesh(const std::string &meshName) -{ - return meshes.get(meshName); -} - -Material* ResourceBase::getMaterial(const std::string &materialName) -{ - return materials.get(materialName); -} - -Shader* ResourceBase::getShader(const std::string &shaderName) -{ - return shaders.get(shaderName); -} - -PhongEntity* ResourceBase::getEntity(const std::string &entityName) -{ - return entities.get(entityName); -} - -Lights* ResourceBase::getLights(const std::string &lightsName) -{ - return lights.get(lightsName); -} - - -std::string ResourceBase::getMaterialName(Material* myMat) -{ - return materials.getName(myMat); -} - -std::string ResourceBase::getTextureName(Texture* myTex) -{ - return textures.getName(myTex); -} diff --git a/resourcebase.h b/resourcebase.h deleted file mode 100644 index e26f687..0000000 --- a/resourcebase.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef RESOURCEBASE_H -#define RESOURCEBASE_H - -class Texture; -class Mesh; -class Material; -class Shader; -class PhongEntity; -class Lights; - -#include -#include -#include -#include - -class ResourceBase -{ -public: - enum {TEXTURES, MESHES, MATERIALS, SHADERS, ENTITIES}; - - static void setTexture(const std::string &textureName, Texture* myTexture); - static void setMesh(const std::string &meshName, Mesh* myMesh); - static void setMaterial(const std::string &materialName, Material* myMaterial); - static void setShader(const std::string &shaderName, Shader* myShader); - static void setEntity(const std::string &entityName, PhongEntity* myEntity); - static void setLights(const std::string &lightsName, Lights* myLights); - - static Texture* getTexture(const std::string &textureName); - static Mesh* getMesh(const std::string &meshName); - static Material* getMaterial(const std::string &materialName); - static Shader* getShader(const std::string &shaderName); - static PhongEntity* getEntity(const std::string &entityName); - static Lights* getLights(const std::string &lightsName); - - static std::string getMaterialName(Material* myMat); - static std::string getTextureName(Texture* myTex); - -protected: - template - class DataBase - { - public: - std::vector names; - std::unordered_map data; - - void add(const std::string &name, T* t) - { - data[name] = t; - names.push_back(name); - } - - T* get(const std::string &name) - { - if(data.count(name)) - return data[name]; - else - { - fprintf(stderr, "Requesting unloaded resource : %s\n", name.c_str()); - return NULL; - } - } - - std::string getName(T* ptr) - { - for(const std::string &n : names) - { - if(ptr == get(n)) - return n; - } - return ""; - } - }; - - static DataBase textures; - static DataBase meshes; - static DataBase materials; - static DataBase shaders; - static DataBase entities; - static DataBase lights; -}; - -#endif // RESOURCEBASE_H diff --git a/sparrowRenderer.pro b/sparrowRenderer.pro index 0ff67ec..6f6c19a 100644 --- a/sparrowRenderer.pro +++ b/sparrowRenderer.pro @@ -29,8 +29,7 @@ SOURCES += shader.cpp \ phongmaterial.cpp \ sphere.cpp \ lights.cpp \ - sparrowrenderer.cpp \ - resourcebase.cpp \ + sparrowrenderer.cpp \ phongmodule.cpp \ skyboxmodule.cpp \ framebuffer.cpp \ @@ -49,8 +48,7 @@ HEADERS += shader.h \ phongmaterial.h \ sphere.h \ lights.h \ - sparrowrenderer.h \ - resourcebase.h \ + sparrowrenderer.h \ phongmodule.h \ skyboxmodule.h \ module.h \ diff --git a/sparrowrenderer.cpp b/sparrowrenderer.cpp index 01d7c21..4024b3b 100644 --- a/sparrowrenderer.cpp +++ b/sparrowrenderer.cpp @@ -79,3 +79,4 @@ Camera* SparrowRenderer::getCamera() return camera; } +