ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
scene_components.hpp
Go to the documentation of this file.
1
11
12#ifndef TOYMAKERENGINE_SCENECOMPONENTS_H
13#define TOYMAKERENGINE_SCENECOMPONENTS_H
14
15#include <glm/glm.hpp>
16#include <glm/gtc/quaternion.hpp>
17#include <nlohmann/json.hpp>
18
19#include "core/ecs_world.hpp"
20
21namespace ToyMaker {
31 using TransformComponentSet = uint8_t;
32
48
56 enum class TransformInheritMode: uint8_t {
64 };
65
73 struct Placement {
78 glm::vec4 mPosition {glm::vec3{ 0.f }, 1.f};
79
86 glm::quat mOrientation { glm::vec3{ 0.f } };
87
92 glm::vec3 mScale { 1.f, 1.f, 1.f };
93
100
106
112 inline static std::string getComponentTypeName() { return "Placement"; }
113
121 inline bool operator==(const Placement& other) {
122 return (
123 mPosition == other.mPosition
124 && mOrientation == other.mOrientation
125 && mScale == other.mScale
126 );
127 }
128
136 inline bool operator!=(const Placement& other) {
137 return !(*this == other);
138 }
139 };
140
150 struct Transform {
155 glm::mat4 mModelMatrix { 1.f };
156
163
169
175 inline static std::string getComponentTypeName() { return "Transform"; }
176
181 inline glm::mat4 getMatrixTranslation() const {
183 }
184
189 inline glm::mat4 getMatrixRotation() const {
191 }
192
197 inline glm::mat4 getMatrixScale() const {
199 }
200 };
201
202
220
228
236
242 inline static std::string getComponentTypeName() { return "SceneHierarchyData"; }
243 };
244
246 NLOHMANN_JSON_SERIALIZE_ENUM(TransformComponentOption, {
250 });
251
253 NLOHMANN_JSON_SERIALIZE_ENUM(TransformInheritMode, {
254 { TransformInheritMode::PARENT, "parent" },
255 { TransformInheritMode::GLOBAL, "global" },
256 { TransformInheritMode::CAMERA, "camera" },
257 });
258
260 inline void from_json(const nlohmann::json& json, Placement& placement) {
261 assert(json.at("type").get<std::string>() == Placement::getComponentTypeName() && "Type mismatch. Component json must have type Placement");
262
263 // Initialize the placement component properly if not done already
264 placement = Placement {};
265
266 if(json.contains("inherit_mode")) {
267 placement.mInheritMode = json.at("inherit_mode");
268 }
269
270 if(json.contains("inherit_components") && json.at("inherit_components").is_array()) {
271 placement.mInheritedComponents = 0x0;
272 for(const auto& component: json.at("inherit_components")) {
273 placement.mInheritedComponents |= component.get<TransformComponentOption>();
274 }
275 }
276
277 json.at("position")[0].get_to(placement.mPosition.x);
278 json.at("position")[1].get_to(placement.mPosition.y);
279 json.at("position")[2].get_to(placement.mPosition.z);
280 json.at("position")[3].get_to(placement.mPosition.w);
281
282 json.at("orientation")[0].get_to(placement.mOrientation.w);
283 json.at("orientation")[1].get_to(placement.mOrientation.x);
284 json.at("orientation")[2].get_to(placement.mOrientation.y);
285 json.at("orientation")[3].get_to(placement.mOrientation.z);
286 placement.mOrientation = glm::normalize(placement.mOrientation);
287
288 json.at("scale")[0].get_to(placement.mScale.x);
289 json.at("scale")[1].get_to(placement.mScale.y);
290 json.at("scale")[2].get_to(placement.mScale.z);
291 }
292
294 inline void to_json(nlohmann::json& json, const Placement& placement) {
295 std::vector<nlohmann::json> inheritComponentList {};
296 if(placement.mInheritedComponents & TRANSFORMCOMPONENT_TRANSLATION) {
297 const nlohmann::json translationComponent = TRANSFORMCOMPONENT_TRANSLATION;
298 inheritComponentList.push_back(translationComponent);
299 }
300 if(placement.mInheritedComponents & TRANSFORMCOMPONENT_ROTATION) {
301 const nlohmann::json rotationComponent = TRANSFORMCOMPONENT_ROTATION;
302 inheritComponentList.push_back(rotationComponent);
303 }
304 if(placement.mInheritedComponents & TRANSFORMCOMPONENT_SCALE) {
305 const nlohmann::json scaleComponent = TRANSFORMCOMPONENT_SCALE;
306 inheritComponentList.push_back(scaleComponent);
307 }
308 json = {
310 {"inherit_mode", placement.mInheritMode},
311 {"inherit_components", inheritComponentList},
312 {"position", {
313 placement.mPosition.x,
314 placement.mPosition.y,
315 placement.mPosition.z,
316 placement.mPosition.w,
317 }},
318 {"orientation", {
319 placement.mOrientation.w,
320 placement.mOrientation.x,
321 placement.mOrientation.y,
322 placement.mOrientation.z,
323 }},
324 {"scale", {
325 placement.mScale.x,
326 placement.mScale.y,
327 placement.mScale.z,
328 }}
329 };
330 }
331
333 inline void to_json(nlohmann::json& json, const SceneHierarchyData& sceneHierarchyData) {
334 (void)json; (void)sceneHierarchyData; // prevent unused parameter warnings
335 }
336
338 inline void from_json(const nlohmann::json& json, SceneHierarchyData& sceneHierarchyData) {
339 (void)json; (void)sceneHierarchyData; // prevent unused parameter warnings
340 }
341
343 inline void to_json(nlohmann::json& json, const Transform& transform) {
344 (void)transform; // prevent unused parameter warnings
345 json = {
347 };
348 }
349
351 inline void from_json(const nlohmann::json& json, Transform& transform) {
352 assert(json.at("type") == Transform::getComponentTypeName() && "Type mismatch. Component json must have type Transform");
353 transform.mModelMatrix = glm::mat4{1.f};
354 }
355
368 template<>
370 const Placement& previousState, const Placement& nextState,
371 float simulationProgress
372 ) const {
373 simulationProgress = mProgressLimits(simulationProgress);
374 return {
375 .mPosition{ (1.f - simulationProgress) * previousState.mPosition + simulationProgress * nextState.mPosition },
376 .mOrientation{ glm::slerp(previousState.mOrientation, nextState.mOrientation, simulationProgress) },
377 .mScale{ (1.f - simulationProgress) * previousState.mScale + simulationProgress * nextState.mScale }
378 };
379 }
380
393 template<>
395 const Transform& previousState, const Transform& nextState,
396 float simulationProgress
397 ) const {
398 simulationProgress = mProgressLimits(simulationProgress);
399 return {
400 previousState.mModelMatrix * (1.f - simulationProgress)
401 + nextState.mModelMatrix * (simulationProgress)
402 };
403 }
404
417 template <>
419 const SceneHierarchyData& previousState, const SceneHierarchyData& nextState,
420 float simulationProgress
421 ) const {
422 (void)previousState; // prevent unused parameter warnings
423 (void)simulationProgress; // prevent unused parameter warnings
424 return nextState;
425 }
426}
427
428#endif
RangeMapperLinear mProgressLimits
a functor that performs the actual interpolation in the default case
Definition ecs_world.hpp:354
ToyMaker Engine's implementation of an ECS system.
glm::mat4 getScaleMatrix(const glm::mat4 &fromTransform)
Given a transform, returns scale matrix that was used to compose the transform.
Definition util.cpp:43
glm::mat4 getRotationMatrix(const glm::mat4 &fromTransform)
Given a transform, returns rotation matrix that was used to compose the transform.
Definition util.cpp:52
glm::mat4 getTranslationMatrix(const glm::mat4 &fromTransform)
Given a transform, returns translation matrix that was used to compose the transform.
Definition util.cpp:58
T operator()(const T &previousState, const T &nextState, float simulationProgress=1.f) const
Returns an interpolated value for a component between two given states.
Definition ecs_world.hpp:2380
constexpr EntityID kMaxEntities
A user-set constant which limits the number of creatable entities in a single ECS system.
Definition ecs_world.hpp:119
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:68
uint8_t TransformComponentSet
Field indicating which transform components this object would like to inherit from its parent in the ...
Definition scene_components.hpp:31
TransformComponentOption
The different kinds of values that can be inherited from a transform belonging to a parent.
Definition scene_components.hpp:40
TransformInheritMode
The different kinds of values that can be inherited from a transform belonging to a parent.
Definition scene_components.hpp:56
@ TRANSFORMCOMPONENT_ROTATION
Rotation is inherited from this object's parent.
Definition scene_components.hpp:44
@ TRANSFORMCOMPONENT_SCALE
Scale is inherited from this object's parent.
Definition scene_components.hpp:46
@ TRANSFORMCOMPONENT_TRANSLATION
Translation is inherited from this objects parent.
Definition scene_components.hpp:42
@ PARENT
Transform components are inherited from this object's parent.
Definition scene_components.hpp:58
@ GLOBAL
Definition scene_components.hpp:61
@ CAMERA
This object's transform is specified relative to the view matrix of the active camera in its ECS worl...
Definition scene_components.hpp:63
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:25
A component representing the position, rotation, and scale of an entity.
Definition scene_components.hpp:73
bool operator!=(const Placement &other)
An explicit specification for the inequality operator.
Definition scene_components.hpp:136
bool operator==(const Placement &other)
Compares this placement object to another for equality.
Definition scene_components.hpp:121
glm::quat mOrientation
This entity's orientation, as a glm::quat value.
Definition scene_components.hpp:86
static std::string getComponentTypeName()
Gets the component type string for this component.
Definition scene_components.hpp:112
TransformInheritMode mInheritMode
Field specifying which coordinate system this object's transform is specified relative to.
Definition scene_components.hpp:105
glm::vec4 mPosition
This entity's position.
Definition scene_components.hpp:78
glm::vec3 mScale
Factors along each axis by which geometry should be multiplied and scaled.
Definition scene_components.hpp:92
TransformComponentSet mInheritedComponents
Field specifying which aspects of its parent's transform this object's transform is specified relativ...
Definition scene_components.hpp:99
Component representing hierarchical information related to this entity.
Definition scene_components.hpp:212
static std::string getComponentTypeName()
Gets the component type string associated with this object.
Definition scene_components.hpp:242
EntityID mSibling
The entityID of this entity's next sibling.
Definition scene_components.hpp:227
EntityID mParent
The entityID of this entity's parent.
Definition scene_components.hpp:219
EntityID mChild
The first child of this entity.
Definition scene_components.hpp:235
The transform component, which moves the vertices of a model to their world space coordinates during ...
Definition scene_components.hpp:150
TransformInheritMode mInheritMode
Field specifying which coordinate system this object's transform is specified relative to.
Definition scene_components.hpp:168
glm::mat4 getMatrixRotation() const
Isolates the world-relative rotation matrix associated with this transform.
Definition scene_components.hpp:189
glm::mat4 mModelMatrix
The actual currently cached model matrix for this entity.
Definition scene_components.hpp:155
glm::mat4 getMatrixScale() const
Isolates the world-relative scale matrix associated with this transform.
Definition scene_components.hpp:197
static std::string getComponentTypeName()
Gets the component type string for this object.
Definition scene_components.hpp:175
TransformComponentSet mInheritedComponents
Field specifying which aspects of its parent's transform this object's transform is specified relativ...
Definition scene_components.hpp:162
glm::mat4 getMatrixTranslation() const
Isolates the world-relative translation matrix associated with this transform.
Definition scene_components.hpp:181