diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index aba318a9..d50e9f37 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -21,6 +21,62 @@ void RenderPipeline::render() { this->renderScene(*this->renderManager.game.scene); } +void RenderPipeline::renderScene(Scene &scene) { + RenderTarget &backBuffer = this->renderManager.getBackBuffer(); + auto cameras = scene.findComponents(); + std::shared_ptr backBufferCamera = nullptr; + + // First, render all non-backbuffer cameras. + auto it = cameras.begin(); + while(it != cameras.end()) { + RenderTarget &cameraTarget = (*it)->getRenderTarget(); + + // Leave the backbuffer camera(s) to last, so we skip them. + if(&cameraTarget == &backBuffer) { + backBufferCamera = *it; + } else { + this->renderSceneCamera(scene, **it); + } + + ++it; + } + + // Now render the backbuffer camera. + if(backBufferCamera == nullptr) return; + this->renderSceneCamera(scene, *backBufferCamera); +} + +void RenderPipeline::renderSceneCamera(Scene &scene, Camera &camera) { + RenderTarget &renderTarget = camera.getRenderTarget(); + renderTarget.bind(); + renderTarget.clear( + RENDER_TARGET_CLEAR_FLAG_DEPTH | + RENDER_TARGET_CLEAR_FLAG_COLOR + ); + + auto meshes = scene.findComponents(); + auto it = meshes.begin(); + while(it != meshes.end()) { + auto mesh = *it; + auto item = mesh->item; + auto material = item.getComponent(); + + // TODO: fallback material? + if(material == nullptr) { + continue; + } + + auto shader = material->getShader(); + shader->bind(); + shader->setGlobalParameters(camera.projection, camera.item.transform); + shader->setMeshParameters(item.transform); + material->setShaderParameters(); + + mesh->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1); + ++it; + } +} + RenderPipeline::~RenderPipeline() { } \ No newline at end of file diff --git a/src/dawn/display/RenderPipeline.hpp b/src/dawn/display/RenderPipeline.hpp index 423ee967..7c70530c 100644 --- a/src/dawn/display/RenderPipeline.hpp +++ b/src/dawn/display/RenderPipeline.hpp @@ -14,11 +14,44 @@ namespace Dawn { public: RenderManager &renderManager; + /** + * Constructs a new RenderPipeline. Render Pipelines are my attempt to + * create both a flexible, but standard way to allow the individual games + * to decide how they want to render the common scene-item models. + * + * @param renderManager Parent render manager this pipeline belongs to. + */ RenderPipeline(RenderManager &renderManager); + + /** + * Initialize the render pipeline. + */ virtual void init(); - void render(); - virtual void renderScene(Scene &scene) = 0; + + /** + * Renders the games' currently active scene, and all of its' cameras. + */ + virtual void render(); + + /** + * Render a specific scene, usually just called for the currently active + * scene, but in future this could include sub-scenes. + * + * @param scene Scene to render. + */ + virtual void renderScene(Scene &scene); + + /** + * Render a specific camera on a specific scene. + * + * @param scene Scene to render. + * @param camera Camera within the scene to render. + */ virtual void renderSceneCamera(Scene &scene, Camera &camera) = 0; + + /** + * Cleanup a render pipeline that has been initialized. + */ virtual ~RenderPipeline(); }; } \ No newline at end of file diff --git a/src/dawn/display/RenderTarget.hpp b/src/dawn/display/RenderTarget.hpp index dfc5f5b2..e38e9d89 100644 --- a/src/dawn/display/RenderTarget.hpp +++ b/src/dawn/display/RenderTarget.hpp @@ -13,10 +13,43 @@ namespace Dawn { class RenderTarget { public: + /** + * Return the width of the render target. + * + * @return The width of the render target. + */ virtual float_t getWidth() = 0; + + /** + * Return the height of the render target. + * + * @return The height of the render target. + */ virtual float_t getHeight() = 0; + + /** + * Sets the clear color of the render target when the clear method for + * the color buffer is requested. + * + * @param color Color to use for the clear operation. + */ virtual void setClearColor(struct Color color) = 0; + + /** + * Request the existing data in the render target to be cleared out. We + * typically assume the render target can support multiple buffer types, + * so you can opt to only clear certain buffer types. + * + * @param clearFlags Flags to request what is going to be cleared. + */ virtual void clear(flag8_t clearFlags) = 0; + + /** + * Bind the render target for rendering to. The proceeding render requests + * will want to render to this render target directly. In future I may + * see if we can have multiple render targets bound at once to make this + * operation perform faster. + */ virtual void bind() = 0; }; } \ No newline at end of file diff --git a/src/dawn/display/_RenderManager.hpp b/src/dawn/display/_RenderManager.hpp index b9695480..23d8536e 100644 --- a/src/dawn/display/_RenderManager.hpp +++ b/src/dawn/display/_RenderManager.hpp @@ -58,7 +58,5 @@ namespace Dawn { * Perform a synchronous frame update on the render manager. */ virtual void update() = 0; - - }; } \ No newline at end of file diff --git a/src/dawn/display/shader/_Shader.hpp b/src/dawn/display/shader/_Shader.hpp index f60a6a3a..a37b4d4b 100644 --- a/src/dawn/display/shader/_Shader.hpp +++ b/src/dawn/display/shader/_Shader.hpp @@ -24,16 +24,33 @@ namespace Dawn { */ virtual void bind() = 0; - // virtual void setCamera(glm::mat4 projection, glm::mat4 view) = 0; - // virtual void setLocalPosition(glm::mat4 position) = 0; - + /** + * Requested by the Material to set the default parameters of the shader. + * Each parameter really should have a default value set so that there is + * no nullptr's or other issues. + * + * @param material Material to set the default parameters on to. + */ virtual void setDefaultParameters(Material &material) = 0; + /** + * Requested by the render pipeline (typically) to set global level (once + * per frame) parameters. + * + * @param projection Projection matrix of the current viewport. + * @param view View matrix of the current viewport. + */ virtual void setGlobalParameters( glm::mat4 projection, glm::mat4 view ) = 0; + /** + * Requested by the render pipeline (typically) to set mesh-level params. + * This may be performed multiple times per frame. + * + * @param position Matrix of the position of the mesh. + */ virtual void setMeshParameters(glm::mat4 position) = 0; /** diff --git a/src/dawn/scene/SceneItemComponent.hpp b/src/dawn/scene/SceneItemComponent.hpp index 0a1aa723..34215378 100644 --- a/src/dawn/scene/SceneItemComponent.hpp +++ b/src/dawn/scene/SceneItemComponent.hpp @@ -15,13 +15,36 @@ namespace Dawn { public: SceneItem &item; + /** + * Constructs a new SceneItemComponent. Components are attached to + * SceneItems and will be individual responsibility components, and must + * communicate to other items/components using methods and events. + * + * @param item Scene Item thsi component belongs to. + */ SceneItemComponent(SceneItem &item); + /** + * Requested on the first frame that the parent scene item has become + * active. + */ virtual void start() = 0; + /** + * Shorthand to return the scene that this component's item belongs to. + * @return The current scene. + */ Scene & getScene(); + + /** + * Shorthand to return the game that this scene belongs to. + * @return The current game. + */ DawnGame & getGame(); + /** + * Cleanup the SceneItemComponent. + */ virtual ~SceneItemComponent(); }; } \ No newline at end of file diff --git a/src/dawn/scene/components/CMakeLists.txt b/src/dawn/scene/components/CMakeLists.txt index 49d05db4..7873eac4 100644 --- a/src/dawn/scene/components/CMakeLists.txt +++ b/src/dawn/scene/components/CMakeLists.txt @@ -3,11 +3,5 @@ # This software is released under the MIT License. # https://opensource.org/licenses/MIT -# Sources -target_sources(${DAWN_TARGET_NAME} - PRIVATE - DummyComponent.cpp -) - # Subdirs add_subdirectory(display) \ No newline at end of file diff --git a/src/dawn/scene/components/Components.hpp b/src/dawn/scene/components/Components.hpp index 99fcc4af..880fdac5 100644 --- a/src/dawn/scene/components/Components.hpp +++ b/src/dawn/scene/components/Components.hpp @@ -4,7 +4,6 @@ // https://opensource.org/licenses/MIT #pragma once -#include "scene/components/DummyComponent.hpp" #include "scene/components/display/Camera.hpp" #include "scene/components/display/MeshRenderer.hpp" #include "scene/components/display/Material.hpp" \ No newline at end of file diff --git a/src/dawn/scene/components/DummyComponent.cpp b/src/dawn/scene/components/DummyComponent.cpp deleted file mode 100644 index b6339851..00000000 --- a/src/dawn/scene/components/DummyComponent.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "DummyComponent.hpp" - -using namespace Dawn; - -void DummyComponent::start() { - std::cout << "Dummy Component Start" << std::endl; -} \ No newline at end of file diff --git a/src/dawn/scene/components/DummyComponent.hpp b/src/dawn/scene/components/DummyComponent.hpp deleted file mode 100644 index 8f6eb64a..00000000 --- a/src/dawn/scene/components/DummyComponent.hpp +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) 2022 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "scene/SceneItemComponent.hpp" - -namespace Dawn { - class DummyComponent : public SceneItemComponent { - public: - DummyComponent(SceneItem &item) : - SceneItemComponent(item) - { - std::cout << "Dummy Component Initialized" << std::endl; - }; - - void start() override; - }; -} \ No newline at end of file diff --git a/src/dawn/scene/components/display/Camera.hpp b/src/dawn/scene/components/display/Camera.hpp index c22d807f..9c81a84a 100644 --- a/src/dawn/scene/components/display/Camera.hpp +++ b/src/dawn/scene/components/display/Camera.hpp @@ -45,7 +45,6 @@ namespace Dawn { void updateProjection(); void lookAt(glm::vec3 position, glm::vec3 look); - void lookAt(glm::vec3 position, glm::vec3 look, glm::vec3 up); /** diff --git a/src/dawn/scene/components/display/Material.cpp b/src/dawn/scene/components/display/Material.cpp index 1642b2c8..3585d00b 100644 --- a/src/dawn/scene/components/display/Material.cpp +++ b/src/dawn/scene/components/display/Material.cpp @@ -17,7 +17,6 @@ Material::Material(SceneItem &item) : } void Material::updateShaderParameters() { - std::cout << "Updating params" << std::endl; this->colorValues.clear(); this->boolValues.clear(); diff --git a/src/dawn/scene/components/display/Material.hpp b/src/dawn/scene/components/display/Material.hpp index 85b83c61..6b847d94 100644 --- a/src/dawn/scene/components/display/Material.hpp +++ b/src/dawn/scene/components/display/Material.hpp @@ -11,29 +11,56 @@ namespace Dawn { class Shader; class Material : public SceneItemComponent { - friend class RenderPipeline; - private: std::shared_ptr shader; + /** + * Internal method that will be invoked to go through and update all of + * the shader parameters whenever the shader is swapped out. + */ void updateShaderParameters(); - public: std::map parameters; - std::map colorValues; std::map boolValues; std::map matrixValues; std::map vec3Values; + /** + * Material component constructor. + * + * @param item Scene Item this component belongs to. + */ Material(SceneItem &item); + /** + * Return the shader this material is currently using. + * + * @return Shader pointer to the currently bound shader. + */ std::shared_ptr getShader(); + + /** + * Sets the shader for the material to use. This will also clear and + * update all of the parameters from the shaders' default parameter list. + * + * @param shader Shader to set. + */ void setShader(std::shared_ptr shader); + /** + * Protected method that can be called, likely by the render pipeline, to + * set and update all of the shader parameters from this material on to + * the shader. + * + * This method assumes that the shader has already been bound. + */ void setShaderParameters(); + /** + * Overrides the default SceneItemComponent start method. + */ void start() override; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/display/MeshRenderer.hpp b/src/dawn/scene/components/display/MeshRenderer.hpp index 47792062..37c10279 100644 --- a/src/dawn/scene/components/display/MeshRenderer.hpp +++ b/src/dawn/scene/components/display/MeshRenderer.hpp @@ -12,8 +12,16 @@ namespace Dawn { public: std::shared_ptr mesh = nullptr; + /** + * Constructs a MeshRenderer scene item component. + * + * @param item Scene Item this mesh renderer belongs to. + */ MeshRenderer(SceneItem &item); + /** + * Overrides the default SceneItemComponent start method. + */ void start() override; }; } \ No newline at end of file diff --git a/src/dawnopengl/display/BackBufferRenderTarget.hpp b/src/dawnopengl/display/BackBufferRenderTarget.hpp index d3e28f7e..fe2dd237 100644 --- a/src/dawnopengl/display/BackBufferRenderTarget.hpp +++ b/src/dawnopengl/display/BackBufferRenderTarget.hpp @@ -18,15 +18,28 @@ namespace Dawn { struct Color clearColor = COLOR_CORNFLOWER_BLUE; public: + /** + * Construct the back buffer render target. + * + * @param renderManager Render Manager for this back buffer target. + */ BackBufferRenderTarget(RenderManager &renderManager); + /** + * Requests to modify the viewport directly. This is mostly to be called + * by whatever is setting the window/display resolution, so that the + * backbuffer can keep track of what the viewport size is. This should + * also be DPI aware, e.g. "4k @ 2xDPI, resulting in a 1080p equiv" should + * still call this method with 3840, 2160. + * + * @param width New width of the back buffer. + * @param height New height of the back buffer. + */ + void setSize(float_t width, float_t height); + float_t getWidth() override; float_t getHeight() override; - - void setSize(float_t width, float_t height); - - void setClearColor(struct Color color); - + void setClearColor(struct Color color) override; void clear(flag8_t clearFlags) override; void bind() override; }; diff --git a/src/dawnopengl/display/StandardRenderPipeline.cpp b/src/dawnopengl/display/StandardRenderPipeline.cpp index 8e9dbcfa..716855e7 100644 --- a/src/dawnopengl/display/StandardRenderPipeline.cpp +++ b/src/dawnopengl/display/StandardRenderPipeline.cpp @@ -11,60 +11,4 @@ using namespace Dawn; StandardRenderPipeline::StandardRenderPipeline(RenderManager &renderManager) : RenderPipeline(renderManager) { -} - -void StandardRenderPipeline::renderScene(Scene &scene) { - RenderTarget &backBuffer = this->renderManager.getBackBuffer(); - auto cameras = scene.findComponents(); - std::shared_ptr backBufferCamera = nullptr; - - // First, render all non-backbuffer cameras. - auto it = cameras.begin(); - while(it != cameras.end()) { - RenderTarget &cameraTarget = (*it)->getRenderTarget(); - - // Leave the backbuffer camera(s) to last, so we skip them. - if(&cameraTarget == &backBuffer) { - backBufferCamera = *it; - } else { - this->renderSceneCamera(scene, **it); - } - - ++it; - } - - // Now render the backbuffer camera. - if(backBufferCamera == nullptr) return; - this->renderSceneCamera(scene, *backBufferCamera); -} - -void StandardRenderPipeline::renderSceneCamera(Scene &scene, Camera &camera) { - RenderTarget &renderTarget = camera.getRenderTarget(); - renderTarget.bind(); - renderTarget.clear( - RENDER_TARGET_CLEAR_FLAG_DEPTH | - RENDER_TARGET_CLEAR_FLAG_COLOR - ); - - auto meshes = scene.findComponents(); - auto it = meshes.begin(); - while(it != meshes.end()) { - auto mesh = *it; - auto item = mesh->item; - auto material = item.getComponent(); - - // TODO: fallback material? - if(material == nullptr) { - continue; - } - - auto shader = material->getShader(); - shader->bind(); - shader->setGlobalParameters(camera.projection, camera.item.transform); - shader->setMeshParameters(item.transform); - material->setShaderParameters(); - - mesh->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1); - ++it; - } } \ No newline at end of file diff --git a/src/dawnopengl/display/StandardRenderPipeline.hpp b/src/dawnopengl/display/StandardRenderPipeline.hpp index 1f159cad..051dad13 100644 --- a/src/dawnopengl/display/StandardRenderPipeline.hpp +++ b/src/dawnopengl/display/StandardRenderPipeline.hpp @@ -11,8 +11,5 @@ namespace Dawn { class StandardRenderPipeline : public RenderPipeline { public: StandardRenderPipeline(RenderManager &renderManager); - - void renderScene(Scene &scene) override; - void renderSceneCamera(Scene &scene, Camera &camera) override; }; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/Shader.hpp b/src/dawnopengl/display/shader/Shader.hpp index a02c6e29..46bcbc0a 100644 --- a/src/dawnopengl/display/shader/Shader.hpp +++ b/src/dawnopengl/display/shader/Shader.hpp @@ -23,10 +23,14 @@ namespace Dawn { GLuint shaderProgram = -1; protected: - void compileShader( - std::string vertexShader, - std::string fragmentShader - ); + /** + * Compiles a GLSL 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::string vertexShader, std::string fragmentShader); public: /** @@ -37,13 +41,21 @@ namespace Dawn { */ shaderparameter_t getParameterByName(std::string name); - void bind() override; + /** + * 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 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; - ~Shader(); + /** + * Destroys and deletes the shader from the GPU. + */ + virtual ~Shader(); }; } \ No newline at end of file diff --git a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp index 4febd064..54323dff 100644 --- a/src/dawnopengl/display/shader/SimpleTexturedShader.hpp +++ b/src/dawnopengl/display/shader/SimpleTexturedShader.hpp @@ -23,8 +23,8 @@ namespace Dawn { std::map ps; ps[this->paramColor] = SHADER_PARAMETER_TYPE_COLOR; + ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN; // ps[paramTexture] SHADER_PARAMETER_TYPE_TEXTURE; - // ps[this->paramHasTexture] = SHADER_PARAMETER_TYPE_BOOLEAN; return ps; }