commit ad9178d234b881315ca3142184d54816c09471f4
parent f59764e62ceb9216ba99ef719c73dbffc73c2a46
Author: Michael Savage <mikejsavage@gmail.com>
Date: Fri, 2 Aug 2019 14:33:28 +0300
GLTF tweaks
Diffstat:
M | gltf.cc | | | 39 | +++++++++++++++++++-------------------- |
1 file changed, 19 insertions(+), 20 deletions(-)
diff --git a/gltf.cc b/gltf.cc
@@ -213,12 +213,20 @@ static bool LoadBinaryBuffers( cgltf_data * data ) {
static AnimationReel::Clip CLIP;
static GLTFModel model;
+static u8 GetJointIdx( const cgltf_node * node ) {
+ return u8( uintptr_t( node->camera ) - 1 );
+}
+
+static void SetJointIdx( cgltf_node * node, u8 joint_idx ) {
+ node->camera = ( cgltf_camera * ) uintptr_t( joint_idx + 1 );
+}
+
static void AddJoint( const cgltf_skin * skin, cgltf_node * node, u8 ** prev ) {
- u8 joint_idx = u8( uintptr_t( node->camera ) - 1 );
+ u8 joint_idx = GetJointIdx( node );
**prev = joint_idx;
*prev = &model.animation.joints[ joint_idx ].next;
- model.animation.joints[ joint_idx ].parent = u8( uintptr_t( node->parent->camera ) - 1 );
+ model.animation.joints[ joint_idx ].parent = node->parent != NULL ? GetJointIdx( node->parent ) : U8_MAX;
cgltf_bool ok = cgltf_accessor_read_float( skin->inverse_bind_matrices, joint_idx, model.animation.joints[ joint_idx ].joint_to_bind.ptr(), 16 );
ASSERT( ok != 0 );
@@ -234,12 +242,12 @@ static void LoadSkin( const cgltf_skin * skin ) {
memset( model.animation.joints, 0, sizeof( AnimationReel::Joint ) * skin->joints_count );
for( size_t i = 0; i < skin->joints_count; i++ ) {
- skin->joints[ i ]->camera = ( cgltf_camera * ) ( i + 1 );
+ SetJointIdx( skin->joints[ i ], i );
}
u8 * prev_ptr = &model.animation.root_joint;
for( size_t i = 0; i < skin->joints_count; i++ ) {
- if( skin->joints[ i ]->parent == NULL || skin->joints[ i ]->parent->camera == NULL ) {
+ if( skin->joints[ i ]->parent == NULL || GetJointIdx( skin->joints[ i ]->parent ) == U8_MAX ) {
ggprint( "root {}\n", skin->joints[ i ]->name );
AddJoint( skin, skin->joints[ i ], &prev_ptr );
}
@@ -300,8 +308,8 @@ static void LoadAnimationReel( MemoryArena * arena, const cgltf_animation * anim
for( size_t i = 0; i < animation->channels_count; i++ ) {
const cgltf_animation_channel * chan = &animation->channels[ i ];
- ASSERT( chan->target_node->camera != NULL );
- u8 joint_idx = u8( uintptr_t( chan->target_node->camera ) - 1 );
+ u8 joint_idx = GetJointIdx( chan->target_node );
+ ASSERT( joint_idx != U8_MAX );
if( chan->target_path == cgltf_animation_path_type_translation ) {
ggprint( "{} translation\n", chan->target_node->name );
@@ -320,19 +328,6 @@ static void LoadAnimationReel( MemoryArena * arena, const cgltf_animation * anim
}
}
- ggprint( "num channels {}\n", animation->channels_count );
- for( size_t i = 0; i < animation->channels_count; i++ ) {
- const cgltf_animation_channel * chan = &animation->channels[ i ];
-
- ASSERT( chan->target_node->camera != NULL );
- u8 joint_idx = u8( uintptr_t( chan->target_node->camera ) - 1 );
-
- const AnimationReel::Joint & joint = model.animation.joints[ joint_idx ];
- if( joint.translations.samples == NULL ) {
- ggprint( "{} no translation channel\n", chan->target_node->name );
- }
- }
-
CLIP.start_time = 0;
CLIP.duration = 10;
CLIP.loop = true;
@@ -435,11 +430,15 @@ static void LoadNode( MemoryArena * arena, const cgltf_node * node, bool animate
// TODO: convert to u8
if( attr.type == cgltf_attribute_type_joints ) {
mesh_config.joints = renderer_new_vb( AccessorToSpan( attr.data ) );
+ if( attr.data->component_type == cgltf_component_type_r_8u )
+ mesh_config.joints_format = VERTEXFMT_U8x4;
}
// TODO: convert to float3 and compute the last weight in the shader
if( attr.type == cgltf_attribute_type_weights ) {
mesh_config.weights = renderer_new_vb( AccessorToSpan( attr.data ) );
+ if( attr.data->component_type == cgltf_component_type_r_8u )
+ mesh_config.weights_format = VERTEXFMT_U8x4;
}
}
@@ -459,7 +458,7 @@ GAME_INIT( game_init ) {
cgltf_options options = { };
cgltf_data * data;
- if( cgltf_parse_file( &options, "padporknoesis.glb", &data ) != cgltf_result_success )
+ if( cgltf_parse_file( &options, "bigvic.glb", &data ) != cgltf_result_success )
FATAL( "cgltf_parse_file" );
if( cgltf_load_buffers( &options, data, "./" ) != cgltf_result_success )
FATAL( "cgltf_load_buffers" );