Working on RenderTarget example

This commit is contained in:
2022-10-19 00:43:16 -07:00
parent 93e77b9c5e
commit fd7c075fa0
17 changed files with 235 additions and 26 deletions

View File

@ -0,0 +1,24 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
namespace Dawn {
struct Color {
float_t r, g, b, a;
};
#define COLOR_WHITE { 1, 1, 1, 1 }
#define COLOR_RED { 1, 0, 0, 1 }
#define COLOR_GREEN { 0, 255, 0, 1 }
#define COLOR_BLUE { 0, 0, 255, 1 }
#define COLOR_BLACK { 0, 0, 0, 1 }
#define COLOR_MAGENTA { 1, 0, 1, 1 }
#define COLOR_CORNFLOWER_BLUE { 0.39215686274f, 0.58431372549f, 0.9294117647f, 1.0f }
#define COLOR_WHITE_TRANSPARENT { 1, 1, 1, 0 }
#define COLOR_BLACK_TRANSPARENT { 0, 0, 0, 0 }
#define COLOR_TRANSPARENT COLOR_BLACK_TRANSPARENT
}

View File

@ -0,0 +1,22 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/flag.hpp"
#include "display/Color.hpp"
#define RENDER_TARGET_CLEAR_FLAG_COLOR FLAG_DEFINE(0)
#define RENDER_TARGET_CLEAR_FLAG_DEPTH FLAG_DEFINE(1)
namespace Dawn {
class RenderTarget {
public:
virtual float_t getWidth() = 0;
virtual float_t getHeight() = 0;
virtual void setClearColor(struct Color color) = 0;
virtual void clear(flag8_t clearFlags) = 0;
virtual void bind() = 0;
};
}

View File

@ -0,0 +1,34 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "RenderTarget.hpp"
namespace Dawn {
class DawnGame;
class IRenderManager {
public:
std::weak_ptr<DawnGame> game;
/**
* Returns the primary render target (the backbuffer) that draws directly
* to the screen.
*
* @return Shared pointer to the backbuffer render target.
*/
virtual std::shared_ptr<RenderTarget> getBackBuffer() = 0;
/**
* Initialize this render manager.
*/
virtual void init() = 0;
/**
* Perform a synchronous frame update on the render manager.
*/
virtual void update() = 0;
};
}

View File

