Improve mesh components
This commit is contained in:
@@ -5,5 +5,7 @@
|
||||
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
MeshComponent.cpp
|
||||
CubeMeshComponent.cpp
|
||||
QuadMeshComponent.cpp
|
||||
)
|
@@ -5,26 +5,24 @@
|
||||
|
||||
#include "CubeMeshComponent.hpp"
|
||||
#include "display/mesh/CubeMesh.hpp"
|
||||
#include "component/display/MeshRenderer.hpp"
|
||||
#include "util/JSON.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void CubeMeshComponent::onInit() {
|
||||
if(!mesh) mesh = std::make_shared<Mesh>();
|
||||
void CubeMeshComponent::meshBuffer(std::shared_ptr<Mesh> 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
|
||||
mesh,
|
||||
-(size/2.0f),
|
||||
size,
|
||||
0, 0
|
||||
);
|
||||
|
||||
auto renderer = getItem()->getComponent<MeshRenderer>();
|
||||
if(renderer) renderer->mesh = mesh;
|
||||
}
|
||||
|
||||
void CubeMeshComponent::onDispose() {
|
||||
mesh = nullptr;
|
||||
}
|
||||
|
||||
void CubeMeshComponent::load(std::shared_ptr<SceneLoadContext> ctx) {
|
||||
SceneComponent::load(ctx);
|
||||
if(!mesh) mesh = std::make_shared<Mesh>();
|
||||
void CubeMeshComponent::meshLoad(std::shared_ptr<SceneLoadContext> ctx) {
|
||||
if(ctx->data.contains("size")) {
|
||||
this->size = JSON::vec3(ctx->data["size"]);
|
||||
} else {
|
||||
this->size = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
}
|
||||
}
|
@@ -4,16 +4,14 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "scene/SceneItem.hpp"
|
||||
#include "display/mesh/Mesh.hpp"
|
||||
#include "MeshComponent.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class CubeMeshComponent final : public SceneComponent {
|
||||
public:
|
||||
std::shared_ptr<Mesh> mesh;
|
||||
class CubeMeshComponent final : public MeshComponent {
|
||||
protected:
|
||||
glm::vec3 size;
|
||||
|
||||
void onInit() override;
|
||||
void onDispose() override;
|
||||
void load(std::shared_ptr<SceneLoadContext> ctx) override;
|
||||
void meshLoad(std::shared_ptr<SceneLoadContext> ctx) override;
|
||||
void meshBuffer(std::shared_ptr<Mesh> mesh) override;
|
||||
};
|
||||
}
|
31
src/dawn/component/display/mesh/MeshComponent.cpp
Normal file
31
src/dawn/component/display/mesh/MeshComponent.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2024 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "MeshComponent.hpp"
|
||||
#include "component/display/MeshRenderer.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void MeshComponent::buffer() {
|
||||
if(!mesh) mesh = std::make_shared<Mesh>();
|
||||
this->meshBuffer(mesh);
|
||||
}
|
||||
|
||||
void MeshComponent::onInit() {
|
||||
this->buffer();
|
||||
|
||||
auto renderer = getItem()->getComponent<MeshRenderer>();
|
||||
if(renderer) renderer->mesh = mesh;
|
||||
}
|
||||
|
||||
void MeshComponent::onDispose() {
|
||||
mesh = nullptr;
|
||||
}
|
||||
|
||||
void MeshComponent::load(std::shared_ptr<SceneLoadContext> ctx) {
|
||||
SceneComponent::load(ctx);
|
||||
if(this->mesh == nullptr) this->mesh = std::make_shared<Mesh>();
|
||||
this->meshLoad(ctx);
|
||||
}
|
39
src/dawn/component/display/mesh/MeshComponent.hpp
Normal file
39
src/dawn/component/display/mesh/MeshComponent.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// 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 MeshComponent : public SceneComponent {
|
||||
protected:
|
||||
/**
|
||||
* Called when the mesh should be loaded.
|
||||
*
|
||||
* @param ctx Context to load the mesh from.
|
||||
*/
|
||||
virtual void meshLoad(std::shared_ptr<SceneLoadContext> ctx) = 0;
|
||||
|
||||
/**
|
||||
* Called when the mesh should be buffered.
|
||||
*
|
||||
* @param mesh Mesh to buffer.
|
||||
*/
|
||||
virtual void meshBuffer(std::shared_ptr<Mesh> mesh) = 0;
|
||||
|
||||
/**
|
||||
* Buffers the mesh.
|
||||
*/
|
||||
void buffer();
|
||||
|
||||
public:
|
||||
std::shared_ptr<Mesh> mesh;
|
||||
|
||||
void onInit() override;
|
||||
void onDispose() override;
|
||||
void load(std::shared_ptr<SceneLoadContext> ctx) override;
|
||||
};
|
||||
}
|
44
src/dawn/component/display/mesh/QuadMeshComponent.cpp
Normal file
44
src/dawn/component/display/mesh/QuadMeshComponent.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright (c) 2024 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "QuadMeshComponent.hpp"
|
||||
#include "display/mesh/QuadMesh.hpp"
|
||||
#include "util/JSON.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void QuadMeshComponent::meshBuffer(std::shared_ptr<Mesh> mesh) {
|
||||
mesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
|
||||
QuadMesh::buffer(
|
||||
mesh,
|
||||
this->positions,
|
||||
this->uvs,
|
||||
0, 0
|
||||
);
|
||||
}
|
||||
|
||||
void QuadMeshComponent::meshLoad(std::shared_ptr<SceneLoadContext> ctx) {
|
||||
if(ctx->data.contains("positions")) {
|
||||
this->positions = JSON::vec4(ctx->data["positions"]);
|
||||
} else {
|
||||
this->positions = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
if(ctx->data.contains("uvs")) {
|
||||
this->uvs = JSON::vec4(ctx->data["uvs"]);
|
||||
} else {
|
||||
this->uvs = glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void QuadMeshComponent::setPositions(const glm::vec4 &positions) {
|
||||
this->positions = positions;
|
||||
this->buffer();
|
||||
}
|
||||
|
||||
void QuadMeshComponent::setUVs(const glm::vec4 &uvs) {
|
||||
this->uvs = uvs;
|
||||
this->buffer();
|
||||
}
|
33
src/dawn/component/display/mesh/QuadMeshComponent.hpp
Normal file
33
src/dawn/component/display/mesh/QuadMeshComponent.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2024 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "MeshComponent.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class QuadMeshComponent final : public MeshComponent {
|
||||
protected:
|
||||
glm::vec4 positions;
|
||||
glm::vec4 uvs;
|
||||
|
||||
void meshLoad(std::shared_ptr<SceneLoadContext> ctx) override;
|
||||
void meshBuffer(std::shared_ptr<Mesh> mesh) override;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Sets the positions of the quad.
|
||||
*
|
||||
* @param positions The positions of the quad.
|
||||
*/
|
||||
void setPositions(const glm::vec4 &positions);
|
||||
|
||||
/**
|
||||
* Sets the UVs of the quad.
|
||||
*
|
||||
* @param uvs The UVs of the quad.
|
||||
*/
|
||||
void setUVs(const glm::vec4 &uvs);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user