Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
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) 2025 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>
20
21namespace rendering_engine
22{
23
24/**
25 * @class SceneComponent
26 * @brief Represents a 3D transformable scene component with position, rotation, and scale.
27 *
28 * SceneComponent holds the core kinematic properties required for positioning and orienting objects in a 3D scene.
29 * It provides methods to set and get position, rotation (as both quaternion and Euler angles, in radians), and scale.
30 * The world transformation matrix is updated automatically when any of these properties change.
31 * Axis direction vectors (forward, right, up) are derived from the current rotation.
32 *
33 * @note
34 * - Euler angles are expected in radians, using the convention (pitch, yaw, roll).
35 * The rendering engine uses a **left-handed** coordinate system:
36 * - **X+** : Forward (world forward direction)
37 * - **Y+** : Right
38 * - **Z+** : Up
39 * - Copy and assignment are disabled for this class.
40 */
42{
43public:
44 /**
45 * @brief Constructs a SceneComponent with default transform (origin, identity rotation, unit scale).
46 */
48
49 /**
50 * @brief Destructor.
51 */
52 virtual ~SceneComponent() = default;
53
54 /**
55 * @brief Updates the world transformation matrix from the current position, rotation, and scale.
56 * @details Normally called automatically after setters; call manually if you modify transform members directly.
57 */
58 void UpdateWorldMatrix();
59
60 /**
61 * @brief Returns the world transformation matrix (model matrix).
62 * @return The current world/model matrix (glm::mat4).
63 */
64 const glm::mat4& GetWorldMatrix() const;
65
66 /**
67 * @brief Sets the position of the component in world space.
68 * @param position The new position as a glm::vec3.
69 */
70 void SetPosition(const glm::vec3& position);
71
72 /**
73 * @brief Sets the rotation using a quaternion.
74 * @param rotation The rotation as a glm::quat.
75 * @note Automatically updates the internal Euler angles (in radians).
76 */
77 void SetRotation(const glm::quat& rotation);
78
79 /**
80 * @brief Sets the rotation using Euler angles (in degrees).
81 * @param rotation Euler angles in degrees: (pitch, yaw, roll).
82 * - pitch: rotation around the Right (Y) axis
83 * - yaw: rotation around the Up (Z) axis
84 * - roll: rotation around the Forward (X) axis
85 * @note Angles are converted internally to radians.
86 */
87 void SetRotation(const glm::vec3& rotation);
88
89 /**
90 * @brief Sets the scale for each dimension.
91 * @param scale The new scale as a glm::vec3.
92 */
93 void SetScale(const glm::vec3& scale);
94
95 /**
96 * @brief Gets the current position.
97 * @return The position as a glm::vec3.
98 */
99 const glm::vec3& GetPosition() const { return mPosition; }
100
101 /**
102 * @brief Gets the current rotation as a quaternion.
103 * @return The rotation as a glm::quat.
104 */
105 const glm::quat& GetRotationQuat() const { return mRotation; }
106
107 /**
108 * @brief Gets the current rotation as Euler angles (in radians).
109 * @return Euler angles (pitch, yaw, roll), where:
110 * - pitch: rotation around the Right (Y) axis
111 * - yaw: rotation around the Up (Z) axis
112 * - roll: rotation around the Forward (X) axis
113 */
114 const glm::vec3& GetRotation() const { return mEulerRotation; }
115
116 /**
117 * @brief Gets the current scale.
118 * @return The scale as a glm::vec3.
119 */
120 const glm::vec3& GetScale() const { return mScale; }
121
122 /**
123 * @brief Returns the forward (local X+) direction vector in world space.
124 * @return The forward direction as a glm::vec3.
125 * @see rendering_engine::axes::ForwardVector
126 */
127 glm::vec3 GetForward() const;
128
129 /**
130 * @brief Returns the right (local Y+) direction vector in world space.
131 * @return The right direction as a glm::vec3.
132 * @see rendering_engine::axes::RightVector
133 */
134 glm::vec3 GetRight() const;
135
136 /**
137 * @brief Returns the up (local Z+) direction vector in world space.
138 * @return The up direction as a glm::vec3.
139 * @see rendering_engine::axes::UpVector
140 */
141 glm::vec3 GetUp() const;
142
143 SceneComponent(const SceneComponent& rhs) = delete;
145
146protected:
147 glm::vec3 mPosition;
148 glm::quat mRotation;
149 glm::vec3 mEulerRotation;
150 glm::vec3 mScale;
151
152 glm::mat4 mWorldMatrix;
153};
154
155} //rendering_engine
glm::vec3 GetRight() const
Returns the right (local Y+) direction vector in world space.
const glm::vec3 & GetPosition() const
Gets the current position.
SceneComponent(const SceneComponent &rhs)=delete
void UpdateWorldMatrix()
Updates the world transformation matrix from the current position, rotation, and scale.
const glm::vec3 & GetRotation() const
Gets the current rotation as Euler angles (in radians).
const glm::mat4 & GetWorldMatrix() const
Returns the world transformation matrix (model matrix).
virtual ~SceneComponent()=default
Destructor.
void SetRotation(const glm::quat &rotation)
Sets the rotation using a quaternion.
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.
const glm::vec3 & GetScale() const
Gets the current scale.
glm::vec3 GetForward() const
Returns the forward (local X+) direction vector in world space.
SceneComponent & operator=(const SceneComponent &rhs)=delete
void SetScale(const glm::vec3 &scale)
Sets the scale for each dimension.
const glm::quat & GetRotationQuat() const
Gets the current rotation as a quaternion.
glm::vec3 GetUp() const
Returns the up (local Z+) direction vector in world space.