Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Mesh.hpp
Go to the documentation of this file.
1/**************************************************************************
2 * EngineSquared v0.1.1
3 *
4 * EngineSquared is a software package, part of the Engine² organization.
5 *
6 * This file is part of the EngineSquared project that is under MIT License.
7 * Copyright © 2024-present by @EngineSquared, All rights reserved.
8 *
9 * EngineSquared is a free software: you can redistribute it and/or modify
10 * it under the terms of the MIT License. See the project's LICENSE file for
11 * the full license text and details.
12 *
13 * @file Mesh.hpp
14 * @brief Mesh struct declaration.
15 *
16 * This struct is used to represent a mesh.
17 *
18 * @author @EngineSquared
19 * @version 0.1.1
20 * @date 2024-12-06
21 **************************************************************************/
22
23#pragma once
24
25#include <glm/glm.hpp>
26#include <vector>
27
28namespace Object::Component {
29
40struct Mesh {
41 explicit Mesh() = default;
43 {
44 vertices.clear();
45 normals.clear();
46 texCoords.clear();
47 indices.clear();
48 _dirty = false;
49 }
50
51 // Move constructor
52 Mesh(Mesh &&other) noexcept
53 : vertices(std::move(other.vertices)), normals(std::move(other.normals)), texCoords(std::move(other.texCoords)),
54 indices(std::move(other.indices)), _dirty(true)
55 {
56 other._dirty = false;
57 }
58
59 // Move assignment operator
60 Mesh &operator=(Mesh &&other) noexcept
61 {
62 if (this != &other)
63 {
64 vertices = std::move(other.vertices);
65 normals = std::move(other.normals);
66 texCoords = std::move(other.texCoords);
67 indices = std::move(other.indices);
68 _dirty = true;
69 other._dirty = false;
70 }
71 return *this;
72 }
73
74 // Copy constructor
75 Mesh(const Mesh &other)
76 : vertices(other.vertices), normals(other.normals), texCoords(other.texCoords), indices(other.indices),
77 _dirty(true)
78 {
79 }
80
81 // Copy assignment operator
82 Mesh &operator=(const Mesh &other)
83 {
84 if (this != &other)
85 {
86 vertices = other.vertices;
87 normals = other.normals;
88 texCoords = other.texCoords;
89 indices = other.indices;
90 _dirty = true;
91 }
92 return *this;
93 }
94
95 // Getters
96 [[nodiscard]] const std::vector<glm::vec3> &GetVertices() const { return vertices; }
97 [[nodiscard]] const std::vector<glm::vec3> &GetNormals() const { return normals; }
98 [[nodiscard]] const std::vector<glm::vec2> &GetTexCoords() const { return texCoords; }
99 [[nodiscard]] const std::vector<uint32_t> &GetIndices() const { return indices; }
100
101 // Setters
102
103 //---------------- Vertex Methods ----------------//
104 void SetVertices(const std::vector<glm::vec3> &newVertices)
105 {
106 vertices = newVertices;
107 _dirty = true;
108 }
109
110 void SetVertexAt(size_t index, const glm::vec3 &vertex)
111 {
112 if (index >= vertices.size())
113 return;
114 vertices[index] = vertex;
115 _dirty = true;
116 }
117
118 void ReserveVertices(size_t count) { vertices.reserve(count); }
119
120 template <typename... Args> void EmplaceVertices(Args &&...args)
121 {
122 vertices.emplace_back(std::forward<Args>(args)...);
123 _dirty = true;
124 }
125
126 //---------------- Normal Methods ----------------//
127 void SetNormals(const std::vector<glm::vec3> &newNormals)
128 {
129 normals = newNormals;
130 _dirty = true;
131 }
132
133 void SetNormalAt(size_t index, const glm::vec3 &normal)
134 {
135 if (index >= normals.size())
136 return;
137 normals[index] = normal;
138 _dirty = true;
139 }
140
141 void ReserveNormals(size_t count) { normals.reserve(count); }
142
143 template <typename... Args> void EmplaceNormals(Args &&...args)
144 {
145 normals.emplace_back(std::forward<Args>(args)...);
146 _dirty = true;
147 }
148
149 //---------------- TexCoord Methods ----------------//
150 void SetTexCoords(const std::vector<glm::vec2> &newTexCoords)
151 {
152 texCoords = newTexCoords;
153 _dirty = true;
154 }
155
156 void SetTexCoordAt(size_t index, const glm::vec2 &texCoord)
157 {
158 if (index >= texCoords.size())
159 return;
160 texCoords[index] = texCoord;
161 _dirty = true;
162 }
163
164 void ReserveTexCoords(size_t count) { texCoords.reserve(count); }
165
166 template <typename... Args> void EmplaceTexCoords(Args &&...args)
167 {
168 texCoords.emplace_back(std::forward<Args>(args)...);
169 _dirty = true;
170 }
171
172 //---------------- Index Methods ----------------//
173 void SetIndices(const std::vector<uint32_t> &newIndices)
174 {
175 indices = newIndices;
176 _dirty = true;
177 }
178
179 void SetIndexAt(size_t index, uint32_t indexValue)
180 {
181 if (index >= indices.size())
182 return;
183 indices[index] = indexValue;
184 _dirty = true;
185 }
186
187 void ReserveIndices(size_t count) { indices.reserve(count); }
188
189 template <typename... Args> void EmplaceIndices(Args &&...args)
190 {
191 indices.emplace_back(std::forward<Args>(args)...);
192 _dirty = true;
193 }
194
201 [[nodiscard]] bool IsDirty() const { return _dirty; }
202
209 void ClearDirty() const { _dirty = false; }
210
211 private:
212 std::vector<glm::vec3> vertices{};
213 std::vector<glm::vec3> normals{};
214 std::vector<glm::vec2> texCoords{};
215 std::vector<uint32_t> indices{};
216
224 mutable bool _dirty = false;
225};
226} // namespace Object::Component
static int count
Definition SystemSetTest.cpp:7
Definition AmbientLight.hpp:5
const std::vector< glm::vec3 > & GetVertices() const
Definition Mesh.hpp:96
void SetVertices(const std::vector< glm::vec3 > &newVertices)
Definition Mesh.hpp:104
void SetNormals(const std::vector< glm::vec3 > &newNormals)
Definition Mesh.hpp:127
const std::vector< glm::vec2 > & GetTexCoords() const
Definition Mesh.hpp:98
void EmplaceNormals(Args &&...args)
Definition Mesh.hpp:143
std::vector< uint32_t > indices
Definition Mesh.hpp:215
std::vector< glm::vec3 > vertices
Definition Mesh.hpp:212
void SetTexCoordAt(size_t index, const glm::vec2 &texCoord)
Definition Mesh.hpp:156
~Mesh()
Definition Mesh.hpp:42
void SetVertexAt(size_t index, const glm::vec3 &vertex)
Definition Mesh.hpp:110
void EmplaceIndices(Args &&...args)
Definition Mesh.hpp:189
std::vector< glm::vec2 > texCoords
Definition Mesh.hpp:214
Mesh(Mesh &&other) noexcept
Definition Mesh.hpp:52
bool IsDirty() const
Check if the mesh data has been modified since last GPU sync.
Definition Mesh.hpp:201
std::vector< glm::vec3 > normals
Definition Mesh.hpp:213
bool _dirty
Dirty flag for GPU synchronization optimization.
Definition Mesh.hpp:224
void SetTexCoords(const std::vector< glm::vec2 > &newTexCoords)
Definition Mesh.hpp:150
void EmplaceVertices(Args &&...args)
Definition Mesh.hpp:120
Mesh & operator=(const Mesh &other)
Definition Mesh.hpp:82
const std::vector< glm::vec3 > & GetNormals() const
Definition Mesh.hpp:97
const std::vector< uint32_t > & GetIndices() const
Definition Mesh.hpp:99
void SetIndices(const std::vector< uint32_t > &newIndices)
Definition Mesh.hpp:173
void ReserveTexCoords(size_t count)
Definition Mesh.hpp:164
Mesh & operator=(Mesh &&other) noexcept
Definition Mesh.hpp:60
void EmplaceTexCoords(Args &&...args)
Definition Mesh.hpp:166
void SetIndexAt(size_t index, uint32_t indexValue)
Definition Mesh.hpp:179
void SetNormalAt(size_t index, const glm::vec3 &normal)
Definition Mesh.hpp:133
Mesh(const Mesh &other)
Definition Mesh.hpp:75
void ReserveIndices(size_t count)
Definition Mesh.hpp:187
void ReserveNormals(size_t count)
Definition Mesh.hpp:141
void ClearDirty() const
Clear the dirty flag after GPU buffer has been updated.
Definition Mesh.hpp:209
void ReserveVertices(size_t count)
Definition Mesh.hpp:118