SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
TransformComponent.cpp
Go to the documentation of this file.
5#include <Camera/Camera.hpp>
8
9namespace Sleak {
10
11 // Constructors
13
18
28
33
49
50 void TransformComponent::Update(float DeltaTime) {
51 UpdateConstantBuffer();
52
54 }
55
56 // Transformation matrix calculation
58 return Transform->CalculateTransformMatrix();
59 }
60
61 // Transformations
62 void TransformComponent::Translate(const Vector3D& translation) {
63 position += translation;
65 UpdateTransform();
66 }
67
68
69 void TransformComponent::Translate(float x, float y, float z) {
70 Translate(Vector3D(x,y,z));
71 }
72
74 this->rotation *= rotation;
75 UpdateTransform();
76 }
77
78 void TransformComponent::Scale(float amount) {
79 Scale(Vector3D(amount, amount, amount));
80 }
81
83 this->scale = scale * this->scale;
84 UpdateTransform();
85 }
86
87 // Setters
89 this->position = position;
90 }
91
93 this->rotation = rotation;
94 }
95
97 this->scale = scale;
98 }
99
100 // Direction vectors
102 return rotation * Vector3D(0.0f, 0.0f, 1.0f);
103 }
104
106 return rotation * Vector3D(1.0f, 0.0f, 0.0f);
107 }
108
110 return rotation * Vector3D(0.0f, 1.0f, 0.0f);
111 }
112
113 // Rotation utilities
114 void TransformComponent::RotateAround(const Vector3D& axis, float angle) {
115 Rotate(Quaternion(axis, angle));
116 }
117
118 void TransformComponent::RotateAroundLocal(const Vector3D& axis, float angle) {
119 Rotate(Quaternion(rotation * axis, angle));
120 }
121
123 Vector3D direction = (target - position).Normalize();
126 }
127
128 // Getters
132
136
140
141 void TransformComponent::UpdateTransform() {
145
147 }
148
149
150 void TransformComponent::UpdateConstantBuffer() {
151 if (!ConstantBuffer) {
152 SLEAK_ERROR("Constant buffer is not found to update for object {}", owner->GetName());
153 return;
154 }
155
156 UpdateTransform();
157
158 auto world = GetTransformMatrix();
159 RenderEngine::TransformBuffer tb(world,
162
163 RenderEngine::RenderCommandQueue::GetInstance()->SubmitUpdateConstantBuffer(ConstantBuffer, tb.GetData(), tb.GetSize());
164 }
165
166
167 Math::Matrix4 TransformComponent::GetMVP() {
168 auto mat = GetTransformMatrix() *
171
172 return mat;
173 }
174
175}
#define SLEAK_ERROR(...)
Definition Logger.hpp:22
#define VECTOR_Up
Definition Vector.hpp:13
static const Math::Matrix4 & GetMainProjectionMatrix()
Definition Camera.hpp:107
static const Math::Matrix4 & GetMainViewMatrix()
Definition Camera.hpp:103
GameObject * owner
Definition Component.hpp:81
Component(GameObject *object)
Definition Component.hpp:61
static Matrix< float, 4, 4 > Rotate(const Quaternion &rotation)
Definition Matrix.hpp:380
static Matrix< float, 4, 4 > Scale(const Vector3D &scale)
Definition Matrix.hpp:385
static Matrix< float, 4, 4 > Translate(const Vector3D &translation)
Definition Matrix.hpp:368
Represents a quaternion for 3D rotations.
static Quaternion LookRotation(Vector3D &forward, Vector3D &up)
const std::string & GetName() const
Definition Object.hpp:25
void SubmitUpdateConstantBuffer(RefPtr< BufferBase > buffer, void *Data, uint16_t Size)
Queues a constant buffer write with data captured at submit time.
static RenderCommandQueue * GetInstance()
Lazily creates and returns the process-wide singleton instance.
void SubmitBindConstantBuffer(RefPtr< BufferBase > buffer, uint8_t slot)
Queues a constant buffer bind at the given slot.
static BufferBase * CreateBuffer(BufferType Type, uint32_t Size, void *Data)
Creates a buffer via the currently registered backend factory.
void SetRotation(const Quaternion &rotation)
void RotateAround(const Vector3D &axis, float angle)
Rotates by angle around axis in world space.
void SetPosition(const Vector3D &position)
TransformComponent(GameObject *object, const Vector3D &position)
void RotateAroundLocal(const Vector3D &axis, float angle)
Rotates by angle around axis expressed in the object's local space.
void Translate(const Vector3D &translation)
Adds translation to position and refreshes the cached matrix.
void Update(float DeltaTime) override
Refreshes and rebinds the transform constant buffer for this frame.
void SetScale(const Vector3D &scale)
bool Initialize() override
Allocates the transform's GPU constant buffer.
void Scale(float amount)
Uniformly scales by amount along all axes.
void Rotate(const Quaternion &rotation)
Composes rotation onto the current orientation.
Quaternion GetWorldRotation() const
void LookAt(const Vector3D &target)
Orients the transform to face target, using world up as reference.
Matrix< float, 4, 4 > Matrix4
Definition Matrix.hpp:413
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
Per-draw world-view-projection and world matrices.
virtual uint16_t GetSize() const override
virtual void * GetData() const override
Cached translation/rotation/scale matrices combined into the final transform matrix.