/
Geometry_2D.h
98 lines (86 loc) · 3.89 KB
/
Geometry_2D.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
#pragma once
#include <stdexcept>
#include <string>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtx/hash.hpp>
#include <vulkan/vulkan.h>
struct Vertex_2D {
glm::vec2 pos;
glm::vec2 texCoord;
static VkVertexInputBindingDescription getBindingDescription() {
static VkVertexInputBindingDescription bindingDescription = {0, sizeof(Vertex_2D), VK_VERTEX_INPUT_RATE_VERTEX};
return bindingDescription;
}
static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions() {
static std::vector<VkVertexInputAttributeDescription> attributeDescriptions = {{
{0, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex_2D, pos)},
{1, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex_2D, texCoord)}
}};
return attributeDescriptions;
}
};
class Geometry_2D {
public:
std::vector<Vertex_2D> vertices;
std::vector<uint32_t> indices;
void AddFaces(std::vector<glm::vec2>& vertices, std::vector<std::vector<uint32_t>>& faces, std::vector<glm::vec2>& texture_coords) {
for (auto face : faces) {
AddFace(vertices, face, texture_coords);
}
}
void AddFace(std::vector<glm::vec2>& vertices, std::vector<uint32_t>& face, std::vector<glm::vec2>& texture_coords) {
switch (face.size()) {
case 3:
AddTriangle(texture_coords, vertices[face[0]], vertices[face[1]], vertices[face[2]]);
break;
case 4:
AddSquare(texture_coords, vertices[face[0]], vertices[face[1]], vertices[face[2]], vertices[face[3]]);
break;
case 5:
AddPentagon(texture_coords, 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(std::vector<glm::vec2>& texture_coords, glm::vec2& vertex_0, glm::vec2& vertex_1, glm::vec2& vertex_2) {
uint32_t base = static_cast<uint32_t>(vertices.size());
vertices.push_back({vertex_0, texture_coords[0]});
vertices.push_back({vertex_1, texture_coords[1]});
vertices.push_back({vertex_2, texture_coords[2]});
indices.push_back(base + 0);
indices.push_back(base + 1);
indices.push_back(base + 2);
}
void AddSquare(std::vector<glm::vec2>& texture_coords, glm::vec2& vertex_0, glm::vec2& vertex_1, glm::vec2& vertex_2, glm::vec2& vertex_3) {
uint32_t base = static_cast<uint32_t>(vertices.size());
vertices.push_back({vertex_0, texture_coords[0]});
vertices.push_back({vertex_1, texture_coords[1]});
vertices.push_back({vertex_2, texture_coords[2]});
vertices.push_back({vertex_3, texture_coords[3]});
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(std::vector<glm::vec2>& texture_coords, glm::vec2& vertex_0, glm::vec2& vertex_1, glm::vec2& vertex_2, glm::vec2& vertex_3, glm::vec2& vertex_4) {
uint32_t base = static_cast<uint32_t>(vertices.size());
vertices.push_back({vertex_0, texture_coords[0]});
vertices.push_back({vertex_1, texture_coords[1]});
vertices.push_back({vertex_2, texture_coords[2]});
vertices.push_back({vertex_3, texture_coords[3]});
vertices.push_back({vertex_4, texture_coords[4]});
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);
}
};