@@ -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 }
0 commit comments