Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
scene_component.cpp
Go to the documentation of this file.
1#include "scene_component.hpp"
2
3namespace rendering_engine
4{
5
7 :
8 mPosition{ 0.0f },
9 mRotation{ 1.0f, 0.0f, 0.0f, 0.0f },
10 mEulerRotation{ 0.0f },
11 mScale{ 1.0f, 1.0f, 1.0f },
12 mWorldMatrix{ 1.0f },
13 mParent(nullptr),
14 bIsDirty(true)
15{
16}
17
19{
20 if (!bIsDirty)
21 {
22 return;
23 }
24 // T * R * S
26 glm::translate(glm::mat4(1.0f), mPosition) *
27 glm::toMat4(mRotation) *
28 glm::scale(glm::mat4(1.0f), mScale);
29}
31{
33
34 if (mParent)
35 {
37 }
38 else
39 {
41 }
42 bIsDirty = false;
43}
45{
46 if (bIsDirty)
47 {
49 }
50 return mWorldMatrix;
51}
52
53void SceneComponent::SetPosition(const glm::vec3& position)
54{
55 mPosition = position;
56
57 bIsDirty = true;
58}
59
60void SceneComponent::SetRotation(const glm::quat& rotation)
61{
62 mRotation = rotation;
63 mEulerRotation = glm::eulerAngles(rotation); // radians
64
65 bIsDirty = true;
66}
67
68/*
69Pitch rotate around Right vector (0.0f, 1.0f, 0.0f)
70Roll rotate around Forward vector (1.0f, 0.0f, 0.0f)
71Yaw rotate around Up vector (0.0f, 0.0f, 1.0f)
72*/
73void SceneComponent::SetRotation(const glm::vec3& rotation)
74{
75 mEulerRotation = rotation;
76 const glm::vec3 radians = glm::radians(rotation);
77 mRotation = glm::angleAxis(-radians.y, axes::UpVector)// Yaw
78 * glm::angleAxis(-radians.x, axes::RightVector) // Pitch
79 * glm::angleAxis(-radians.z, axes::ForwardVector);// Roll
80
81 bIsDirty = true;
82}
83
84void SceneComponent::SetScale(const glm::vec3& scale)
85{
86 mScale = scale;
87
88 bIsDirty = true;
89}
90
92{
93 return glm::rotate(mRotation, axes::ForwardVector);
94}
95
96glm::vec3 SceneComponent::GetRight() const
97{
98 return glm::rotate(mRotation, axes::RightVector);
99}
100
101glm::vec3 SceneComponent::GetUp() const
102{
103 return glm::rotate(mRotation, axes::UpVector);
104}
105
107{
108 const glm::mat4& world = const_cast<SceneComponent*>(this)->GetWorldMatrix();
109 return glm::vec3(world[3]);
110}
111
113{
114 // Extract rotation from the upper-left 3x3 matrix
115 const glm::mat4& world = const_cast<SceneComponent*>(this)->GetWorldMatrix();
116 glm::mat3 rotationMatrix(world);
117 return glm::quat_cast(rotationMatrix);
118}
119
121{
122 const glm::mat4& world = const_cast<SceneComponent*>(this)->GetWorldMatrix();
123 return glm::vec3(
124 glm::length(glm::vec3(world[0])),
125 glm::length(glm::vec3(world[1])),
126 glm::length(glm::vec3(world[2]))
127 );
128}
129
131{
132 mParent = parent;
133
134 bIsDirty = true;
135}
136
137} //rendering_engine
Represents a 3D transformable scene component with position, rotation, and scale.
glm::vec3 GetRight() const
Returns the right (local Y+) direction vector in world space.
void AttachTo(SceneComponent *parent)
Attaches this scene component to a parent scene component.
void UpdateLocalMatrix()
Updates the world transformation matrix from the current position, rotation, and scale.
const glm::mat4 & GetWorldMatrix()
Returns the world transformation matrix (model matrix).
void SetRotation(const glm::quat &rotation)
Sets the rotation using a quaternion.
glm::vec3 GetWorldPosition() const
Returns the world-space position of this component.
SceneComponent()
Constructs a SceneComponent with default transform (origin, identity rotation, unit scale).
void SetPosition(const glm::vec3 &position)
Sets the position of the component in world space.
glm::vec3 GetForward() const
Returns the forward (local X+) direction vector in world space.
glm::quat GetWorldRotation() const
Returns the world-space rotation of this component.
void SetScale(const glm::vec3 &scale)
Sets the scale for each dimension.
glm::vec3 GetWorldScale() const
Returns the world-space scale of this component.
glm::vec3 GetUp() const
Returns the up (local Z+) direction vector in world space.
static const glm::vec3 UpVector
static const glm::vec3 RightVector
static const glm::vec3 ForwardVector