Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
MeshComponent.cpp
Go to the documentation of this file.
1#include "scene/components/MeshComponent.h"
2
3#include "Engine.h"
4#include "Log.h"
5#include "render/Material.h"
6#include "render/Mesh.h"
7#include "render/RenderQueue.h"
8#include "scene/GameObject.h"
9
10namespace mnd
11{
12
13MeshComponent::MeshComponent(const std::shared_ptr<Material> &material, const std::shared_ptr<Mesh> &mesh)
14 : m_material(material)
15 , m_mesh(mesh)
16{
17}
18
20{
21 if (!m_material || !m_mesh)
22 {
23 const char *ownerName = GetOwner() ? GetOwner()->GetName().c_str() : "<no-owner>";
25 "MeshComponent on '%s' missing %s%s%s",
26 ownerName,
27 m_material ? "" : "material",
28 (!m_material && !m_mesh) ? " and " : "",
29 m_mesh ? "" : "mesh"
30 );
31 return;
32 }
33
34 RenderCommand command;
35 command.material = m_material.get();
36 command.mesh = m_mesh.get();
38
39 auto &renderQueue = Engine::GetInstance().GetRenderQueue();
40 renderQueue.Submit(command);
41}
42
43void MeshComponent::SetMaterial(const std::shared_ptr<Material> &material)
44{
45 m_material = material;
46}
47
48void MeshComponent::SetMesh(const std::shared_ptr<Mesh> &mesh)
49{
50 m_mesh = mesh;
51}
52
53void MeshComponent::LoadProperties(const nlohmann::json &json)
54{
55 // Material
56 if (json.contains("material"))
57 {
58 auto &matObj = json["material"];
59 const str path = matObj.value("path", "");
60 auto mat = Material::Load(path);
61 if (mat && matObj.contains("params"))
62 {
63 auto &paramsObj = matObj["params"];
64
65 // Float
66 if (paramsObj.contains("float"))
67 {
68 for (auto &param : paramsObj["float"])
69 {
70 str name = param.value("name", "");
71 f32 value = param.value("value", 0.0f);
72 mat->SetParam(name, value);
73 }
74 }
75
76 // Float 2
77 if (paramsObj.contains("float2"))
78 {
79 for (auto &param : paramsObj["float2"])
80 {
81 str name = param.value("name", "");
82 f32 v0 = param.value("value0", 0.0F);
83 f32 v1 = param.value("value1", 0.0F);
84
85 mat->SetParam(name, v0, v1);
86 }
87 }
88
89 // Float 3
90 if (paramsObj.contains("float3"))
91 {
92 for (auto &param : paramsObj["float3"])
93 {
94 str name = param.value("name", "");
95 f32 v0 = param.value("value0", 0.0F);
96 f32 v1 = param.value("value1", 0.0F);
97 f32 v2 = param.value("value2", 0.0F);
98
99 mat->SetParam(name, vec3(v0, v1, v2));
100 }
101 }
102
103 // Float 4
104 if (paramsObj.contains("float4"))
105 {
106 for (auto &param : paramsObj["float4"])
107 {
108 str name = param.value("name", "");
109 f32 v0 = param.value("value0", 0.0F);
110 f32 v1 = param.value("value1", 0.0F);
111 f32 v2 = param.value("value2", 0.0F);
112 f32 v3 = param.value("value3", 0.0F);
113
114 mat->SetParam(name, vec4(v0, v1, v2, v3));
115 }
116 }
117
118 // Textures
119 if (paramsObj.contains("textures"))
120 {
121 for (auto &param : paramsObj["textures"])
122 {
123 str name = param.value("name", "");
124 str texPath = param.value("path", "");
125 auto texture = Texture::Load(texPath);
126
127 mat->SetParam(name, texture);
128 }
129 }
130 }
131
132 SetMaterial(mat);
133 }
134
135 // Mesh
136 if (json.contains("mesh"))
137 {
138 const auto &mesh = json["mesh"];
139 const std::string type = mesh.value("type", "box");
140 if (type == "box")
141 {
142 vec3 extents(mesh.value("x", 1.0F), mesh.value("y", 1.0F), mesh.value("z", 1.0F));
143 auto mesh = Mesh::CreateBox(extents);
144 SetMesh(mesh);
145 } else if (type == "sphere")
146 {
147 f32 r = mesh.value("r", 1.0F);
148 auto mesh = Mesh::CreateSphere(r, 16, 16);
149 SetMesh(mesh);
150 }
151 }
152}
153
154} // namespace mnd
Console logging macros for all engine and game code.
#define LOG_WARN(fmt,...)
Definition Log.h:80
std::shared_ptr< Material > material
std::shared_ptr< Mesh > mesh
GameObject * GetOwner()
Definition Component.cpp:12
RenderQueue & GetRenderQueue()
Returns the per-frame command queue that batches and issues draw calls.
Definition Engine.cpp:423
static Engine & GetInstance()
Returns the single Engine instance (created on first call).
Definition Engine.cpp:59
const str & GetName() const
Returns the object's display name.
mat4 GetWorldTransform() const
Compute the world transform by walking up the parent chain.
static std::shared_ptr< Material > Load(const str &path)
Load a material from a descriptor file.
Definition Material.cpp:54
void LoadProperties(const nlohmann::json &json) override
MeshComponent()=default
Construct a MeshComponent with a mesh-material pair.
void Update(f32 deltaTime) override
Submit a RenderCommand for this object's current world transform.
void SetMesh(const std::shared_ptr< Mesh > &mesh)
void SetMaterial(const std::shared_ptr< Material > &material)
static std::shared_ptr< Mesh > CreateSphere(f32 radius, int sectors, int stacks)
Definition Mesh.cpp:322
static std::shared_ptr< Mesh > CreateBox(const glm::vec3 &extents=glm::vec3(1.0f))
Definition Mesh.cpp:209
void Submit(const RenderCommand &command)
Add a draw command to the queue for this frame.
static std::shared_ptr< Texture > Load(const std::string &path)
Load a texture from a file path using stb_image.
Definition Texture.cpp:76
glm::vec4 vec4
4-component float vector (e.g. RGBA colour, homogeneous coords).
Definition Types.h:52
glm::vec3 vec3
3-component float vector (e.g. world position, RGB colour, normals).
Definition Types.h:51
std::string str
Convenience alias for std::string.
Definition Types.h:45
float f32
32-bit IEEE float — the standard GL scalar type.
Definition Types.h:40
One draw call unit: a Mesh + Material + world-space transform.
Definition RenderQueue.h:44
mat4 modelMatrix
Object → World transform for this draw call.
Definition RenderQueue.h:47
Mesh * mesh
Geometry to draw (non-owning raw pointer).
Definition RenderQueue.h:45
Material * material
Surface definition (non-owning raw pointer).
Definition RenderQueue.h:46