Material update, before changing prefabs
This commit is contained in:
		| @@ -8,7 +8,6 @@ target_sources(${DAWN_TARGET_NAME} | ||||
|   PRIVATE | ||||
|     RenderManager.cpp | ||||
|     BackBufferRenderTarget.cpp | ||||
|     StandardRenderPipeline.cpp | ||||
|     Texture.cpp | ||||
|     TextureRenderTarget.cpp | ||||
| ) | ||||
|   | ||||
| @@ -6,21 +6,20 @@ | ||||
| #include "dawnopengl.hpp" | ||||
| #include "game/DawnGame.hpp" | ||||
| #include "display/RenderManager.hpp" | ||||
| #include "display/StandardRenderPipeline.hpp" | ||||
|  | ||||
| using namespace Dawn; | ||||
|  | ||||
| RenderManager::RenderManager(DawnGame *game) : | ||||
|   IRenderManager(game), | ||||
|   backBuffer(*this) | ||||
|   backBuffer(*this), | ||||
|   renderPipeline(this) | ||||
| { | ||||
|   this->standardRenderPipeline = new StandardRenderPipeline(this); | ||||
|   this->simpleShader = new SimpleTexturedShader(); | ||||
|   this->uiShader = new UIShader(); | ||||
| } | ||||
|  | ||||
| void RenderManager::init() { | ||||
|   this->standardRenderPipeline->init(); | ||||
|   this->renderPipeline.init(); | ||||
|   this->simpleShader->compile(); | ||||
|   this->uiShader->compile(); | ||||
|  | ||||
| @@ -36,7 +35,7 @@ RenderTarget * RenderManager::getBackBuffer() { | ||||
| } | ||||
|  | ||||
| RenderPipeline * RenderManager::getRenderPipeline() { | ||||
|   return this->standardRenderPipeline; | ||||
|   return &this->renderPipeline; | ||||
| } | ||||
|  | ||||
| Shader * RenderManager::getDefaultShader() { | ||||
| @@ -68,7 +67,6 @@ void RenderManager::update() { | ||||
| } | ||||
|  | ||||
| RenderManager::~RenderManager() { | ||||
|   delete this->standardRenderPipeline; | ||||
|   delete this->simpleShader; | ||||
|   delete this->uiShader; | ||||
| } | ||||
| @@ -7,13 +7,12 @@ | ||||
| #include "display/_RenderManager.hpp" | ||||
| #include "display/BackBufferRenderTarget.hpp" | ||||
| #include "display/shader/SimpleTexturedShader.hpp" | ||||
| #include "display/RenderPipeline.hpp" | ||||
|  | ||||
| namespace Dawn { | ||||
|   class StandardRenderPipeline; | ||||
|  | ||||
|   class RenderManager : public IRenderManager { | ||||
|     private: | ||||
|       StandardRenderPipeline *standardRenderPipeline; | ||||
|       RenderPipeline renderPipeline; | ||||
|  | ||||
|     public: | ||||
|       BackBufferRenderTarget backBuffer; | ||||
|   | ||||
| @@ -1,14 +0,0 @@ | ||||
| // Copyright (c) 2022 Dominic Masters | ||||
| //  | ||||
| // This software is released under the MIT License. | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #include "StandardRenderPipeline.hpp" | ||||
| #include "scene/components/Components.hpp" | ||||
|  | ||||
| using namespace Dawn; | ||||
|  | ||||
| StandardRenderPipeline::StandardRenderPipeline(RenderManager *renderManager) : | ||||
|   RenderPipeline(renderManager) | ||||
| { | ||||
| } | ||||
| @@ -1,15 +0,0 @@ | ||||
| // Copyright (c) 2022 Dominic Masters | ||||
| //  | ||||
| // This software is released under the MIT License. | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #pragma once | ||||
| #include "dawnopengl.hpp" | ||||
| #include "display/RenderPipeline.hpp" | ||||
|  | ||||
| namespace Dawn { | ||||
|   class StandardRenderPipeline : public RenderPipeline { | ||||
|     public: | ||||
|       StandardRenderPipeline(RenderManager *renderManager); | ||||
|   }; | ||||
| } | ||||
| @@ -6,5 +6,5 @@ | ||||
| # Sources | ||||
| target_sources(${DAWN_TARGET_NAME} | ||||
|   PRIVATE | ||||
|     Shader.cpp | ||||
|     ShaderProgram.cpp | ||||
| ) | ||||
| @@ -3,11 +3,11 @@ | ||||
| // This software is released under the MIT License.
 | ||||
| // https://opensource.org/licenses/MIT
 | ||||
| 
 | ||||
| #include "Shader.hpp" | ||||
| #include "ShaderProgram.hpp" | ||||
| 
 | ||||
| using namespace Dawn; | ||||
| 
 | ||||
| void Shader::compileShader( | ||||
| void ShaderProgram::compileShader( | ||||
|   std::string vertexShader, | ||||
|   std::string fragmentShader | ||||
| ) { | ||||
| @@ -61,51 +61,40 @@ void Shader::compileShader( | ||||
|   // Now parse out the variables.
 | ||||
| } | ||||
| 
 | ||||
| void Shader::setTextureSlot(shaderparameter_t param, textureslot_t slot) { | ||||
| void ShaderProgram::setTexture(shaderparameter_t param, textureslot_t slot) { | ||||
|   glUniform1i(param, slot); | ||||
| } | ||||
| 
 | ||||
| shaderparameter_t Shader::getParameterByName(std::string name) { | ||||
| shaderparameter_t ShaderProgram::getParameterByName(std::string name) { | ||||
|   return glGetUniformLocation(this->shaderProgram, name.c_str()); | ||||
| } | ||||
| 
 | ||||
| void Shader::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) { | ||||
| void ShaderProgram::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) { | ||||
|   glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix)); | ||||
| } | ||||
| 
 | ||||
| void Shader::setBoolean(shaderparameter_t uni, bool value) { | ||||
| void ShaderProgram::setBoolean(shaderparameter_t uni, bool value) { | ||||
|   glUniform1i(uni, value); | ||||
| } | ||||
| 
 | ||||
| void Shader::setColor(shaderparameter_t uniform, struct Color color) { | ||||
| void ShaderProgram::setColor(shaderparameter_t uniform, struct Color color) { | ||||
|   glUniform4f(uniform, color.r, color.g, color.b, color.a); | ||||
| } | ||||
| 
 | ||||
| void Shader::setVector3(shaderparameter_t uniform, glm::vec3 vector) { | ||||
| void ShaderProgram::setVector3(shaderparameter_t uniform, glm::vec3 vector) { | ||||
|   glUniform3f(uniform, vector.x, vector.y, vector.z); | ||||
| } | ||||
| 
 | ||||
| void Shader::setTexture( | ||||
|   shaderparameter_t param, | ||||
|   Texture *texture | ||||
| ) { | ||||
|   if(texture == nullptr || !texture->isReady()) { | ||||
|     this->bindTexture(param, nullptr); | ||||
|     return; | ||||
|   } | ||||
|   this->bindTexture(param, texture); | ||||
| } | ||||
| 
 | ||||
| void Shader::setFloat(shaderparameter_t param, float_t value) { | ||||
| void ShaderProgram::setFloat(shaderparameter_t param, float_t value) { | ||||
|   glUniform1f(param, value); | ||||
| } | ||||
| 
 | ||||
| void Shader::bind() { | ||||
| void ShaderProgram::bind() { | ||||
|   if(this->shaderProgram == -1) throw "Shader has not yet been compiled"; | ||||
|   glUseProgram(this->shaderProgram); | ||||
| } | ||||
| 
 | ||||
| Shader::~Shader() { | ||||
| ShaderProgram::~ShaderProgram() { | ||||
|   if(this->shaderProgram != -1) glDeleteProgram(this->shaderProgram); | ||||
|   if(this->shaderVertex != -1) glDeleteShader(this->shaderVertex); | ||||
|   if(this->shaderFrag != -1) glDeleteShader(this->shaderFrag); | ||||
| @@ -4,14 +4,14 @@ | ||||
| // https://opensource.org/licenses/MIT
 | ||||
| 
 | ||||
| #pragma once | ||||
| #include "display/shader/_Shader.hpp" | ||||
| #include "display/shader/_ShaderProgram.hpp" | ||||
| #include "dawnopengl.hpp" | ||||
| #include "display/Color.hpp" | ||||
| 
 | ||||
| typedef GLuint shaderparameter_t; | ||||
| 
 | ||||
| namespace Dawn { | ||||
|   class Shader : public IShader<shaderparameter_t> { | ||||
|   class ShaderProgram : public IShaderProgram<shaderparameter_t> { | ||||
|     private: | ||||
|       /** Pointer to an uploaded vertex shader program */ | ||||
|       GLuint shaderVertex = -1; | ||||
| @@ -32,28 +32,6 @@ namespace Dawn { | ||||
|        */ | ||||
|       void compileShader(std::string vertexShader, std::string fragmentShader); | ||||
| 
 | ||||
|       /**
 | ||||
|        * Bind a specific texture slot (of an already bound texture) to a shader | ||||
|        * parameter. | ||||
|        *  | ||||
|        * @param param Parameter to bind the texture slot for. | ||||
|        * @param slot Slot to bind. | ||||
|        */ | ||||
|       void setTextureSlot(shaderparameter_t param, textureslot_t slot); | ||||
|      | ||||
|       /**
 | ||||
|        * Method designed to be overwritten by child shaders on how to handle a | ||||
|        * bind texture request. This is left to the discretion of the child  | ||||
|        * shader for which slot(s) to use, how to handle nullptr textures, etc. | ||||
|        *  | ||||
|        * @param param Parameter to bind the requested texture to. | ||||
|        * @param texture Texture trying to be bound, may be nullptr. | ||||
|        */ | ||||
|       virtual void bindTexture( | ||||
|         shaderparameter_t param, | ||||
|         Texture *texture | ||||
|       ) = 0; | ||||
| 
 | ||||
| 
 | ||||
|     public: | ||||
|       /**
 | ||||
| @@ -75,12 +53,12 @@ namespace Dawn { | ||||
|       void setBoolean(shaderparameter_t parameter, bool_t value) override; | ||||
|       void setColor(shaderparameter_t parameter, struct Color color) override; | ||||
|       void setVector3(shaderparameter_t parameter, glm::vec3 vector) override; | ||||
|       void setTexture(shaderparameter_t parameter, Texture *texture) override; | ||||
|       void setTexture(shaderparameter_t parameter, textureslot_t texture) override; | ||||
|       void setFloat(shaderparameter_t parameter, float_t value) override; | ||||
| 
 | ||||
|       /**
 | ||||
|        * Destroys and deletes the shader from the GPU. | ||||
|        */ | ||||
|       virtual ~Shader(); | ||||
|       virtual ~ShaderProgram(); | ||||
|   }; | ||||
| } | ||||
| @@ -1,102 +1,33 @@ | ||||
| // Copyright (c) 2022 Dominic Masters | ||||
| // Copyright (c) 2023 Dominic Masters | ||||
| //  | ||||
| // This software is released under the MIT License. | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #pragma once | ||||
| #include "display/shader/Shader.hpp" | ||||
| #include "scene/components/Components.hpp" | ||||
| #include "SimpleTexturedShaderProgram.hpp" | ||||
|  | ||||
| namespace Dawn { | ||||
|   class SimpleTexturedShader : public Shader { | ||||
|     protected: | ||||
|       SimpleTexturedShaderProgram program; | ||||
|  | ||||
|     public: | ||||
|       shaderparameter_t paramProjection; | ||||
|       shaderparameter_t paramView; | ||||
|       shaderparameter_t paramModel; | ||||
|       shaderparameter_t paramColor; | ||||
|       shaderparameter_t paramTexture; | ||||
|       shaderparameter_t paramHasTexture; | ||||
|  | ||||
|       std::map<shaderparameter_t, enum ShaderParameterType> | ||||
|       getParameters() override { | ||||
|         std::map<shaderparameter_t, enum ShaderParameterType> ps; | ||||
|  | ||||
|         ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR; | ||||
|         ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN; | ||||
|         ps[this->paramTexture] = SHADER_PARAMETER_TYPE_TEXTURE; | ||||
|  | ||||
|         return ps; | ||||
|       } | ||||
|        | ||||
|       void setDefaultParameters(Material *material) override { | ||||
|         material->colorValues[this->paramColor] = COLOR_WHITE; | ||||
|         material->textureValues[this->paramTexture] = nullptr; | ||||
|       } | ||||
|  | ||||
|       void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override { | ||||
|         this->setMatrix(this->paramProjection, proj); | ||||
|         this->setMatrix(this->paramView, view); | ||||
|       } | ||||
|  | ||||
|       void setMeshParameters(glm::mat4 transform) override { | ||||
|         this->setMatrix(this->paramModel, transform); | ||||
|       } | ||||
|  | ||||
|       void bindTexture( | ||||
|         shaderparameter_t param, | ||||
|         Texture *texture | ||||
|       ) override { | ||||
|         if(texture == nullptr || !texture->isReady()) { | ||||
|           this->setBoolean(this->paramHasTexture, false); | ||||
|         } else { | ||||
|           this->setBoolean(this->paramHasTexture, true); | ||||
|           this->setTextureSlot(param, 0x01); | ||||
|           texture->bind(0x01); | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       void compile() override { | ||||
|         this->compileShader( | ||||
|           // Vertex Shader | ||||
|           "#version 330 core\n" | ||||
|           "layout (location = 0) in vec3 aPos;\n" | ||||
|           "layout (location = 1) in vec2 aTexCoord;\n" | ||||
|         this->program.compile(); | ||||
|       } | ||||
|  | ||||
|           "uniform mat4 u_Proj;\n" | ||||
|           "uniform mat4 u_View;\n" | ||||
|           "uniform mat4 u_Model;\n" | ||||
|       std::vector<struct ShaderPass> getItemPasses( | ||||
|         MeshRenderer *mesh, | ||||
|         Material *material | ||||
|       ) override { | ||||
|         return std::vector<struct ShaderPass>(); | ||||
|       } | ||||
|  | ||||
|           "out vec2 o_TextCoord;\n" | ||||
|           "void main() {\n" | ||||
|             "gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" | ||||
|             "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" | ||||
|           "}", | ||||
|  | ||||
|           // Fragment Shader | ||||
|           "#version 330 core\n" | ||||
|           "in vec2 o_TextCoord;\n" | ||||
|           "out vec4 o_Color;\n" | ||||
|           "uniform vec4 u_Color;\n" | ||||
|           "uniform bool u_HasTexture;\n" | ||||
|           "uniform sampler2D u_Text;\n" | ||||
|  | ||||
|           "void main() {\n" | ||||
|             "if(u_HasTexture) {\n" | ||||
|               "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" | ||||
|             "} else {\n" | ||||
|               "o_Color = u_Color;" | ||||
|             "}\n" | ||||
|           "}\n" | ||||
|         ); | ||||
|  | ||||
|         this->paramProjection = this->getParameterByName("u_Proj"); | ||||
|         this->paramView = this->getParameterByName("u_View"); | ||||
|         this->paramModel = this->getParameterByName("u_Model"); | ||||
|         this->paramColor = this->getParameterByName("u_Color"); | ||||
|         this->paramTexture = this->getParameterByName("u_Text"); | ||||
|         this->paramHasTexture = this->getParameterByName("u_HasTexture"); | ||||
|  | ||||
|         this->setBoolean(this->paramHasTexture, false); | ||||
|       void setGlobalParameters( | ||||
|         glm::mat4 cameraProjection, | ||||
|         glm::mat4 cameraView | ||||
|       ) override { | ||||
|       } | ||||
|   }; | ||||
| } | ||||
| @@ -0,0 +1,62 @@ | ||||
| // Copyright (c) 2022 Dominic Masters | ||||
| //  | ||||
| // This software is released under the MIT License. | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #pragma once | ||||
| #include "display/shader/Shader.hpp" | ||||
| #include "scene/components/Components.hpp" | ||||
|  | ||||
| namespace Dawn { | ||||
|   class SimpleTexturedShaderProgram : public ShaderProgram { | ||||
|     public: | ||||
|       shaderparameter_t paramProjection; | ||||
|       shaderparameter_t paramView; | ||||
|       shaderparameter_t paramModel; | ||||
|       shaderparameter_t paramColor; | ||||
|       shaderparameter_t paramTexture; | ||||
|       shaderparameter_t paramHasTexture; | ||||
|  | ||||
|       void compile() override { | ||||
|         this->compileShader( | ||||
|           // Vertex Shader | ||||
|           "#version 330 core\n" | ||||
|           "layout (location = 0) in vec3 aPos;\n" | ||||
|           "layout (location = 1) in vec2 aTexCoord;\n" | ||||
|  | ||||
|           "uniform mat4 u_Proj;\n" | ||||
|           "uniform mat4 u_View;\n" | ||||
|           "uniform mat4 u_Model;\n" | ||||
|  | ||||
|           "out vec2 o_TextCoord;\n" | ||||
|           "void main() {\n" | ||||
|             "gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" | ||||
|             "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" | ||||
|           "}", | ||||
|  | ||||
|           // Fragment Shader | ||||
|           "#version 330 core\n" | ||||
|           "in vec2 o_TextCoord;\n" | ||||
|           "out vec4 o_Color;\n" | ||||
|           "uniform vec4 u_Color;\n" | ||||
|           "uniform bool u_HasTexture;\n" | ||||
|           "uniform sampler2D u_Text;\n" | ||||
|  | ||||
|           "void main() {\n" | ||||
|             "if(u_HasTexture) {\n" | ||||
|               "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" | ||||
|             "} else {\n" | ||||
|               "o_Color = u_Color;" | ||||
|             "}\n" | ||||
|           "}\n" | ||||
|         ); | ||||
|  | ||||
|         this->paramProjection = this->getParameterByName("u_Proj"); | ||||
|         this->paramView = this->getParameterByName("u_View"); | ||||
|         this->paramModel = this->getParameterByName("u_Model"); | ||||
|         this->paramColor = this->getParameterByName("u_Color"); | ||||
|         this->paramTexture = this->getParameterByName("u_Text"); | ||||
|         this->paramHasTexture = this->getParameterByName("u_HasTexture"); | ||||
|       } | ||||
|   }; | ||||
| } | ||||
| @@ -4,116 +4,9 @@ | ||||
| // https://opensource.org/licenses/MIT | ||||
|  | ||||
| #pragma once | ||||
| #include "display/shader/Shader.hpp" | ||||
| #include "scene/components/Components.hpp" | ||||
| #include "SimpleTexturedShader.hpp" | ||||
|  | ||||
| namespace Dawn { | ||||
|   class UIShader : public Shader { | ||||
|     public: | ||||
|       shaderparameter_t paramProjection; | ||||
|       shaderparameter_t paramView; | ||||
|       shaderparameter_t paramModel; | ||||
|       shaderparameter_t paramColor; | ||||
|       shaderparameter_t paramTexture; | ||||
|       shaderparameter_t paramHasTexture; | ||||
|  | ||||
|       std::map<shaderparameter_t, enum ShaderParameterType> | ||||
|       getParameters() override { | ||||
|         std::map<shaderparameter_t, enum ShaderParameterType> ps; | ||||
|  | ||||
|         ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR; | ||||
|         ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN; | ||||
|         ps[this->paramTexture] = SHADER_PARAMETER_TYPE_TEXTURE; | ||||
|  | ||||
|         return ps; | ||||
|       } | ||||
|        | ||||
|       void setDefaultParameters(Material *material) override { | ||||
|         material->colorValues[this->paramColor] = COLOR_WHITE; | ||||
|         material->textureValues[this->paramTexture] = nullptr; | ||||
|       } | ||||
|  | ||||
|       void setGlobalParameters(glm::mat4 proj, glm::mat4 view) override { | ||||
|         this->setMatrix(this->paramProjection, proj); | ||||
|         this->setMatrix(this->paramView, view); | ||||
|       } | ||||
|  | ||||
|       void setMeshParameters(glm::mat4 transform) override { | ||||
|         this->setMatrix(this->paramModel, transform); | ||||
|       } | ||||
|  | ||||
|       void bindTexture( | ||||
|         shaderparameter_t param, | ||||
|         Texture *texture | ||||
|       ) override { | ||||
|         if(texture == nullptr || !texture->isReady()) { | ||||
|           this->setBoolean(this->paramHasTexture, false); | ||||
|         } else { | ||||
|           this->setBoolean(this->paramHasTexture, true); | ||||
|           this->setTextureSlot(param, 0x00); | ||||
|           texture->bind(0x00); | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       void compile() override { | ||||
|         this->compileShader( | ||||
|           // Vertex Shader | ||||
|           "#version 330 core\n" | ||||
|           "layout (location = 0) in vec3 aPos;\n" | ||||
|           "layout (location = 1) in vec2 aTexCoord;\n" | ||||
|  | ||||
|           "uniform mat4 u_Proj;\n" | ||||
|           "uniform mat4 u_View;\n" | ||||
|           "uniform mat4 u_Model;\n" | ||||
|  | ||||
|           "out vec2 o_TextCoord;\n" | ||||
|           "void main() {\n" | ||||
|             "gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n" | ||||
|             "o_TextCoord = vec2(aTexCoord.x, aTexCoord.y);\n" | ||||
|           "}", | ||||
|  | ||||
|           // Fragment Shader | ||||
|           "#version 330 core\n" | ||||
|           "in vec2 o_TextCoord;\n" | ||||
|           "out vec4 o_Color;\n" | ||||
|           "uniform vec4 u_Color;\n" | ||||
|           "uniform bool u_HasTexture;\n" | ||||
|           "uniform sampler2D u_Text;\n" | ||||
|  | ||||
|           "void main() {\n" | ||||
|             "if(u_HasTexture) {\n" | ||||
|               "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" | ||||
|             "} else {\n" | ||||
|               "o_Color = u_Color;" | ||||
|             "}\n" | ||||
|           "}\n" | ||||
|         ); | ||||
|  | ||||
|         this->paramProjection = this->getParameterByName("u_Proj"); | ||||
|         this->paramView = this->getParameterByName("u_View"); | ||||
|         this->paramModel = this->getParameterByName("u_Model"); | ||||
|         this->paramColor = this->getParameterByName("u_Color"); | ||||
|         this->paramTexture = this->getParameterByName("u_Text"); | ||||
|         this->paramHasTexture = this->getParameterByName("u_HasTexture"); | ||||
|  | ||||
|         this->setBoolean(this->paramHasTexture, false); | ||||
|       } | ||||
|  | ||||
|       void setUICamera(glm::mat4 projection, glm::mat4 view) { | ||||
|         this->setMatrix(this->paramProjection, projection); | ||||
|         this->setMatrix(this->paramView, view); | ||||
|       } | ||||
|  | ||||
|       void setUIModel(glm::mat4 model) { | ||||
|         this->setMatrix(this->paramModel, model); | ||||
|       } | ||||
|  | ||||
|       void setUITexture(Texture *texture) { | ||||
|         this->bindTexture(this->paramTexture, texture); | ||||
|       } | ||||
|  | ||||
|       void setUIColor(struct Color color) { | ||||
|         this->setColor(this->paramColor, color); | ||||
|       } | ||||
|   class UIShader : public SimpleTexturedShader { | ||||
|   }; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user