This commit is contained in:
2023-11-15 23:13:22 -06:00
parent d8bc1d0fe3
commit 04dbead9a2
19 changed files with 330 additions and 25 deletions

View File

@ -5,16 +5,24 @@
#include "assert/assert.hpp"
#include "Camera.hpp"
#include "game/Game.hpp"
using namespace Dawn;
void Camera::onInit() {
std::cout << "Camera" << std::endl;
if(renderTarget == nullptr) {
this->setRenderTarget(
getGame()->renderHost.backBufferRenderTarget
);
}
}
void Camera::onDispose() {
std::cout << "~Camera" << std::endl;
// renderTarget = nullptr;
renderTarget = nullptr;
}
std::shared_ptr<RenderTarget> Camera::getRenderTarget() {
return this->renderTarget;
}
glm::mat4 Camera::getProjection() {
@ -44,4 +52,11 @@ glm::mat4 Camera::getProjection() {
float_t Camera::getAspect() {
return 1.0f;
}
void Camera::setRenderTarget(std::shared_ptr<RenderTarget> renderTarget) {
if(this->renderTarget != nullptr) {
}
this->renderTarget = renderTarget;
}

View File

@ -5,6 +5,7 @@
#pragma once
#include "scene/SceneItem.hpp"
#include "display/RenderTarget.hpp"
namespace Dawn {
enum CameraType {
@ -12,7 +13,10 @@ namespace Dawn {
ORTHOGONAL
};
class Camera : public SceneComponent {
class Camera final : public SceneComponent {
private:
std::shared_ptr<RenderTarget> renderTarget;
public:
float_t clipNear = 0.01f;
float_t clipFar = 1000.0f;
@ -25,8 +29,6 @@ namespace Dawn {
float_t orthoBottom = -1.0f;
float_t orthoTop = 1.0f;
// std::shared_ptr<RenderTarget> renderTarget;
void onInit() override;
void onDispose() override;
@ -39,11 +41,25 @@ namespace Dawn {
*/
float_t getAspect();
/**
* Returns the render target for this camera.
*
* @return Render target.
*/
std::shared_ptr<RenderTarget> getRenderTarget();
/**
* Returns the projection matrix for this camera.
*
* @return Projection matrix.
*/
glm::mat4 getProjection();
/**
* Sets the render target for this camera.
*
* @param renderTarget The render target to set.
*/
void setRenderTarget(std::shared_ptr<RenderTarget> renderTarget);
};
}

View File

@ -8,7 +8,7 @@
#include "scene/SceneItem.hpp"
namespace Dawn {
class MeshRenderer : public SceneComponent {
class MeshRenderer final : public SceneComponent {
public:
std::shared_ptr<Mesh> mesh;

View File

@ -11,5 +11,5 @@ target_sources(${DAWN_TARGET_NAME}
# Subdirs
# add_subdirectory(font)
# add_subdirectory(mesh)
add_subdirectory(mesh)
# add_subdirectory(shader)

View File

@ -5,6 +5,7 @@
#pragma once
#include "dawnlibs.hpp"
#include "display/RenderTarget.hpp"
namespace Dawn {
class Game;
@ -34,6 +35,14 @@ namespace Dawn {
*/
virtual bool_t isCloseRequested() = 0;
/**
* Returns the back buffer render target. This is the render target that
* is used to render to the screen.
*
* @return The back buffer render target.
*/
virtual std::shared_ptr<RenderTarget> getBackBufferRenderTarget() = 0;
/**
* Destroys the render host.
*/

View File

@ -14,7 +14,6 @@ enum RenderTargetClearFlag {
namespace Dawn {
class RenderTarget {
public:
/**
* Return the width of the render target.
*
@ -52,7 +51,7 @@ namespace Dawn {
*
* @param clearFlags Flags to request what is going to be cleared.
*/
virtual void clear(const flag8_t clearFlags) = 0;
virtual void clear(const enum RenderTargetClearFlag clearFlags) = 0;
/**
* Bind the render target for rendering to. The proceeding render requests
@ -65,7 +64,7 @@ namespace Dawn {
/**
* Destroys the render target.
*/
virtual RenderTarget() {
virtual ~RenderTarget() {
}
};

View File

@ -0,0 +1,10 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
CubeMesh.cpp
)

View File

@ -0,0 +1,70 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "CubeMesh.hpp"
using namespace Dawn;
void Dawn::CubeMesh::buffer(
const std::shared_ptr<Mesh> mesh,
const glm::vec3 pos,
const glm::vec3 size,
const int32_t verticeStart,
const int32_t indiceStart
) {
glm::vec3 positions[CUBE_VERTICE_COUNT] = {
pos,
glm::vec3(pos.x+size.x, pos.y, pos.z),
glm::vec3(pos.x, pos.y+size.y, pos.z),
glm::vec3(pos.x+size.x, pos.y+size.y, pos.z),
glm::vec3(pos.x, pos.y, pos.z+size.z),
glm::vec3(pos.x+size.x, pos.y, pos.z+size.z),
glm::vec3(pos.x, pos.y+size.y, pos.z+size.z),
pos + size
};
glm::vec2 coordinates[CUBE_VERTICE_COUNT] = {
glm::vec2(0, 0),
glm::vec2(1, 0),
glm::vec2(0, 1),
glm::vec2(1, 1),
glm::vec2(0, 0),
glm::vec2(1, 0),
glm::vec2(0, 1),
glm::vec2(1, 1)
};
int32_t indices[CUBE_INDICE_COUNT] = {
// Back
verticeStart, verticeStart + 1, verticeStart + 3,
verticeStart, verticeStart + 2, verticeStart + 3,
// Right
verticeStart + 1, verticeStart + 5, verticeStart + 7,
verticeStart + 1, verticeStart + 3, verticeStart + 7,
// Left
verticeStart + 4, verticeStart, verticeStart + 2,
verticeStart + 4, verticeStart + 6, verticeStart + 2,
// Front
verticeStart + 5, verticeStart + 4, verticeStart + 6,
verticeStart + 5, verticeStart + 7, verticeStart + 6,
// Top
verticeStart + 7, verticeStart + 2, verticeStart + 6,
verticeStart + 7, verticeStart + 3, verticeStart + 2,
// Bottom
verticeStart + 1, verticeStart, verticeStart + 4,
verticeStart + 1, verticeStart + 4, verticeStart + 5
};
mesh->bufferPositions(verticeStart, positions, CUBE_VERTICE_COUNT);
mesh->bufferCoordinates(verticeStart, coordinates, CUBE_VERTICE_COUNT);
mesh->bufferIndices(indiceStart, indices, CUBE_INDICE_COUNT);
}

View File

@ -0,0 +1,30 @@
// 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"
#define CUBE_VERTICE_COUNT 8
#define CUBE_INDICE_COUNT 36
namespace Dawn {
class CubeMesh {
public:
/**
* Buffers cube mesh vertices and indices into the given mesh.
*
* @param size The size of the cube.
* @param verticeStart The starting index of the vertices.
* @param indiceStart The starting index of the indices.
*/
static void buffer(
const std::shared_ptr<Mesh> mesh,
const glm::vec3 pos,
const glm::vec3 size,
const int32_t verticeStart,
const int32_t indiceStart
);
};
}

View File

@ -5,7 +5,8 @@
#include "assert/assert.hpp"
#include "util/Flag.hpp"
#include "SceneComponent.hpp"
#include "scene/Scene.hpp"
#include "game/Game.hpp"
using namespace Dawn;
@ -46,6 +47,16 @@ std::shared_ptr<SceneItem> SceneComponent::getItem() {
return this->item.lock();
}
std::shared_ptr<Scene> SceneComponent::getScene() {
auto item = this->getItem();
return item->getScene();
}
std::shared_ptr<Game> SceneComponent::getGame() {
auto scene = this->getScene();
return scene->getGame();
}
SceneComponent::~SceneComponent() {
if(Flag::isOn<uint_fast8_t>(
sceneComponentState,

View File

@ -10,6 +10,8 @@
#define SCENE_COMPONENT_STATE_DISPOSED 0x02
namespace Dawn {
class Game;
class Scene;
class SceneItem;
class SceneComponent : std::enable_shared_from_this<SceneComponent> {
@ -50,6 +52,20 @@ namespace Dawn {
*/
std::shared_ptr<SceneItem> getItem();
/**
* Returns the scene that this scene component belongs to.
*
* @return Reference to the scene that this component belongs to.
*/
std::shared_ptr<Scene> getScene();
/**
* Returns the game that this scene component belongs to.
*
* @return Reference to the game that this component belongs to.
*/
std::shared_ptr<Game> getGame();
/**
* Disposes this scene component.
*/