Example scene loading

This commit is contained in:
2024-12-02 14:53:41 -06:00
parent ac0f0e86c5
commit 2af55041c8
48 changed files with 698 additions and 38 deletions

View File

@@ -10,4 +10,5 @@ target_sources(${DAWN_TARGET_NAME}
)
# Subdirs
add_subdirectory(material)
add_subdirectory(material)
add_subdirectory(mesh)

View File

@@ -21,6 +21,68 @@ void Camera::onDispose() {
renderTarget = nullptr;
}
void Camera::load(const SceneComponentLoadContext &ctx) {
SceneComponent::load(ctx);
if(ctx.data.contains("fov")) {
this->fov = Math::deg2rad(ctx.data["fov"].get<float_t>());
}
if(ctx.data.contains("cameraType")) {
if(
ctx.data["cameraType"] == "orthogonal" ||
ctx.data["cameraType"] == "orthographic" ||
ctx.data["cameraType"] == "ortho"
) {
this->type = CameraType::ORTHOGONAL;
} else if(ctx.data["cameraType"] == "perspective") {
this->type = CameraType::PERSPECTIVE;
} else {
assertUnreachable("Invalid Camera Type!");
}
}
if(ctx.data.contains("orthoLeft")) {
this->orthoLeft = ctx.data["orthoLeft"].get<float_t>();
} else if(ctx.data.contains("left")) {
this->orthoLeft = ctx.data["left"].get<float_t>();
}
if(ctx.data.contains("orthoRight")) {
this->orthoRight = ctx.data["orthoRight"].get<float_t>();
} else if(ctx.data.contains("right")) {
this->orthoRight = ctx.data["right"].get<float_t>();
}
if(ctx.data.contains("orthoBottom")) {
this->orthoBottom = ctx.data["orthoBottom"].get<float_t>();
} else if(ctx.data.contains("bottom")) {
this->orthoBottom = ctx.data["bottom"].get<float_t>();
}
if(ctx.data.contains("orthoTop")) {
this->orthoTop = ctx.data["orthoTop"].get<float_t>();
} else if(ctx.data.contains("top")) {
this->orthoTop = ctx.data["top"].get<float_t>();
}
if(ctx.data.contains("clipNear")) {
this->clipNear = ctx.data["clipNear"].get<float_t>();
} else if(ctx.data.contains("near")) {
this->clipNear = ctx.data["near"].get<float_t>();
} else if(ctx.data.contains("zNear")) {
this->clipNear = ctx.data["zNear"].get<float_t>();
}
if(ctx.data.contains("clipFar")) {
this->clipFar = ctx.data["clipFar"].get<float_t>();
} else if(ctx.data.contains("far")) {
this->clipFar = ctx.data["far"].get<float_t>();
} else if(ctx.data.contains("zFar")) {
this->clipFar = ctx.data["zFar"].get<float_t>();
}
}
std::shared_ptr<RenderTarget> Camera::getRenderTarget() {
if(this->renderTarget) return this->renderTarget;
return getGame()->renderHost->getBackBufferRenderTarget();

View File

@@ -33,6 +33,7 @@ namespace Dawn {
void onInit() override;
void onDispose() override;
void load(const SceneComponentLoadContext &ctx) override;
/**
* Returns the aspect ratio that the camera is using. In future I may

View File

@@ -4,9 +4,24 @@
// https://opensource.org/licenses/MIT
#include "SimpleTexturedMaterial.hpp"
#include "util/JSON.hpp"
#include "asset/loader/TextureLoader.hpp"
using namespace Dawn;
void SimpleTexturedMaterial::load(const SceneComponentLoadContext &ctx) {
if(ctx.data.contains("color")) {
this->setColor(JSON::color(ctx.data["color"]));
}
if(ctx.data.contains("texture")) {
auto asset = ctx.getAsset<TextureLoader>(
ctx.data["texture"].get<std::string>()
);
this->setTexture(asset->getTexture());
}
}
struct Color SimpleTexturedMaterial::getColor() {
return this->data.color;
}

View File

@@ -15,6 +15,8 @@ namespace Dawn {
std::shared_ptr<Texture> texture;
public:
void load(const SceneComponentLoadContext &ctx) override;
/**
* Returns the color of this material.
*/

View File

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

View File

@@ -0,0 +1,33 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "CubeMeshComponent.hpp"
#include "display/mesh/CubeMesh.hpp"
#include "component/display/MeshRenderer.hpp"
using namespace Dawn;
void CubeMeshComponent::onInit() {
if(!mesh) {
mesh = std::make_shared<Mesh>();
}
mesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(
mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1.0f, 1.0f, 1.0f), 0, 0
);
auto renderer = getItem()->getComponent<MeshRenderer>();
if(renderer) renderer->mesh = mesh;
}
void CubeMeshComponent::onDispose() {
mesh = nullptr;
}
void CubeMeshComponent::load(const SceneComponentLoadContext &ctx) {
if(!mesh) mesh = std::make_shared<Mesh>();
}

View File

@@ -0,0 +1,19 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItem.hpp"
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class CubeMeshComponent final : public SceneComponent {
public:
std::shared_ptr<Mesh> mesh;
void onInit() override;
void onDispose() override;
void load(const SceneComponentLoadContext &ctx) override;
};
}