SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
MeshComponent.cpp
Go to the documentation of this file.
7#include <Core/GameObject.hpp>
10
11namespace Sleak {
12
13namespace {
14 // World point (row-vector convention: world = local * M) as a
15 // degenerate AABB for accumulation.
16 Math::AABB TransformedPoint(const Math::Vector3D& p,
17 const Math::Matrix4& m) {
18 float x = p.GetX(), y = p.GetY(), z = p.GetZ();
20 x * m(0, 0) + y * m(1, 0) + z * m(2, 0) + m(3, 0),
21 x * m(0, 1) + y * m(1, 1) + z * m(2, 1) + m(3, 1),
22 x * m(0, 2) + y * m(1, 2) + z * m(2, 2) + m(3, 2));
23 return Math::AABB(w, w);
24 }
25} // namespace
26
41
43 if (!VertexBuffer.IsValid())
44 return false;
45
46 if (!IndexBuffer.IsValid())
47 return false;
48
49 bIsInitialized = true;
50
51 return true;
52 }
53
54 void MeshComponent::Update(float deltaTime) {
55 if (!bIsInitialized)
56 return;
57
58 if (m_hasLocalBounds || m_hasCullBounds) {
59 Math::AABB worldBounds = m_cullBounds;
60 bool cullReady = m_hasCullBounds;
61
62 if (m_hasLocalBounds && owner) {
63 if (auto* tc = owner->GetComponent<TransformComponent>()) {
64 Math::Matrix4 m = tc->GetTransformMatrix();
65 Math::AABB acc = TransformedPoint(m_localBounds.Corner(0), m);
66 for (int i = 1; i < 8; ++i)
67 acc.Merge(TransformedPoint(m_localBounds.Corner(i), m));
68 worldBounds = acc;
69 cullReady = true;
70 }
71 }
72
73 if (cullReady &&
75 return;
76 }
77
78 RenderEngine::RenderCommandQueue::GetInstance()->SubmitDrawIndexed(VertexBuffer,IndexBuffer,ConstantBuffers,IndexCount);
79 }
80
82 this->VertexBuffer = buffer;
83 }
84
86 this->IndexBuffer = buffer;
87 }
88
96
99 ConstantBuffers.add(buffer);
100 }
101
103 m_cullBounds = bounds;
104 m_hasCullBounds = true;
105 m_hasLocalBounds = false;
106 }
107
109 m_localBounds = bounds;
110 m_hasLocalBounds = true;
111 }
112
113 }
GameObject * owner
Definition Component.hpp:81
Component(GameObject *object)
Definition Component.hpp:61
static bool IsVisibleFrustumOnly(const Math::AABB &box)
Frustum-only visibility test, skipping the occlusion buffer entirely.
void * GetRawData()
Definition List.hpp:159
size_t GetByteSize() const
Definition List.hpp:152
size_t GetSize() const
Definition List.hpp:151
virtual void Update(float deltaTime) override
Culls against cull bounds if set, then submits an indexed draw call.
void SetIndexBuffer(RefPtr< RenderEngine::BufferBase > &buffer)
void AddConstantBuffer(RenderEngine::TransformBuffer &bufferData)
Creates a new constant buffer from bufferData and appends it to the draw's buffer list.
void SetVertexBuffer(RefPtr< RenderEngine::BufferBase > &buffer)
virtual bool Initialize() override
Validates that vertex and index buffers were created successfully.
void SetCullBoundsLocal(const Math::AABB &bounds)
void SetCullBoundsWorld(const Math::AABB &bounds)
MeshComponent(GameObject *object)
static RenderCommandQueue * GetInstance()
Lazily creates and returns the process-wide singleton instance.
void SubmitDrawIndexed(RefPtr< BufferBase > vertexBuffer, RefPtr< BufferBase > indexBuffer, List< RefPtr< BufferBase > > constantBuffers, uint32_t indexCount, uint32_t startIndexLocation=0, int32_t baseVertexLocation=0, bool castsShadow=true)
Queues an indexed draw with its vertex/index buffers and constant buffers.
static BufferBase * CreateBuffer(BufferType Type, uint32_t Size, void *Data)
Creates a buffer via the currently registered backend factory.
Represents the position, rotation, and scale of an entity in 3D space.
void * GetRawData()
Definition MeshData.hpp:90
size_t GetSize() const
Definition MeshData.hpp:91
size_t GetSizeInBytes() const
Definition MeshData.hpp:92
Matrix< float, 4, 4 > Matrix4
Definition Matrix.hpp:413
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
void Merge(const AABB &o)
Grows this box to also cover o.
Definition AABB.hpp:36
IndexGroup indices
Definition MeshData.hpp:105
VertexGroup vertices
Definition MeshData.hpp:104
Per-draw world-view-projection and world matrices.