First render.

This commit is contained in:
2023-11-17 00:02:04 -06:00
parent 55f629c7b5
commit 490da9d0c1
43 changed files with 1039 additions and 67 deletions

View File

@ -7,6 +7,8 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Color.cpp
RenderPipeline.cpp
IRenderHost.cpp
)
# Subdirs

View File

@ -4,35 +4,37 @@
// https://opensource.org/licenses/MIT
#include "Color.hpp"
#include "util/String.hpp"
#include "assert/assert.hpp"
using namespace Dawn;
struct Color Color::fromString(const std::string str) {
// Convert to lowercase
auto lower = stringToLowercase(str);
auto lower = String::toLowercase(str);
if(stringIncludes(lower, "cornflower")) {
if(String::includes(lower, "cornflower")) {
return COLOR_CORNFLOWER_BLUE;
} else if(stringIncludes(lower, "magenta")) {
} else if(String::includes(lower, "magenta")) {
return COLOR_MAGENTA;
} else if(stringIncludes(lower, "white")) {
} else if(String::includes(lower, "white")) {
return COLOR_WHITE;
} else if(stringIncludes(lower, "black")) {
} else if(String::includes(lower, "black")) {
return COLOR_BLACK;
} else if(stringIncludes(lower, "red")) {
} else if(String::includes(lower, "red")) {
return COLOR_RED;
} else if(stringIncludes(lower, "green")) {
} else if(String::includes(lower, "green")) {
return COLOR_GREEN;
} else if(stringIncludes(lower, "blue")) {
} else if(String::includes(lower, "blue")) {
return COLOR_BLUE;
} else if(stringIncludes(lower, "transparent")) {
} else if(String::includes(lower, "transparent")) {
return COLOR_TRANSPARENT;
}
@ -57,7 +59,7 @@ struct Color Color::fromString(const std::string str) {
}
// Split by comma
auto splitByComma = stringSplit(str, ",");
auto splitByComma = String::split(str, ",");
if(splitByComma.size() == 3) {
// RGB
return {

View File

@ -0,0 +1,14 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "IRenderHost.hpp"
using namespace Dawn;
IRenderHost::IRenderHost() : renderPipeline(), shaderManager() {
}
IRenderHost::~IRenderHost() {
}

View File

@ -6,12 +6,22 @@
#pragma once
#include "dawnlibs.hpp"
#include "display/RenderTarget.hpp"
#include "display/RenderPipeline.hpp"
#include "display/shader/ShaderManager.hpp"
namespace Dawn {
class Game;
class IRenderHost {
public:
RenderPipeline renderPipeline;
ShaderManager shaderManager;
/**
* Creates a render host.
*/
IRenderHost();
/**
* Initializes the render host, called by the game during the initial
* set up of the engine.
@ -22,9 +32,9 @@ namespace Dawn {
/**
* Performs an update/tick of the render host. This would be the game
* asking the RenderHost to the rendering.
* asking the RenderHost to do the rendering.
*/
virtual void update() = 0;
virtual void update(const std::shared_ptr<Game> game) = 0;
/**
* Overridable request from the game that asks if the RenderHost has any
@ -46,8 +56,6 @@ namespace Dawn {
/**
* Destroys the render host.
*/
virtual ~IRenderHost() {
}
virtual ~IRenderHost();
};
}

View File

@ -0,0 +1,110 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "assert/assert.hpp"
#include "RenderPipeline.hpp"
#include "game/Game.hpp"
#include "scene/Scene.hpp"
#include "component/display/Camera.hpp"
#include "component/display/IRenderableComponent.hpp"
using namespace Dawn;
RenderPipeline::RenderPipeline() {
}
void RenderPipeline::render(
const std::shared_ptr<Game> game
) {
assertNotNull(game, "Game cannot be null");
auto scene = game->getCurrentScene();
if(!scene) return;
this->renderScene(game, scene);
}
void RenderPipeline::renderScene(
const std::shared_ptr<Game> game,
const std::shared_ptr<Scene> scene
) {
assertNotNull(game, "Game cannot be null");
assertNotNull(scene, "Scene cannot be null");
// TODO: Render Subscenes First
// Get a list of all cameras in the scene
auto cameras = scene->findComponents<Camera>();
auto backBuffer = scene->getGame()->renderHost.getBackBufferRenderTarget();
std::shared_ptr<Camera> backbufferCamera = nullptr;
for(auto camera : cameras) {
auto rt = camera->getRenderTarget();
// Is this camera the backbuffer camera?
if(rt == backBuffer || rt == nullptr) {
backbufferCamera = camera;
continue;
}
// Render scene with this camera
renderSceneCamera(game, scene, camera, camera->getRenderTarget());
}
if(backbufferCamera) {
// Render the backbuffer camera
renderSceneCamera(game, scene, backbufferCamera, backBuffer);
}
}
void RenderPipeline::renderSceneCamera(
const std::shared_ptr<Game> game,
const std::shared_ptr<Scene> scene,
const std::shared_ptr<Camera> camera,
const std::shared_ptr<RenderTarget> renderTarget
) {
assertNotNull(game, "Game cannot be null");
assertNotNull(scene, "Scene cannot be null");
assertNotNull(camera, "Camera cannot be null");
struct RenderPassContext ctx = {
game,
scene,
camera,
renderTarget
};
// Get list of renderables
std::vector<std::shared_ptr<struct IRenderPass>> renderPasses;
auto renderables = scene->findComponents<IRenderableComponent>();
for(auto renderable : renderables) {
auto rp = renderable->getPasses(ctx);
renderPasses.insert(renderPasses.end(), rp.begin(), rp.end());
}
// Sort the render passes
// TODO: Make clearing the buffers editable!
renderTarget->bind();
renderTarget->clear(
RENDER_TARGET_CLEAR_COLOR |
RENDER_TARGET_CLEAR_DEPTH
);
std::for_each(
renderPasses.begin(),
renderPasses.end(),
[&](std::shared_ptr<struct IRenderPass> pass) {
pass->bind();
pass->setData();
pass->upload();
pass->draw();
}
);
}
RenderPipeline::~RenderPipeline() {
}

View File

@ -0,0 +1,63 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
namespace Dawn {
class Game;
class Scene;
class Camera;
class RenderTarget;
class RenderPipeline {
public:
/**
* Creates a new RenderPipeline.
*/
RenderPipeline();
/**
* Renders the game. This will render the current scene.
*
* @param game Game to render.
*/
void render(
const std::shared_ptr<Game> game
);
/**
* Renders a specific scene. This will render all cameras within the
* scene.
*
* @param game Game to render.
* @param scene Scene to render.
*/
void renderScene(
const std::shared_ptr<Game> game,
const std::shared_ptr<Scene> scene
);
/**
* Renders a specific scene with a specific camera.
*
* @param game Game to render.
* @param scene Scene to render.
* @param camera Camera to render.
* @param renderTarget Render target to render to.
*/
void renderSceneCamera(
const std::shared_ptr<Game> game,
const std::shared_ptr<Scene> scene,
const std::shared_ptr<Camera> camera,
const std::shared_ptr<RenderTarget> renderTarget
);
/**
* Destroys the RenderPipeline.
*/
virtual ~RenderPipeline();
};
}

View File

@ -6,10 +6,8 @@
#pragma once
#include "dawnlibs.hpp"
enum RenderTargetClearFlag {
COLOR = 1 << 0,
DEPTH = 1 << 0
};
#define RENDER_TARGET_CLEAR_COLOR 1 << 0
#define RENDER_TARGET_CLEAR_DEPTH 1 << 1
namespace Dawn {
class RenderTarget {
@ -51,7 +49,7 @@ namespace Dawn {
*
* @param clearFlags Flags to request what is going to be cleared.
*/
virtual void clear(const enum RenderTargetClearFlag clearFlags) = 0;
virtual void clear(const int32_t clearFlags) = 0;
/**
* Bind the render target for rendering to. The proceeding render requests

View File

@ -7,7 +7,7 @@
using namespace Dawn;
void Dawn::CubeMesh::buffer(
void CubeMesh::buffer(
const std::shared_ptr<Mesh> mesh,
const glm::vec3 pos,
const glm::vec3 size,

View File

@ -7,9 +7,29 @@
#include "dawnlibs.hpp"
namespace Dawn {
template<struct T>
class IShader {
private:
enum ShaderParameterType {
VEC2,
VEC3,
VEC4,
MAT3,
MAT4,
COLOR,
FLOAT,
INT,
TEXTURE,
BOOLEAN
};
class IShaderBase {
public:
virtual ~IShaderBase() {
}
};
template<typename T>
class IShader : public IShaderBase {
protected:
T data;
public:

View File

@ -7,7 +7,7 @@
using namespace Dawn;
IShaderStage::IShaderStage(const enum ShaderType type) :
IShaderStage::IShaderStage(const enum ShaderStageType type) :
type(type)
{

View File

@ -7,20 +7,22 @@
#include "dawnlibs.hpp"
namespace Dawn {
enum ShaderType {
enum ShaderStageType {
VERTEX,
FRAGMENT,
COMPUTE
// COMPUTE
};
class IShaderStage {
public:
const enum ShaderStageType type;
/**
* Constructs a new Shader Stage.
*
* @param type Type of shader stage.
*/
IShaderStage(const enum ShaderType type);
IShaderStage(const enum ShaderStageType type);
/**
* Destroy the IShaderStage object

View File

@ -0,0 +1,45 @@
// 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 ShaderManager {
private:
std::vector<std::shared_ptr<IShaderBase>> shaders;
public:
/**
* Retreives an instance of the shader from the shader manager. If the
* shader does not exist it will be created.
*
* @tparam T Type of shader to retreive.
* @return Shader instance.
*/
template<class T>
std::shared_ptr<T> getShader() {
auto itShaders = shaders.begin();
while(itShaders != shaders.end()) {
// auto shader = itShaders->lock();
// if(!shader) {
// itShaders = shaders.erase(itShaders);
// continue;
// }
// std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(shader);
auto shader = *itShaders;
std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(shader);
if(casted) return casted;
itShaders++;
}
auto newShader = std::make_shared<T>();
shaders.push_back(newShader);
newShader->init();
return newShader;
}
};
}