CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
MeshBuilder.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
12
14
16
17namespace CeresEngine {
18
22 template<typename V, typename I = UInt32> class TriangleMeshBuilder {
23 public:
27
28 public:
29 explicit TriangleMeshBuilder() { subMeshes.emplace_back(); }
30
31 public:
32 void addTriangle(const V& v0, const V& v1, const V& v2) {
33 vertices.emplace_back(v0);
34 vertices.emplace_back(v1);
35 vertices.emplace_back(v2);
36
37 addTriangleIndices(UInt32(indices.size() - 3), UInt32(indices.size() - 2), UInt32(indices.size() - 1));
38 }
39
42 indices.emplace_back(v0);
43 indices.emplace_back(v1);
44 indices.emplace_back(v2);
45 currentSubMesh.indexCount += VertexCount(3);
46 }
47
48 void addTriangleStrip(const V& v) {
50
51 vertices.emplace_back(v);
52 if(currentSubMesh.indexCount >= 3) { // on the second triangle we do this
53 const UInt32 i = currentSubMesh.indexCount / 3;
54 if(i % 2 == 1) {
55 addTriangleIndices(UInt32(vertices.size() - 3), UInt32(vertices.size() - 1), UInt32(vertices.size() - 2));
56 } else {
57 addTriangleIndices(UInt32(vertices.size() - 3), UInt32(vertices.size() - 2), UInt32(vertices.size() - 1));
58 }
59 currentSubMesh.indexCount += 3;
60 } else {
61 indices.emplace_back(UInt32(vertices.size() - 1));
62 currentSubMesh.indexCount += 1;
63 }
64 }
65
66 void addTriangleFan(const V& v) {
68
69 vertices.emplace_back(v);
70 if(currentSubMesh.indexCount >= 3) { // on the second triangle we do this
71 indices.emplace_back(indices[currentSubMesh.indexOffset]);
72 indices.emplace_back(UInt32(vertices.size() - 2));
73 indices.emplace_back(UInt32(vertices.size() - 1));
74 currentSubMesh.indexCount += 3;
75 } else {
76 indices.emplace_back(UInt32(vertices.size() - 1));
77 currentSubMesh.indexCount += 1;
78 }
79 }
80
83 if(currentSubMesh.indexCount == 0) {
84 return UInt32(subMeshes.size() - 1);
85 }
86
87 CE_ASSERT(indices.size() % 3 == 0);
88 CE_ASSERT(currentSubMesh.indexCount % 3 == 0);
89
90 SubMesh& newSubMesh = subMeshes.emplace_back();
91 newSubMesh.indexOffset = UInt32(indices.size()); // next-vertex!
92 newSubMesh.indexCount = 0;
93 return UInt32(subMeshes.size() - 1);
94 }
95
96 [[nodiscard]] const SubMesh& getCurrentSubMesh() const { return subMeshes.back(); }
97
98 void validate() {
99 for(const UInt32 index : indices) {
100 (void)index;
101 CE_ASSERT(index < vertices.size());
102 }
103 }
104
105 private:
106 SubMesh& getLatestSubMesh() { return subMeshes.back(); }
107 };
108
109} // namespace CeresEngine
#define CE_ASSERT(...)
Definition Macros.hpp:323
Helper class that makes building triangle meshes easier.
Definition MeshBuilder.hpp:22
void addTriangleStrip(const V &v)
Definition MeshBuilder.hpp:48
const SubMesh & getCurrentSubMesh() const
Definition MeshBuilder.hpp:96
Vector< SubMesh > subMeshes
Definition MeshBuilder.hpp:26
void addTriangle(const V &v0, const V &v1, const V &v2)
Definition MeshBuilder.hpp:32
Vector< V > vertices
Definition MeshBuilder.hpp:24
void addTriangleFan(const V &v)
Definition MeshBuilder.hpp:66
void validate()
Definition MeshBuilder.hpp:98
SubMesh & getLatestSubMesh()
Definition MeshBuilder.hpp:106
void addTriangleIndices(UInt32 v0, UInt32 v1, UInt32 v2)
Definition MeshBuilder.hpp:40
UInt32 nextSubMesh()
Definition MeshBuilder.hpp:81
Vector< I > indices
Definition MeshBuilder.hpp:25
TriangleMeshBuilder()
Definition MeshBuilder.hpp:29
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
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Mesh.hpp:68
UInt32 indexOffset
The offset of the sub mesh as an offset of the mesh index buffer.
Definition Mesh.hpp:70