aboutsummaryrefslogtreecommitdiff
path: root/source/CommonVertexIndex.cpp
blob: 786274e07fd9d9d15a9ee8da5702ee2fd6f8acba (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "CommonVertexIndex.hpp"

template <class TNumber>
static void AssignIndices(TNumber indices[6], TNumber startIdx) {
	// Triangle #1
	indices[0] = startIdx + 1; // Top right
	indices[1] = startIdx + 0; // Top left
	indices[2] = startIdx + 3; // Bottom left
	// Triangle #2
	indices[3] = startIdx + 1; // Top right
	indices[4] = startIdx + 3; // Bottom left
	indices[5] = startIdx + 2; // Bottom right
}

template <class TNumber>
static void AssignIndices(TNumber indices[6], TNumber topLeft, TNumber topRight, TNumber bottomRight, TNumber bottomLeft) {
	// Triangle #1
	indices[0] = topRight;
	indices[1] = topLeft;
	indices[2] = bottomLeft;
	// Triangle #2
	indices[3] = topRight;
	indices[4] = bottomLeft;
	indices[5] = bottomRight;
}

template <class TVertex>
static void AssignPositions(TVertex vertices[4], const Rect<float>& rect) {
	// Top left
	vertices[0].x = rect.x0();
	vertices[0].y = rect.y0();
	// Top right
	vertices[1].x = rect.x1();
	vertices[1].y = rect.y0();
	// Bottom right
	vertices[2].x = rect.x1();
	vertices[2].y = rect.y1();
	// Bottom left
	vertices[3].x = rect.x0();
	vertices[3].y = rect.y1();
}

template <class TVertex>
static void AssignPositions(TVertex vertices[4], glm::vec2 bl, glm::vec2 tr) {
	// Top left
	vertices[0].x = bl.x;
	vertices[0].y = tr.y;
	// Top right
	vertices[1].x = tr.x;
	vertices[1].y = tr.y;
	// Bottom right
	vertices[2].x = tr.x;
	vertices[2].y = bl.y;
	// Bottom left
	vertices[3].x = bl.x;
	vertices[3].y = bl.y;
}

template <class TVertex>
static void AssignDepths(TVertex vertices[4], float z) {
	for (int i = 0; i < 4; ++i) {
		auto& vert = vertices[i];
		vert.z = z;
	}
}

template <class TVertex>
static void AssignTexCoords(TVertex vertices[4], const Subregion& texcoords) {
	// Top left
	vertices[0].u = texcoords.u0;
	vertices[0].v = texcoords.v1;
	// Top right
	vertices[1].u = texcoords.u1;
	vertices[1].v = texcoords.v1;
	// Bottom right
	vertices[2].u = texcoords.u1;
	vertices[2].v = texcoords.v0;
	// Bottom left
	vertices[3].u = texcoords.u0;
	vertices[3].v = texcoords.v0;
}

template <class TVertex>
static void AssignColors(TVertex vertices[4], RgbaColor color) {
	for (int i = 0; i < 4; ++i) {
		auto& vert = vertices[i];
		vert.r = color.r;
		vert.g = color.g;
		vert.b = color.b;
		vert.a = color.a;
	}
}

void Index_U16::Assign(uint16_t indices[6], uint16_t startIdx) {
	::AssignIndices(indices, startIdx);
}

void Index_U16::Assign(uint16_t indices[6], uint16_t startIdx, uint16_t topLeft, uint16_t topRight, uint16_t bottomRight, uint16_t bottomLeft) {
	::AssignIndices<uint16_t>(indices, startIdx + topLeft, startIdx + topRight, startIdx + bottomRight, startIdx + bottomLeft);
}

void Index_U16::Assign(uint16_t indices[6], uint16_t topLeft, uint16_t topRight, uint16_t bottomRight, uint16_t bottomLeft) {
	::AssignIndices<uint16_t>(indices, topLeft, topRight, bottomRight, bottomLeft);
}

void Index_U32::Assign(uint32_t indices[6], uint32_t startIdx) {
	::AssignIndices(indices, startIdx);
}

void Index_U32::Assign(uint32_t indices[6], uint32_t startIdx, uint32_t topLeft, uint32_t topRight, uint32_t bottomRight, uint32_t bottomLeft) {
	::AssignIndices<uint32_t>(indices, startIdx + topLeft, startIdx + topRight, startIdx + bottomRight, startIdx + bottomLeft);
}

void Index_U32::Assign(uint32_t indices[6], uint32_t topLeft, uint32_t topRight, uint32_t bottomRight, uint32_t bottomLeft) {
	::AssignIndices<uint32_t>(indices, topLeft, topRight, bottomRight, bottomLeft);
}

void Vertex_PC::Assign(Vertex_PC vertices[4], const Rect<float>& rect) {
	::AssignPositions(vertices, rect);
}

void Vertex_PC::Assign(Vertex_PC vertices[4], glm::vec2 bottomLeft, glm::vec2 topRight) {
	::AssignPositions(vertices, bottomLeft, topRight);
}

void Vertex_PC::Assign(Vertex_PC vertices[4], float z) {
	::AssignDepths(vertices, z);
}

void Vertex_PC::Assign(Vertex_PC vertices[4], RgbaColor color) {
	::AssignColors(vertices, color);
}

void Vertex_PTC::Assign(Vertex_PTC vertices[4], const Rect<float>& rect) {
	::AssignPositions(vertices, rect);
}

void Vertex_PTC::Assign(Vertex_PTC vertices[4], glm::vec2 bottomLeft, glm::vec2 topRight) {
	::AssignPositions(vertices, bottomLeft, topRight);
}

void Vertex_PTC::Assign(Vertex_PTC vertices[4], float z) {
	::AssignDepths(vertices, z);
}

void Vertex_PTC::Assign(Vertex_PTC vertices[4], const Subregion& texcoords) {
	::AssignTexCoords(vertices, texcoords);
}

void Vertex_PTC::Assign(Vertex_PTC vertices[4], RgbaColor color) {
	::AssignColors(vertices, color);
}