From 96dd33c6b844519c45fb484f0d117731f34db0e9 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 29 May 2023 15:52:08 -0700 Subject: [PATCH] Added IRenderable, removed shader programs and allow more nuanced control of render passes, UI items are now rendered same as other scene item components. --- src/dawn/display/RenderPipeline.cpp | 82 ++---- src/dawn/display/RenderPipeline.hpp | 5 +- src/dawn/display/shader/Shader.hpp | 65 ----- src/dawn/display/shader/ShaderPass.hpp | 33 +++ .../{_ShaderProgram.hpp => _Shader.hpp} | 15 +- .../scene/components/display/IRenderable.hpp | 20 ++ .../scene/components/display/Material.hpp | 10 +- src/dawn/scene/components/ui/UIBorder.cpp | 13 +- src/dawn/scene/components/ui/UIBorder.hpp | 4 +- src/dawn/scene/components/ui/UIComponent.cpp | 11 + src/dawn/scene/components/ui/UIComponent.hpp | 15 +- src/dawn/scene/components/ui/UIImage.cpp | 13 +- src/dawn/scene/components/ui/UIImage.hpp | 4 +- src/dawn/scene/components/ui/UILabel.cpp | 13 +- src/dawn/scene/components/ui/UILabel.hpp | 4 +- src/dawnopengl/display/RenderManager.hpp | 2 +- src/dawnopengl/display/shader/CMakeLists.txt | 2 +- src/dawnopengl/display/shader/FontShader.cpp | 17 +- src/dawnopengl/display/shader/FontShader.hpp | 17 +- .../shader/{ShaderProgram.cpp => Shader.cpp} | 256 +++++++++--------- .../shader/{ShaderProgram.hpp => Shader.hpp} | 171 ++++++------ .../shader/SimpleBillboardedShader.cpp | 43 +-- .../shader/SimpleBillboardedShader.hpp | 17 +- .../display/shader/SimpleTexturedShader.cpp | 83 +----- .../display/shader/SimpleTexturedShader.hpp | 30 +- .../{UIShaderProgram.hpp => UIShader.hpp} | 26 +- .../material/SimpleBillboardedMaterial.cpp | 6 +- .../material/SimpleBillboardedMaterial.hpp | 3 +- .../material/SimpleTexturedMaterial.cpp | 35 ++- .../material/SimpleTexturedMaterial.hpp | 4 +- 30 files changed, 419 insertions(+), 600 deletions(-) delete mode 100644 src/dawn/display/shader/Shader.hpp create mode 100644 src/dawn/display/shader/ShaderPass.hpp rename src/dawn/display/shader/{_ShaderProgram.hpp => _Shader.hpp} (87%) create mode 100644 src/dawn/scene/components/display/IRenderable.hpp rename src/dawnopengl/display/shader/{ShaderProgram.cpp => Shader.cpp} (76%) rename src/dawnopengl/display/shader/{ShaderProgram.hpp => Shader.hpp} (87%) rename src/dawnopengl/display/shader/{UIShaderProgram.hpp => UIShader.hpp} (76%) diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index 2f6bd2e4..b5c6f175 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -166,62 +166,18 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { auto renderTarget = camera->getRenderTarget(); assertNotNull(renderTarget); + // Update shader parameter buffers with current knowledge + this->camera = camera; + // Get the list of things to render first. std::vector shaderPassItems; - // Meshes - auto meshes = scene->findComponents(); - auto itMesh = meshes.begin(); - while(itMesh != meshes.end()) { - // Get Mesh - auto mesh = *itMesh; - assertNotNull(mesh); - assertNotNull(mesh->mesh); - - // Make sure this mesh has a material - auto mat = mesh->item->getComponent(); - assertNotNull(mat); - - auto shader = mat->getShader(); - assertNotNull(shader); - - // Now get and validate the pass items for this material/shader - auto materialPassItems = shader->getPassItems(mesh->mesh, mat, camera); - itPassItem = materialPassItems.begin(); - while(itPassItem != materialPassItems.end()) { - auto item = *itPassItem; - - // Validate the pass - assertNotNull(item.mesh); - assertTrue(item.start >= 0); - assertTrue(item.count > 0 || item.count == -1); - assertNotNull(item.shaderProgram); - - // Queue - shaderPassItems.push_back(item); - ++itPassItem; - } - ++itMesh; - } - - // UI Elements - if(renderTarget == this->renderManager->getBackBuffer()) { - auto canvases = scene->findComponents(); - auto itCanvas = canvases.begin(); - while(itCanvas != canvases.end()) { - auto canvas = *itCanvas; - glm::mat4 projection; - glm::mat4 view; - canvas->getProjectionAndView(&projection, &view); - - auto renderables = canvas->item->findChildrenDeep(); - auto itChild = renderables.begin(); - while(itChild != renderables.end()) { - vectorAppend(&shaderPassItems,(*itChild)->getPassItems(projection, view)); - ++itChild; - } - ++itCanvas; - } + // Renderables + auto renderables = scene->findComponents(); + auto itRenderables = renderables.begin(); + while(itRenderables != renderables.end()) { + vectorAppend(&shaderPassItems, (*itRenderables)->getRenderPasses()); + ++itRenderables; } // Debug Lines @@ -261,7 +217,7 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { ); // Now we've sorted everything! Let's actually start rendering. - ShaderProgram *boundProgram = nullptr; + Shader *boundShader = nullptr; std::map boundTextures; // TODO: This will be editable! @@ -277,9 +233,9 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { auto item = *itPassItem; // Bind the program. - if(boundProgram != item.shaderProgram) { - boundProgram = item.shaderProgram; - boundProgram->bind(); + if(boundShader != item.shader) { + boundShader = item.shader; + boundShader->bind(); } // Bind the textures to the slots @@ -298,37 +254,37 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { // Now set each of the parameters. Nothing exciting here. auto itColors = item.colorValues.begin(); while(itColors != item.colorValues.end()) { - item.shaderProgram->setColor(itColors->first, itColors->second); + item.shader->setColor(itColors->first, itColors->second); ++itColors; } auto itBool = item.boolValues.begin(); while(itBool != item.boolValues.end()) { - item.shaderProgram->setBoolean(itBool->first, itBool->second); + item.shader->setBoolean(itBool->first, itBool->second); ++itBool; } auto itMat = item.matrixValues.begin(); while(itMat != item.matrixValues.end()) { - item.shaderProgram->setMatrix(itMat->first, itMat->second); + item.shader->setMatrix(itMat->first, itMat->second); ++itMat; } auto itVec3 = item.vec3Values.begin(); while(itVec3 != item.vec3Values.end()) { - item.shaderProgram->setVector3(itVec3->first, itVec3->second); + item.shader->setVector3(itVec3->first, itVec3->second); ++itVec3; } auto itText = item.textureValues.begin(); while(itText != item.textureValues.end()) { - item.shaderProgram->setTexture(itText->first, itText->second); + item.shader->setTexture(itText->first, itText->second); ++itText; } auto itFloat = item.floatValues.begin(); while(itFloat != item.floatValues.end()) { - item.shaderProgram->setFloat(itFloat->first, itFloat->second); + item.shader->setFloat(itFloat->first, itFloat->second); ++itFloat; } diff --git a/src/dawn/display/RenderPipeline.hpp b/src/dawn/display/RenderPipeline.hpp index da1bcd64..7334607a 100644 --- a/src/dawn/display/RenderPipeline.hpp +++ b/src/dawn/display/RenderPipeline.hpp @@ -6,10 +6,10 @@ #pragma once #include "dawnlibs.hpp" #include "scene/components/display/Material.hpp" -#include "scene/components/display/mesh/MeshRenderer.hpp" #include "scene/components/display/Camera.hpp" #include "scene/components/scene/SubSceneController.hpp" #include "scene/components/ui/UIComponent.hpp" +#include "display/shader/ShaderPass.hpp" namespace Dawn { class RenderManager; @@ -21,6 +21,9 @@ namespace Dawn { public: RenderManager *renderManager; + // Temporary hack + Camera *camera = nullptr; + /** * Constructs a new RenderPipeline. Render Pipelines are my attempt to * create both a flexible, but standard way to allow the individual games diff --git a/src/dawn/display/shader/Shader.hpp b/src/dawn/display/shader/Shader.hpp deleted file mode 100644 index 9eac673a..00000000 --- a/src/dawn/display/shader/Shader.hpp +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "display/shader/ShaderProgram.hpp" -#include "display/mesh/Mesh.hpp" -#include "display/_RenderManager.hpp" - -namespace Dawn { - class Material; - class MeshRenderer; - class Camera; - - struct ShaderPassItem { - ShaderProgram *shaderProgram = nullptr; - int32_t priority = 0; - - Mesh *mesh; - int32_t start = 0; - int32_t count = -1; - float_t w = 0; - renderflag_t renderFlags = RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST; - enum MeshDrawMode drawMode = MESH_DRAW_MODE_TRIANGLES; - - // Parameters - std::map colorValues; - std::map boolValues; - std::map matrixValues; - std::map vec3Values; - std::map textureValues; - std::map floatValues; - - // Textures - std::map textureSlots; - }; - - class Shader { - public: - int32_t shaderId = -1; - int_fast16_t renderId = 0; - - /** - * Compile all programs for this shader. This amy not remain forever as I - * may decide to allow shaders to share programs somehow. For now though - * this is clean enough. - */ - virtual void compile() = 0; - - /** - * Returns the list of pass items to render for the given scene item. - * - * @param mesh Mesh Renderer for the scene item. - * @param material Material for the scene item. - * @param camera Camera for the scene. - * @return List of passes to render. - */ - virtual std::vector getPassItems( - Mesh *mesh, - Material *material, - Camera *camera - ) = 0; - }; -} \ No newline at end of file diff --git a/src/dawn/display/shader/ShaderPass.hpp b/src/dawn/display/shader/ShaderPass.hpp new file mode 100644 index 00000000..bda6b946 --- /dev/null +++ b/src/dawn/display/shader/ShaderPass.hpp @@ -0,0 +1,33 @@ +// 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 "display/mesh/Mesh.hpp" + +namespace Dawn { + struct ShaderPassItem { + Shader *shader = nullptr; + int32_t priority = 0; + + Mesh *mesh; + int32_t start = 0; + int32_t count = -1; + float_t w = 0; + renderflag_t renderFlags = RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST; + enum MeshDrawMode drawMode = MESH_DRAW_MODE_TRIANGLES; + + // Parameters + std::map colorValues; + std::map boolValues; + std::map matrixValues; + std::map vec3Values; + std::map textureValues; + std::map floatValues; + + // Textures + std::map textureSlots; + }; +} \ No newline at end of file diff --git a/src/dawn/display/shader/_ShaderProgram.hpp b/src/dawn/display/shader/_Shader.hpp similarity index 87% rename from src/dawn/display/shader/_ShaderProgram.hpp rename to src/dawn/display/shader/_Shader.hpp index 29a4d038..736f1397 100644 --- a/src/dawn/display/shader/_ShaderProgram.hpp +++ b/src/dawn/display/shader/_Shader.hpp @@ -1,16 +1,26 @@ -// 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/mesh/Mesh.hpp" +#include "display/_RenderManager.hpp" #include "display/Texture.hpp" #include "display/shader/ShaderParameterBuffer.hpp" namespace Dawn { template - class IShaderProgram { + class IShader { public: + int32_t shaderId = -1; + int_fast16_t renderId = 0; + + /** + * Compile all programs for this shader. + */ + virtual void compile() = 0; + /** * Attaches the supplied shader as the current shader. */ @@ -72,5 +82,6 @@ namespace Dawn { * @param Float to bind. */ virtual void setFloat(T parameter, float_t value) = 0; + }; } \ No newline at end of file diff --git a/src/dawn/scene/components/display/IRenderable.hpp b/src/dawn/scene/components/display/IRenderable.hpp new file mode 100644 index 00000000..bdb1d858 --- /dev/null +++ b/src/dawn/scene/components/display/IRenderable.hpp @@ -0,0 +1,20 @@ +// 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" + +namespace Dawn { + class IRenderable { + public: + /** + * Returns the render passes for this renderable item, typically a scene + * item component, e.g. a Material or a UI Item. + * + * @return Array of renderable passes. + */ + virtual std::vector getRenderPasses() = 0; + }; +} \ No newline at end of file diff --git a/src/dawn/scene/components/display/Material.hpp b/src/dawn/scene/components/display/Material.hpp index 988aadf6..fe010725 100644 --- a/src/dawn/scene/components/display/Material.hpp +++ b/src/dawn/scene/components/display/Material.hpp @@ -6,9 +6,10 @@ #pragma once #include "scene/SceneItemComponent.hpp" #include "display/shader/ShaderManager.hpp" +#include "scene/components/display/IRenderable.hpp" namespace Dawn { - class Material : public SceneItemComponent { + class Material : public SceneItemComponent, public IRenderable { public: /** * Material component constructor. @@ -16,12 +17,5 @@ namespace Dawn { * @param item Scene Item this component belongs to. */ Material(SceneItem *item); - - /** - * Returns the shader that this material uses. - * - * @return Shader that belongs to this material. - */ - virtual Shader * getShader() = 0; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIBorder.cpp b/src/dawn/scene/components/ui/UIBorder.cpp index f9f79691..8bbebcbd 100644 --- a/src/dawn/scene/components/ui/UIBorder.cpp +++ b/src/dawn/scene/components/ui/UIBorder.cpp @@ -25,12 +25,15 @@ float_t UIBorder::getContentHeight() { return this->height; } -std::vector UIBorder::getPassItems( - glm::mat4 proj, glm::mat4 view -) { +std::vector UIBorder::getRenderPasses() { + glm::mat4 view, proj; + auto canvas = this->getCanvas(); + assertNotNull(canvas); + canvas->getProjectionAndView(&proj, &view); + struct ShaderPassItem item; - auto shader = &getGame()->renderManager.uiShader->program; - item.shaderProgram = shader; + auto shader = getGame()->renderManager.uiShader; + item.shader = shader; item.colorValues[shader->paramColor] = COLOR_WHITE; item.matrixValues[shader->paramProjection] = proj; item.matrixValues[shader->paramView] = view; diff --git a/src/dawn/scene/components/ui/UIBorder.hpp b/src/dawn/scene/components/ui/UIBorder.hpp index 91d85e44..1169e6bb 100644 --- a/src/dawn/scene/components/ui/UIBorder.hpp +++ b/src/dawn/scene/components/ui/UIBorder.hpp @@ -24,9 +24,7 @@ namespace Dawn { float_t getContentWidth() override; float_t getContentHeight() override; - std::vector getPassItems( - glm::mat4 proj, glm::mat4 view - ) override; + std::vector getRenderPasses() override; void onStart() override; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UIComponent.cpp b/src/dawn/scene/components/ui/UIComponent.cpp index e79daf61..25d12428 100644 --- a/src/dawn/scene/components/ui/UIComponent.cpp +++ b/src/dawn/scene/components/ui/UIComponent.cpp @@ -161,6 +161,17 @@ void UIComponent::calculateDimensions( } } +UICanvas * UIComponent::getCanvas() { + // TODO: Cache this on first hit. + auto parent = this->transform->getParent(); + while(parent != nullptr) { + auto canvas = parent->item->getComponent(); + if(canvas != nullptr) return canvas; + parent = parent->getParent(); + } + return nullptr; +} + float_t UIComponent::getWidth() { return this->width; } diff --git a/src/dawn/scene/components/ui/UIComponent.hpp b/src/dawn/scene/components/ui/UIComponent.hpp index 17058dfd..a91934c5 100644 --- a/src/dawn/scene/components/ui/UIComponent.hpp +++ b/src/dawn/scene/components/ui/UIComponent.hpp @@ -5,6 +5,7 @@ #pragma once #include "scene/SceneItemComponent.hpp" +#include "scene/components/display/IRenderable.hpp" #include "UICanvas.hpp" #include "util/mathutils.hpp" @@ -21,7 +22,7 @@ namespace Dawn { UI_COMPONENT_ALIGN_UNIT_PERCENT }; - class UIComponentRenderable { + class UIComponentRenderable : public IRenderable { public: /** * Implemented UI Components that have rendering should implement this and @@ -32,10 +33,7 @@ namespace Dawn { * @param view Camera view, obtained from the canvas. * @return A list of renderable shader pass items for this renderable. */ - virtual std::vector getPassItems( - glm::mat4 projection, - glm::mat4 view - ) = 0; + virtual std::vector getRenderPasses() = 0; }; class UIComponent : public SceneItemComponent, public UIComponentDimensional { @@ -114,6 +112,13 @@ namespace Dawn { UIComponent(SceneItem *item); + /** + * Returns the canvas that this UI element is belonging to. + * + * @return Pointer to the UI Canvas this component is a child of. + */ + UICanvas * getCanvas(); + float_t getWidth() override; float_t getHeight() override; void onStart() override; diff --git a/src/dawn/scene/components/ui/UIImage.cpp b/src/dawn/scene/components/ui/UIImage.cpp index a4d70a9d..fba4909c 100644 --- a/src/dawn/scene/components/ui/UIImage.cpp +++ b/src/dawn/scene/components/ui/UIImage.cpp @@ -25,12 +25,15 @@ float_t UIImage::getContentHeight() { return this->height; } -std::vector UIImage::getPassItems( - glm::mat4 proj, glm::mat4 view -) { +std::vector UIImage::getRenderPasses() { + glm::mat4 view, proj; + auto canvas = this->getCanvas(); + assertNotNull(canvas); + canvas->getProjectionAndView(&proj, &view); + struct ShaderPassItem item; - auto shader = &getGame()->renderManager.uiShader->program; - item.shaderProgram = shader; + auto shader = getGame()->renderManager.uiShader; + item.shader = shader; item.colorValues[shader->paramColor] = this->color; item.matrixValues[shader->paramProjection] = proj; item.matrixValues[shader->paramView] = view; diff --git a/src/dawn/scene/components/ui/UIImage.hpp b/src/dawn/scene/components/ui/UIImage.hpp index ad391f4d..3cd86e7f 100644 --- a/src/dawn/scene/components/ui/UIImage.hpp +++ b/src/dawn/scene/components/ui/UIImage.hpp @@ -22,9 +22,7 @@ namespace Dawn { float_t getContentWidth() override; float_t getContentHeight() override; - std::vector getPassItems( - glm::mat4 proj, glm::mat4 view - ) override; + std::vector getRenderPasses() override; void onStart() override; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UILabel.cpp b/src/dawn/scene/components/ui/UILabel.cpp index 95782dca..af969619 100644 --- a/src/dawn/scene/components/ui/UILabel.cpp +++ b/src/dawn/scene/components/ui/UILabel.cpp @@ -67,15 +67,18 @@ void UILabel::updateMesh() { this->eventFontRebuffered.invoke(); } -std::vector UILabel::getPassItems( - glm::mat4 proj, glm::mat4 view -) { +std::vector UILabel::getRenderPasses() { if(!this->hasText()) return {}; this->updateMesh(); + + glm::mat4 view, proj; + auto canvas = this->getCanvas(); + assertNotNull(canvas); + canvas->getProjectionAndView(&proj, &view); struct ShaderPassItem item; - auto shader = &getGame()->renderManager.fontShader->program; - item.shaderProgram = shader; + auto shader = getGame()->renderManager.fontShader; + item.shader = shader; item.colorValues[shader->paramColor] = textColor; item.matrixValues[shader->paramProjection] = proj; item.matrixValues[shader->paramView] = view; diff --git a/src/dawn/scene/components/ui/UILabel.hpp b/src/dawn/scene/components/ui/UILabel.hpp index 7037fa03..9a2edeea 100644 --- a/src/dawn/scene/components/ui/UILabel.hpp +++ b/src/dawn/scene/components/ui/UILabel.hpp @@ -53,9 +53,7 @@ namespace Dawn { float_t getContentWidth() override; float_t getContentHeight() override; - std::vector getPassItems( - glm::mat4 projection, glm::mat4 view - ) override; + std::vector getRenderPasses() override; void onStart() override; }; } \ No newline at end of file diff --git a/src/dawnopengl/display/RenderManager.hpp b/src/dawnopengl/display/RenderManager.hpp index 4ee5c765..4c5d26db 100644 --- a/src/dawnopengl/display/RenderManager.hpp +++ b/src/dawnopengl/display/RenderManager.hpp @@ -9,7 +9,7 @@ #include "display/shader/ShaderManager.hpp" #include "display/shader/SimpleTexturedShader.hpp" #include "display/shader/FontShader.hpp" -#include "display/shader/UIShaderProgram.hpp" +#include "display/shader/UIShader.hpp" #include "display/RenderPipeline.hpp" #include "display/font/FontManager.hpp" #include "display/font/ExampleFont.hpp" diff --git a/src/dawnopengl/display/shader/CMakeLists.txt b/src/dawnopengl/display/shader/CMakeLists.txt index 35011df1..7bb59203 100644 --- a/src/dawnopengl/display/shader/CMakeLists.txt +++ b/src/dawnopengl/display/shader/CMakeLists.txt @@ -6,7 +6,7 @@ # Sources target_sources(${DAWN_TARGET_NAME} PRIVATE - ShaderProgram.cpp + Shader.cpp FontShader.cpp SimpleTexturedShader.cpp SimpleBillboardedShader.cpp diff --git a/src/dawnopengl/display/shader/FontShader.cpp b/src/dawnopengl/display/shader/FontShader.cpp index bd9ca2f9..71f4e168 100644 --- a/src/dawnopengl/display/shader/FontShader.cpp +++ b/src/dawnopengl/display/shader/FontShader.cpp @@ -4,12 +4,10 @@ // https://opensource.org/licenses/MIT #include "FontShader.hpp" -#include "scene/components/display/mesh/MeshRenderer.hpp" -#include "scene/components/display/Camera.hpp" using namespace Dawn; -void FontShaderProgram::compile() { +void FontShader::compile() { #if DAWN_OPENGL_GLSL this->compileShader( { @@ -53,17 +51,4 @@ void FontShaderProgram::compile() { this->paramModel = this->getParameterByName("u_Model"); this->paramColor = this->getParameterByName("u_Color"); this->paramTexture = this->getParameterByName("u_Text"); -} - -void FontShader::compile() { - this->program.compile(); -} - -std::vector FontShader::getPassItems( - Mesh *mesh, - Material *material, - Camera *camera -) { - std::vector passes; - return passes; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/FontShader.hpp b/src/dawnopengl/display/shader/FontShader.hpp index 2c287919..d988b86f 100644 --- a/src/dawnopengl/display/shader/FontShader.hpp +++ b/src/dawnopengl/display/shader/FontShader.hpp @@ -10,10 +10,10 @@ // https://opensource.org/licenses/MIT #pragma once -#include "display/shader/ShaderManager.hpp" +#include "display/shader/Shader.hpp" namespace Dawn { - class FontShaderProgram : public ShaderProgram { + class FontShader : public Shader { public: shaderparameter_t paramProjection; shaderparameter_t paramView; @@ -23,17 +23,4 @@ namespace Dawn { void compile() override; }; - - class FontShader : public Shader { - public: - FontShaderProgram program; - - void compile() override; - - std::vector getPassItems( - Mesh *mesh, - Material *material, - Camera *camera - ) override; - }; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/ShaderProgram.cpp b/src/dawnopengl/display/shader/Shader.cpp similarity index 76% rename from src/dawnopengl/display/shader/ShaderProgram.cpp rename to src/dawnopengl/display/shader/Shader.cpp index f4112f6c..0cb90c99 100644 --- a/src/dawnopengl/display/shader/ShaderProgram.cpp +++ b/src/dawnopengl/display/shader/Shader.cpp @@ -1,129 +1,129 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "ShaderProgram.hpp" - -using namespace Dawn; - -void ShaderProgram::compileShader( - std::map attributeLocations, - std::string vertexShader, - std::string fragmentShader -) { - GLint isSuccess; - int32_t maxLength; - char error[1024]; - - // Load the vertex shader first - this->shaderVertex = glCreateShader(GL_VERTEX_SHADER); - auto vertShaderC = vertexShader.c_str(); - glShaderSource(this->shaderVertex, 1, &vertShaderC, 0); - glCompileShader(this->shaderVertex); - - // Validate - glGetShaderiv(this->shaderVertex, GL_COMPILE_STATUS, &isSuccess); - if(!isSuccess) { - glGetShaderiv(this->shaderVertex, GL_INFO_LOG_LENGTH, &maxLength); - glGetShaderInfoLog(this->shaderVertex, maxLength, &maxLength, error); - debugMessage("Error compiling vert shader"); - debugMessage(error); - throw error; - } - - // Now load the Frag shader - this->shaderFrag = glCreateShader(GL_FRAGMENT_SHADER); - auto fragShaderC = fragmentShader.c_str(); - glShaderSource(this->shaderFrag, 1, &fragShaderC, 0); - glCompileShader(this->shaderFrag); - glGetShaderiv(this->shaderFrag, GL_COMPILE_STATUS, &isSuccess); - if(!isSuccess) { - glGetShaderiv(this->shaderFrag, GL_INFO_LOG_LENGTH, &maxLength); - glGetShaderInfoLog(this->shaderFrag, maxLength, &maxLength, error); - glDeleteShader(this->shaderVertex); - debugMessage("Error compiling frag shader"); - debugMessage(error); - throw error; - } - - // Now create the shader program. - this->shaderProgram = glCreateProgram(); - glAttachShader(this->shaderProgram, this->shaderVertex); - glAttachShader(this->shaderProgram, this->shaderFrag); - - // Now parse out the variables. - #if DAWN_OPENGL_HLSL - auto itAttr = attributeLocations.begin(); - while(itAttr != attributeLocations.end()) { - this->bindAttributeLocation(itAttr->first, itAttr->second); - ++itAttr; - } - #endif - - //Bind, Verify & Use the shader program - glLinkProgram(this->shaderProgram); - glGetProgramiv(this->shaderProgram, GL_LINK_STATUS, &isSuccess); - if(!isSuccess) { - glGetProgramiv(this->shaderProgram, GL_INFO_LOG_LENGTH, &maxLength); - glGetProgramInfoLog(this->shaderProgram, maxLength, &maxLength, error); - glDeleteShader(this->shaderVertex); - glDeleteShader(this->shaderFrag); - debugMessage("Error compiling shader program"); - debugMessage(error); - throw error; - } -} - -void ShaderProgram::bindAttributeLocation(std::string name, int32_t location) { - if(this->shaderProgram == -1) throw "Shader has not yet been compiled"; - glBindAttribLocation(this->shaderProgram, location, name.c_str()); -} - -void ShaderProgram::setTexture(shaderparameter_t param, textureslot_t slot) { - glUniform1i(param, slot); -} - -shaderparameter_t ShaderProgram::getParameterByName(std::string name) { - return glGetUniformLocation(this->shaderProgram, name.c_str()); -} - -shaderbufferlocation_t ShaderProgram::getBufferLocationByName(std::string name) { - return glGetUniformBlockIndex(this->shaderProgram, name.c_str()); -} - -void ShaderProgram::setParameterBuffer(shaderbufferlocation_t location, shaderbufferslot_t slot) { - glUniformBlockBinding(this->shaderProgram, location, slot); -} - -void ShaderProgram::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) { - glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix)); -} - -void ShaderProgram::setBoolean(shaderparameter_t uni, bool value) { - glUniform1i(uni, value); -} - -void ShaderProgram::setColor(shaderparameter_t uniform, struct Color color) { - auto precise = color.precision(); - glUniform4f(uniform, precise.r, precise.g, precise.b, precise.a); -} - -void ShaderProgram::setVector3(shaderparameter_t uniform, glm::vec3 vector) { - glUniform3f(uniform, vector.x, vector.y, vector.z); -} - -void ShaderProgram::setFloat(shaderparameter_t param, float_t value) { - glUniform1f(param, value); -} - -void ShaderProgram::bind() { - if(this->shaderProgram == -1) throw "Shader has not yet been compiled"; - glUseProgram(this->shaderProgram); -} - -ShaderProgram::~ShaderProgram() { - if(this->shaderProgram != -1) glDeleteProgram(this->shaderProgram); - if(this->shaderVertex != -1) glDeleteShader(this->shaderVertex); - if(this->shaderFrag != -1) glDeleteShader(this->shaderFrag); +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "Shader.hpp" + +using namespace Dawn; + +void Shader::compileShader( + std::map attributeLocations, + std::string vertexShader, + std::string fragmentShader +) { + GLint isSuccess; + int32_t maxLength; + char error[1024]; + + // Load the vertex shader first + this->shaderVertex = glCreateShader(GL_VERTEX_SHADER); + auto vertShaderC = vertexShader.c_str(); + glShaderSource(this->shaderVertex, 1, &vertShaderC, 0); + glCompileShader(this->shaderVertex); + + // Validate + glGetShaderiv(this->shaderVertex, GL_COMPILE_STATUS, &isSuccess); + if(!isSuccess) { + glGetShaderiv(this->shaderVertex, GL_INFO_LOG_LENGTH, &maxLength); + glGetShaderInfoLog(this->shaderVertex, maxLength, &maxLength, error); + debugMessage("Error compiling vert shader"); + debugMessage(error); + throw error; + } + + // Now load the Frag shader + this->shaderFrag = glCreateShader(GL_FRAGMENT_SHADER); + auto fragShaderC = fragmentShader.c_str(); + glShaderSource(this->shaderFrag, 1, &fragShaderC, 0); + glCompileShader(this->shaderFrag); + glGetShaderiv(this->shaderFrag, GL_COMPILE_STATUS, &isSuccess); + if(!isSuccess) { + glGetShaderiv(this->shaderFrag, GL_INFO_LOG_LENGTH, &maxLength); + glGetShaderInfoLog(this->shaderFrag, maxLength, &maxLength, error); + glDeleteShader(this->shaderVertex); + debugMessage("Error compiling frag shader"); + debugMessage(error); + throw error; + } + + // Now create the shader program. + this->shaderProgram = glCreateProgram(); + glAttachShader(this->shaderProgram, this->shaderVertex); + glAttachShader(this->shaderProgram, this->shaderFrag); + + // Now parse out the variables. + #if DAWN_OPENGL_HLSL + auto itAttr = attributeLocations.begin(); + while(itAttr != attributeLocations.end()) { + this->bindAttributeLocation(itAttr->first, itAttr->second); + ++itAttr; + } + #endif + + //Bind, Verify & Use the shader program + glLinkProgram(this->shaderProgram); + glGetProgramiv(this->shaderProgram, GL_LINK_STATUS, &isSuccess); + if(!isSuccess) { + glGetProgramiv(this->shaderProgram, GL_INFO_LOG_LENGTH, &maxLength); + glGetProgramInfoLog(this->shaderProgram, maxLength, &maxLength, error); + glDeleteShader(this->shaderVertex); + glDeleteShader(this->shaderFrag); + debugMessage("Error compiling shader program"); + debugMessage(error); + throw error; + } +} + +void Shader::bindAttributeLocation(std::string name, int32_t location) { + if(this->shaderProgram == -1) throw "Shader has not yet been compiled"; + glBindAttribLocation(this->shaderProgram, location, name.c_str()); +} + +void Shader::setTexture(shaderparameter_t param, textureslot_t slot) { + glUniform1i(param, slot); +} + +shaderparameter_t Shader::getParameterByName(std::string name) { + return glGetUniformLocation(this->shaderProgram, name.c_str()); +} + +shaderbufferlocation_t Shader::getBufferLocationByName(std::string name) { + return glGetUniformBlockIndex(this->shaderProgram, name.c_str()); +} + +void Shader::setParameterBuffer(shaderbufferlocation_t location, shaderbufferslot_t slot) { + glUniformBlockBinding(this->shaderProgram, location, slot); +} + +void Shader::setMatrix(shaderparameter_t uniform, glm::mat4 matrix) { + glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(matrix)); +} + +void Shader::setBoolean(shaderparameter_t uni, bool value) { + glUniform1i(uni, value); +} + +void Shader::setColor(shaderparameter_t uniform, struct Color color) { + auto precise = color.precision(); + glUniform4f(uniform, precise.r, precise.g, precise.b, precise.a); +} + +void Shader::setVector3(shaderparameter_t uniform, glm::vec3 vector) { + glUniform3f(uniform, vector.x, vector.y, vector.z); +} + +void Shader::setFloat(shaderparameter_t param, float_t value) { + glUniform1f(param, value); +} + +void Shader::bind() { + if(this->shaderProgram == -1) throw "Shader has not yet been compiled"; + glUseProgram(this->shaderProgram); +} + +Shader::~Shader() { + if(this->shaderProgram != -1) glDeleteProgram(this->shaderProgram); + if(this->shaderVertex != -1) glDeleteShader(this->shaderVertex); + if(this->shaderFrag != -1) glDeleteShader(this->shaderFrag); } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/ShaderProgram.hpp b/src/dawnopengl/display/shader/Shader.hpp similarity index 87% rename from src/dawnopengl/display/shader/ShaderProgram.hpp rename to src/dawnopengl/display/shader/Shader.hpp index 4627274c..4be9f74e 100644 --- a/src/dawnopengl/display/shader/ShaderProgram.hpp +++ b/src/dawnopengl/display/shader/Shader.hpp @@ -1,89 +1,84 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "display/shader/_ShaderProgram.hpp" -#include "dawnopengl.hpp" -#include "display/Color.hpp" -#include "debug/debug.hpp" - -#include "ShaderParameterBuffer.hpp" - -typedef GLuint shaderparameter_t; - -namespace Dawn { - class ShaderProgram : public IShaderProgram { - private: - /** Pointer to an uploaded vertex shader program */ - GLuint shaderVertex = -1; - - /** Pointer to an uploaded fragment shader program */ - GLuint shaderFrag = -1; - - /** Pointer to an uploaded shader program linked */ - GLuint shaderProgram = -1; - - protected: - /** - * Compiles a GLSL/HLSL shader and stores it on the GPU, updates the - * underlying pointers for you. - * - * @param vertexShader The string source of the vertex shader. - * @param fragmentShader The string source of the fragment shader. - */ - void compileShader( - std::map attributeLocations, - std::string vertexShader, - std::string fragmentShader - ); - - /** - * Typically HLSL only, this method allows you to specify where vbo - * attributes are bound. Typically 0 for positions, 1 for coordinates, - * etc. - * - * @param name Attribute name in the HLSL shader. - * @param location Index pointing to which location it is to be bound to. - */ - void bindAttributeLocation(std::string name, int32_t location); - - public: - /** - * Locate a shader parameter by its name. - * - * @param name Name of the parameter to get. - * @return The shader parameter. - */ - shaderparameter_t getParameterByName(std::string name); - - /** - * Locate a shader buffer parameter set by its name. - * - * @param name Name of the buffer to get. - * @return The shader buffer. - */ - shaderbufferlocation_t getBufferLocationByName(std::string name); - - /** - * Method to request that this shader be compiled and put on the GPU. This - * method should call the protected compileShader method. - */ - virtual void compile() = 0; - - void bind() override; - void setParameterBuffer(shaderbufferlocation_t location, shaderbufferslot_t slot); - void setMatrix(shaderparameter_t parameter, glm::mat4 matrix) override; - 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, textureslot_t texture) override; - void setFloat(shaderparameter_t parameter, float_t value) override; - - /** - * Destroys and deletes the shader from the GPU. - */ - virtual ~ShaderProgram(); - }; +// 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 "dawnopengl.hpp" +#include "display/Color.hpp" +#include "debug/debug.hpp" + +#include "ShaderParameterBuffer.hpp" + +typedef GLuint shaderparameter_t; + +namespace Dawn { + class Shader : public IShader { + private: + /** Pointer to an uploaded vertex shader program */ + GLuint shaderVertex = -1; + + /** Pointer to an uploaded fragment shader program */ + GLuint shaderFrag = -1; + + /** Pointer to an uploaded shader program linked */ + GLuint shaderProgram = -1; + + protected: + /** + * Compiles a GLSL/HLSL shader and stores it on the GPU, updates the + * underlying pointers for you. + * + * @param vertexShader The string source of the vertex shader. + * @param fragmentShader The string source of the fragment shader. + */ + void compileShader( + std::map attributeLocations, + std::string vertexShader, + std::string fragmentShader + ); + + /** + * Typically HLSL only, this method allows you to specify where vbo + * attributes are bound. Typically 0 for positions, 1 for coordinates, + * etc. + * + * @param name Attribute name in the HLSL shader. + * @param location Index pointing to which location it is to be bound to. + */ + void bindAttributeLocation(std::string name, int32_t location); + + public: + /** + * Locate a shader parameter by its name. + * + * @param name Name of the parameter to get. + * @return The shader parameter. + */ + shaderparameter_t getParameterByName(std::string name); + + /** + * Locate a shader buffer parameter set by its name. + * + * @param name Name of the buffer to get. + * @return The shader buffer. + */ + shaderbufferlocation_t getBufferLocationByName(std::string name); + + virtual void compile() = 0; + void bind() override; + void setParameterBuffer(shaderbufferlocation_t location, shaderbufferslot_t slot); + void setMatrix(shaderparameter_t parameter, glm::mat4 matrix) override; + 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, textureslot_t texture) override; + void setFloat(shaderparameter_t parameter, float_t value) override; + + /** + * Destroys and deletes the shader from the GPU. + */ + virtual ~Shader(); + }; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleBillboardedShader.cpp b/src/dawnopengl/display/shader/SimpleBillboardedShader.cpp index 95d62b37..02e1d2c7 100644 --- a/src/dawnopengl/display/shader/SimpleBillboardedShader.cpp +++ b/src/dawnopengl/display/shader/SimpleBillboardedShader.cpp @@ -4,12 +4,10 @@ // https://opensource.org/licenses/MIT #include "SimpleBillboardedShader.hpp" -#include "scene/components/display/mesh/MeshRenderer.hpp" -#include "scene/components/display/Camera.hpp" using namespace Dawn; -void SimpleBillboardedShaderProgram::compile() { +void SimpleBillboardedShader::compile() { #if DAWN_OPENGL_GLSL this->compileShader( { @@ -61,43 +59,4 @@ void SimpleBillboardedShaderProgram::compile() { this->paramColor = this->getParameterByName("u_Color"); this->paramTexture = this->getParameterByName("u_Text"); this->paramHasTexture = this->getParameterByName("u_HasTexture"); -} - - - -void SimpleBillboardedShader::compile() { - this->program.compile(); -} - -std::vector SimpleBillboardedShader::getPassItems( - Mesh *mesh, - Material *material, - Camera *camera -) { - auto simpleMaterial = dynamic_cast(material); - assertNotNull(simpleMaterial); - - struct ShaderPassItem onlyPass; - onlyPass.mesh = mesh; - onlyPass.shaderProgram = &program; - onlyPass.colorValues[program.paramColor] = simpleMaterial->color; - onlyPass.matrixValues[program.paramModel] = material->transform->getWorldTransform(); - onlyPass.matrixValues[program.paramView] = camera->transform->getWorldTransform(); - onlyPass.matrixValues[program.paramProjection] = camera->getProjection(); - onlyPass.renderFlags = ( - RENDER_MANAGER_RENDER_FLAG_BLEND | - RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST - ); - - if(simpleMaterial->texture != nullptr) { - onlyPass.boolValues[program.paramHasTexture] = true; - onlyPass.textureSlots[0] = simpleMaterial->texture; - onlyPass.textureValues[program.paramTexture] = 0; - } else { - onlyPass.boolValues[program.paramHasTexture] = false; - } - - std::vector passes; - passes.push_back(onlyPass); - return passes; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleBillboardedShader.hpp b/src/dawnopengl/display/shader/SimpleBillboardedShader.hpp index 6c21c398..648f48cb 100644 --- a/src/dawnopengl/display/shader/SimpleBillboardedShader.hpp +++ b/src/dawnopengl/display/shader/SimpleBillboardedShader.hpp @@ -4,10 +4,10 @@ // https://opensource.org/licenses/MIT #pragma once -#include "scene/components/display/material/SimpleBillboardedMaterial.hpp" +#include "display/shader/Shader.hpp" namespace Dawn { - class SimpleBillboardedShaderProgram : public ShaderProgram { + class SimpleBillboardedShader : public Shader { public: shaderparameter_t paramProjection; shaderparameter_t paramView; @@ -18,17 +18,4 @@ namespace Dawn { void compile() override; }; - - class SimpleBillboardedShader : public Shader { - public: - SimpleBillboardedShaderProgram program; - - void compile() override; - - std::vector getPassItems( - Mesh *mesh, - Material *material, - Camera *camera - ) override; - }; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleTexturedShader.cpp b/src/dawnopengl/display/shader/SimpleTexturedShader.cpp index ae95e068..b371403a 100644 --- a/src/dawnopengl/display/shader/SimpleTexturedShader.cpp +++ b/src/dawnopengl/display/shader/SimpleTexturedShader.cpp @@ -4,12 +4,10 @@ // https://opensource.org/licenses/MIT #include "SimpleTexturedShader.hpp" -#include "scene/components/display/mesh/MeshRenderer.hpp" -#include "scene/components/display/Camera.hpp" using namespace Dawn; -void SimpleTexturedShaderProgram::compile() { +void SimpleTexturedShader::compile() { #if DAWN_OPENGL_GLSL this->compileShader( { @@ -34,12 +32,6 @@ void SimpleTexturedShaderProgram::compile() { // Fragment Shader "#version 330 core\n" - "layout (std140) uniform uniTest {\n" - "float r;\n" - "float g;\n" - "float b;\n" - "};\n" - "in vec2 o_TextCoord;\n" "out vec4 o_Color;\n" "uniform vec4 u_Color;\n" @@ -47,12 +39,11 @@ void SimpleTexturedShaderProgram::compile() { "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" - "o_Color = vec4(r, g, b, 1);\n" + "if(u_HasTexture) {\n" + "o_Color = texture(u_Text, o_TextCoord) * u_Color;\n" + "} else {\n" + "o_Color = u_Color;" + "}\n" "}\n" ); #elif DAWN_OPENGL_HLSL @@ -102,66 +93,4 @@ void SimpleTexturedShaderProgram::compile() { this->paramColor = this->getParameterByName("u_Color"); this->paramTexture = this->getParameterByName("u_Text"); this->paramHasTexture = this->getParameterByName("u_HasTexture"); - - - this->test.init(); - this->test.bind(0); - auto bufferLoc = this->getBufferLocationByName("uniTest"); - this->setParameterBuffer(bufferLoc, 0); - - struct Test123 data; - data.r = 1.0f; - data.g = 0.0f; - data.b = 1.0f; - this->test.buffer(&data); - - data.g = 1.0f; - this->test.buffer(&data); - - data.g = 0.0f; - data.b = 0.0f; - this->test.buffer(&data, &data.g); - - data.g = 1.0f; - data.b = 0.0f; - this->test.buffer(&data, &data.g, &data.b); -} - - - -void SimpleTexturedShader::compile() { - this->program.compile(); -} - -std::vector SimpleTexturedShader::getPassItems( - Mesh *mesh, - Material *material, - Camera *camera -) { - auto simpleMaterial = dynamic_cast(material); - assertNotNull(simpleMaterial); - - struct ShaderPassItem onlyPass; - onlyPass.mesh = mesh; - onlyPass.shaderProgram = &program; - onlyPass.colorValues[program.paramColor] = simpleMaterial->color; - onlyPass.matrixValues[program.paramModel] = material->transform->getWorldTransform(); - onlyPass.matrixValues[program.paramView] = camera->transform->getWorldTransform(); - onlyPass.matrixValues[program.paramProjection] = camera->getProjection(); - onlyPass.renderFlags = ( - RENDER_MANAGER_RENDER_FLAG_BLEND | - RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST - ); - - if(simpleMaterial->texture != nullptr) { - onlyPass.boolValues[program.paramHasTexture] = true; - onlyPass.textureSlots[0] = simpleMaterial->texture; - onlyPass.textureValues[program.paramTexture] = 0; - } else { - onlyPass.boolValues[program.paramHasTexture] = false; - } - - std::vector passes; - passes.push_back(onlyPass); - return passes; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp index d828c3fc..c8e6bd6c 100644 --- a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp +++ b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp @@ -4,24 +4,11 @@ // https://opensource.org/licenses/MIT #pragma once -#include "display/shader/ShaderManager.hpp" -#include "scene/components/display/material/SimpleTexturedMaterial.hpp" -#include "display/shader/ShaderParameterBuffer.hpp" +#include "display/shader/Shader.hpp" namespace Dawn { - struct Test123 { - float_t r; - float_t g; - float_t b; - }; - - class SimpleTexturedShaderTest : public ShaderParameterBuffer { - }; - - class SimpleTexturedShaderProgram : public ShaderProgram { + class SimpleTexturedShader : public Shader { public: - SimpleTexturedShaderTest test; - shaderparameter_t paramProjection; shaderparameter_t paramView; shaderparameter_t paramModel; @@ -31,17 +18,4 @@ namespace Dawn { void compile() override; }; - - class SimpleTexturedShader : public Shader { - public: - SimpleTexturedShaderProgram program; - - void compile() override; - - std::vector getPassItems( - Mesh *mesh, - Material *material, - Camera *camera - ) override; - }; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/UIShaderProgram.hpp b/src/dawnopengl/display/shader/UIShader.hpp similarity index 76% rename from src/dawnopengl/display/shader/UIShaderProgram.hpp rename to src/dawnopengl/display/shader/UIShader.hpp index 73ea1a7b..4c929790 100644 --- a/src/dawnopengl/display/shader/UIShaderProgram.hpp +++ b/src/dawnopengl/display/shader/UIShader.hpp @@ -1,14 +1,14 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "SimpleTexturedShader.hpp" - -#define UI_SHADER_PROGRAM_PRIORITY 100 - -namespace Dawn { - class UIShaderProgram : public SimpleTexturedShaderProgram { - }; +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "SimpleTexturedShader.hpp" + +#define UI_SHADER_PROGRAM_PRIORITY 100 + +namespace Dawn { + class UIShader : public SimpleTexturedShader { + }; } \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.cpp b/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.cpp index 401470c1..7fc102de 100644 --- a/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.cpp +++ b/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.cpp @@ -22,6 +22,8 @@ void SimpleBillboardedMaterial::onDispose() { this->getGame()->renderManager.getShaderManager()->releaseShader(this->shaderLock); } -Shader * SimpleBillboardedMaterial::getShader() { - return this->getGame()->renderManager.getShaderManager()->getShader(this->shaderLock); +std::vector SimpleBillboardedMaterial::getRenderPasses() { + return { + + }; } \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.hpp b/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.hpp index 66757465..563c0168 100644 --- a/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.hpp +++ b/src/dawnopengl/scene/components/display/material/SimpleBillboardedMaterial.hpp @@ -26,7 +26,6 @@ namespace Dawn { void onStart() override; void onDispose() override; - - Shader * getShader() override; + std::vector getRenderPasses() override; }; } \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.cpp b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.cpp index eee92a0b..c876f6ae 100644 --- a/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.cpp +++ b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.cpp @@ -21,6 +21,37 @@ void SimpleTexturedMaterial::onDispose() { this->getGame()->renderManager.getShaderManager()->releaseShader(this->shaderLock); } -Shader * SimpleTexturedMaterial::getShader() { - return this->getGame()->renderManager.getShaderManager()->getShader(this->shaderLock); +std::vector SimpleTexturedMaterial::getRenderPasses() { + auto mesh = this->item->getComponent(); + auto shader = this->getGame()->renderManager.getShaderManager()->getShader(this->shaderLock); + auto camera = this->getGame()->renderManager.getRenderPipeline()->camera; + + assertNotNull(mesh); + assertNotNull(mesh->mesh); + assertNotNull(shader); + assertNotNull(camera); + + struct ShaderPassItem onlyPass; + onlyPass.mesh = mesh->mesh; + onlyPass.shader = shader; + onlyPass.colorValues[shader->paramColor] = this->color; + onlyPass.matrixValues[shader->paramModel] = this->transform->getWorldTransform(); + onlyPass.matrixValues[shader->paramView] = camera->transform->getWorldTransform(); + onlyPass.matrixValues[shader->paramProjection] = camera->getProjection(); + onlyPass.renderFlags = ( + RENDER_MANAGER_RENDER_FLAG_BLEND | + RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST + ); + + if(this->texture != nullptr) { + onlyPass.boolValues[shader->paramHasTexture] = true; + onlyPass.textureSlots[0] = this->texture; + onlyPass.textureValues[shader->paramTexture] = 0; + } else { + onlyPass.boolValues[shader->paramHasTexture] = false; + } + + std::vector passes; + passes.push_back(onlyPass); + return passes; } \ No newline at end of file diff --git a/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.hpp b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.hpp index 86acfa8a..d4548531 100644 --- a/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.hpp +++ b/src/dawnopengl/scene/components/display/material/SimpleTexturedMaterial.hpp @@ -5,6 +5,7 @@ #pragma once #include "scene/components/display/Material.hpp" +#include "scene/components/display/mesh/MeshRenderer.hpp" namespace Dawn { class SimpleTexturedMaterial : public Material { @@ -26,7 +27,6 @@ namespace Dawn { void onStart() override; void onDispose() override; - - Shader * getShader() override; + std::vector getRenderPasses() override; }; } \ No newline at end of file