-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.cpp
119 lines (102 loc) · 4.5 KB
/
block.cpp
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
// block.cpp
#include "block.hpp"
#include "chunk.hpp"
constexpr COLOR color = 0xFFFF;
const std::array<VERTEX, 24> Block::origin_cube_vertices = {
VERTEX(0 , 0 , block_size, 0 , 0 , color),
VERTEX(0 , 0 , 0 , tex_size, 0 , color),
VERTEX(0 , block_size, 0 , tex_size, tex_size, color),
VERTEX(0 , block_size, block_size, 0 , tex_size, color),
VERTEX(block_size, 0 , 0 , 0 , 0 , color),
VERTEX(block_size, 0 , block_size, tex_size, 0 , color),
VERTEX(block_size, block_size, block_size, tex_size, tex_size, color),
VERTEX(block_size, block_size, 0 , 0 , tex_size, color),
VERTEX(0 , 0 , 0 , 0 , 0 , color),
VERTEX(0 , 0 , block_size, tex_size, 0 , color),
VERTEX(block_size, 0 , block_size, tex_size, tex_size, color),
VERTEX(block_size, 0 , 0 , 0 , tex_size, color),
VERTEX(0 , block_size, 0 , 0 , 0 , color),
VERTEX(block_size, block_size, 0 , tex_size, 0 , color),
VERTEX(block_size, block_size, block_size, tex_size, tex_size, color),
VERTEX(0 , block_size, block_size, 0 , tex_size, color),
VERTEX(0 , 0 , 0 , 0 , 0 , color),
VERTEX(block_size, 0 , 0 , tex_size, 0 , color),
VERTEX(block_size, block_size, 0 , tex_size, tex_size, color),
VERTEX(0 , block_size, 0 , 0 , tex_size, color),
VERTEX(0 , 0 , block_size, 0 , 0 , color),
VERTEX(block_size, 0 , block_size, tex_size, 0 , color),
VERTEX(block_size, block_size, block_size, tex_size, tex_size, color),
VERTEX(0 , block_size, block_size, 0 , tex_size, color),
};
auto& vi = CubicChunk::xyz_to_vert_idx;
const std::array<std::array<IndexedVertex, 4>, 6> Block::origin_cube_ivertices{{
{
IndexedVertex(vi(0, 0, 1), GLFix{0}, GLFix{0}, color),
IndexedVertex(vi(0, 0, 0), tex_size, GLFix{0}, color),
IndexedVertex(vi(0, 1, 0), tex_size, tex_size, color),
IndexedVertex(vi(0, 1, 1), GLFix{0}, tex_size, color),
}, {
IndexedVertex(vi(1, 0, 0), GLFix{0}, GLFix{0}, color),
IndexedVertex(vi(1, 0, 1), tex_size, GLFix{0}, color),
IndexedVertex(vi(1, 1, 1), tex_size, tex_size, color),
IndexedVertex(vi(1, 1, 0), GLFix{0}, tex_size, color),
}, {
IndexedVertex(vi(0, 0, 0), GLFix{0}, GLFix{0}, color),
IndexedVertex(vi(0, 0, 1), tex_size, GLFix{0}, color),
IndexedVertex(vi(1, 0, 1), tex_size, tex_size, color),
IndexedVertex(vi(1, 0, 0), GLFix{0}, tex_size, color),
}, {
IndexedVertex(vi(0, 1, 0), GLFix{0}, GLFix{0}, color),
IndexedVertex(vi(1, 1, 0), tex_size, GLFix{0}, color),
IndexedVertex(vi(1, 1, 1), tex_size, tex_size, color),
IndexedVertex(vi(0, 1, 1), GLFix{0}, tex_size, color),
}, {
IndexedVertex(vi(0, 0, 0), GLFix{0}, GLFix{0}, color),
IndexedVertex(vi(1, 0, 0), tex_size, GLFix{0}, color),
IndexedVertex(vi(1, 1, 0), tex_size, tex_size, color),
IndexedVertex(vi(0, 1, 0), GLFix{0}, tex_size, color),
}, {
IndexedVertex(vi(0, 0, 1), GLFix{0}, GLFix{0}, color),
IndexedVertex(vi(1, 0, 1), tex_size, GLFix{0}, color),
IndexedVertex(vi(1, 1, 1), tex_size, tex_size, color),
IndexedVertex(vi(0, 1, 1), GLFix{0}, tex_size, color),
}
}};
void Block::write_vertices(std::vector<VERTEX>& vertices,
VECTOR3 block_pos, std::array<bool, 6> faces) const
{
for (unsigned int idx = 0; idx < origin_cube_vertices.size(); idx++)
{
if (faces[idx / 4] == false) continue;
VERTEX vertex = origin_cube_vertices[idx];
vertices.emplace_back(
vertex.x + block_pos.x,
vertex.y + block_pos.y,
vertex.z + block_pos.z,
vertex.u + type * static_cast<int>(tex_size),
vertex.v,
vertex.c
);
}
}
void Block::write_ivertices(std::vector<IndexedVertex>& iverts,
VECTOR3 subchunk_pos, std::array<bool, 6> faces) const
{
// The indices for vertex positions are in a 3D array of size
// (dim+1, dim+1, dim+1) where dim is the dimension of the chunk (CubicChunk::dim).
for (int i = 0; i < 6; ++i)
{
if (faces[i] == false) continue;
// axis is used for choosing which brightness texture to use
int axis = i / 2;
for (IndexedVertex vert : origin_cube_ivertices[i])
{
iverts.emplace_back(
vert.index + vi(subchunk_pos.x, subchunk_pos.y, subchunk_pos.z),
vert.u + static_cast<int>(tex_size) * 4 * axis,
vert.v + static_cast<int>(tex_size) * 4 * type,
vert.c
);
}
}
}