Changeset 477 for trunk/src/gfx


Ignore:
Timestamp:
10/23/15 19:35:39 (10 years ago)
Author:
epyon
Message:
  • resource handling rewrite
  • skeletal_mesh/mesh_data updates
Location:
trunk/src/gfx
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gfx/skeletal_mesh.cc

    r475 r477  
    1313#include "nv/core/logging.hh"
    1414
    15 void nv::skeletal_animation_entry::update_skeleton( mat4* data, uint32 a_ms_time ) const
     15void nv::skeletal_animation_entry::update_skeleton( skeleton_instance& data, uint32 a_ms_time ) const
    1616{
    1717        float  fframe   = ( a_ms_time * 0.001f ) * m_fps;
     
    3838        }
    3939
    40         m_data.animate( data, fframe );
     40        if ( data.size() == 0 )
     41                data.initialize( m_temp_anim->size() );
     42        data.animate( m_temp_anim, m_data, fframe );
    4143}
    4244
    4345void nv::skeletal_animation_entry::prepare( const mesh_nodes_data* bones )
    4446{
    45         m_data.prepare( bones );
     47        m_data.prepare( m_temp_anim, bones ? bones : m_temp_anim );
    4648}
    4749
    4850nv::skeletal_mesh::skeletal_mesh( context* a_context, const data_channel_set* a_mesh, const mesh_nodes_data* a_bone_data )
    49         : m_context( a_context ), m_bone_data( a_bone_data ), m_index_count( 0 ), m_transform( nullptr ), m_parent_id(-1)
     51        : m_skeleton( a_bone_data ? a_bone_data->size() : 0 ), m_context( a_context ), m_bone_data( a_bone_data ), m_index_count( 0 ), m_parent_id(-1)
    5052{
    5153        if ( a_mesh )
     
    5557                m_parent_id = a_mesh->get_parent_id();
    5658        }
    57         if ( m_bone_data )
    58         {
    59                 m_transform = new mat4[ m_bone_data->size() ];
    60         }
    6159}
    6260
    63 void nv::skeletal_mesh::update_animation( animation_entry* a_anim, uint32 a_anim_time )
     61void nv::skeletal_mesh::update_animation( animation_entry& a_anim, uint32 a_anim_time )
    6462{
    65         if ( m_bone_data && a_anim )
    66         {
    67                 skeletal_animation_entry * anim = static_cast<skeletal_animation_entry*>( a_anim );
    68                 anim->prepare( m_bone_data );
    69                 anim->update_skeleton( m_transform, a_anim_time );
    70         }
     63        skeletal_animation_entry& anim = static_cast<skeletal_animation_entry&>( a_anim );
     64        anim.prepare( m_bone_data );
     65        anim.update_skeleton( m_skeleton, a_anim_time );
    7166}
    7267
    73 void nv::skeletal_mesh::update( program a_program )
     68void nv::skeletal_mesh::update_program( program a_program )
    7469{
    75         if ( m_bone_data )
    76                 m_context->get_device()->set_opt_uniform_array( a_program, "nv_m_bones", m_transform, m_bone_data->size() );
     70        m_context->get_device()->set_opt_uniform_array( a_program, "nv_m_bones", m_skeleton.transforms(), m_skeleton.size() );
    7771}
    7872
    7973nv::transform nv::skeletal_mesh::get_node_transform( uint32 node_id ) const
    8074{
    81         return transform( m_transform[ node_id ] );
     75        return transform( get_node_matrix( node_id ) );
    8276}
    8377
    8478nv::mat4 nv::skeletal_mesh::get_node_matrix( uint32 node_id ) const
    8579{
    86         return m_transform[ node_id ];
     80        return m_skeleton.transforms()[ node_id ];
    8781}
  • trunk/src/gfx/skeleton_instance.cc

    r475 r477  
    77#include "nv/gfx/skeleton_instance.hh"
    88
    9 void nv::skeleton_instance::prepare( const mesh_nodes_data* bones )
     9void nv::skeleton_binding::prepare( const mesh_nodes_data* node_data, const mesh_nodes_data* bone_data )
    1010{
    11         if ( m_offsets || m_indices ) return;
    12         hash_store< shash64, uint16 > bone_names;
    13         m_offsets = new mat4[bones->size()];
    14         m_indices = new sint16[m_data->size()];
     11        if ( !m_offsets || !m_indices )
     12        {
     13                // TODO: either fixed size struct or static allocator
     14                hash_store< shash64, uint16 > bone_names;
     15                m_offsets = new mat4[bone_data->size()];
     16                m_indices = new sint16[node_data->size()];
    1517
    16         for ( nv::uint16 bi = 0; bi < bones->size(); ++bi )
    17         {
    18                 const data_channel_set* bone = ( *bones )[bi];
    19                 bone_names[bone->get_name()] = bi;
    20                 m_offsets[bi] = bone->get_transform();
     18                for ( nv::uint16 bi = 0; bi < bone_data->size(); ++bi )
     19                {
     20                        const data_channel_set* bone = ( *bone_data )[bi];
     21                        bone_names[bone->get_name()] = bi;
     22                        m_offsets[bi] = bone->get_transform();
     23                }
     24
     25                for ( uint32 n = 0; n < node_data->size(); ++n )
     26                {
     27                        const data_channel_set* node = ( *node_data )[n];
     28                        sint16 bone_id = -1;
     29
     30                        auto bi = bone_names.find( node->get_name() );
     31                        if ( bi != bone_names.end() )
     32                        {
     33                                bone_id = sint16( bi->second );
     34                        }
     35                        m_indices[n] = bone_id;
     36
     37                }
    2138        }
    2239
    23         for ( uint32 n = 0; n < m_data->size(); ++n )
     40        if ( m_key.size() == 0 )
    2441        {
    25                 const data_channel_set* node = ( *m_data )[n];
    26                 sint16 bone_id = -1;
     42                for ( uint32 n = 0; n < node_data->size(); ++n )
     43                        if ( ( *node_data )[n]->size() > 0 )
     44                        {
     45                                m_key = ( *node_data )[n]->get_interpolation_key();
     46                                break;
     47                        }
     48        }
    2749
    28                 auto bi = bone_names.find( node->get_name() );
    29                 if ( bi != bone_names.end() )
     50}
     51
     52void nv::skeleton_instance::animate( const mesh_nodes_data* node_data, const skeleton_binding& binding, float frame )
     53{
     54        if ( m_transform.size() > 0 )
     55        {
     56                if ( node_data->is_flat() )
    3057                {
    31                         bone_id = sint16( bi->second );
     58                        animate_flat( node_data, binding, frame );
    3259                }
    33                 m_indices[n] = bone_id;
    34 
    35                 if ( m_key.size() == 0 && node->size() > 0 )
    36                         m_key = node->get_interpolation_key();
     60                else
     61                {
     62                        for ( uint32 n = 0; n < node_data->size(); ++n )
     63                                if ( ( *node_data )[n]->get_parent_id() == -1 )
     64                                        animate_rec( node_data, binding, frame, n, mat4() );
     65                }
    3766        }
    3867}
    3968
    40 void nv::skeleton_instance::animate( mat4* data, float frame ) const
    41 {
    42         if ( m_data->is_flat() )
    43         {
    44                 animate_flat( data, frame );
    45         }
    46         else
    47         {
    48                 for ( uint32 n = 0; n < m_data->size(); ++n )
    49                         if ( ( *m_data )[n]->get_parent_id() == -1 )
    50                                 animate_rec( data, frame, n, mat4() );
    51         }
    52 }
    53 
    54 void nv::skeleton_instance::animate_flat( mat4* data, float frame ) const
    55 {
    56         for ( uint32 n = 0; n < m_data->size(); ++n )
    57                 if ( m_indices[n] >= 0 )
    58                 {
    59                         const data_channel_set* node = ( *m_data )[n];
    60                         nv::mat4 node_mat( node->get_transform() );
    61 
    62                         if ( node->size() > 0 )
    63                         {
    64                                 raw_channel_interpolator interpolator( node, m_key );
    65                                 node_mat = interpolator.get< mat4 >( frame );
    66                         }
    67 
    68                         sint16 bone_id = m_indices[n];
    69                         data[bone_id] = node_mat * m_offsets[bone_id];
    70                 }
    71 }
    72 
    73 void nv::skeleton_instance::animate_rec( mat4* data, float frame, uint32 id, const mat4& parent ) const
     69void nv::skeleton_instance::animate_rec( const mesh_nodes_data* node_data, const skeleton_binding& binding, float frame, uint32 id, const mat4& parent )
    7470{
    7571        // TODO: fix transforms, which are now embedded,
    7672        //       see note in assimp_loader.cc:load_node
    77         const data_channel_set* node = ( *m_data )[id];
     73        const data_channel_set* node = ( *node_data )[id];
    7874        mat4 node_mat( node->get_transform() );
    7975
    8076        if ( node->size() > 0 )
    8177        {
    82                 raw_channel_interpolator interpolator( node, m_key );
     78                raw_channel_interpolator interpolator( node, binding.m_key );
    8379                node_mat = interpolator.get< mat4 >( frame );
    8480        }
     
    8682        mat4 global_mat = parent * node_mat;
    8783
    88         sint16 bone_id = m_indices[id];
     84        sint16 bone_id = binding.m_indices[id];
    8985        if ( bone_id >= 0 )
    9086        {
    91                 data[bone_id] = global_mat * m_offsets[bone_id];
     87                m_transform[bone_id] = global_mat * binding.m_offsets[bone_id];
    9288        }
    9389
    94         for ( auto child : m_data->children( id ) )
     90        for ( auto child : node_data->children( id ) )
    9591        {
    96                 animate_rec( data, frame, child, global_mat );
     92                animate_rec( node_data, binding, frame, child, global_mat );
    9793        }
    9894}
     95
     96void nv::skeleton_instance::animate_flat( const mesh_nodes_data* node_data, const skeleton_binding& binding, float frame )
     97{
     98        for ( uint32 n = 0; n < node_data->size(); ++n )
     99                if ( binding.m_indices[n] >= 0 )
     100                {
     101                        const data_channel_set* node = ( *node_data )[n];
     102                        nv::mat4 node_mat( node->get_transform() );
     103
     104                        if ( node->size() > 0 )
     105                        {
     106                                raw_channel_interpolator interpolator( node, binding.m_key );
     107                                node_mat = interpolator.get< mat4 >( frame );
     108                        }
     109
     110                        sint16 bone_id = binding.m_indices[n];
     111                        m_transform[bone_id] = node_mat * binding.m_offsets[bone_id];
     112                }
     113}
Note: See TracChangeset for help on using the changeset viewer.