6 #include <nanovdb/tools/GridBuilder.h> 7 #include <nanovdb/tools/CreateNanoGrid.h> 9 #define OGT_VOX_IMPLEMENTATION 17 inline const ogt_vox_scene*
load_vox_scene(
const char* filename, uint32_t scene_read_flags = 0)
19 #if defined(_MSC_VER) && _MSC_VER >= 1400 21 if (0 != fopen_s(&fp, filename,
"rb"))
24 FILE* fp = fopen(filename,
"rb");
28 fseek(fp, 0, SEEK_END);
29 uint32_t buffer_size = ftell(fp);
30 fseek(fp, 0, SEEK_SET);
31 uint8_t* buffer =
new uint8_t[buffer_size];
32 size_t bytes = fread(buffer, buffer_size, 1, fp);
34 const ogt_vox_scene* scene = ogt_vox_read_scene_with_flags(buffer, buffer_size, scene_read_flags);
39 inline nanovdb::Vec4f
matMult4x4(
const float* mat,
const nanovdb::Vec4f& rhs)
41 #define _mat(m, r, c) m[c * 4 + r] 43 return nanovdb::Vec4f(
_mat(mat, 0, 0) * rhs[0] +
_mat(mat, 0, 1) * rhs[1] +
_mat(mat, 0, 2) * rhs[2] +
_mat(mat, 0, 3) * rhs[3],
44 _mat(mat, 1, 0) * rhs[0] +
_mat(mat, 1, 1) * rhs[1] +
_mat(mat, 1, 2) * rhs[2] +
_mat(mat, 1, 3) * rhs[3],
45 _mat(mat, 2, 0) * rhs[0] +
_mat(mat, 2, 1) * rhs[1] +
_mat(mat, 2, 2) * rhs[2] +
_mat(mat, 2, 3) * rhs[3],
46 _mat(mat, 3, 0) * rhs[0] +
_mat(mat, 3, 1) * rhs[1] +
_mat(mat, 3, 2) * rhs[2] +
_mat(mat, 3, 3) * rhs[3]);
50 inline ogt_vox_transform
matMult4x4(
const float* m,
const float* n)
52 #define _mat(m, c, r) m[c * 4 + r] 54 return ogt_vox_transform{
55 _mat(m, 0, 0) *
_mat(n, 0, 0) +
_mat(m, 0, 1) *
_mat(n, 1, 0) +
_mat(m, 0, 2) *
_mat(n, 2, 0) +
_mat(m, 0, 3) *
_mat(n, 3, 0),
56 _mat(m, 0, 0) *
_mat(n, 0, 1) +
_mat(m, 0, 1) *
_mat(n, 1, 1) +
_mat(m, 0, 2) *
_mat(n, 2, 1) +
_mat(m, 0, 3) *
_mat(n, 3, 1),
57 _mat(m, 0, 0) *
_mat(n, 0, 2) +
_mat(m, 0, 1) *
_mat(n, 1, 2) +
_mat(m, 0, 2) *
_mat(n, 2, 2) +
_mat(m, 0, 3) *
_mat(n, 3, 2),
58 _mat(m, 0, 0) *
_mat(n, 0, 3) +
_mat(m, 0, 1) *
_mat(n, 1, 3) +
_mat(m, 0, 2) *
_mat(n, 2, 3) +
_mat(m, 0, 3) *
_mat(n, 3, 3),
60 _mat(m, 1, 0) *
_mat(n, 0, 0) +
_mat(m, 1, 1) *
_mat(n, 1, 0) +
_mat(m, 1, 2) *
_mat(n, 2, 0) +
_mat(m, 1, 3) *
_mat(n, 3, 0),
61 _mat(m, 1, 0) *
_mat(n, 0, 1) +
_mat(m, 1, 1) *
_mat(n, 1, 1) +
_mat(m, 1, 2) *
_mat(n, 2, 1) +
_mat(m, 1, 3) *
_mat(n, 3, 1),
62 _mat(m, 1, 0) *
_mat(n, 0, 2) +
_mat(m, 1, 1) *
_mat(n, 1, 2) +
_mat(m, 1, 2) *
_mat(n, 2, 2) +
_mat(m, 1, 3) *
_mat(n, 3, 2),
63 _mat(m, 1, 0) *
_mat(n, 0, 3) +
_mat(m, 1, 1) *
_mat(n, 1, 3) +
_mat(m, 1, 2) *
_mat(n, 2, 3) +
_mat(m, 1, 3) *
_mat(n, 3, 3),
65 _mat(m, 2, 0) *
_mat(n, 0, 0) +
_mat(m, 2, 1) *
_mat(n, 1, 0) +
_mat(m, 2, 2) *
_mat(n, 2, 0) +
_mat(m, 2, 3) *
_mat(n, 3, 0),
66 _mat(m, 2, 0) *
_mat(n, 0, 1) +
_mat(m, 2, 1) *
_mat(n, 1, 1) +
_mat(m, 2, 2) *
_mat(n, 2, 1) +
_mat(m, 2, 3) *
_mat(n, 3, 1),
67 _mat(m, 2, 0) *
_mat(n, 0, 2) +
_mat(m, 2, 1) *
_mat(n, 1, 2) +
_mat(m, 2, 2) *
_mat(n, 2, 2) +
_mat(m, 2, 3) *
_mat(n, 3, 2),
68 _mat(m, 2, 0) *
_mat(n, 0, 3) +
_mat(m, 2, 1) *
_mat(n, 1, 3) +
_mat(m, 2, 2) *
_mat(n, 2, 3) +
_mat(m, 2, 3) *
_mat(n, 3, 3),
70 _mat(m, 3, 0) *
_mat(n, 0, 0) +
_mat(m, 3, 1) *
_mat(n, 1, 0) +
_mat(m, 3, 2) *
_mat(n, 2, 0) +
_mat(m, 3, 3) *
_mat(n, 3, 0),
71 _mat(m, 3, 0) *
_mat(n, 0, 1) +
_mat(m, 3, 1) *
_mat(n, 1, 1) +
_mat(m, 3, 2) *
_mat(n, 2, 1) +
_mat(m, 3, 3) *
_mat(n, 3, 1),
72 _mat(m, 3, 0) *
_mat(n, 0, 2) +
_mat(m, 3, 1) *
_mat(n, 1, 2) +
_mat(m, 3, 2) *
_mat(n, 2, 2) +
_mat(m, 3, 3) *
_mat(n, 3, 2),
73 _mat(m, 3, 0) *
_mat(n, 0, 3) +
_mat(m, 3, 1) *
_mat(n, 1, 3) +
_mat(m, 3, 2) *
_mat(n, 2, 3) +
_mat(m, 3, 3) *
_mat(n, 3, 3),
78 ogt_vox_transform
getXform(
const ogt_vox_scene& scene,
const ogt_vox_instance& instance)
80 ogt_vox_transform transform = instance.transform;
82 auto groupIndex = instance.group_index;
83 while (groupIndex != 0 && groupIndex != k_invalid_group_index) {
84 const auto& group = scene.groups[groupIndex];
85 transform =
matMult4x4((
const float*)&transform, (
const float*)&group.transform);
86 groupIndex = group.parent_group_index;
92 bool isVisible(
const ogt_vox_scene& scene,
const ogt_vox_instance& instance)
97 if (scene.layers[instance.layer_index].hidden)
100 auto groupIndex = instance.group_index;
101 while (groupIndex != 0 && groupIndex != k_invalid_group_index) {
102 const auto& group = scene.groups[groupIndex];
105 if (scene.layers[group.layer_index].hidden)
107 groupIndex = group.parent_group_index;
108 printf(
"group.parent_group_index = %d\n", groupIndex);
116 template<
typename BufferT = nanovdb::HostBuffer>
122 ogt_vox_transform translate{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1};
123 ogt_vox_transform
scale{10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 1};
124 ogt_vox_transform translate2{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1};
128 std::cout << v[0] <<
' ' << v[1] <<
' ' << v[2] <<
'\n';
136 auto acc = grid.getAccessor();
138 auto processModelFn = [&](
int modelIndex,
const ogt_vox_transform& xform) {
139 const auto* model = scene->models[modelIndex];
141 uint32_t voxel_index = 0;
142 for (uint32_t z = 0; z < model->size_z; ++z) {
143 for (uint32_t y = 0; y < model->size_y; ++y) {
144 for (uint32_t x = 0; x < model->size_x; ++x, ++voxel_index) {
145 if (uint8_t color_index = model->voxel_data[voxel_index]) {
146 ogt_vox_rgba rgba = scene->palette.color[color_index];
148 acc.setValue(nanovdb::Coord(ijk[0], ijk[2], -ijk[1]), *reinterpret_cast<nanovdb::math::Rgba8*>(&rgba));
155 if (scene->num_instances > 0) {
156 printf(
"scene processing begin... %d instances\n", scene->num_instances);
158 for (uint32_t instanceIndex = 0; instanceIndex < scene->num_instances; instanceIndex++) {
159 const auto& instance = scene->instances[instanceIndex];
160 uint32_t modelIndex = instance.model_index;
171 auto xform = instance.transform;
173 processModelFn(modelIndex, xform);
176 printf(
"scene processing begin... %d models\n", scene->num_models);
178 ogt_vox_transform xform{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
180 for (uint32_t modelIndex = 0; modelIndex < scene->num_models; modelIndex++) {
181 processModelFn(modelIndex, xform);
186 printf(
"scene processing end.\n");
187 ogt_vox_destroy_scene(scene);
188 return nanovdb::tools::createNanoGrid(grid);
190 std::ostringstream ss;
191 ss <<
"Invalid file \"" << inFilename <<
"\"";
192 throw std::runtime_error(ss.str());
195 catch (
const std::exception& e) {
196 std::cerr <<
"An exception occurred: \"" << e.what() <<
"\"" << std::endl;
const ogt_vox_scene * load_vox_scene(const char *filename, uint32_t scene_read_flags=0)
Definition: VoxToNanoVDB.h:17
bool isVisible(const ogt_vox_scene &scene, const ogt_vox_instance &instance)
Definition: VoxToNanoVDB.h:92
nanovdb::GridHandle< BufferT > convertVoxToNanoVDB(const std::string &inFilename, const std::string &modelName)
load a .vox file.
Definition: VoxToNanoVDB.h:117
This class serves to manage a buffer containing one or more NanoVDB Grids.
Definition: GridHandle.h:37
Definition: VoxToNanoVDB.h:15
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:615
int Floor(float x)
Return the floor of x.
Definition: Math.h:848
ogt_vox_transform getXform(const ogt_vox_scene &scene, const ogt_vox_instance &instance)
Definition: VoxToNanoVDB.h:78
nanovdb::Vec4f matMult4x4(const float *mat, const nanovdb::Vec4f &rhs)
Definition: VoxToNanoVDB.h:39