Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
scene_component.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
8#define GLM_FORCE_RADIANS
9#define GLM_FORCE_DEPTH_ZERO_TO_ONE
10#define GLM_ENABLE_EXPERIMENTAL
11#include <glm/vec4.hpp>
12#include <glm/mat4x4.hpp>
13#include <glm/glm.hpp>
14#include <glm/gtc/quaternion.hpp>
15#include <glm/gtx/quaternion.hpp>
16#include <glm/gtc/matrix_transform.hpp>
17#include <glm/gtc/matrix_inverse.hpp>
18#include <memory>
21
22namespace rendering_engine
23{
24
25/**
26 * @class SceneComponent
27 * @brief Represents a 3D transformable scene component with position, rotation, and scale.
28 *
29 * SceneComponent holds the core kinematic properties required for positioning and orienting objects in a 3D scene.
30 * It provides methods to set and get position, rotation (as both quaternion and Euler angles, in radians), and scale.
31 * The world transformation matrix is updated automatically when any of these properties change.
32 * Axis direction vectors (forward, right, up) are derived from the current rotation.
33 *
34 * @note
35 * - Euler angles are expected in radians, using the convention (pitch, yaw, roll).
36 * The rendering engine uses a **left-handed** coordinate system:
37 * - **X+** : Forward (world forward direction)
38 * - **Y+** : Right
39 * - **Z+** : Up
40 * - Copy and assignment are disabled for this class.
41 */
43{
44public:
45 /**
46 * @brief Constructs a SceneComponent with default transform (origin, identity rotation, unit scale).
47 */
49
50 /**
51 * @brief Destructor.
52 */
53 virtual ~SceneComponent() = default;
54
55 /**
56 * @brief Updates the world transformation matrix from the current position, rotation, and scale.
57 * @details Normally called automatically after setters; call manually if you modify transform members directly.
58 */
59 void UpdateLocalMatrix();
60
61 void UpdateWorldMatrix();
62
63 /**
64 * @brief Returns the world transformation matrix (model matrix).
65 * @return The current world/model matrix (glm::mat4).
66 */
67 const glm::mat4& GetWorldMatrix();
68
69 /**
70 * @brief Sets the position of the component in world space.
71 * @param position The new position as a glm::vec3.
72 */
73 void SetPosition(const glm::vec3& position);
74
75 /**
76 * @brief Sets the rotation using a quaternion.
77 * @param rotation The rotation as a glm::quat.
78 * @note Automatically updates the internal Euler angles (in radians).
79 */
80 void SetRotation(const glm::quat& rotation);
81
82 /**
83 * @brief Sets the rotation using Euler angles (in degrees).
84 * @param rotation Euler angles in degrees: (pitch, yaw, roll).
85 * - pitch: rotation around the Right (Y) axis
86 * - yaw: rotation around the Up (Z) axis
87 * - roll: rotation around the Forward (X) axis
88 * @note Angles are converted internally to radians.
89 */
90 void SetRotation(const glm::vec3& rotation);
91
92 /**
93 * @brief Sets the scale for each dimension.
94 * @param scale The new scale as a glm::vec3.
95 */
96 void SetScale(const glm::vec3& scale);
97
98 /**
99 * @brief Gets the current position.
100 * @return The position as a glm::vec3.
101 */
102 const glm::vec3& GetPosition() const { return mPosition; }
103
104 /**
105 * @brief Gets the current rotation as a quaternion.
106 * @return The rotation as a glm::quat.
107 */
108 const glm::quat& GetRotationQuat() const { return mRotation; }
109
110 /**
111 * @brief Gets the current rotation as Euler angles (in radians).
112 * @return Euler angles (pitch, yaw, roll), where:
113 * - pitch: rotation around the Right (Y) axis
114 * - yaw: rotation around the Up (Z) axis
115 * - roll: rotation around the Forward (X) axis
116 */
117 const glm::vec3& GetRotation() const { return mEulerRotation; }
118
119 /**
120 * @brief Gets the current scale.
121 * @return The scale as a glm::vec3.
122 */
123 const glm::vec3& GetScale() const { return mScale; }
124
125 /**
126 * @brief Returns the forward (local X+) direction vector in world space.
127 * @return The forward direction as a glm::vec3.
128 * @see rendering_engine::axes::ForwardVector
129 */
130 glm::vec3 GetForward() const;
131
132 /**
133 * @brief Returns the right (local Y+) direction vector in world space.
134 * @return The right direction as a glm::vec3.
135 * @see rendering_engine::axes::RightVector
136 */
137 glm::vec3 GetRight() const;
138
139 /**
140 * @brief Returns the up (local Z+) direction vector in world space.
141 * @return The up direction as a glm::vec3.
142 * @see rendering_engine::axes::UpVector
143 */
144 glm::vec3 GetUp() const;
145
146 /**
147 * @brief Returns the world-space position of this component.
148 * @return World-space position extracted from the world matrix.
149 */
150 glm::vec3 GetWorldPosition() const;
151
152 /**
153 * @brief Returns the world-space rotation of this component.
154 * @return World-space rotation as a quaternion.
155 *
156 * @note Result is reliable for uniform scale.
157 */
158 glm::quat GetWorldRotation() const;
159
160 /**
161 * @brief Returns the world-space scale of this component.
162 * @return World-space scale extracted from the world matrix.
163 */
164 glm::vec3 GetWorldScale() const;
165
166 /**
167 * @brief Attaches this scene component to a parent scene component.
168 *
169 * After attachment, this component's transform becomes relative to the parent.
170 * The world transformation is computed by combining the parent's world transform
171 * with this component's local transform.
172 *
173 * Passing nullptr detaches the component and makes it a root-level component.
174 *
175 * @param parent Pointer to the parent SceneComponent, or nullptr to detach.
176 */
177 void AttachTo(SceneComponent* parent);
178
179 SceneComponent(const SceneComponent& rhs) = delete;
181
182protected:
183 glm::vec3 mPosition;
184 glm::quat mRotation;
185 glm::vec3 mEulerRotation;
186 glm::vec3 mScale;
187
188 glm::mat4 mLocalMatrix;
189 glm::mat4 mWorldMatrix;
190
193};
194
195} //rendering_engine
Represents a 3D transformable scene component with position, rotation, and scale.
const glm::vec3 & GetPosition() const
Gets the current position.
SceneComponent(const SceneComponent &rhs)=delete
const glm::vec3 & GetRotation() const
Gets the current rotation as Euler angles (in radians).
virtual ~SceneComponent()=default
Destructor.
const glm::vec3 & GetScale() const
Gets the current scale.
SceneComponent & operator=(const SceneComponent &rhs)=delete
const glm::quat & GetRotationQuat() const
Gets the current rotation as a quaternion.
#define RE_API