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
30
32 if (ConstantBuffer.IsValid()) return true;
33
34 RenderEngine::TransformBuffer tb(WorldMatrix(),
37 ConstantBuffer = RefPtr<RenderEngine::BufferBase>(
40 tb.GetSize(), tb.GetData()));
41
42 return true;
43 }
44
45 void TransformComponent::Update(float DeltaTime) {
46 MeshComponent* mesh =
47 owner ? owner->GetComponent<MeshComponent>() : nullptr;
48 if (mesh && mesh->IsInitialized()) return;
49
50 SubmitRenderState();
51 }
52
53 void TransformComponent::SubmitRenderState() {
54 UpdateConstantBuffer();
55
57 }
58
60
61 const Matrix4& TransformComponent::WorldMatrix() {
62 if (m_worldDirty) {
65 m_worldDirty = false;
66 }
67 return m_world;
68 }
69
70 // Transformations
71 void TransformComponent::Translate(const Vector3D& translation) {
72 position += translation;
73 m_worldDirty = true;
74 }
75
76
77 void TransformComponent::Translate(float x, float y, float z) {
78 Translate(Vector3D(x,y,z));
79 }
80
82 this->rotation *= rotation;
83 m_worldDirty = true;
84 }
85
86 void TransformComponent::Scale(float amount) {
87 Scale(Vector3D(amount, amount, amount));
88 }
89
91 this->scale = scale * this->scale;
92 m_worldDirty = true;
93 }
94
95 // Setters
97 this->position = position;
98 m_worldDirty = true;
99 }
100
102 this->rotation = rotation;
103 m_worldDirty = true;
104 }
105
107 this->scale = scale;
108 m_worldDirty = true;
109 }
110
111 // Direction vectors
113 return rotation * Vector3D(0.0f, 0.0f, 1.0f);
114 }
115
117 return rotation * Vector3D(1.0f, 0.0f, 0.0f);
118 }
119
121 return rotation * Vector3D(0.0f, 1.0f, 0.0f);
122 }
123
124 // Rotation utilities
125 void TransformComponent::RotateAround(const Vector3D& axis, float angle) {
126 Rotate(Quaternion(axis, angle));
127 }
128
129 void TransformComponent::RotateAroundLocal(const Vector3D& axis, float angle) {
130 Rotate(Quaternion(rotation * axis, angle));
131 }
132
134 Vector3D direction = (target - position).Normalize();
137 m_worldDirty = true;
138 }
139
140 // Getters
144
148
152
153 void TransformComponent::UpdateConstantBuffer() {
154 if (!ConstantBuffer) {
155 SLEAK_ERROR("Constant buffer is not found to update for object {}", owner->GetName());
156 return;
157 }
158
159 RenderEngine::TransformBuffer tb(WorldMatrix(),
162
163 RenderEngine::RenderCommandQueue::GetInstance()->SubmitUpdateConstantBuffer(ConstantBuffer, tb.GetData(), tb.GetSize());
164 }
165
166
167 Math::Matrix4 TransformComponent::GetMVP() {
168 auto mat = WorldMatrix() * Camera::GetMainViewMatrix() *
170
171 return mat;
172 }
173
174}
#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
bool IsInitialized() const
True once Initialize() has run and succeeded.
Definition Component.hpp:81
Component(GameObject *object)
Definition Component.hpp:61
T * GetComponent()
Finds the first attached component of type T, or nullptr.
static Matrix< float, 4, 4 > Rotate(const Quaternion &rotation)
Definition Matrix.hpp:371
static Matrix< float, 4, 4 > Scale(const Vector3D &scale)
Definition Matrix.hpp:376
static Matrix< float, 4, 4 > Translate(const Vector3D &translation)
Definition Matrix.hpp:359
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.
Matrix4 GetTransformMatrix()
World matrix (S*R*T), recomputed only after a transform change.
void Update(float DeltaTime) override
Submits this frame's constant buffer unless a sibling mesh does it.
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:404
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