CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Mesh.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "PrimitiveTopology.hpp"
11#include "VertexDeclaration.hpp"
12
15
20
24
25namespace CeresEngine {
26
29
30 template<typename T> using VertexBufferView = StridedMemoryView<T>;
32
35
36 template<typename T> using IndexBufferView = StridedMemoryView<T>;
37 template<typename T> using ConstIndexBufferView = IndexBufferView<const T>;
38
40 using VertexCount = unsigned int;
41
43 using VertexIndex = unsigned int;
44
49
51 struct Vertex {
53 Vector3f position;
54
56 Vector3f normal;
57
59 Vector2f texCoords;
60
62 Vector3f tangent;
63
65 Vector3f bitangent;
66 };
67
84
105
112 class Mesh : public TResource<Mesh> {
115
116 public:
119
120 public:
122 explicit Mesh();
123
125 explicit Mesh(MeshProperties&& properties);
126
128 ~Mesh() override;
129
130 public:
133
136 template<typename T> [[nodiscard]] VertexBufferView<const T> getVertexBufferView() const {
137 const size_t stride = mProperties.vertexDeclaration.getSize();
138 CE_ASSERT(stride >= sizeof(T));
140 }
141
144
147 template<typename T> [[nodiscard]] IndexBufferView<const T> getIndexBufferView() const {
148 const size_t stride = mProperties.indexType.getSize();
149 CE_ASSERT(stride == sizeof(T));
151 }
152
153 public:
156
159
162
164 [[nodiscard]] const IndexType& getIndexType() const { return mProperties.indexType; }
165
167 [[nodiscard]] const Sphere& getBoundingSphere() const { return mProperties.boundingSphere; }
168
170 [[nodiscard]] const AABox& getBoundingBox() const { return mProperties.boundingBox; }
171
172 protected:
175
176 public: // Reflection
179 template<typename Processor> static constexpr void reflect(Processor&& RTTI) {
180 RTTI.property("properties", &Mesh::mProperties);
181 }
182 };
183
184 class MemoryMesh final : public TResource<MemoryMesh, Mesh> {
187
188 public:
191
194
195 public:
199 explicit MemoryMesh(Vector<Byte> vertexBuffer, MeshProperties&& properties);
200
205 explicit MemoryMesh(Vector<Byte> vertexBuffer, Vector<Byte> indexBuffer, MeshProperties&& properties);
206
210 explicit MemoryMesh(MemoryView<const Byte> vertexBuffer, MeshProperties&& properties);
211
217
222 explicit MemoryMesh(Vector<Byte> vertexBuffer, VertexDeclaration vertexDeclaration, Vector<SubMesh> subMeshes)
223 : MemoryMesh(std::move(vertexBuffer),
225 .subMeshes = std::move(subMeshes),
226 .vertexDeclaration = std::move(vertexDeclaration),
227 }) {}
228
235 explicit MemoryMesh(Vector<Byte> vertexBuffer, VertexDeclaration vertexDeclaration, Vector<Byte> indexBuffer, const IndexType indexType,
236 Vector<SubMesh> subMeshes)
237 : MemoryMesh(std::move(vertexBuffer), std::move(indexBuffer),
239 .subMeshes = std::move(subMeshes),
240 .vertexDeclaration = std::move(vertexDeclaration),
241 .indexType = indexType,
242 }) {}
243
250 explicit MemoryMesh(Vector<Byte> vertexBuffer, const VertexDeclaration& vertexDeclaration, Vector<Byte> indexBuffer, const Vector<SubMesh> subMeshes)
251 : MemoryMesh(std::move(vertexBuffer), std::move(indexBuffer),
253 .subMeshes = std::move(subMeshes),
254 .vertexDeclaration = std::move(vertexDeclaration),
255 .indexType = IndexType::UInt32,
256 }) {}
257
258 private:
259 explicit MemoryMesh() = default;
260
261 public:
264
267
268 public:
271
274
275 public: // Reflection
278 template<typename Processor> static constexpr void reflect(Processor&& RTTI) {
279 RTTI.property("vertexData", &MemoryMesh::mVertexData);
280 RTTI.property("indexData", &MemoryMesh::mIndexData);
281 }
282 };
283
287 class StreamingMesh final : public TResource<StreamingMesh, Mesh> {
290
291 public:
295 explicit StreamingMesh(Vector<Byte> vertexBuffer, MeshProperties&& properties);
296
301 explicit StreamingMesh(Vector<Byte> vertexBuffer, Vector<Byte> indexBuffer, MeshProperties&& properties);
302
306 explicit StreamingMesh(MemoryView<const Byte> vertexBuffer, MeshProperties&& properties);
307
313
318 explicit StreamingMesh(Vector<Byte> vertexBuffer, VertexDeclaration vertexDeclaration, Vector<SubMesh> subMeshes)
319 : StreamingMesh(std::move(vertexBuffer),
321 .subMeshes = std::move(subMeshes),
322 .vertexDeclaration = std::move(vertexDeclaration),
323 }) {}
324
331 explicit StreamingMesh(Vector<Byte> vertexBuffer, VertexDeclaration vertexDeclaration, Vector<Byte> indexBuffer, const IndexType indexType,
332 Vector<SubMesh> subMeshes)
333 : StreamingMesh(std::move(vertexBuffer), std::move(indexBuffer),
335 .subMeshes = std::move(subMeshes),
336 .vertexDeclaration = std::move(vertexDeclaration),
337 .indexType = indexType,
338 }) {}
339
346 explicit StreamingMesh(Vector<Byte> vertexBuffer, const VertexDeclaration& vertexDeclaration, Vector<Byte> indexBuffer, const Vector<SubMesh> subMeshes)
347 : StreamingMesh(std::move(vertexBuffer), std::move(indexBuffer),
349 .subMeshes = std::move(subMeshes),
350 .vertexDeclaration = std::move(vertexDeclaration),
351 .indexType = IndexType::UInt32,
352 }) {}
353
354 public:
357
360
361 public:
364
367
368 public: // Reflection
371 template<typename Processor> static constexpr void reflect(Processor&& RTTI) {
372 }
373 };
374
377 class GPUMesh final : public TResource<GPUMesh, Mesh> {
380
381 private:
384
387
388 // /// The GPU memory mapped buffer pointer.
389 // void* mVertexGPUBufferPtr = nullptr;
390 //
391 // /// The GPU memory mapped buffer pointer.
392 // void* mIndexGPUBufferPtr = nullptr;
393
394 public:
399 explicit GPUMesh(const GPUVertexBuffer& vertexBuffer, VertexDeclaration vertexDeclaration, Vector<SubMesh> subMeshes);
400
407 explicit GPUMesh(const GPUVertexBuffer& vertexBuffer, VertexDeclaration vertexDeclaration, const GPUIndexBuffer& indexBuffer, IndexType indexType,
408 Vector<SubMesh> subMeshes);
409
416 explicit GPUMesh(const GPUVertexBuffer& vertexBuffer, const VertexDeclaration& vertexDeclaration, const GPUIndexBuffer& indexBuffer,
417 const Vector<SubMesh> subMeshes)
418 : GPUMesh(vertexBuffer, vertexDeclaration, indexBuffer, IndexType::UInt32, subMeshes) {}
419
420 public:
423
426
427 public:
430
433 };
434
435} // namespace CeresEngine
436
437
438
439
440
441
#define CE_ASSERT(...)
Definition Macros.hpp:323
#define CE_META_CLASS_FRIEND(T)
Definition Forward.hpp:48
Definition GPUBufferObject.hpp:169
A mesh that is backed directly by a GPU vertex (and optionally an index buffer).
Definition Mesh.hpp:377
GPUIndexBuffer mIndexBuffer
The GPU index buffer.
Definition Mesh.hpp:386
const GPUIndexBuffer & getIndexBuffer() const noexcept
The GPU index buffer.
Definition Mesh.hpp:432
const GPUVertexBuffer & getVertexBuffer() const noexcept
The GPU vertex buffer.
Definition Mesh.hpp:429
GPUMesh(const GPUVertexBuffer &vertexBuffer, VertexDeclaration vertexDeclaration, Vector< SubMesh > subMeshes)
Creates a new GPUMesh from an existing vertex buffer.
ConstRawVertexBufferView getRawVertexBufferView() const final
Gets a raw view of the vertex buffer data.
Definition Mesh.hpp:422
GPUMesh(const GPUVertexBuffer &vertexBuffer, VertexDeclaration vertexDeclaration, const GPUIndexBuffer &indexBuffer, IndexType indexType, Vector< SubMesh > subMeshes)
Creates a new GPUMesh from an existing vertex and index buffers.
ConstRawIndexBufferView getRawIndexBufferView() const final
Gets a raw view of the index buffer data.
Definition Mesh.hpp:425
GPUMesh(const GPUVertexBuffer &vertexBuffer, const VertexDeclaration &vertexDeclaration, const GPUIndexBuffer &indexBuffer, const Vector< SubMesh > subMeshes)
Creates a new GPUMesh from an existing vertex and index buffers.
Definition Mesh.hpp:416
GPUVertexBuffer mVertexBuffer
The GPU vertex buffer.
Definition Mesh.hpp:383
Definition GPUBufferObject.hpp:126
Definition Mesh.hpp:184
MemoryMesh(MemoryView< const Byte > vertexBuffer, MemoryView< const Byte > indexBuffer, MeshProperties &&properties)
Creates a new MemoryMesh from an existing vertex and index buffers.
MemoryMesh(Vector< Byte > vertexBuffer, MeshProperties &&properties)
Creates a new MemoryMesh from an existing vertex buffer.
Vector< Byte > mIndexData
A vector containing the index data.
Definition Mesh.hpp:193
MemoryView< const Byte > getVertexBuffer() const noexcept
Definition Mesh.hpp:270
MemoryMesh(MemoryView< const Byte > vertexBuffer, MeshProperties &&properties)
Creates a new MemoryMesh from an existing vertex buffer.
MemoryMesh(Vector< Byte > vertexBuffer, VertexDeclaration vertexDeclaration, Vector< Byte > indexBuffer, const IndexType indexType, Vector< SubMesh > subMeshes)
Creates a new MemoryMesh from an existing vertex and index buffers.
Definition Mesh.hpp:235
MemoryMesh(Vector< Byte > vertexBuffer, Vector< Byte > indexBuffer, MeshProperties &&properties)
Creates a new MemoryMesh from an existing vertex and index buffers.
MemoryView< const Byte > getIndexBuffer() const noexcept
Definition Mesh.hpp:273
Vector< Byte > mVertexData
A vector containing the vertex data.
Definition Mesh.hpp:190
MemoryMesh(Vector< Byte > vertexBuffer, VertexDeclaration vertexDeclaration, Vector< SubMesh > subMeshes)
Creates a new MemoryMesh from an existing vertex buffer.
Definition Mesh.hpp:222
ConstRawVertexBufferView getRawVertexBufferView() const final
Gets a raw view of the vertex buffer data.
Definition Mesh.hpp:263
MemoryMesh(Vector< Byte > vertexBuffer, const VertexDeclaration &vertexDeclaration, Vector< Byte > indexBuffer, const Vector< SubMesh > subMeshes)
Creates a new MemoryMesh from an existing vertex and index buffers.
Definition Mesh.hpp:250
static constexpr void reflect(Processor &&RTTI)
Executes the given processor for every field of the struct.
Definition Mesh.hpp:278
ConstRawIndexBufferView getRawIndexBufferView() const final
Gets a raw view of the index buffer data.
Definition Mesh.hpp:266
A base class for all mesh implementations.
Definition Mesh.hpp:112
MeshProperties mProperties
A structure that contains a set of properties for the mesh.
Definition Mesh.hpp:118
Mesh(MeshProperties &&properties)
Creates a new Mesh by constructing a new MeshProperties object.
IndexBufferView< const T > getIndexBufferView() const
Gets a structured view of the index buffer data.
Definition Mesh.hpp:147
const AABox & getBoundingBox() const
The mesh axis-aligned bounding box, can be used to perform culling.
Definition Mesh.hpp:170
~Mesh() override
Destroys the mesh object.
const VertexDeclaration & getVertexDeclaration() const
The vertex declaration format used by the mesh.
Definition Mesh.hpp:161
static constexpr void reflect(Processor &&RTTI)
Executes the given processor for every field of the struct.
Definition Mesh.hpp:179
const MeshProperties & getProperties() const noexcept
A structure that contains a set of properties for the mesh.
Definition Mesh.hpp:155
virtual ConstRawVertexBufferView getRawVertexBufferView() const =0
Gets a raw view of the vertex buffer data.
const Sphere & getBoundingSphere() const
The mesh bounding sphere, can be used to perform culling.
Definition Mesh.hpp:167
void recalculateMeshBounds()
Recalculate the mesh bounds based on the sub-mesh bounds.
virtual ConstRawIndexBufferView getRawIndexBufferView() const =0
Gets a raw view of the index buffer data.
const Vector< SubMesh > & getSubMeshes() const
The sub meshes in the mesh.
Definition Mesh.hpp:158
Mesh()
Creates a new empty mesh.
const IndexType & getIndexType() const
The format used by the mesh index buffer, if an index buffer is available.
Definition Mesh.hpp:164
VertexBufferView< const T > getVertexBufferView() const
Gets a structured view of the vertex buffer data.
Definition Mesh.hpp:136
A mesh that stores it's vertex and index streams to a resource stream.
Definition Mesh.hpp:287
static constexpr void reflect(Processor &&RTTI)
Executes the given processor for every field of the struct.
Definition Mesh.hpp:371
ConstRawVertexBufferView getRawVertexBufferView() const final
Gets a raw view of the vertex buffer data.
Definition Mesh.hpp:356
ConstRawIndexBufferView getRawIndexBufferView() const final
Gets a raw view of the index buffer data.
Definition Mesh.hpp:359
StreamingMesh(Vector< Byte > vertexBuffer, VertexDeclaration vertexDeclaration, Vector< Byte > indexBuffer, const IndexType indexType, Vector< SubMesh > subMeshes)
Creates a new MemoryMesh from an existing vertex and index buffers.
Definition Mesh.hpp:331
MemoryView< const Byte > getVertexBuffer() const noexcept
Definition Mesh.hpp:363
StreamingMesh(Vector< Byte > vertexBuffer, Vector< Byte > indexBuffer, MeshProperties &&properties)
Creates a new MemoryMesh from an existing vertex and index buffers.
StreamingMesh(Vector< Byte > vertexBuffer, MeshProperties &&properties)
Creates a new MemoryMesh from an existing vertex buffer.
StreamingMesh(MemoryView< const Byte > vertexBuffer, MeshProperties &&properties)
Creates a new MemoryMesh from an existing vertex buffer.
StreamingMesh(MemoryView< const Byte > vertexBuffer, MemoryView< const Byte > indexBuffer, MeshProperties &&properties)
Creates a new MemoryMesh from an existing vertex and index buffers.
StreamingMesh(Vector< Byte > vertexBuffer, VertexDeclaration vertexDeclaration, Vector< SubMesh > subMeshes)
Creates a new MemoryMesh from an existing vertex buffer.
Definition Mesh.hpp:318
StreamingMesh(Vector< Byte > vertexBuffer, const VertexDeclaration &vertexDeclaration, Vector< Byte > indexBuffer, const Vector< SubMesh > subMeshes)
Creates a new MemoryMesh from an existing vertex and index buffers.
Definition Mesh.hpp:346
MemoryView< const Byte > getIndexBuffer() const noexcept
Definition Mesh.hpp:366
A memory view is a class which attaches to an chunk of memory and provides a view to it (optionally c...
Definition MemoryView.hpp:439
Utility template class that can be extended by Resources to automatically implement methods that are ...
Definition Resource.hpp:272
Definition Application.hpp:19
unsigned int VertexCount
A type that represents the number vertices in a mesh.
Definition Mesh.hpp:40
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
auto move(Vector3 position)
Moves a entity to the given position.
Definition Helpers.hpp:22
unsigned int VertexIndex
A type that represents a vertex index.
Definition Mesh.hpp:43
MemoryView< const Byte > ConstRawVertexBufferView
Definition Mesh.hpp:28
MemoryView< const Byte > ConstRawIndexBufferView
Definition Mesh.hpp:34
std::uint32_t UInt32
Definition DataTypes.hpp:23
Vector< VertexIndex > VertexIndexSet
A type that represents a set of vertex indexes.
Definition Mesh.hpp:48
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
Definition IndexType.hpp:20
@ UInt32
The index buffer is represented as a 32-bit unsigned integer.
Definition IndexType.hpp:26
std::size_t getSize() const noexcept
Gets the size of index.
A structure that describes properties of a mesh.
Definition Mesh.hpp:86
VertexCount vertexCount
The total number of vertices (or indices) in the mesh.
Definition Mesh.hpp:88
IndexType indexType
The format used by the mesh index buffer, if an index buffer is available.
Definition Mesh.hpp:97
Sphere boundingSphere
The mesh bounding sphere, can be used to perform culling.
Definition Mesh.hpp:100
AABox boundingBox
The mesh axis-aligned bounding box, can be used to perform culling.
Definition Mesh.hpp:103
VertexDeclaration vertexDeclaration
The vertex declaration format used by the mesh.
Definition Mesh.hpp:94
Vector< SubMesh > subMeshes
The sub meshes in the mesh.
Definition Mesh.hpp:91
Primitive topology enumeration.
Definition PrimitiveTopology.hpp:21
@ Triangles
Triangle list, where each set of three vertices represent a single triangle primitive.
Definition PrimitiveTopology.hpp:50
Definition Mesh.hpp:68
AABox boundingBox
The sub-mesh axis-aligned bounding box, can be used to perform culling.
Definition Mesh.hpp:82
PrimitiveTopology primitiveTopology
The primitive topology used to render the sub-mesh.
Definition Mesh.hpp:76
VertexCount indexCount
The number of indexes in the sub mesh.
Definition Mesh.hpp:73
UInt32 indexOffset
The offset of the sub mesh as an offset of the mesh index buffer.
Definition Mesh.hpp:70
Sphere boundingSphere
The sub-mesh bounding sphere, can be used to perform culling.
Definition Mesh.hpp:79
Determines how vertices are laid-out on a vertex buffer or a mesh data.
Definition VertexDeclaration.hpp:205
std::size_t getSize() const noexcept
Computes the size of a single vertex, in bytes.
A basic vertex type.
Definition Mesh.hpp:51
Vector3f position
The vertex position.
Definition Mesh.hpp:53
Vector3f bitangent
The vertex bitangent vector.
Definition Mesh.hpp:65
Vector3f normal
The vertex normal vector.
Definition Mesh.hpp:56
Vector2f texCoords
The vertex texture coordinates.
Definition Mesh.hpp:59
Vector3f tangent
The vertex tangent vector.
Definition Mesh.hpp:62