Skip to content

Commit 9215e8d

Browse files
committed
Add reflectivity channel ID
1 parent bdb8a46 commit 9215e8d

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

include/gvox/gvox.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef enum {
3838
#define GVOX_CHANNEL_ID_IOR 6
3939
#define GVOX_CHANNEL_ID_EMISSIVITY 7
4040
#define GVOX_CHANNEL_ID_HARDNESS 8
41+
#define GVOX_CHANNEL_ID_REFLECTIVITY 9
4142
#define GVOX_CHANNEL_ID_LAST_STANDARD 23
4243
#define GVOX_CHANNEL_ID_LAST 31
4344

@@ -50,6 +51,7 @@ typedef enum {
5051
#define GVOX_CHANNEL_BIT_IOR (1 << GVOX_CHANNEL_ID_IOR)
5152
#define GVOX_CHANNEL_BIT_EMISSIVITY (1 << GVOX_CHANNEL_ID_EMISSIVITY)
5253
#define GVOX_CHANNEL_BIT_HARDNESS (1 << GVOX_CHANNEL_ID_HARDNESS)
54+
#define GVOX_CHANNEL_BIT_REFLECTIVITY (1 << GVOX_CHANNEL_ID_REFLECTIVITY)
5355
#define GVOX_CHANNEL_BIT_LAST_STANDARD (1 << GVOX_CHANNEL_ID_LAST_STANDARD)
5456
#define GVOX_CHANNEL_BIT_LAST (1 << GVOX_CHANNEL_ID_LAST)
5557

src/adapters/parse/magicavoxel.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ extern "C" void gvox_parse_adapter_magicavoxel_blit_begin(GvoxBlitContext *blit_
394394

395395
if (num_voxels_in_chunk == 0) {
396396
user_state.offset += chunk_size - sizeof(num_voxels_in_chunk);
397-
}
398-
else {
397+
} else {
399398
user_state.offset += static_cast<size_t>(num_voxels_in_chunk) * 4;
400399
}
401400
} break;
@@ -563,29 +562,27 @@ extern "C" void gvox_parse_adapter_magicavoxel_blit_begin(GvoxBlitContext *blit_
563562
}
564563
}
565564
constexpr auto material_property_ids = std::array{
566-
std::pair<char const *, uint32_t>{"_metal", magicavoxel::MATERIAL_METAL_BIT},
567-
std::pair<char const *, uint32_t>{"_rough", magicavoxel::MATERIAL_ROUGH_BIT},
568-
std::pair<char const *, uint32_t>{"_spec", magicavoxel::MATERIAL_SPEC_BIT},
569-
std::pair<char const *, uint32_t>{"_ior", magicavoxel::MATERIAL_IOR_BIT},
570-
std::pair<char const *, uint32_t>{"_att", magicavoxel::MATERIAL_ATT_BIT},
571-
std::pair<char const *, uint32_t>{"_flux", magicavoxel::MATERIAL_FLUX_BIT},
572-
std::pair<char const *, uint32_t>{"_emit", magicavoxel::MATERIAL_EMIT_BIT},
573-
std::pair<char const *, uint32_t>{"_ldr", magicavoxel::MATERIAL_LDR_BIT},
574-
std::pair<char const *, uint32_t>{"_trans", magicavoxel::MATERIAL_TRANS_BIT},
575-
std::pair<char const *, uint32_t>{"_alpha", magicavoxel::MATERIAL_ALPHA_BIT},
576-
std::pair<char const *, uint32_t>{"_d", magicavoxel::MATERIAL_D_BIT},
577-
std::pair<char const *, uint32_t>{"_sp", magicavoxel::MATERIAL_SP_BIT},
578-
std::pair<char const *, uint32_t>{"_g", magicavoxel::MATERIAL_G_BIT},
579-
std::pair<char const *, uint32_t>{"_media", magicavoxel::MATERIAL_MEDIA_BIT},
565+
std::tuple<char const *, uint32_t, uint32_t>{"_metal", magicavoxel::MATERIAL_METAL_BIT, offsetof(magicavoxel::Material, metal)},
566+
std::tuple<char const *, uint32_t, uint32_t>{"_rough", magicavoxel::MATERIAL_ROUGH_BIT, offsetof(magicavoxel::Material, rough)},
567+
std::tuple<char const *, uint32_t, uint32_t>{"_spec", magicavoxel::MATERIAL_SPEC_BIT, offsetof(magicavoxel::Material, spec)},
568+
std::tuple<char const *, uint32_t, uint32_t>{"_ior", magicavoxel::MATERIAL_IOR_BIT, offsetof(magicavoxel::Material, ior)},
569+
std::tuple<char const *, uint32_t, uint32_t>{"_att", magicavoxel::MATERIAL_ATT_BIT, offsetof(magicavoxel::Material, att)},
570+
std::tuple<char const *, uint32_t, uint32_t>{"_flux", magicavoxel::MATERIAL_FLUX_BIT, offsetof(magicavoxel::Material, flux)},
571+
std::tuple<char const *, uint32_t, uint32_t>{"_emit", magicavoxel::MATERIAL_EMIT_BIT, offsetof(magicavoxel::Material, emit)},
572+
std::tuple<char const *, uint32_t, uint32_t>{"_ldr", magicavoxel::MATERIAL_LDR_BIT, offsetof(magicavoxel::Material, ldr)},
573+
std::tuple<char const *, uint32_t, uint32_t>{"_trans", magicavoxel::MATERIAL_TRANS_BIT, offsetof(magicavoxel::Material, trans)},
574+
std::tuple<char const *, uint32_t, uint32_t>{"_alpha", magicavoxel::MATERIAL_ALPHA_BIT, offsetof(magicavoxel::Material, alpha)},
575+
std::tuple<char const *, uint32_t, uint32_t>{"_d", magicavoxel::MATERIAL_D_BIT, offsetof(magicavoxel::Material, d)},
576+
std::tuple<char const *, uint32_t, uint32_t>{"_sp", magicavoxel::MATERIAL_SP_BIT, offsetof(magicavoxel::Material, sp)},
577+
std::tuple<char const *, uint32_t, uint32_t>{"_g", magicavoxel::MATERIAL_G_BIT, offsetof(magicavoxel::Material, g)},
578+
std::tuple<char const *, uint32_t, uint32_t>{"_media", magicavoxel::MATERIAL_MEDIA_BIT, offsetof(magicavoxel::Material, media)},
580579
};
581-
size_t field_offset = 0;
582-
for (auto const &[mat_str, mat_bit] : material_property_ids) {
580+
for (auto const &[mat_str, mat_bit, offset] : material_property_ids) {
583581
char const *prop_str = temp_dict.get<char const *>(mat_str, NULL);
584582
if (prop_str != nullptr) {
585583
user_state.materials[static_cast<size_t>(material_id)].content_flags |= mat_bit;
586-
*(&user_state.materials[static_cast<size_t>(material_id)].metal + field_offset) = static_cast<float>(atof(prop_str));
584+
*reinterpret_cast<float *>(reinterpret_cast<std::byte *>(&user_state.materials[static_cast<size_t>(material_id)]) + offset) = static_cast<float>(atof(prop_str));
587585
}
588-
++field_offset;
589586
}
590587
} break;
591588
case magicavoxel::CHUNK_ID_MATT: {
@@ -600,6 +597,14 @@ extern "C" void gvox_parse_adapter_magicavoxel_blit_begin(GvoxBlitContext *blit_
600597
read_var(material_type);
601598
float material_weight = 0.0f;
602599
read_var(material_weight);
600+
// bit(0) : Plastic
601+
// bit(1) : Roughness
602+
// bit(2) : Specular
603+
// bit(3) : IOR
604+
// bit(4) : Attenuation
605+
// bit(5) : Power
606+
// bit(6) : Glow
607+
// bit(7) : isTotalPower (*no value)
603608
uint32_t property_bits = 0u;
604609
read_var(property_bits);
605610
switch (material_type) {
@@ -749,6 +754,19 @@ extern "C" auto gvox_parse_adapter_magicavoxel_sample_region(GvoxBlitContext *bl
749754
voxel_data = 0;
750755
}
751756
break;
757+
case GVOX_CHANNEL_ID_REFLECTIVITY:
758+
if (palette_id < 255) {
759+
if ((user_state.materials[palette_id].content_flags & magicavoxel::MATERIAL_SP_BIT) != 0u) {
760+
voxel_data = std::bit_cast<uint32_t>(user_state.materials[palette_id].sp);
761+
} else if ((user_state.materials[palette_id].content_flags & magicavoxel::MATERIAL_SPEC_BIT) != 0u) {
762+
voxel_data = std::bit_cast<uint32_t>(user_state.materials[palette_id].spec);
763+
} else {
764+
voxel_data = std::bit_cast<uint32_t>(1.0f);
765+
}
766+
} else {
767+
voxel_data = 0;
768+
}
769+
break;
752770
default:
753771
gvox_adapter_push_error(ctx, GVOX_RESULT_ERROR_PARSE_ADAPTER_REQUESTED_CHANNEL_NOT_PRESENT, "Requested unsupported channel from magicavoxel file");
754772
break;
@@ -766,7 +784,7 @@ extern "C" auto gvox_parse_adapter_magicavoxel_load_region(GvoxBlitContext * /*u
766784
auto const available_channels =
767785
uint32_t{GVOX_CHANNEL_BIT_COLOR | GVOX_CHANNEL_BIT_MATERIAL_ID | GVOX_CHANNEL_BIT_ROUGHNESS |
768786
GVOX_CHANNEL_BIT_METALNESS | GVOX_CHANNEL_BIT_TRANSPARENCY | GVOX_CHANNEL_BIT_IOR |
769-
GVOX_CHANNEL_BIT_EMISSIVITY};
787+
GVOX_CHANNEL_BIT_EMISSIVITY | GVOX_CHANNEL_BIT_REFLECTIVITY};
770788
if ((channel_flags & ~available_channels) != 0) {
771789
gvox_adapter_push_error(ctx, GVOX_RESULT_ERROR_PARSE_ADAPTER_REQUESTED_CHANNEL_NOT_PRESENT, "Tried loading a region with a channel that wasn't present in the original data");
772790
}
@@ -818,7 +836,7 @@ extern "C" void gvox_parse_adapter_magicavoxel_parse_region(GvoxBlitContext *bli
818836
auto const available_channels =
819837
uint32_t{GVOX_CHANNEL_BIT_COLOR | GVOX_CHANNEL_BIT_MATERIAL_ID | GVOX_CHANNEL_BIT_ROUGHNESS |
820838
GVOX_CHANNEL_BIT_METALNESS | GVOX_CHANNEL_BIT_TRANSPARENCY | GVOX_CHANNEL_BIT_IOR |
821-
GVOX_CHANNEL_BIT_EMISSIVITY};
839+
GVOX_CHANNEL_BIT_EMISSIVITY | GVOX_CHANNEL_BIT_REFLECTIVITY};
822840
if ((channel_flags & ~available_channels) != 0) {
823841
gvox_adapter_push_error(ctx, GVOX_RESULT_ERROR_PARSE_ADAPTER_REQUESTED_CHANNEL_NOT_PRESENT, "Tried loading a region with a channel that wasn't present in the original data");
824842
}

src/adapters/serialize/colored_text.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ namespace {
104104
bool const is_normalized_float =
105105
(channel_i == GVOX_CHANNEL_ID_ROUGHNESS) ||
106106
(channel_i == GVOX_CHANNEL_ID_METALNESS) ||
107-
(channel_i == GVOX_CHANNEL_ID_TRANSPARENCY);
107+
(channel_i == GVOX_CHANNEL_ID_TRANSPARENCY) ||
108+
(channel_i == GVOX_CHANNEL_ID_REFLECTIVITY);
109+
float const normalized_range_min = 0.0f;
110+
float const normalized_range_max = channel_i == GVOX_CHANNEL_ID_REFLECTIVITY ? 2.0f : 1.0f;
108111
for (uint32_t zi = 0; zi < range->extent.z; zi += user_state.config.downscale_factor) {
109112
for (uint32_t yi = 0; yi < range->extent.y; yi += user_state.config.downscale_factor) {
110113
for (uint32_t xi = 0; xi < range->extent.x; xi += user_state.config.downscale_factor) {
@@ -187,7 +190,7 @@ namespace {
187190
g = (voxel >> 0x08) & 0xff;
188191
b = (voxel >> 0x10) & 0xff;
189192
} else if (is_normalized_float) {
190-
r = static_cast<uint8_t>(std::bit_cast<float>(voxel) * 255.0f);
193+
r = static_cast<uint8_t>(std::min(std::max((std::bit_cast<float>(voxel) - normalized_range_min) / (normalized_range_max - normalized_range_min), 0.0f), 1.0f) * 255.0f);
191194
g = r;
192195
b = r;
193196
} else {

0 commit comments

Comments
 (0)