CG-Project2
Loading...
Searching...
No Matches
MeshVertices.hpp
1#pragma once
2
3#include <glm/ext/scalar_constants.hpp>
4#include <glm/geometric.hpp>
5#include <glm/glm.hpp>
6#include <iostream>
7#include <vector>
8#include "Utils.hpp"
9
10const float PI = glm::pi<float>();
11
15struct MeshInfo {
17 std::vector<glm::vec3> vertex{};
19 std::vector<glm::vec3> normals{};
21 std::vector<glm::vec2> texCoords{};
23 std::vector<unsigned int> indices{};
24};
25
26inline std::vector<glm::vec3> skyboxGeometry{
27 // vertex pos
28 {-1.0f, -1.0f, -1.0f},
29 {1.0f, 1.0f, -1.0f},
30 {1.0f, -1.0f, -1.0f},
31 {1.0f, 1.0f, -1.0f},
32 {-1.0f, -1.0f, -1.0f},
33 {-1.0f, 1.0f, -1.0f},
34
35 {-1.0f, -1.0f, 1.0f},
36 {1.0f, -1.0f, 1.0f},
37 {1.0f, 1.0f, 1.0f},
38 {1.0f, 1.0f, 1.0f},
39 {-1.0f, 1.0f, 1.0f},
40 {-1.0f, -1.0f, 1.0f},
41
42 {-1.0f, 1.0f, 1.0f},
43 {-1.0f, 1.0f, -1.0f},
44 {-1.0f, -1.0f, -1.0f},
45 {-1.0f, -1.0f, -1.0f},
46 {-1.0f, -1.0f, 1.0f},
47 {-1.0f, 1.0f, 1.0f},
48
49 {1.0f, 1.0f, 1.0f},
50 {1.0f, -1.0f, -1.0f},
51 {1.0f, 1.0f, -1.0f},
52 {1.0f, -1.0f, -1.0f},
53 {1.0f, 1.0f, 1.0f},
54 {1.0f, -1.0f, 1.0f},
55
56 {-1.0f, -1.0f, -1.0f},
57 {1.0f, -1.0f, -1.0f},
58 {1.0f, -1.0f, 1.0f},
59 {1.0f, -1.0f, 1.0f},
60 {-1.0f, -1.0f, 1.0f},
61 {-1.0f, -1.0f, -1.0f},
62
63 {-1.0f, 1.0f, -1.0f},
64 {1.0f, 1.0f, 1.0f},
65 {1.0f, 1.0f, -1.0f},
66 {1.0f, 1.0f, 1.0f},
67 {-1.0f, 1.0f, -1.0f},
68 {-1.0f, 1.0f, 1.0f}};
69
70// cube
71inline std::vector<glm::vec3> cubeGeometry{
72 {-1, -1, -1},
73 {-1, 1, -1},
74 {1, -1, -1},
75 {1, 1, -1},
76
77 {-1, -1, 1},
78 {-1, 1, 1},
79 {1, -1, 1},
80 {1, 1, 1},
81
82 {-1, -1, 1},
83 {-1, 1, 1},
84 {-1, -1, -1},
85 {-1, 1, -1},
86
87 {1, -1, 1},
88 {1, 1, 1},
89 {1, -1, -1},
90 {1, 1, -1},
91
92 {-1, 1, -1},
93 {-1, 1, 1},
94 {1, 1, -1},
95 {1, 1, 1},
96
97 {-1, -1, -1},
98 {-1, -1, 1},
99 {1, -1, -1},
100 {1, -1, 1},
101};
102
103inline std::vector<unsigned int> cubeIndices{
104 0, 3, 2, 1, 3, 0,
105 6, 7, 4, 4, 7, 5,
106 8, 11, 10, 9, 11, 8,
107 14, 15, 12, 12, 15, 13,
108 16, 19, 18, 17, 19, 16,
109 22, 23, 20, 20, 23, 21};
110inline std::vector<glm::vec2> cubeTexCoord{
111 // texCoord
112 {0.0f, 0.0f},
113 {1.0f, 0.0f},
114 {1.0f, 1.0f},
115 {0.0f, 1.0f},
116
117 {0.0f, 0.0f},
118 {1.0f, 0.0f},
119 {1.0f, 1.0f},
120 {0.0f, 1.0f},
121
122 {0.0f, 0.0f},
123 {1.0f, 0.0f},
124 {1.0f, 1.0f},
125 {0.0f, 1.0f},
126
127 {0.0f, 0.0f},
128 {1.0f, 0.0f},
129 {1.0f, 1.0f},
130 {0.0f, 1.0f},
131
132 {0.0f, 0.0f},
133 {1.0f, 0.0f},
134 {1.0f, 1.0f},
135 {0.0f, 1.0f},
136
137 {0.0f, 0.0f},
138 {1.0f, 0.0f},
139 {1.0f, 1.0f},
140 {0.0f, 1.0f},
141};
142
143inline std::vector<glm::vec3> cubeNormals{
144 {0, 0, -1},
145 {0, 0, -1},
146 {0, 0, -1},
147 {0, 0, -1},
148 {0, 0, 1},
149 {0, 0, 1},
150 {0, 0, 1},
151 {0, 0, 1},
152 {-1, 0, 0},
153 {-1, 0, 0},
154 {-1, 0, 0},
155 {-1, 0, 0},
156 {1, 0, 0},
157 {1, 0, 0},
158 {1, 0, 0},
159 {1, 0, 0},
160 {0, 1, 0},
161 {0, 1, 0},
162 {0, 1, 0},
163 {0, 1, 0},
164 {0, -1, 0},
165 {0, -1, 0},
166 {0, -1, 0},
167 {0, -1, 0},
168};
169
170inline std::vector<glm::vec3> pyramidGeometry{
171 // base
172 {-1, -1, -1},
173 {1, -1, -1},
174 {1, -1, 1},
175 {-1, -1, 1},
176
177 // front
178 {1, -1, 1},
179 {0, 1, 0},
180 {-1, -1, 1},
181
182 // back
183 {-1, -1, -1},
184 {0, 1, 0},
185 {1, -1, -1},
186
187 // right
188 {1, -1, -1},
189 {0, 1, 0},
190 {1, -1, 1},
191
192 // left
193 {-1, -1, 1},
194 {0, 1, 0},
195 {-1, -1, -1},
196};
197
198inline std::vector<unsigned int> pyramidIndices{
199 0, 1, 2, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
200
201inline std::vector<glm::vec3> pyramidNormals{
202 {0, -1, 0},
203 {0, -1, 0},
204 {0, -1, 0},
205 {0, -1, 0},
206 {0, 0, 1},
207 {0, 0, 1},
208 {0, 0, 1},
209 {0, 0, -1},
210 {0, 0, -1},
211 {0, 0, -1},
212 {1, 0, 0},
213 {1, 0, 0},
214 {1, 0, 0},
215 {-1, 0, 0},
216 {-1, 0, 0},
217 {-1, 0, 0},
218 {0, 0, 0},
219};
220
221inline std::vector<glm::vec2> pyramidTexCoords{
222 {0.f, 0.f},
223 {1.f, 0.f},
224 {1.f, 1.f},
225 {0.f, 1.f},
226 {1.f, 0.f},
227 {0.5f, 1.f},
228 {0.f, 1.f},
229 {1.f, 0.f},
230 {0.5f, 1.f},
231 {0.f, 1.f},
232 {1.f, 0.f},
233 {0.5f, 1.f},
234 {0.f, 1.f},
235 {1.f, 0.f},
236 {0.5f, 1.f},
237 {0.f, 1.f},
238};
239
240// sphere
241inline MeshInfo getSphereVertices(const glm::vec3 &center = {0, 0, 0}, const glm::vec3 &radius = {1, 1, 1}, const unsigned int &stacks = 30, const unsigned int &slices = 30) {
242 MeshInfo coords{};
243 float s, t;
244
245 for (auto i = 0; i <= stacks; ++i) {
246
247 float V = i / (float)stacks;
248 float phi = V * PI;
249
250 for (int j = 0; j <= slices; ++j) {
251
252 float U = j / (float)slices;
253 float theta = U * (PI * 2);
254
255 float x = center.x + radius.x * (cosf(theta) * sinf(phi));
256 float y = center.y + radius.y * cosf(phi);
257 float z = center.z + radius.z * sinf(theta) * sinf(phi);
258
259 coords.vertex.push_back(glm::vec3(x, y, z));
260 coords.normals.push_back(glm::vec3(x, y, z));
261
262 s = U;
263 t = V;
264 coords.texCoords.push_back(glm::vec2(s, t));
265 }
266 }
267
268 for (auto i = 0; i < slices * stacks + slices; ++i) {
269
270 coords.indices.push_back(i);
271 coords.indices.push_back(i + slices + 1);
272 coords.indices.push_back(i + slices);
273
274 coords.indices.push_back(i + slices + 1);
275 coords.indices.push_back(i);
276 coords.indices.push_back(i + 1);
277 }
278 return coords;
279}
280
281inline MeshInfo getUnitCirclePoints(const unsigned int &slices = 20) {
282 MeshInfo coords{};
283 auto step = 2 * PI / slices;
284 float sectorAngle;
285
286 for (auto i = 0; i <= slices; i++) {
287 sectorAngle = i * step;
288 coords.vertex.emplace_back(glm::cos(sectorAngle), glm::sin(sectorAngle), 0);
289 }
290
291 return coords;
292}
293
294inline MeshInfo getCylinderVertices(const float &height = 5.f, const unsigned int &slices = 20, const Pair<float> radius = {1, 1}, const unsigned int &stacks = 20) {
295 MeshInfo coords{};
296 auto unitCircle = getUnitCirclePoints(slices);
297
298 for (auto i = 0; i < 2; i++) {
299 auto h = -height / 2.f + i * height;
300 auto t = 1.f - i;
301
302 for (auto j = 0; j <= slices; j++) {
303 auto uv = unitCircle.vertex[j];
304
305 // Vertices
306 coords.vertex.emplace_back(uv.x * radius.x, uv.y * radius.y, h);
307 // Normals
308 coords.normals.emplace_back(uv);
309 // Texture
310 coords.texCoords.emplace_back((float)j / slices, t);
311 }
312 }
313
314 auto baseCenterIndex = unitCircle.vertex.size();
315 auto topCenterIndex = baseCenterIndex + slices + 1;
316
317 int factor = -1;
318 for (int i = 0; i < 2; i++) {
319 auto h = (height / 2.f) * factor;
320 auto nz = factor;
321
322 coords.vertex.push_back({0, 0, h});
323 coords.normals.push_back({0, 0, nz});
324 coords.texCoords.push_back({0.5f, 0.5f});
325
326 for (int j = 0; j <= slices; j++) {
327 auto uv = unitCircle.vertex[j];
328
329 // Vertices
330 coords.vertex.push_back({uv.x * radius.x, uv.y * radius.y, h});
331 // Normals
332 coords.normals.push_back({0, 0, nz});
333 // Texture
334 coords.texCoords.push_back({-uv.x * 0.5f + 0.5f, -uv.y * 0.5f + 0.5f});
335 }
336 factor *= -1;
337 }
338
339 // indices
340 auto k1 = 0;
341 auto k2 = slices + 1;
342
343 for (auto i = 0; i < slices; i++, k1++, k2++) {
344 coords.indices.emplace_back(k1);
345 coords.indices.emplace_back(k1 + 1);
346 coords.indices.emplace_back(k2);
347
348 coords.indices.emplace_back(k2);
349 coords.indices.emplace_back(k1 + 1);
350 coords.indices.emplace_back(k2 + 1);
351 }
352
353 for (int i = 0, k = baseCenterIndex + 1; i < slices; i++, k++) {
354 coords.indices.push_back(baseCenterIndex);
355 coords.indices.push_back((i < slices - 1) ? k + 1 : baseCenterIndex + 1);
356 coords.indices.push_back(k);
357 }
358
359 for (int i = 0, k = topCenterIndex + 1; i < slices; i++, k++) {
360 coords.indices.push_back(topCenterIndex);
361 coords.indices.push_back(k);
362 coords.indices.push_back((i < slices - 1) ? k + 1 : topCenterIndex + 1);
363 }
364
365 return coords;
366}
367
368inline MeshInfo getThorusVertices(const unsigned int &side = 20, const unsigned int &sector = 20) {
369 MeshInfo coords{};
370 float R = 1, r = 0.5; // radius and minorRadius
371
372 float lenghtInv = 1.f / r;
373 float sectorStep = 2 * PI / sector;
374 float sideStep = 2 * PI / side;
375
376 float sideAngle, sectorAngle, xy, x, y, z;
377 float s, t; // Texture Coords
378
379 // Calc The Vertices
380 for (auto i = 0; i <= side; ++i) {
381 sideAngle = PI - i * sideStep;
382 xy = r * cosf(sideAngle);
383 z = r * sinf(sideAngle);
384
385 // Loop Through slices
386 for (auto j = 0; j <= sector; ++j) {
387 sectorAngle = j * sectorStep;
388
389 x = xy * cosf(sectorAngle);
390 y = xy * sinf(sectorAngle);
391
392 coords.normals.push_back(glm::vec3{x, y, z} * lenghtInv);
393
394 x += R * cosf(sectorAngle);
395 y += R * sinf(sectorAngle);
396 coords.vertex.push_back(glm::vec3(x, y, z));
397
398 // Texture Coord
399 s = (float)j / sector;
400 t = (float)i / side;
401 coords.texCoords.push_back(glm::vec2(s, t));
402 }
403 }
404
405 unsigned int k1, k2;
406 for (int i = 0; i < side; ++i) {
407 k1 = i * (sector + 1); // beginning of current side
408 k2 = k1 + sector + 1; // beginning of next side
409 for (int j = 0; j < sector; ++j, ++k1, ++k2) {
410 coords.indices.push_back(k1);
411 coords.indices.push_back(k2);
412 coords.indices.push_back(k1 + 1);
413 coords.indices.push_back(k1 + 1);
414 coords.indices.push_back(k2);
415 coords.indices.push_back(k2 + 1);
416 }
417 }
418 return coords;
419}
420
421inline std::vector<glm::vec4> getColorVector(const glm::vec4 &color, const size_t &size) {
422 auto res = std::vector<glm::vec4>{};
423 for (size_t i = 0; i < size; i++) {
424 res.push_back(color);
425 }
426 return res;
427}
Utility struct to temporally store mesh vectors.
Definition MeshVertices.hpp:15
std::vector< glm::vec3 > vertex
mesh normalized position coords vertices
Definition MeshVertices.hpp:17
std::vector< glm::vec2 > texCoords
mesh texture coords vertices
Definition MeshVertices.hpp:21
std::vector< glm::vec3 > normals
mesh normal coords vertices
Definition MeshVertices.hpp:19
std::vector< unsigned int > indices
mesh vertex coords indices
Definition MeshVertices.hpp:23
Definition Utils.hpp:4