56 const float halfSize = size * 0.5f;
58 std::vector<Vertex> vertices;
59 std::vector<uint32_t> indices;
61 auto addFace = [&](glm::vec3 normal, glm::vec3 v0, glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) {
62 uint32_t startIndex = (uint32_t) vertices.size();
64 Vertex a{ v0, normal, {0.0f, 0.0f} };
65 Vertex b{ v1, normal, {1.0f, 0.0f} };
66 Vertex c{ v2, normal, {1.0f, 1.0f} };
67 Vertex d{ v3, normal, {0.0f, 1.0f} };
69 vertices.push_back(a);
70 vertices.push_back(b);
71 vertices.push_back(c);
72 vertices.push_back(d);
74 indices.push_back(startIndex);
75 indices.push_back(startIndex + 1);
76 indices.push_back(startIndex + 2);
77 indices.push_back(startIndex);
78 indices.push_back(startIndex + 2);
79 indices.push_back(startIndex + 3);
84 { -halfSize, -halfSize, halfSize },
85 { halfSize, -halfSize, halfSize },
86 { halfSize, halfSize, halfSize },
87 { -halfSize, halfSize, halfSize });
91 { halfSize, -halfSize, -halfSize },
92 { -halfSize, -halfSize, -halfSize },
93 { -halfSize, halfSize, -halfSize },
94 { halfSize, halfSize, -halfSize });
98 { halfSize, -halfSize, halfSize },
99 { halfSize, -halfSize, -halfSize },
100 { halfSize, halfSize, -halfSize },
101 { halfSize, halfSize, halfSize });
104 addFace({ -1, 0, 0 },
105 { -halfSize, -halfSize, -halfSize },
106 { -halfSize, -halfSize, halfSize },
107 { -halfSize, halfSize, halfSize },
108 { -halfSize, halfSize, -halfSize });
112 { -halfSize, halfSize, halfSize },
113 { halfSize, halfSize, halfSize },
114 { halfSize, halfSize, -halfSize },
115 { -halfSize, halfSize, -halfSize });
118 addFace({ 0, -1, 0 },
119 { -halfSize, -halfSize, -halfSize },
120 { halfSize, -halfSize, -halfSize },
121 { halfSize, -halfSize, halfSize },
122 { -halfSize, -halfSize, halfSize });
124 return Mesh(vertices, indices, {});
129 if (longitudes < 3) longitudes = 3;
130 if (latitudes < 2) latitudes = 2;
132 const float pi = std::acos(-1.0f);
133 std::vector<Vertex> vertices;
134 std::vector<uint32_t> indices;
136 float latStep = pi / latitudes;
137 float longStep = 2 * pi / longitudes;
139 for (
int lat = 0; lat <= latitudes; ++lat)
141 float phi = (pi / 2) - (lat * latStep);
142 const float xy = radius * cosf(phi);
143 const float z = radius * sinf(phi);
145 for (
int lon = 0; lon <= longitudes; ++lon)
147 float theta = lon * longStep;
156 (float) lon / longitudes,
157 (
float) lat / latitudes
160 vertices.push_back(v);
164 for (
int lat = 0; lat < latitudes; ++lat)
166 for (
int lon = 0; lon < longitudes; ++lon)
168 const uint32_t curr = lat * (longitudes + 1) + lon;
169 const uint32_t next = curr + longitudes + 1;
171 indices.insert(indices.end(), {
172 curr, next, curr + 1,
173 next, next + 1, curr + 1
178 return Mesh(vertices, indices, {});
192 std::vector<Vertex> vertices;
193 std::vector<uint32_t> indices;
195 float step = size / resolution;
196 float offset = size * 0.5f;
198 for (
int i = 0; i <= resolution; i++) {
199 for (
int j = 0; j <= resolution; j++) {
201 v.
Position = { j * step - offset, 0.0f, i * step - offset };
202 v.
Normal = { 0.0f, 1.0f, 0.0f };
203 v.
TexCoords = { (float)j / resolution, (
float)i / resolution };
204 vertices.push_back(v);
208 for (
int i = 0; i < resolution; i++) {
209 for (
int j = 0; j < resolution; j++) {
210 int row1 = i * (resolution + 1);
211 int row2 = (i + 1) * (resolution + 1);
213 uint32_t v0 = row1 + j;
214 uint32_t v1 = row1 + j + 1;
215 uint32_t v2 = row2 + j;
216 uint32_t v3 = row2 + j + 1;
218 indices.push_back(v0);
219 indices.push_back(v2);
220 indices.push_back(v1);
222 indices.push_back(v1);
223 indices.push_back(v2);
224 indices.push_back(v3);
228 return Mesh(vertices, indices, {});
233 size_t indexOffset = 0;
235 std::vector<Vertex> vertices;
236 std::vector<uint32_t> indices;
237 std::vector<std::shared_ptr<Texture>> textures;
239 for (
const Mesh& mesh : meshes)
243 mesh.m_data.Vertices.begin(),
244 mesh.m_data.Vertices.end()
247 for (uint32_t index : mesh.m_data.Indices)
249 indices.push_back(index + (uint32_t)indexOffset);
251 indexOffset += mesh.m_data.
Vertices.size();
253 for (std::shared_ptr<Texture> tex : mesh.m_data.Textures)
258 for (std::shared_ptr<Texture> insertedTex : textures)
260 if (insertedTex->GetNativeHandle() == tex->GetNativeHandle())
269 textures.push_back(tex);
274 return Mesh(vertices, indices, textures);