@ -20,9 +20,9 @@ namespace Dawn {
{ {
private: private:
std::shared_ptr<Scene> scene; std::shared_ptr<Scene> scene;
std::weak_ptr<DawnHost> host;
public: public:
std::weak_ptr<DawnHost> host;
RenderManager renderManager; RenderManager renderManager;
/** /**

View File

@ -5,6 +5,8 @@
#include "SceneItemComponent.hpp" #include "SceneItemComponent.hpp"
#include "SceneItem.hpp" #include "SceneItem.hpp"
#include "Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn; using namespace Dawn;
@ -12,6 +14,14 @@ SceneItemComponent::SceneItemComponent(std::weak_ptr<SceneItem> item) {
this->item = item; this->item = item;
} }
std::shared_ptr<Scene> SceneItemComponent::getScene() {
return this->item.lock()->scene.lock();
}
std::shared_ptr<DawnGame> SceneItemComponent::getGame() {
return this->item.lock()->scene.lock()->game.lock();
}
SceneItemComponent::~SceneItemComponent() { SceneItemComponent::~SceneItemComponent() {
} }

View File

@ -8,6 +8,8 @@
namespace Dawn { namespace Dawn {
class SceneItem; class SceneItem;
class Scene;
class DawnGame;
class SceneItemComponent { class SceneItemComponent {
public: public:
@ -17,6 +19,9 @@ namespace Dawn {
virtual void start() = 0; virtual void start() = 0;
std::shared_ptr<Scene> getScene();
std::shared_ptr<DawnGame> getGame();
virtual ~SceneItemComponent(); virtual ~SceneItemComponent();
}; };
} }

View File

@ -4,6 +4,8 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#include "Camera.hpp" #include "Camera.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn; using namespace Dawn;
@ -34,8 +36,17 @@ void Camera::updateProjection() {
} }
} }
std::shared_ptr<RenderTarget> Camera::getRenderTarget() {
if(this->target == nullptr) {
return this->getGame()->renderManager.getBackBuffer();
}
return this->target;
}
float_t Camera::getAspect() { float_t Camera::getAspect() {
return 16.0f / 9.0f; auto target = this->getRenderTarget();
return target->getWidth() / target->getHeight();
} }
void Camera::start() { void Camera::start() {

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include "scene/SceneItemComponent.hpp" #include "scene/SceneItemComponent.hpp"
#include "display/RenderTarget.hpp"
namespace Dawn { namespace Dawn {
enum CameraType { enum CameraType {
@ -15,6 +16,7 @@ namespace Dawn {
class Camera : public SceneItemComponent { class Camera : public SceneItemComponent {
public: public:
glm::mat4 projection; glm::mat4 projection;
std::shared_ptr<RenderTarget> target = nullptr;
// Perspective // Perspective
enum CameraType type = CAMERA_TYPE_PERSPECTIVE; enum CameraType type = CAMERA_TYPE_PERSPECTIVE;
@ -42,6 +44,8 @@ namespace Dawn {
*/ */
void updateProjection(); void updateProjection();
std::shared_ptr<RenderTarget> getRenderTarget();
/** /**
* Returs the aspect ratio of the camera. * Returs the aspect ratio of the camera.
* *

11
src/dawn/util/flag.hpp Normal file
View File

@ -0,0 +1,11 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
typedef uint_fast8_t flag8_t;
#define FLAG_DEFINE(n) (1 << n)

View File

@ -8,6 +8,7 @@
#include "DawnGLFWHost.hpp" #include "DawnGLFWHost.hpp"
#include "game/DawnGame.hpp" #include "game/DawnGame.hpp"
#include "dawnopengl.hpp" #include "dawnopengl.hpp"
#include "display/BackBufferRenderTarget.hpp"
using namespace Dawn; using namespace Dawn;
@ -53,6 +54,11 @@ int32_t DawnHost::init(std::weak_ptr<DawnGame> game) {
if(auto g = game.lock()) { if(auto g = game.lock()) {
auto result = g->init(); auto result = g->init();
if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result; if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result;
g->renderManager.renderTarget->setSize(
DAWN_GLFW_WINDOW_WIDTH_DEFAULT,
DAWN_GLFW_WINDOW_HEIGHT_DEFAULT
);
} else { } else {
return DAWN_GLFW_INIT_RESULT_GAME_LOCK_FAILED; return DAWN_GLFW_INIT_RESULT_GAME_LOCK_FAILED;
} }
@ -107,6 +113,8 @@ int32_t DawnHost::update(std::weak_ptr<DawnGame> game, float_t delta) {
return ret; return ret;
} }
} }
return DAWN_GLFW_UPDATE_RESULT_GAME_LOCK_FAILED;
} }
void DawnHost::unload(std::weak_ptr<DawnGame> game) { void DawnHost::unload(std::weak_ptr<DawnGame> game) {

View File

@ -18,6 +18,8 @@
#define DAWN_GLFW_START_RESULT_UPDATE_FAILED -1 #define DAWN_GLFW_START_RESULT_UPDATE_FAILED -1
#define DAWN_GLFW_UPDATE_RESULT_GAME_LOCK_FAILED -1
namespace Dawn { namespace Dawn {
class DawnHostData { class DawnHostData {
public: public:

View File

@ -0,0 +1,46 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "BackBufferRenderTarget.hpp"
using namespace Dawn;
BackBufferRenderTarget::BackBufferRenderTarget(
std::weak_ptr<RenderManager> renderManager
) {
this->renderManager = renderManager;
}
float_t BackBufferRenderTarget::getWidth() {
return this->width;
}
float_t BackBufferRenderTarget::getHeight() {
return this->height;
}
void BackBufferRenderTarget::setSize(float_t width, float_t height) {
this->width = width;
this->height = height;
}
void BackBufferRenderTarget::setClearColor(struct Color color) {
this->clearColor = color;
}
void BackBufferRenderTarget::clear(flag8_t clearFlags) {
glClearColor(
this->clearColor.r,
this->clearColor.g,
this->clearColor.b,
this->clearColor.a
);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void BackBufferRenderTarget::bind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, (GLsizei)this->width, (GLsizei)this->height);
}

View File

@ -0,0 +1,32 @@
// 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/RenderTarget.hpp"
#include "display/RenderManager.hpp"
namespace Dawn {
class BackBufferRenderTarget : public RenderTarget {
private:
std::weak_ptr<RenderManager> renderManager;
float_t width = 1;
float_t height = 1;
struct Color clearColor = COLOR_CORNFLOWER_BLUE;
public:
BackBufferRenderTarget(std::weak_ptr<RenderManager> renderManager);
float_t getWidth() override;
float_t getHeight() override;
void setSize(float_t width, float_t height);
void setClearColor(struct Color color);
void clear(flag8_t clearFlags) override;
void bind() override;
};
}

View File

@ -7,6 +7,7 @@
target_sources(${DAWN_TARGET_NAME} target_sources(${DAWN_TARGET_NAME}
PRIVATE PRIVATE
RenderManager.cpp RenderManager.cpp
BackBufferRenderTarget.cpp
) )
# Subdirs # Subdirs

View File

@ -6,26 +6,28 @@
#include "dawnopengl.hpp" #include "dawnopengl.hpp"
#include "game/DawnGame.hpp" #include "game/DawnGame.hpp"
#include "display/RenderManager.hpp" #include "display/RenderManager.hpp"
#include "display/BackBufferRenderTarget.hpp"
using namespace Dawn; using namespace Dawn;
RenderManager::RenderManager(std::weak_ptr<DawnGame> game) { RenderManager::RenderManager(std::weak_ptr<DawnGame> game) {
this->game = game; this->game = game;
this->renderTarget=std::make_shared<BackBufferRenderTarget>(weak_from_this());
} }
void RenderManager::init() { void RenderManager::init() {
glClearColor( }
0.39215686274f,
0.58431372549f,
0.9294117647f,
1.0f
);
glViewport(0, 0, 1280, 720); std::shared_ptr<RenderTarget> RenderManager::getBackBuffer() {
return this->renderTarget;
} }
void RenderManager::update() { void RenderManager::update() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); this->getBackBuffer()->bind();
this->getBackBuffer()->clear(
RENDER_TARGET_CLEAR_FLAG_COLOR |
RENDER_TARGET_CLEAR_FLAG_DEPTH
);
} }
RenderManager::~RenderManager() { RenderManager::~RenderManager() {

View File

@ -4,15 +4,19 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#pragma once #pragma once
#include "dawnlibs.hpp" #include "display/_RenderManager.hpp"
namespace Dawn { namespace Dawn {
class DawnGame; class BackBufferRenderTarget;
class RenderManager :
public IRenderManager,
public std::enable_shared_from_this<RenderManager>
{
class RenderManager {
public: public:
std::weak_ptr<DawnGame> game; std::shared_ptr<BackBufferRenderTarget> renderTarget;
/** /**
* Construct a new RenderManager for a game instance. * Construct a new RenderManager for a game instance.
* *
@ -20,16 +24,10 @@ namespace Dawn {
*/ */
RenderManager(std::weak_ptr<DawnGame> game); RenderManager(std::weak_ptr<DawnGame> game);
/** std::shared_ptr<RenderTarget> getBackBuffer() override;
* Initialize this render manager. void init() override;
*/ void update() override;
void init();
/**
* Perform a synchronous frame update on the render manager.
*/
void update();
/** /**
* Destroy a previously initialized RenderManager. * Destroy a previously initialized RenderManager.
*/ */

View File

@ -35,7 +35,6 @@ int32_t DawnGame::update(float_t delta) {
auto meshRenderer = this->scene->findComponent<MeshRenderer>(); auto meshRenderer = this->scene->findComponent<MeshRenderer>();
if(meshRenderer != nullptr) { if(meshRenderer != nullptr) {
std::cout << "REndering" << std::endl;
meshRenderer->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1); meshRenderer->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
} }