Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
scene_components.hpp
Go to the documentation of this file.
1
11
12#ifndef FOOLSENGINE_SCENECOMPONENTS_H
13#define FOOLSENGINE_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 {
22
30 struct Placement {
35 glm::vec4 mPosition {glm::vec3{ 0.f }, 1.f};
36
43 glm::quat mOrientation { glm::vec3{ 0.f } };
44
49 glm::vec3 mScale { 1.f, 1.f, 1.f };
50
56 inline static std::string getComponentTypeName() { return "Placement"; }
57
65 inline bool operator==(const Placement& other) {
66 return (
67 mPosition == other.mPosition
68 && mOrientation == other.mOrientation
69 && mScale == other.mScale
70 );
71 }
72
80 inline bool operator!=(const Placement& other) {
81 return !(*this == other);
82 }
83 };
84
94 struct Transform {
99 glm::mat4 mModelMatrix {1.f};
100
106 inline static std::string getComponentTypeName() { return "Transform"; }
107 };
108
126
134
142
148 inline static std::string getComponentTypeName() { return "SceneHierarchyData"; }
149 };
150
152 inline void from_json(const nlohmann::json& json, Placement& placement) {
153 assert(json.at("type").get<std::string>() == Placement::getComponentTypeName() && "Type mismatch. Component json must have type Placement");
154 json.at("position")[0].get_to(placement.mPosition.x);
155 json.at("position")[1].get_to(placement.mPosition.y);
156 json.at("position")[2].get_to(placement.mPosition.z);
157 json.at("position")[3].get_to(placement.mPosition.w);
158
159 json.at("orientation")[0].get_to(placement.mOrientation.w);
160 json.at("orientation")[1].get_to(placement.mOrientation.x);
161 json.at("orientation")[2].get_to(placement.mOrientation.y);
162 json.at("orientation")[3].get_to(placement.mOrientation.z);
163 placement.mOrientation = glm::normalize(placement.mOrientation);
164
165 json.at("scale")[0].get_to(placement.mScale.x);
166 json.at("scale")[1].get_to(placement.mScale.y);
167 json.at("scale")[2].get_to(placement.mScale.z);
168 }
169
171 inline void to_json(nlohmann::json& json, const Placement& placement) {
172 json = {
174 {"position", {
175 placement.mPosition.x,
176 placement.mPosition.y,
177 placement.mPosition.z,
178 placement.mPosition.w,
179 }},
180 {"orientation", {
181 placement.mOrientation.w,
182 placement.mOrientation.x,
183 placement.mOrientation.y,
184 placement.mOrientation.z,
185 }},
186 {"scale", {
187 placement.mScale.x,
188 placement.mScale.y,
189 placement.mScale.z,
190 }}
191 };
192 }
193
195 inline void to_json(nlohmann::json& json, const SceneHierarchyData& sceneHierarchyData) {
196 (void)json; (void)sceneHierarchyData; // prevent unused parameter warnings
197 }
198
200 inline void from_json(const nlohmann::json& json, SceneHierarchyData& sceneHierarchyData) {
201 (void)json; (void)sceneHierarchyData; // prevent unused parameter warnings
202 }
203
205 inline void to_json(nlohmann::json& json, const Transform& transform) {
206 (void)transform; // prevent unused parameter warnings
207 json = {
209 };
210 }
211
213 inline void from_json(const nlohmann::json& json, Transform& transform) {
214 assert(json.at("type") == Transform::getComponentTypeName() && "Type mismatch. Component json must have type Transform");
215 transform.mModelMatrix = glm::mat4{1.f};
216 }
217
230 template<>
232 const Placement& previousState, const Placement& nextState,
233 float simulationProgress
234 ) const {
235 simulationProgress = mProgressLimits(simulationProgress);
236 return {
237 .mPosition{ (1.f - simulationProgress) * previousState.mPosition + simulationProgress * nextState.mPosition },
238 .mOrientation{ glm::slerp(previousState.mOrientation, nextState.mOrientation, simulationProgress) },
239 .mScale{ (1.f - simulationProgress) * previousState.mScale + simulationProgress * nextState.mScale }
240 };
241 }
242
255 template<>
257 const Transform& previousState, const Transform& nextState,
258 float simulationProgress
259 ) const {
260 simulationProgress = mProgressLimits(simulationProgress);
261 return {
262 previousState.mModelMatrix * (1.f - simulationProgress)
263 + nextState.mModelMatrix * (simulationProgress)
264 };
265 }
266
279 template <>
281 const SceneHierarchyData& previousState, const SceneHierarchyData& nextState,
282 float simulationProgress
283 ) const {
284 (void)previousState; // prevent unused parameter warnings
285 (void)simulationProgress; // prevent unused parameter warnings
286 return nextState;
287 }
288
289}
290
291#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.
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:2346
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
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
A component representing the position, rotation, and scale of an entity.
Definition scene_components.hpp:30
bool operator!=(const Placement &other)
An explicit specification for the inequality operator.
Definition scene_components.hpp:80
bool operator==(const Placement &other)
Compares this placement object to another for equality.
Definition scene_components.hpp:65
glm::quat mOrientation
This entity's orientation, as a glm::quat value.
Definition scene_components.hpp:43
static std::string getComponentTypeName()
Gets the component type string for this component.
Definition scene_components.hpp:56
glm::vec4 mPosition
This entity's position.
Definition scene_components.hpp:35
glm::vec3 mScale
Factors along each axis by which geometry should be multiplied and scaled.
Definition scene_components.hpp:49
Component representing hierarchical information related to this entity.
Definition scene_components.hpp:118
static std::string getComponentTypeName()
Gets the component type string associated with this object.
Definition scene_components.hpp:148
EntityID mSibling
The entityID of this entity's next sibling.
Definition scene_components.hpp:133
EntityID mParent
The entityID of this entity's parent.
Definition scene_components.hpp:125
EntityID mChild
The first child of this entity.
Definition scene_components.hpp:141
The transform component, which moves the vertices of a model to their world space coordinates during ...
Definition scene_components.hpp:94
glm::mat4 mModelMatrix
The actual currently cached model matrix for this entity.
Definition scene_components.hpp:99
static std::string getComponentTypeName()
Gets the component type string for this object.
Definition scene_components.hpp:106