Progress
This commit is contained in:
@ -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;
|
||||
}
|
@ -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);
|
||||
};
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -11,5 +11,5 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
|
||||
# Subdirs
|
||||
# add_subdirectory(font)
|
||||
# add_subdirectory(mesh)
|
||||
add_subdirectory(mesh)
|
||||
# add_subdirectory(shader)
|
@ -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.
|
||||
*/
|
||||
|
@ -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() {
|
||||
|
||||
}
|
||||
};
|
||||
|
10
src/dawn/display/mesh/CMakeLists.txt
Normal file
10
src/dawn/display/mesh/CMakeLists.txt
Normal 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
|
||||
)
|
70
src/dawn/display/mesh/CubeMesh.cpp
Normal file
70
src/dawn/display/mesh/CubeMesh.cpp
Normal 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);
|
||||
}
|
30
src/dawn/display/mesh/CubeMesh.hpp
Normal file
30
src/dawn/display/mesh/CubeMesh.hpp
Normal 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
|
||||
);
|
||||
};
|
||||
}
|
@ -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,
|
||||
|
@ -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.
|
||||
*/
|
||||
|
@ -54,6 +54,8 @@ void RenderHost::init(std::shared_ptr<Game> game) {
|
||||
assertNoGLError();
|
||||
|
||||
// Get the resolution and scale/dpi
|
||||
backBufferRenderTarget = std::make_shared<BackBufferRenderTarget>();
|
||||
|
||||
int32_t fbWidth, fbHeight;
|
||||
int32_t windowWidth, windowHeight;
|
||||
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
|
||||
@ -64,6 +66,9 @@ void RenderHost::init(std::shared_ptr<Game> game) {
|
||||
assertTrue(windowWidth > 0, "Detected window width is too small?");
|
||||
assertTrue(windowHeight > 0, "Detected window height is too small?");
|
||||
|
||||
backBufferRenderTarget->setSize(fbWidth, fbHeight);
|
||||
backBufferRenderTarget->scale = (float_t)fbWidth / (float_t)windowWidth;
|
||||
|
||||
// Framebuffer callback
|
||||
// glfwSetFramebufferSizeCallback(window, [&](
|
||||
// GLFWwindow *window,
|
||||
@ -88,6 +93,10 @@ bool_t RenderHost::isCloseRequested() {
|
||||
return glfwWindowShouldClose(this->window);
|
||||
}
|
||||
|
||||
std::shared_ptr<RenderTarget> RenderHost::getBackBufferRenderTarget() {
|
||||
return std::static_pointer_cast<RenderTarget>(backBufferRenderTarget);
|
||||
}
|
||||
|
||||
RenderHost::~RenderHost() {
|
||||
if(this->window != nullptr) {
|
||||
glfwDestroyWindow(this->window);
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "display/IRenderHost.hpp"
|
||||
#include "display/BackBufferRenderTarget.hpp"
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
@ -16,6 +17,7 @@ namespace Dawn {
|
||||
|
||||
class RenderHost : public IRenderHost {
|
||||
public:
|
||||
std::shared_ptr<BackBufferRenderTarget> backBufferRenderTarget;
|
||||
GLFWwindow *window = nullptr;
|
||||
|
||||
/**
|
||||
@ -24,10 +26,9 @@ namespace Dawn {
|
||||
RenderHost();
|
||||
|
||||
void init(std::shared_ptr<Game> game) override;
|
||||
|
||||
void update() override;
|
||||
|
||||
bool_t isCloseRequested() override;
|
||||
std::shared_ptr<RenderTarget> getBackBufferRenderTarget() override;
|
||||
|
||||
~RenderHost();
|
||||
};
|
||||
|
@ -5,6 +5,8 @@
|
||||
|
||||
#include "scene/SceneList.hpp"
|
||||
#include "component/display/Camera.hpp"
|
||||
#include "component/display/MeshRenderer.hpp"
|
||||
#include "display/mesh/CubeMesh.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
@ -13,4 +15,11 @@ void Dawn::helloWorldScene(Scene &s) {
|
||||
|
||||
auto cameraItem = s.createSceneItem();
|
||||
auto camera = cameraItem->addComponent<Camera>();
|
||||
|
||||
auto cubeMesh = std::make_shared<Mesh>();
|
||||
CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(1, 1, 1), 0, 0);
|
||||
|
||||
auto cubeItem = s.createSceneItem();
|
||||
auto cubeMeshRenderer = cubeItem->addComponent<MeshRenderer>();
|
||||
cubeMeshRenderer->mesh = cubeMesh;
|
||||
}
|
@ -26,5 +26,5 @@ target_include_directories(${DAWN_TARGET_NAME}
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(assert)
|
||||
# add_subdirectory(display)
|
||||
add_subdirectory(display)
|
||||
# add_subdirectory(scene)
|
69
src/dawnopengl/display/BackBufferRenderTarget.cpp
Normal file
69
src/dawnopengl/display/BackBufferRenderTarget.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "dawnopengl.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
#include "assert/assertgl.hpp"
|
||||
#include "util/Flag.hpp"
|
||||
#include "BackBufferRenderTarget.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
BackBufferRenderTarget::BackBufferRenderTarget() {
|
||||
|
||||
}
|
||||
|
||||
float_t BackBufferRenderTarget::getScale() {
|
||||
return this->scale;
|
||||
}
|
||||
|
||||
float_t BackBufferRenderTarget::getWidth() {
|
||||
return this->width;
|
||||
}
|
||||
|
||||
float_t BackBufferRenderTarget::getHeight() {
|
||||
return this->height;
|
||||
}
|
||||
|
||||
void BackBufferRenderTarget::setSize(
|
||||
const float_t width,
|
||||
const float_t height
|
||||
) {
|
||||
if(this->width == width && this->height == height) return;
|
||||
|
||||
// Fixes a new bug that it seems GLFW has introduced.
|
||||
this->width = width == 0 ? 1 : width;
|
||||
this->height = height == 0 ? 1 : height;
|
||||
// this->eventRenderTargetResized.invoke(*this, width, height);
|
||||
}
|
||||
|
||||
void BackBufferRenderTarget::setClearColor(const struct Color color) {
|
||||
this->clearColor = color;
|
||||
}
|
||||
|
||||
void BackBufferRenderTarget::clear(
|
||||
const enum RenderTargetClearFlag clearFlags
|
||||
) {
|
||||
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
|
||||
assertNoGLError();
|
||||
|
||||
GLbitfield mask = 0;
|
||||
if(Flag::isOn(clearFlags, RenderTargetClearFlag::COLOR)) {
|
||||
mask |= GL_COLOR_BUFFER_BIT;
|
||||
}
|
||||
if(Flag::isOn(clearFlags, RenderTargetClearFlag::DEPTH)) {
|
||||
mask |= GL_DEPTH_BUFFER_BIT;
|
||||
}
|
||||
|
||||
glClear(mask);
|
||||
assertNoGLError();
|
||||
}
|
||||
|
||||
void BackBufferRenderTarget::bind() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
assertNoGLError();
|
||||
glViewport(0, 0, (GLsizei)this->width, (GLsizei)this->height);
|
||||
assertNoGLError();
|
||||
}
|
44
src/dawnopengl/display/BackBufferRenderTarget.hpp
Normal file
44
src/dawnopengl/display/BackBufferRenderTarget.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "display/Color.hpp"
|
||||
#include "display/RenderTarget.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class BackBufferRenderTarget final : public RenderTarget {
|
||||
private:
|
||||
float_t width = 1;
|
||||
float_t height = 1;
|
||||
struct Color clearColor = COLOR_CORNFLOWER_BLUE;
|
||||
|
||||
public:
|
||||
float_t scale = 1.0f;
|
||||
|
||||
/**
|
||||
* Construct the back buffer render target.
|
||||
*/
|
||||
BackBufferRenderTarget();
|
||||
|
||||
float_t getScale() override;
|
||||
float_t getWidth() override;
|
||||
float_t getHeight() override;
|
||||
void setClearColor(const struct Color color) override;
|
||||
void clear(const enum RenderTargetClearFlag) override;
|
||||
void bind() override;
|
||||
|
||||
/**
|
||||
* 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(const float_t width, const float_t height);
|
||||
};
|
||||
}
|
@ -4,13 +4,10 @@
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
# target_sources(${DAWN_TARGET_NAME}
|
||||
# PRIVATE
|
||||
# RenderManager.cpp
|
||||
# BackBufferRenderTarget.cpp
|
||||
# Texture.cpp
|
||||
# TextureRenderTarget.cpp
|
||||
# )
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
BackBufferRenderTarget.cpp
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(mesh)
|
@ -22,7 +22,7 @@ void Mesh::createBuffers(
|
||||
this->indiceCount = indiceCount;
|
||||
|
||||
auto sizePos = sizeof(glm::vec3) * verticeCount;
|
||||
auto sizeInds = sizeof(meshindice_t) * indiceCount;
|
||||
auto sizeInds = sizeof(int32_t) * indiceCount;
|
||||
auto sizeCoords = sizeof(glm::vec2) * verticeCount;
|
||||
|
||||
// Generate vertex array, I don't think I need to do this tbh.
|
||||
@ -160,8 +160,8 @@ void Mesh::bufferIndices(
|
||||
assertNoGLError();
|
||||
glBufferSubData(
|
||||
GL_ELEMENT_ARRAY_BUFFER,
|
||||
sizeof(meshindice_t) * pos,
|
||||
sizeof(meshindice_t) * len,
|
||||
sizeof(int32_t) * pos,
|
||||
sizeof(int32_t) * len,
|
||||
(void*)indices
|
||||
);
|
||||
assertNoGLError();
|
||||
|
Reference in New Issue
Block a user