/
Geometry_Color.h
100 lines (88 loc) · 3.72 KB
/
Geometry_Color.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once
#include <stdexcept>
#include <string>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtx/hash.hpp>
#include <vulkan/vulkan.h>
struct Vertex_Color {
glm::vec3 pos;
glm::vec3 color;
static VkVertexInputBindingDescription getBindingDescription() {
static VkVertexInputBindingDescription bindingDescription = {0, sizeof(Vertex_Color), VK_VERTEX_INPUT_RATE_VERTEX};
return bindingDescription;
}
static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions() {
static std::vector<VkVertexInputAttributeDescription> attributeDescriptions = {{
{0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex_Color, pos)},
{1, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex_Color, color)}
}};
return attributeDescriptions;
}
};
class Geometry_Color {
public:
std::vector<Vertex_Color> vertices;
std::vector<uint32_t> indices;
void AddFaces(std::vector<glm::vec3>& vertices, std::vector<std::vector<uint32_t>>& faces, std::vector<glm::vec3>& colors) {
size_t maxColor = colors.size();
size_t color = 0;
for (auto face : faces) {
AddFace(vertices, face, colors[color++ % maxColor]);
}
}
void AddFace(std::vector<glm::vec3>& vertices, std::vector<uint32_t>& face, glm::vec3& color) {
switch (face.size()) {
case 3:
AddTriangle(color, vertices[face[0]], vertices[face[1]], vertices[face[2]]);
break;
case 4:
AddSquare(color, vertices[face[0]], vertices[face[1]], vertices[face[2]], vertices[face[3]]);
break;
case 5:
AddPentagon(color, vertices[face[0]], vertices[face[1]], vertices[face[2]], vertices[face[3]], vertices[face[4]]);
break;
default:
throw std::runtime_error(std::string{"faces with "} +std::to_string(face.size()) + std::string{" vertices are not supported"});
}
}
void AddTriangle(glm::vec3 color, glm::vec3& vertex_0, glm::vec3& vertex_1, glm::vec3& vertex_2) {
uint32_t base = static_cast<uint32_t>(vertices.size());
vertices.push_back({vertex_0, color});
vertices.push_back({vertex_1, color});
vertices.push_back({vertex_2, color});
indices.push_back(base + 0);
indices.push_back(base + 1);
indices.push_back(base + 2);
}
void AddSquare(glm::vec3 color, glm::vec3& vertex_0, glm::vec3& vertex_1, glm::vec3& vertex_2, glm::vec3& vertex_3) {
uint32_t base = static_cast<uint32_t>(vertices.size());
vertices.push_back({vertex_0, color});
vertices.push_back({vertex_1, color});
vertices.push_back({vertex_2, color});
vertices.push_back({vertex_3, color});
indices.push_back(base + 0);
indices.push_back(base + 1);
indices.push_back(base + 2);
indices.push_back(base + 2);
indices.push_back(base + 3);
indices.push_back(base + 0);
}
void AddPentagon(glm::vec3 color, glm::vec3& vertex_0, glm::vec3& vertex_1, glm::vec3& vertex_2, glm::vec3& vertex_3, glm::vec3& vertex_4) {
uint32_t base = static_cast<uint32_t>(vertices.size());
vertices.push_back({vertex_0, color});
vertices.push_back({vertex_1, color});
vertices.push_back({vertex_2, color});
vertices.push_back({vertex_3, color});
vertices.push_back({vertex_4, color});
indices.push_back(base + 0);
indices.push_back(base + 3);
indices.push_back(base + 4);
indices.push_back(base + 0);
indices.push_back(base + 1);
indices.push_back(base + 3);
indices.push_back(base + 1);
indices.push_back(base + 2);
indices.push_back(base + 3);
}
};