Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
scene_component_2d.cpp
Go to the documentation of this file.
2#include <glm/gtc/matrix_transform.hpp>
3#include <glm/gtc/constants.hpp>
4
5namespace rendering_engine
6{
7
9 :
10 mPosition{ 0.0f, 0.0f },
11 mRotation{ 0.0f },
12 mScale{ 1.0f, 1.0f },
13 mWorldMatrix{ 1.0f },
14 mLocalMatrix{ 1.0f },
15 mParent(nullptr),
16 bIsDirty(true)
17{
18}
19
20void SceneComponent2D::SetPosition(const glm::vec2& position)
21{
22 mPosition = position;
23 bIsDirty = true;
24}
25
26void SceneComponent2D::SetRotation(float angleDegrees)
27{
28 mRotation = angleDegrees;
29 bIsDirty = true;
30}
31
32void SceneComponent2D::SetScale(const glm::vec2& scale)
33{
34 mScale = scale;
35 bIsDirty = true;
36}
37
39{
40 const glm::mat4& world = const_cast<SceneComponent2D*>(this)->GetWorldMatrix();
41 return glm::vec2(world[3]);
42}
43
45{
46 const glm::mat4& world = const_cast<SceneComponent2D*>(this)->GetWorldMatrix();
47
48 float angle = atan2(world[1][0], world[0][0]);
49 return glm::degrees(angle);
50}
51
53{
54 const glm::mat4& world = const_cast<SceneComponent2D*>(this)->GetWorldMatrix();
55
56 return glm::vec2(
57 glm::length(glm::vec2(world[0])),
58 glm::length(glm::vec2(world[1]))
59 );
60}
61
63{
64 if (bIsDirty)
65 {
67 }
68 return mWorldMatrix;
69}
70
72{
73 if (!bIsDirty)
74 {
75 return;
76 }
77 const float r = glm::radians(mRotation);
78
79 const glm::mat4 S = glm::scale(glm::mat4(1.0f), glm::vec3(mScale, 1.0f));
80 const glm::mat4 R = glm::rotate(glm::mat4(1.0f), r, glm::vec3(0.0f, 0.0f, 1.0f));
81 const glm::mat4 T = glm::translate(glm::mat4(1.0f), glm::vec3(mPosition, 0.0f));
82
83 mLocalMatrix = T * R * S;
84}
85
87{
89
90 if (mParent)
91 {
93 }
94 else
95 {
97 }
98 bIsDirty = false;
99}
100
102{
103 mParent = parent;
104
105 bIsDirty = true;
106}
107
108} //rendering_engine
Represents a hierarchical 2D transform component.
void UpdateLocalMatrix()
Recomputes the local transformation matrix.
SceneComponent2D()
Constructs a SceneComponent2D with identity transform.
void UpdateWorldMatrix()
Recomputes the world transformation matrix.
glm::vec2 GetWorldScale() const
Returns the accumulated world-space scale.
glm::vec2 GetWorldPosition() const
Returns the world-space position of this component.
void AttachTo(SceneComponent2D *parent)
Attaches this scene component to a parent scene component.
void SetScale(const glm::vec2 &scale)
Sets the scale in each dimension.
float GetWorldRotation() const
Returns the accumulated world-space rotation.
const glm::mat4 & GetWorldMatrix()
Returns the world transformation matrix.
void SetRotation(float angleDegrees)
Sets the rotation angle in degrees.
void SetPosition(const glm::vec2 &position)
Sets the local position of this component.