SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
MeshComponent.cpp
Go to the documentation of this file.
1#include <Core/GameObject.hpp>
7#include <cmath>
8
13
14namespace Sleak {
15
16namespace {
17/// World AABB of a local box under m (world = local * m), via Arvo's method.
18Math::AABB TransformBounds(const Math::AABB& box, const Math::Matrix4& m) {
19 const Math::Vector3D c = box.Center();
20 const Math::Vector3D e = box.Extents();
21 const float cx = c.GetX(), cy = c.GetY(), cz = c.GetZ();
22 const float ex = std::fabs(e.GetX());
23 const float ey = std::fabs(e.GetY());
24 const float ez = std::fabs(e.GetZ());
25
26 const Math::Vector3D center(
27 cx * m(0, 0) + cy * m(1, 0) + cz * m(2, 0) + m(3, 0),
28 cx * m(0, 1) + cy * m(1, 1) + cz * m(2, 1) + m(3, 1),
29 cx * m(0, 2) + cy * m(1, 2) + cz * m(2, 2) + m(3, 2));
30 const Math::Vector3D extents(
31 ex * std::fabs(m(0, 0)) + ey * std::fabs(m(1, 0)) +
32 ez * std::fabs(m(2, 0)),
33 ex * std::fabs(m(0, 1)) + ey * std::fabs(m(1, 1)) +
34 ez * std::fabs(m(2, 1)),
35 ex * std::fabs(m(0, 2)) + ey * std::fabs(m(1, 2)) +
36 ez * std::fabs(m(2, 2)));
37 return Math::AABB(center - extents, center + extents);
38}
39} // namespace
40
55
57 if (!VertexBuffer.IsValid())
58 return false;
59
60 if (!IndexBuffer.IsValid())
61 return false;
62
63 bIsInitialized = true;
64
65 return true;
66 }
67
68 void MeshComponent::Update(float deltaTime) {
69 if (!bIsInitialized)
70 return;
71
74
75 if (m_hasLocalBounds || m_hasCullBounds) {
76 Math::AABB worldBounds = m_cullBounds;
77 bool cullReady = m_hasCullBounds;
78
79 if (m_hasLocalBounds && tc) {
80 worldBounds =
81 TransformBounds(m_localBounds, tc->GetTransformMatrix());
82 cullReady = true;
83 }
84
85 if (cullReady &&
87 return;
88 }
89
90 if (tc) tc->SubmitRenderState();
91 if (MaterialComponent* mc =
93 mc->SubmitRenderState();
94
95 RenderEngine::RenderCommandQueue::GetInstance()->SubmitDrawIndexed(VertexBuffer,IndexBuffer,ConstantBuffers,IndexCount);
96 }
97
99 this->VertexBuffer = buffer;
100 }
101
103 this->IndexBuffer = buffer;
104 }
105
109 &bufferData));
110
111 ConstantBuffers.add(buffer);
112 }
113
116 ConstantBuffers.add(buffer);
117 }
118
120 m_cullBounds = bounds;
121 m_hasCullBounds = true;
122 m_hasLocalBounds = false;
123 }
124
126 m_localBounds = bounds;
127 m_hasLocalBounds = true;
128 }
129
130 }
friend class GameObject
Definition Component.hpp:88
GameObject * owner
Definition Component.hpp:84
Component(GameObject *object)
Definition Component.hpp:61
static bool IsVisibleFrustumOnly(const Math::AABB &box)
Frustum-only visibility test, skipping the occlusion buffer entirely.
T * GetComponent()
Finds the first attached component of type T, or nullptr.
void * GetRawData()
Definition List.hpp:130
size_t GetByteSize() const
Definition List.hpp:123
size_t GetSize() const
Definition List.hpp:122
float GetX() const
Definition Vector.hpp:361
virtual void Update(float deltaTime) override
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)
friend class RefPtr
Definition RefPtr.hpp:65
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.
Matrix4 GetTransformMatrix()
World matrix (S*R*T), recomputed only after a transform change.
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:404
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
IndexGroup indices
Definition MeshData.hpp:105
VertexGroup vertices
Definition MeshData.hpp:104
Per-draw world-view-projection and world matrices.