Documenting done.

This commit is contained in:
2022-10-20 07:49:42 -07:00
parent 0033785251
commit 221e5ee1f1
19 changed files with 243 additions and 123 deletions

View File

@ -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<Camera>();
std::shared_ptr<Camera> 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<MeshRenderer>();
auto it = meshes.begin();
while(it != meshes.end()) {
auto mesh = *it;
auto item = mesh->item;
auto material = item.getComponent<Material>();
// 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() {
}

View File

@ -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();
};
}

View File

@ -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;
};
}

View File

@ -58,7 +58,5 @@ namespace Dawn {
* Perform a synchronous frame update on the render manager.
*/
virtual void update() = 0;
};
}

View File

@ -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;
/**

View File

@ -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();
};
}

View File

@ -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)

View File

@ -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"

View File

@ -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;
}

View File

@ -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;
};
}

View File

@ -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);
/**

View File

@ -17,7 +17,6 @@ Material::Material(SceneItem &item) :
}
void Material::updateShaderParameters() {
std::cout << "Updating params" << std::endl;
this->colorValues.clear();
this->boolValues.clear();

View File

@ -11,29 +11,56 @@ namespace Dawn {
class Shader;
class Material : public SceneItemComponent {
friend class RenderPipeline;
private:
std::shared_ptr<Shader> 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<shaderparameter_t, enum ShaderParameterType> parameters;
std::map<shaderparameter_t, struct Color> colorValues;
std::map<shaderparameter_t, bool_t> boolValues;
std::map<shaderparameter_t, glm::mat4> matrixValues;
std::map<shaderparameter_t, glm::vec3> 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<Shader> 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> 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;
};
}

View File

@ -12,8 +12,16 @@ namespace Dawn {
public:
std::shared_ptr<Mesh> 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;
};
}

View File

@ -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;
};

View File

@ -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<Camera>();
std::shared_ptr<Camera> 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<MeshRenderer>();
auto it = meshes.begin();
while(it != meshes.end()) {
auto mesh = *it;
auto item = mesh->item;
auto material = item.getComponent<Material>();
// 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;
}
}

View File

@ -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;
};
}

View File

@ -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();
};
}

View File

@ -23,8 +23,8 @@ namespace Dawn {
std::map<shaderparameter_t, enum ShaderParameterType> 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;
}