Example scene loading
This commit is contained in:
@@ -10,4 +10,5 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(material)
|
||||
add_subdirectory(material)
|
||||
add_subdirectory(mesh)
|
@@ -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();
|
||||
|
@@ -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
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -15,6 +15,8 @@ namespace Dawn {
|
||||
std::shared_ptr<Texture> texture;
|
||||
|
||||
public:
|
||||
void load(const SceneComponentLoadContext &ctx) override;
|
||||
|
||||
/**
|
||||
* Returns the color of this material.
|
||||
*/
|
||||
|
9
src/dawn/component/display/mesh/CMakeLists.txt
Normal file
9
src/dawn/component/display/mesh/CMakeLists.txt
Normal 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
|
||||
)
|
33
src/dawn/component/display/mesh/CubeMeshComponent.cpp
Normal file
33
src/dawn/component/display/mesh/CubeMeshComponent.cpp
Normal 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>();
|
||||
|
||||
}
|
19
src/dawn/component/display/mesh/CubeMeshComponent.hpp
Normal file
19
src/dawn/component/display/mesh/CubeMeshComponent.hpp
Normal 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;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user