forked from ThomasSneddon/vxl-renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvxl.cpp
166 lines (131 loc) · 4.2 KB
/
vxl.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "vxl.h"
vxl::vxl(const std::string& filename) :vxl()
{
load(filename);
}
bool vxl::load(const std::string& filename)
{
auto data = read_whole_file(filename);
return load(data.get());
}
bool vxl::load(const void* data)
{
if (!data)
{
return false;
}
purge();
byte* floating_cur = reinterpret_cast<byte*>(const_cast<void*>(data));
memcpy(&_fileheader, floating_cur, sizeof _fileheader);
for (color& color : _fileheader.internal_palette)
{
color.r <<= 2;
color.g <<= 2;
color.b <<= 2;
}
size_t limb_count = _fileheader.limb_count;
_body_data.resize(limb_count);
_headers.resize(limb_count);
_tailers.resize(limb_count);
floating_cur += sizeof _fileheader;
memcpy(_headers.data(), floating_cur, limb_count * sizeof vxl_limb_header);
floating_cur += limb_count * sizeof vxl_limb_header + _fileheader.body_size;
memcpy(_tailers.data(), floating_cur, limb_count * sizeof vxl_limb_tailer);
floating_cur -= _fileheader.body_size;
for (size_t i = 0; i < limb_count; i++)
{
vxl_limb_header& current_header = _headers[i];
vxl_limb_tailer& current_tailer = _tailers[i];
vxl_limb& current_body = _body_data[i];
size_t span_count = current_tailer.xsize * current_tailer.ysize;
current_body.span_data_blocks.resize(span_count);
current_body.span_ends.resize(span_count);
current_body.span_starts.resize(span_count);
byte* span_data_blocks = floating_cur + current_tailer.span_data_offset;
byte* span_start_data = floating_cur + current_tailer.span_start_offset;
byte* span_end_data = floating_cur + current_tailer.span_end_offset;
memcpy(current_body.span_starts.data(), span_start_data, span_count * sizeof uint32_t);
memcpy(current_body.span_ends.data(), span_end_data, span_count * sizeof uint32_t);
for (size_t n = 0; n < span_count; n++)
{
span_data& current_span = current_body.span_data_blocks[n];
current_span.voxels_size = 0;
current_span.voxels.resize(current_tailer.zsize);
if (current_body.span_starts[n] == 0xffffffffu || current_body.span_ends[n] == 0xffffffffu)
{
continue;
}
byte* current_span_data = span_data_blocks + current_body.span_starts[n];
byte* current_span_end = span_data_blocks + current_body.span_ends[n];
size_t current_vox_idx = 0;
byte skip_count, voxel_count, voxel_end;
do
{
skip_count = *current_span_data++;
voxel_count = *current_span_data++;
current_vox_idx += skip_count;
if (voxel_count)
{
memcpy(¤t_span.voxels[current_vox_idx], current_span_data, voxel_count * sizeof voxel);
current_span_data += voxel_count * sizeof voxel;
}
current_vox_idx += voxel_count;
voxel_end = *current_span_data++;
if (voxel_count != voxel_end);
//error report here
} while (current_span_data <= current_span_end);
for (size_t z = 0; z < current_tailer.zsize; z++)
{
if (current_span.voxels[z].color)
current_span.voxels_size++;
}
}
}
return true;
}
bool vxl::is_loaded() const
{
return !_tailers.empty();
}
void vxl::purge()
{
_fileheader = vxl_header();
_body_data.clear();
_headers.clear();
_tailers.clear();
}
file_type vxl::type() const
{
return file_type::vxl;
}
size_t vxl::limb_count() const
{
return _fileheader.limb_count;
}
const vxl_limb_tailer* vxl::limb_tailer(const size_t limb) const
{
if (!is_loaded() || limb >= limb_count())
return nullptr;
return &_tailers[limb];
}
const vxl_limb_header* vxl::limb_header(const size_t limb) const
{
if (!is_loaded() || limb >= limb_count())
return nullptr;
return &_headers[limb];
}
voxel vxl::voxel_lh(const size_t limb, const uint32_t x, const uint32_t y, const uint32_t z) const
{
return voxel_rh(limb, y, x, z);
}
voxel vxl::voxel_rh(const size_t limb, const uint32_t x, const uint32_t y, const uint32_t z) const
{
voxel result;
if (!is_loaded() || limb >= limb_count())
return result;
const vxl_limb_tailer& tailer = _tailers[limb];
const vxl_limb& body = _body_data[limb];
if (x >= tailer.xsize || y >= tailer.ysize || z >= tailer.zsize)
return result;
return body.span_data_blocks[y * tailer.xsize + x].voxels[z];
}