SleakEngine 0.1.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
36 UpdateTransform();
37 if (ConstantBuffer.IsValid()) return true;
38
39 auto world = GetTransformMatrix();
43 ConstantBuffer = RefPtr<RenderEngine::BufferBase>(
46 tb.GetSize(), tb.GetData()));
47
48 return true;
49 }
50
51 void TransformComponent::Update(float DeltaTime) {
52 UpdateConstantBuffer();
53
55 }
56
57 // Transformation matrix calculation
61
62 // Transformations
63 void TransformComponent::Translate(const Vector3D& translation) {
64 position += translation;
66 UpdateTransform();
67 }
68
69
70 void TransformComponent::Translate(float x, float y, float z) {
71 Translate(Vector3D(x,y,z));
72 }
73
75 this->rotation *= rotation;
76 UpdateTransform();
77 }
78
79 void TransformComponent::Scale(float amount) {
80 Scale(Vector3D(amount, amount, amount));
81 }
82
84 this->scale = scale * this->scale;
85 UpdateTransform();
86 }
87
88 // Setters
90 this->position = position;
91 }
92
94 this->rotation = rotation;
95 }
96
98 this->scale = scale;
99 }
100
101 // Direction vectors
103 return rotation * Vector3D(0.0f, 0.0f, 1.0f);
104 }
105
107 return rotation * Vector3D(1.0f, 0.0f, 0.0f);
108 }
109
111 return rotation * Vector3D(0.0f, 1.0f, 0.0f);
112 }
113
114 // Rotation utilities
115 void TransformComponent::RotateAround(const Vector3D& axis, float angle) {
116 Rotate(Quaternion(axis, angle));
117 }
118
119 void TransformComponent::RotateAroundLocal(const Vector3D& axis, float angle) {
120 Rotate(Quaternion(rotation * axis, angle));
121 }
122
124 Vector3D direction = (target - position).Normalize();
127 }
128
129 // Getters
133
137
141
142 void TransformComponent::UpdateTransform() {
146
148 }
149
150
151 void TransformComponent::UpdateConstantBuffer() {
152 if (!ConstantBuffer) {
153 SLEAK_ERROR("Constant buffer is not found to update for object {}", owner->GetName());
154 return;
155 }
156
157 UpdateTransform();
158
159 auto world = GetTransformMatrix();
160 RenderEngine::TransformBuffer tb(world,
163
164 RenderEngine::RenderCommandQueue::GetInstance()->SubmitUpdateConstantBuffer(ConstantBuffer, tb.GetData(), tb.GetSize());
165 }
166
167
168 Math::Matrix4 TransformComponent::GetMVP() {
169 auto mat = GetTransformMatrix() *
172
173 return mat;
174 }
175
176}
#define SLEAK_ERROR(...)
Definition Logger.hpp:22
#define VECTOR_Up
Definition Vector.hpp:14
static const Math::Matrix4 & GetMainProjectionMatrix()
Definition Camera.hpp:107
static const Math::Matrix4 & GetMainViewMatrix()
Definition Camera.hpp:103
friend class GameObject
Definition Component.hpp:88
GameObject * owner
Definition Component.hpp:84
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:22
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