Rendering Engine 0.2.17
Modular Graphics Rendering Engine | v0.2.17
Loading...
Searching...
No Matches
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#include <cmath>
5
6namespace rendering_engine
7{
8
10 :
11 mPosition{ 0.0f, 0.0f },
12 mRotation{ 0.0f },
13 mScale{ 1.0f, 1.0f },
14 mLocalMatrix{ 1.0f },
15 mWorldMatrix{ 1.0f },
16 mParent(nullptr),
17 bIsDirty(true)
18{
19}
20
21void SceneComponent2D::SetPosition(const glm::vec2& position)
22{
23 mPosition = position;
24 bIsDirty = true;
25}
26
27void SceneComponent2D::SetRotation(float angleDegrees)
28{
29 mRotation = angleDegrees;
30 bIsDirty = true;
31}
32
33void SceneComponent2D::SetScale(const glm::vec2& scale)
34{
35 mScale = scale;
36 bIsDirty = true;
37}
38
40{
41 const glm::mat4& world = const_cast<SceneComponent2D*>(this)->GetWorldMatrix();
42 return glm::vec2(world[3]);
43}
44
46{
47 const glm::mat4& world = const_cast<SceneComponent2D*>(this)->GetWorldMatrix();
48
49 const float angle = std::atan2(world[1][0], world[0][0]);
50 return glm::degrees(angle);
51}
52
54{
55 const glm::mat4& world = const_cast<SceneComponent2D*>(this)->GetWorldMatrix();
56
57 return glm::vec2(
58 glm::length(glm::vec2(world[0])),
59 glm::length(glm::vec2(world[1]))
60 );
61}
62
64{
65 if (bIsDirty)
66 {
68 }
69 return mWorldMatrix;
70}
71
73{
74 if (!bIsDirty)
75 {
76 return;
77 }
78 const float r = glm::radians(mRotation);
79
80 const glm::mat4 S = glm::scale(glm::mat4(1.0f), glm::vec3(mScale, 1.0f));
81 const glm::mat4 R = glm::rotate(glm::mat4(1.0f), r, glm::vec3(0.0f, 0.0f, 1.0f));
82 const glm::mat4 T = glm::translate(glm::mat4(1.0f), glm::vec3(mPosition, 0.0f));
83
84 mLocalMatrix = T * R * S;
85}
86
88{
90
91 if (mParent)
92 {
94 }
95 else
96 {
98 }
99 bIsDirty = false;
100}
101
103{
104 mParent = parent;
105
106 bIsDirty = true;
107}
108
109} //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.