-
Notifications
You must be signed in to change notification settings - Fork 4
/
Voxels.h
65 lines (51 loc) · 1.95 KB
/
Voxels.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
#pragma once
#include <vector>
#include <glm/glm.hpp>
#define VOXEL_SIZE (0.25f)
struct VoxelSet {
glm::ivec3 size;
std::vector<glm::vec4> colors;
// Constructs a voxel set of the given dimensions
inline VoxelSet(glm::ivec3 size) {
int elemCount = size.x * size.y * size.z;
this->size = size;
colors.resize(elemCount);
for (int i = 0; i < elemCount; ++i) {
colors[i] = glm::vec4(0, 0, 0, 0);
}
}
// Returns true if the given index is valid for the voxel set
inline bool IsValid(glm::ivec3 idx) {
return idx.x >= 0 && idx.y >= 0 && idx.z >= 0
&& idx.x < size.x && idx.y < size.y && idx.z < size.z;
}
// Returns true if idx is a valid index and represents a solid voxel; false otherwise.
inline bool IsSolid(glm::ivec3 idx) {
return IsValid(idx) && At(idx).a > 0.1f;
}
// Access the voxel at the given index
inline glm::vec4& At(glm::ivec3 idx) {
return colors[idx.z * (size.x * size.y) + idx.y * size.x + idx.x];
}
// Make the voxel set into a colored sphere
inline void MakeSphere() {
float radius = size.x / 2.0f - 0.5f;
glm::vec3 color = 1.0f / (glm::vec3(size) - glm::vec3(1, 1, 1));
glm::vec3 center = glm::vec3(size) / 2.0f;
for (int z = 0; z < size.z; ++z) {
for (int y = 0; y < size.y; ++y) {
for (int x = 0; x < size.x; ++x) {
glm::ivec3 idx(x, y, z);
glm::vec3 delta = (glm::vec3(idx) + glm::vec3(0.5f, 0.5f, 0.5f)) - center;
if ((glm::dot(delta, delta) - 0.1f) <= radius * radius) {
// Inside sphere
At(idx) = glm::vec4(color * glm::vec3(idx), 1.0f);
} else {
// Outside sphere
At(idx) = glm::vec4(0, 0, 0, 0);
}
}
}
}
}
};