-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel.cpp
More file actions
138 lines (118 loc) · 3.89 KB
/
Level.cpp
File metadata and controls
138 lines (118 loc) · 3.89 KB
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
#include "template.h"
#include "Level.h"
#include "MaterialManager.h"
#include "ogt_vox.h"
#include "scene.h"
#include "Sphere.h"
void Level::SetupLazyLoadVOX(const std::string& filename)
{
Init([filename](Level* self)
{
FILE* fp = fopen(filename.c_str(), "rb");
const uint32_t buffer_size = _filelength(_fileno(fp));
uint8_t* buffer = new uint8_t[buffer_size];
fread(buffer, buffer_size, 1, fp);
fclose(fp);
const ogt_vox_scene* voxscene = ogt_vox_read_scene(buffer, buffer_size);
// the buffer can be safely deleted once the scene is instantiated.
delete[] buffer;
for (int dataid = 0; dataid < 256; dataid++)
{
ogt_vox_rgba voxucolor = voxscene->palette.color[dataid];
uint ucolor = R_G_B_to_RGB24(voxucolor.r, voxucolor.g, voxucolor.b);
float3 fcolor = RGB8_to_RGBF32(ucolor);
self->m_materialManager.Import(voxscene->materials.matl[dataid], fcolor);
}
});
m_load = [filename](Inputs /*inputs*/, Outputs& outputs)
{
FILE* fp = fopen(filename.c_str(), "rb");
const uint32_t buffer_size = _filelength(_fileno(fp));
uint8_t* buffer = new uint8_t[buffer_size];
fread(buffer, buffer_size, 1, fp);
fclose(fp);
const ogt_vox_scene* voxscene = ogt_vox_read_scene(buffer, buffer_size);
// the buffer can be safely deleted once the scene is instantiated.
delete[] buffer;
const size_t modelIndex = 0;
int voxid = 0;
for (uint z = 0; z < voxscene->models[modelIndex]->size_z; ++z)
for (uint y = 0; y < voxscene->models[modelIndex]->size_y; ++y) // inverted X and Y, legacy, since all scenes account for the error
for (uint x = 0; x < voxscene->models[modelIndex]->size_x; ++x)
{
const uint8_t dataid = voxscene->models[modelIndex]->voxel_data[voxid];
Material& mat = outputs.m_self->m_materialManager.GetMaterialRW(dataid);
mat.m_isUsed = true;
ogt_vox_rgba color{ 0, 0, 0, 0 };
if (dataid != 0)
{
color = voxscene->palette.color[dataid];
}
outputs.m_scene.Set(y, z, x, Voxel{ dataid });
voxid++;
}
};
}
void Level::Init(std::function<void(Level* self)> func)
{
func(this);
}
void Level::SetupLazyGenerate(std::function<void(Inputs, Outputs&)> func)
{
m_load = func;
}
void Level::Load(Inputs inputs, Outputs& outputs) const
{
m_load(inputs, outputs);
}
void Level::FillBoxSolid(Outputs& out, int3 min, int3 max, Material::ID_T mat)
{
FillBox(out, min, max, [mat](int3 /*worldPos*/, int3 /*localPos*/, float3 /*relPos*/)
{
return mat;
});
}
void Level::FillBox(Outputs& out, int3 min, int3 max, Shader3D func)
{
min = clamp(min, int3(0), int3(WORLDSIZE));
max = clamp(max, int3(0), int3(WORLDSIZE));
const int3 size = max - min;
const float3 invSize = 1.0f / float3(size);
#pragma omp parallel for schedule(static)
for (int x = 0; x < size.x; ++x)
for (int y = 0; y < size.y; ++y)
for (int z = 0; z < size.z; ++z)
{
const int3 localPos(x, y, z);
const int3 worldPos = min + localPos;
const float3 relPos = make_float3(localPos) * invSize;
const Material::ID_T matid = func(worldPos, localPos, relPos);
out.m_scene.Set(worldPos.x, worldPos.y, worldPos.z, Voxel{ matid });
}
}
void Level::FillSphere(Outputs& out, int3 min, int3 max, Shader3D func)
{
FillBox(out, min, max, [func](int3 worldPos, int3 localPos, float3 relPos)
{
if (length(relPos - float3(0.5f)) >= 0.5f) return (Material::ID_T)0;
return func(worldPos, localPos, relPos);
});
}
void Level::FillSphereSolid(Outputs& out, int3 min, int3 max, Material::ID_T mat)
{
FillSphere(out, min, max, [mat](int3 /*worldPos*/, int3 /*localPos*/, float3 /*relPos*/)
{
return mat;
});
}
void Level::AddSphere(float3 center, float radius, Material::ID_T matid)
{
m_spheres.objects.emplace_back(center, radius, matid);
m_hasSpheres = true;
}
void Level::AddSphere(Sphere copy)
{
m_spheres.objects.push_back(copy);
m_materialManager.GetMaterialRW(copy.m_material).Use();
m_hasSpheres = true;
}