8 #define OGT_VOX_IMPLEMENTATION 16 inline const ogt_vox_scene*
load_vox_scene(
const char* filename, uint32_t scene_read_flags = 0)
18 #if defined(_MSC_VER) && _MSC_VER >= 1400 20 if (0 != fopen_s(&fp, filename,
"rb"))
23 FILE* fp = fopen(filename,
"rb");
27 fseek(fp, 0, SEEK_END);
28 uint32_t buffer_size = ftell(fp);
29 fseek(fp, 0, SEEK_SET);
30 uint8_t* buffer =
new uint8_t[buffer_size];
31 fread(buffer, buffer_size, 1, fp);
33 const ogt_vox_scene* scene = ogt_vox_read_scene_with_flags(buffer, buffer_size, scene_read_flags);
40 #define _mat(m, r, c) m[c * 4 + r] 43 _mat(mat, 1, 0) * rhs[0] +
_mat(mat, 1, 1) * rhs[1] +
_mat(mat, 1, 2) * rhs[2] +
_mat(mat, 1, 3) * rhs[3],
44 _mat(mat, 2, 0) * rhs[0] +
_mat(mat, 2, 1) * rhs[1] +
_mat(mat, 2, 2) * rhs[2] +
_mat(mat, 2, 3) * rhs[3],
45 _mat(mat, 3, 0) * rhs[0] +
_mat(mat, 3, 1) * rhs[1] +
_mat(mat, 3, 2) * rhs[2] +
_mat(mat, 3, 3) * rhs[3]);
49 inline ogt_vox_transform
matMult4x4(
const float* m,
const float* n)
51 #define _mat(m, c, r) m[c * 4 + r] 53 return ogt_vox_transform{
54 _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),
55 _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),
56 _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),
57 _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),
59 _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),
60 _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),
61 _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),
62 _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),
64 _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),
65 _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),
66 _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),
67 _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),
69 _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),
70 _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),
71 _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),
72 _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),
77 ogt_vox_transform
getXform(
const ogt_vox_scene& scene,
const ogt_vox_instance& instance)
79 ogt_vox_transform transform = instance.transform;
81 auto groupIndex = instance.group_index;
82 while (groupIndex != 0 && groupIndex != k_invalid_group_index) {
83 const auto& group = scene.groups[groupIndex];
84 transform =
matMult4x4((
const float*)&transform, (
const float*)&group.transform);
85 groupIndex = group.parent_group_index;
91 bool isVisible(
const ogt_vox_scene& scene,
const ogt_vox_instance& instance)
96 if (scene.layers[instance.layer_index].hidden)
99 auto groupIndex = instance.group_index;
100 while (groupIndex != 0 && groupIndex != k_invalid_group_index) {
101 const auto& group = scene.groups[groupIndex];
104 if (scene.layers[group.layer_index].hidden)
106 groupIndex = group.parent_group_index;
107 printf(
"group.parent_group_index = %d\n", groupIndex);
115 template<
typename BufferT = nanovdb::HostBuffer>
121 ogt_vox_transform translate{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1};
122 ogt_vox_transform
scale{10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 1};
123 ogt_vox_transform translate2{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1};
127 std::cout << v[0] <<
' ' << v[1] <<
' ' << v[2] <<
'\n';
137 auto processModelFn = [&](
int modelIndex,
const ogt_vox_transform& xform) {
138 const auto* model = scene->models[modelIndex];
140 uint32_t voxel_index = 0;
141 for (uint32_t z = 0; z < model->size_z; ++z) {
142 for (uint32_t y = 0; y < model->size_y; ++y) {
143 for (uint32_t x = 0; x < model->size_x; ++x, ++voxel_index) {
144 if (uint8_t color_index = model->voxel_data[voxel_index]) {
145 ogt_vox_rgba rgba = scene->palette.color[color_index];
147 acc.setValue(
nanovdb::Coord(ijk[0], ijk[2], -ijk[1]), *reinterpret_cast<nanovdb::PackedRGBA8*>(&rgba));
154 if (scene->num_instances > 0) {
155 printf(
"scene processing begin... %d instances\n", scene->num_instances);
157 for (uint32_t instanceIndex = 0; instanceIndex < scene->num_instances; instanceIndex++) {
158 const auto& instance = scene->instances[instanceIndex];
159 uint32_t modelIndex = instance.model_index;
170 auto xform = instance.transform;
172 processModelFn(modelIndex, xform);
175 printf(
"scene processing begin... %d models\n", scene->num_models);
177 ogt_vox_transform xform{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
179 for (uint32_t modelIndex = 0; modelIndex < scene->num_models; modelIndex++) {
180 processModelFn(modelIndex, xform);
185 printf(
"scene processing end.\n");
186 ogt_vox_destroy_scene(scene);
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:16
bool isVisible(const ogt_vox_scene &scene, const ogt_vox_instance &instance)
Definition: VoxToNanoVDB.h:91
A simple vector class with three double components, similar to openvdb::math::Vec4.
Definition: NanoVDB.h:1188
Allows for the construction of NanoVDB grids without any dependecy.
Definition: GridBuilder.h:91
nanovdb::GridHandle< BufferT > convertVoxToNanoVDB(const std::string &inFilename, const std::string &modelName)
load a .vox file.
Definition: VoxToNanoVDB.h:116
This class serves to manage a raw memory buffer of a NanoVDB Grid.
Definition: GridHandle.h:70
Vec4< float > Vec4f
Definition: NanoVDB.h:1304
ValueAccessor getAccessor()
Definition: GridBuilder.h:180
Definition: VoxToNanoVDB.h:14
static Coord Floor(const Vec3T &xyz)
Return the largest integer coordinates that are not greater than xyz (node centered conversion)...
Definition: NanoVDB.h:1019
Generates a NanoVDB grid from any volume or function.
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:637
void setGridClass(GridClass mode=GridClass::Unknown)
Definition: GridBuilder.h:201
ogt_vox_transform getXform(const ogt_vox_scene &scene, const ogt_vox_instance &instance)
Definition: VoxToNanoVDB.h:77
GridHandle< BufferT > getHandle(double voxelSize=1.0, const Vec3d &gridOrigin=Vec3d(0), const std::string &name="", const OracleT &oracle=OracleT(), const BufferT &buffer=BufferT())
Return an instance of a GridHandle (invoking move semantics)
Definition: GridBuilder.h:535
Vec3< double > Vec3d
Definition: NanoVDB.h:1174
nanovdb::Vec4f matMult4x4(const float *mat, const nanovdb::Vec4f &rhs)
Definition: VoxToNanoVDB.h:38
Signed (i, j, k) 32-bit integer coordinate class, similar to openvdb::math::Coord.
Definition: NanoVDB.h:859