PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
transform.h
1#pragma once
2
3#define GLM_ENABLE_EXPERIMENTAL
4#include "pixelbullet/serialization/glm_node.h"
5
6#include <glm/glm.hpp>
7#include <glm/gtc/matrix_transform.hpp>
8#include <glm/gtx/matrix_decompose.hpp>
9#include <glm/gtx/quaternion.hpp>
10
11namespace pixelbullet
12{
13struct Transform
14{
15 glm::vec3 position = glm::vec3(0.0f);
16 glm::vec3 rotation = glm::vec3(0.0f);
17 glm::vec3 scale = glm::vec3(1.0f);
18
19 Transform() = default;
20 Transform(const glm::vec3& pos)
21 : position(pos)
22 {
23 }
24 Transform(const glm::vec3& pos, const glm::vec3& rot, const glm::vec3& scl)
25 : position(pos)
26 , rotation(rot)
27 , scale(scl)
28 {
29 }
30
31 [[nodiscard]] glm::mat4 GetMatrix() const
32 {
33 glm::mat4 rotMat = glm::toMat4(glm::quat(glm::radians(rotation)));
34 return glm::translate(glm::mat4(1.0f), position) * rotMat * glm::scale(glm::mat4(1.0f), scale);
35 }
36};
37
38inline bool TryDecomposeTransform(const glm::mat4& matrix, Transform& transform)
39{
40 glm::vec3 scale(1.0f);
41 glm::quat orientation = glm::quat(glm::vec3(0.0f));
42 glm::vec3 translation(0.0f);
43 glm::vec3 skew(0.0f);
44 glm::vec4 perspective(0.0f);
45
46 if (!glm::decompose(matrix, scale, orientation, translation, skew, perspective))
47 {
48 return false;
49 }
50
51 orientation = glm::normalize(orientation);
52 transform.position = translation;
53 transform.rotation = glm::degrees(glm::eulerAngles(orientation));
54 transform.scale = scale;
55 return true;
56}
57
58inline bool operator==(const Transform& lhs, const Transform& rhs) noexcept
59{
60 return glm::all(glm::equal(lhs.position, rhs.position)) && glm::all(glm::equal(lhs.rotation, rhs.rotation)) &&
61 glm::all(glm::equal(lhs.scale, rhs.scale));
62}
63
64inline bool operator!=(const Transform& lhs, const Transform& rhs) noexcept
65{
66 return !(lhs == rhs);
67}
68
69inline Node& operator<<(Node& node, const Transform& t)
70{
71 node["position"] << t.position;
72 node["rotation"] << t.rotation;
73 node["scale"] << t.scale;
74 return node;
75}
76
77inline const Node& operator>>(const Node& node, Transform& t)
78{
79 node["position"] >> t.position;
80 node["rotation"] >> t.rotation;
81 node["scale"] >> t.scale;
82 return node;
83}
84} // namespace pixelbullet
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:49
Definition transform.h:14