Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ogt_vox: support material property _media_type #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions src/ogt_vox.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
float m20, m21, m22, m23; // column 2 of 4x4 matrix, 1st three elements = z axis vector, last element always 0.0
float m30, m31, m32, m33; // column 3 of 4x4 matrix. 1st three elements = translation vector, last element always 1.0
} ogt_vox_transform;

ogt_vox_transform ogt_vox_transform_get_identity();
ogt_vox_transform ogt_vox_transform_multiply(const ogt_vox_transform & a, const ogt_vox_transform & b);

Expand Down Expand Up @@ -291,25 +291,34 @@
static const uint32_t k_ogt_vox_matl_have_g = 1 << 12;
static const uint32_t k_ogt_vox_matl_have_media = 1 << 13;

// media type for blend, glass and cloud materials
enum ogt_media_type {
ogt_media_type_absorb, // Absorb media
ogt_media_type_scatter, // Scatter media
ogt_media_type_emit, // Emissive media
ogt_media_type_sss, // Subsurface scattering media
};

// Extended Material Chunk MATL information
typedef struct ogt_vox_matl
{
uint32_t content_flags; // set of k_ogt_vox_matl_* OR together to denote contents available
ogt_matl_type type;
float metal;
float rough;
float spec;
float ior;
float att;
float flux;
float emit;
float ldr;
float trans;
float alpha;
float d;
float sp;
float g;
float media;
uint32_t content_flags; // set of k_ogt_vox_matl_* OR together to denote contents available
ogt_media_type media_type; // media type for blend, glass and cloud materials
ogt_matl_type type;
float metal;
float rough; // roughness
float spec; // specular
float ior; // index of refraction
float att; // attenuation
float flux; // radiant flux (power)
float emit; // emissive
float ldr; // low dynamic range
float trans; // transparency
float alpha;
float d; // density
float sp;
float g;
float media;
} ogt_vox_matl;

// Extended Material Chunk MATL array of materials
Expand Down Expand Up @@ -458,7 +467,7 @@
// sample the model index for a given instance at the given frame
uint32_t ogt_vox_sample_instance_model(const ogt_vox_instance* instance, uint32_t frame_index);

// samples the transform for an instance at a given frame.
// samples the transform for an instance at a given frame.
// ogt_vox_sample_instance_transform_global returns the transform in world space (aka global)
// ogt_vox_sample_instance_transform_local returns the transform relative to its parent group
ogt_vox_transform ogt_vox_sample_instance_transform_global(const ogt_vox_instance* instance, uint32_t frame_index, const ogt_vox_scene* scene);
Expand Down Expand Up @@ -507,7 +516,7 @@
static const uint32_t CHUNK_ID_rCAM = MAKE_VOX_CHUNK_ID('r','C','A','M');

static const uint32_t NAME_MAX_LEN = 256; // max name len = 255 plus 1 for null terminator
static const uint32_t CHUNK_HEADER_LEN = 12; // 4 bytes for each of: chunk_id, chunk_size, chunk_child_size
static const uint32_t CHUNK_HEADER_LEN = 12; // 4 bytes for each of: chunk_id, chunk_size, chunk_child_size

// Some older .vox files will not store a palette, in which case the following palette will be used!
static const uint8_t k_default_vox_palette[256 * 4] = {
Expand Down Expand Up @@ -831,7 +840,7 @@
return data.size();
}

// gets the offset of a pointer that was allocated within this array.
// gets the offset of a pointer that was allocated within this array.
size_t offset_of(void* ptr) const {
size_t unaligned_data = (size_t)&data[0];
size_t unaligned_ptr = (size_t)ptr;
Expand Down Expand Up @@ -862,7 +871,7 @@
return data.alloc_many(num_bytes);
}

// allocates and returns a pointer to many elements of the specified type.
// allocates and returns a pointer to many elements of the specified type.
// if align != 0, will use that alignment, otherwise will align to the size of the type T.
template <class T>
T* alloc_many(size_t num_elements, size_t align=0) {
Expand Down Expand Up @@ -1515,7 +1524,7 @@
// make space in misc_data array for the number of transforms we'll need for this node
ogt_vox_keyframe_transform* keyframes = misc_data.alloc_many<ogt_vox_keyframe_transform>(num_frames);
size_t keyframe_offset = misc_data.offset_of(keyframes);

for (uint32_t i = 0; i < num_frames; i++) {
// Parse the frame dictionary that contains:
// _r : int8 ROTATION (c)
Expand Down Expand Up @@ -1677,6 +1686,20 @@
materials.matl[material_id].type = ogt_matl_type_media;
}
}
materials.matl[material_id].media_type = ogt_media_type_absorb;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also support writing _media_type back out to vox too?

const char* media_type_string = _vox_dict_get_value_as_string(&dict, "_media_type", NULL);
if (media_type_string) {
if (0 == _vox_strcmp(media_type_string,"_scatter")) {
materials.matl[material_id].media_type = ogt_media_type_scatter;
}
else if (0 == _vox_strcmp(media_type_string,"_emit")) {
materials.matl[material_id].media_type = ogt_media_type_emit;
}
else if (0 == _vox_strcmp(media_type_string,"_sss")) {
materials.matl[material_id].media_type = ogt_media_type_sss;
}
}

const char* metal_string = _vox_dict_get_value_as_string(&dict, "_metal", NULL);
if (metal_string) {
materials.matl[material_id].content_flags |= k_ogt_vox_matl_have_metal;
Expand Down Expand Up @@ -1859,7 +1882,7 @@
} // end switch

if (g_progress_callback_func) {
// we indicate progress as 0.8f * amount of buffer read + 0.2f at end after processing
// we indicate progress as 0.8f * amount of buffer read + 0.2f at end after processing
if (!g_progress_callback_func(0.8f*(float)(fp->offset)/(float)(fp->buffer_size), g_progress_callback_user_data))
{
return 0;
Expand Down Expand Up @@ -2443,7 +2466,7 @@
}
}

if (g_progress_callback_func) {
if (g_progress_callback_func) {
// we indicate progress as number of models written, with an extra progress value for ending write
if (!g_progress_callback_func((float)(i + 1)/(float)(scene->num_models + 1), g_progress_callback_user_data))
{
Expand Down Expand Up @@ -2755,7 +2778,7 @@
*main_chunk_child_size = *buffer_size - offset_post_main_chunk;
}

if (g_progress_callback_func) {
if (g_progress_callback_func) {
// we indicate progress as number of models written, with an extra progress value for ending write
g_progress_callback_func(1.0f,g_progress_callback_user_data); // we ignore the return as exiting here anyway
}
Expand Down
Loading