Changeset 470 for trunk


Ignore:
Timestamp:
09/17/15 17:19:14 (10 years ago)
Author:
epyon
Message:
  • animation time definition fixes
Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/formats/nmd_loader.hh

    r431 r470  
    6565        {
    6666                uint16 frame_rate;
    67                 float  duration;
     67                uint16 frame_count;
    6868                bool   flat;
    6969        };
  • trunk/nv/gfx/animation.hh

    r454 r470  
    117117                                        return keyfresult;
    118118                                }
    119                                 index0 = math::clamp( int( time ), 0, int( channel.size() ) - 2 );
     119                                index0 = math::clamp( int( time ), 0, int( channel.size() ) - 1 );
    120120                                index1 = index0 + 1;
     121                                if ( index1 == int( channel.size() ) ) index1 = 0;
    121122                                factor = math::clamp( time - index0, 0.0f, 1.0f );
    122123                        }
  • trunk/nv/gfx/keyframed_mesh.hh

    r430 r470  
    2626                virtual transform get_node_transform( uint32 node_id ) const;
    2727                virtual mat4 get_node_matrix( uint32 node_id ) const;
    28                 virtual void update_animation( animation_entry*, uint32 a_anim_time );
     28                virtual void update_animation( animation_entry*, uint32 a_ms_anim_time );
    2929                virtual void update( program a_program );
    3030                virtual ~keyframed_mesh();
  • trunk/nv/gfx/skeletal_mesh.hh

    r467 r470  
    2121        public:
    2222                skeletal_animation_entry( shash64 name, const mesh_nodes_data* anim, bool a_looping )
    23                         : animation_entry( name, a_looping, anim->get_frame_rate(), 0.0f, anim->get_duration() )
     23                        : animation_entry( name, a_looping, anim->get_fps(), 0, anim->get_frame_count() )
    2424                        , m_node_data( anim )
    2525                {
     
    2727                }
    2828
    29                 skeletal_animation_entry( shash64 name, const mesh_nodes_data* anim, float time_start, float time_end, bool a_looping )
    30                         : animation_entry( name, a_looping, anim->get_frame_rate(), time_start, time_end )
     29                skeletal_animation_entry( shash64 name, const mesh_nodes_data* anim, uint32 time_start, uint32 time_end, bool a_looping )
     30                        : animation_entry( name, a_looping, anim->get_fps(), time_start, time_end )
    3131                        , m_node_data( anim )
    3232                {
     
    3535
    3636                void prepare( const mesh_nodes_data* bones );
    37                 void update_skeleton( mat4* tr, uint32 time ) const;
     37                void update_skeleton( mat4* tr, uint32 a_ms_time ) const;
    3838                ~skeletal_animation_entry();
     39
    3940        protected:
    4041                void initialize();
     
    4647                sint16* m_bone_ids;
    4748                mat4* m_offsets;
     49                uint32 m_root;
    4850                bool m_prepared;
    4951                data_descriptor m_interpolation_key;
     
    6567                virtual transform get_node_transform( uint32 node_id ) const;
    6668                virtual mat4 get_node_matrix( uint32 node_id ) const;
     69                virtual sint16 get_parent_id() const { return m_parent_id; }
     70                virtual uint32 get_node_count() const { return m_bone_data ? m_bone_data->size() : 0; }
    6771                ~skeletal_mesh()
    6872                {
     
    7781                uint32 m_index_count;
    7882                mat4*  m_transform;
     83                sint16 m_parent_id;
    7984        };
    8085
  • trunk/nv/interface/animated_mesh.hh

    r468 r470  
    2525        {
    2626        public:
    27                 animation_entry( shash64 name, bool looping, uint32 frame_rate, float a_start, float a_end ) : m_name( name ), m_looping( looping ), m_frame_rate( frame_rate ),
    28                 m_start( a_start ), m_end( a_end ), m_duration( m_end - m_start ) {}
     27                animation_entry( shash64 name, bool looping, uint32 frame_rate, uint32 a_start, uint32 a_end ) : m_name( name ), m_looping( looping ), m_fps( frame_rate ),
     28                m_fstart( a_start ), m_fend( a_end ) {}
    2929                shash64 get_name() const { return m_name; }
    30                 uint32 get_frame_rate() const { return m_frame_rate; }
    31                 float get_duration() const { return m_duration; }
    32                 float get_start() const { return m_start; }
    33                 float get_end() const  { return m_end; }
     30                uint32 get_fps() const { return m_fps; }
     31                // TODO : if loop +1?
     32                uint32 get_frame_count() const { int todo;  return m_fend - m_fstart; }
     33                uint32 get_start_frame() const { return m_fstart; }
     34                uint32 get_end_frame() const  { return m_fend; }
    3435                bool is_looping() const { return m_looping; }
    35                 void set_range( float a_start, float a_end )
     36                void set_frame_range( uint32 a_start, uint32 a_end )
    3637                {
    37                         m_start = a_start;
    38                         m_end   = a_end;
    39                         m_duration = m_end - m_start;
     38                        m_fstart = a_start;
     39                        m_fend   = a_end;
    4040                }
    41                 void set_frame_rate( uint32 rate )
     41                void set_fps( uint32 rate )
    4242                {
    43                         m_frame_rate = rate;
     43                        m_fps = rate;
    4444                }
    4545                virtual ~animation_entry() {}
    4646        protected:
    47                 shash64 m_name;
    48                 bool    m_looping;
    49                 uint32  m_frame_rate;
    50                 float   m_start;
    51                 float   m_end;
    52                 float   m_duration;
     47                shash64 m_name;    /// string hash of name
     48                bool    m_looping; /// does the animation loop
     49                uint32  m_fps;     /// frames per second
     50                uint32  m_fstart;  /// start time (in frames)
     51                uint32  m_fend;    /// end time (in frames)
    5352        };
    5453
     
    6160                virtual transform get_node_transform( uint32 ) const { return transform(); }
    6261                virtual mat4 get_node_matrix( uint32 ) const { return mat4(); }
     62                virtual uint32 get_node_count() const { return 0; }
     63                virtual sint16 get_parent_id() const { return -1; }
     64
    6365        };
    6466
  • trunk/nv/interface/mesh_data.hh

    r457 r470  
    3333
    3434                explicit mesh_nodes_data( shash64 name )
    35                         : m_name( name ), m_frame_rate(0), m_duration(0.0f), m_flat( false )
     35                        : m_name( name ), m_frame_rate(0), m_frame_count(0), m_flat( false )
    3636                {
    3737                }
    3838
    39                 explicit mesh_nodes_data( shash64 name, uint16 a_fps, float a_frames, bool a_flat )
    40                         : m_name( name ), m_frame_rate(a_fps), m_duration(a_frames), m_flat( a_flat )
     39                explicit mesh_nodes_data( shash64 name, uint16 a_fps, uint16 a_frames, bool a_flat )
     40                        : m_name( name ), m_frame_rate(a_fps), m_frame_count(a_frames), m_flat( a_flat )
    4141                {
    4242                }
     
    8484
    8585                bool is_flat() const { return m_flat; }
    86                 uint16 get_frame_rate() const { return m_frame_rate; }
    87                 float get_duration() const { return m_duration; }
     86                uint16 get_fps() const { return m_frame_rate; }
     87                uint16 get_frame_count() const { return m_frame_count; }
    8888                shash64 get_name() const { return m_name; }
    8989
     
    9797                shash64 m_name;
    9898                uint16  m_frame_rate;
    99                 float   m_duration;
     99                uint16  m_frame_count;
    100100                bool    m_flat;
    101101        };
  • trunk/src/formats/assimp_loader.cc

    r454 r470  
    350350
    351351        uint16 frame_rate     = static_cast<uint16>( anim->mTicksPerSecond );
    352         float  duration       = static_cast<float>( anim->mDuration );
     352        int check_this;
     353        uint16 duration       = static_cast<uint16>( anim->mDuration );
    353354        bool   flat           = false;
    354355
  • trunk/src/formats/nmd_loader.cc

    r442 r470  
    9898        nmd_animation_header animation_header;
    9999        source.read( &animation_header, sizeof( animation_header ), 1 );
    100         m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.duration, animation_header.flat );
     100        m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.frame_count, animation_header.flat );
    101101        for ( uint32 i = 0; i < e.children; ++i )
    102102        {
     
    215215
    216216        nmd_animation_header aheader;
    217         aheader.frame_rate = nodes.get_frame_rate();
    218         aheader.duration = nodes.get_duration();
     217        aheader.frame_rate  = nodes.get_fps();
     218        aheader.frame_count = nodes.get_frame_count();
    219219        aheader.flat = nodes.is_flat();
    220220        stream_out.write( &aheader, sizeof( aheader ), 1 );
  • trunk/src/gfx/keyframed_mesh.cc

    r454 r470  
    7272}
    7373
    74 void nv::keyframed_mesh::update_animation( animation_entry* anim, uint32 a_anim_time )
     74void nv::keyframed_mesh::update_animation( animation_entry* anim, uint32 a_ms_anim_time )
    7575{
    7676        if ( m_active )
    7777        {
    78                 float tick_time = ( static_cast<float>( a_anim_time ) * 0.001f ) * anim->get_frame_rate();
    79                 float duration  = anim->is_looping() ? anim->get_duration() + 1.0f : anim->get_duration();
    80                 if ( tick_time >= duration )
     78                float  fframe   = ( static_cast<float>( a_ms_anim_time ) * 0.001f ) * anim->get_fps();
     79                uint32 frame    = uint32( fframe );
     80                float  reminder = fframe - static_cast<float>( frame );
     81                uint32 duration = anim->is_looping() ? anim->get_frame_count() + 1 : anim->get_frame_count();
     82
     83                if ( frame >= duration )
    8184                {
    8285                        if ( anim->is_looping() )
    8386                        {
    84                                 tick_time = fmodf( tick_time, duration );
     87                                frame  = frame % duration;
     88                                fframe = static_cast<float>( frame ) + reminder;
    8589                        }
    8690                        else
    8791                        {
    88                                 m_active     = false;
    89                                 m_last_frame = static_cast<uint32>( anim->get_end() );
    90                                 m_next_frame = m_last_frame;
     92                                m_active        = false;
     93                                m_last_frame    = anim->get_end_frame();
     94                                m_next_frame    = m_last_frame;
    9195                                m_interpolation = 0.0f;
    9296                                return;
    9397                        }
    9498                }
    95                 m_last_frame    = static_cast<uint32>( math::floor( tick_time ) + anim->get_start() );
     99                m_last_frame    = frame + anim->get_start_frame();
    96100                m_next_frame    = m_last_frame + 1;
    97                 if ( m_next_frame > static_cast<uint32>( anim->get_end() ) ) m_next_frame = static_cast<uint32>( anim->get_start() );
    98                 m_interpolation = tick_time - math::floor( tick_time );
     101                if ( m_next_frame > anim->get_end_frame() ) m_next_frame = anim->get_start_frame();
     102                m_interpolation = reminder;
    99103        }
    100104}
  • trunk/src/gfx/mesh_creator.cc

    r458 r470  
    99#include "nv/interface/data_channel_access.hh"
    1010
     11#include "nv/core/logging.hh"
     12
    1113struct nv_key_transform { nv::transform tform; };
    1214
     
    1618        merge_keys();
    1719        uint32 max_frames = 0;
    18         for ( auto keys : m_data->m_data )
    19         {
     20
     21        nv::vector< sint16 > ids;
     22        {
     23                // TODO: simplify this shit!
     24                // The complexity here is that we cannot pre-transform in any order
     25                // as the bones depend on previous bones, but ARE NOT IN ORDER
     26                //
     27                // Either rewrite this a lot nicer, or sort the bones on creation
     28                // by tree-order.
     29
     30                ids.reserve( m_data->m_data.size() );
     31                {
     32                        nv::vector< sint16 > ids_next;
     33                        ids_next.reserve( m_data->m_data.size() );
     34                        ids_next.push_back( -1 );
     35                        while ( !ids_next.empty() )
     36                        {
     37                                sint16 pid = ids_next.back();
     38                                ids_next.pop_back();
     39                                for ( sint16 i = 0; i < sint16(m_data->m_data.size()); ++i )
     40                                        if ( m_data->m_data[i]->get_parent_id() == pid )
     41                                        {
     42                                                sint16* it = nv::find( ids.begin(), ids.end(), i );
     43                                                if ( it == ids.end() )
     44                                                {
     45                                                        ids.push_back( i );
     46                                                        ids_next.push_back( i );
     47                                                }
     48                                        }
     49                        }
     50                }
     51
     52                if ( ids.size() != m_data->m_data.size() )
     53                {
     54                        NV_LOG_WARNING( "Bad skeleton!" );
     55                }
     56        }
     57
     58        NV_LOG_DEBUG( "ID/PID" );
     59        for ( auto id : ids )
     60        {
     61                data_channel_set* keys = m_data->m_data[id];
    2062                sint16 parent_id = keys->get_parent_id();
    21                 data_channel_set* pkeys  = ( parent_id != -1 ? m_data->m_data[parent_id] : nullptr );
     63                NV_LOG_DEBUG( "Id : ", id, " PID", parent_id );
     64                data_channel_set* pkeys = ( parent_id != -1 ? m_data->m_data[parent_id] : nullptr );
    2265                size_t count     = ( keys ? keys->get_channel_size(0) : 0 );
    2366                size_t pcount    = ( pkeys ? pkeys->get_channel_size(0) : 0 );
     
    2669                {
    2770                        data_channel_access< nv_key_transform > channel_creator( keys, 0 );
     71
    2872                        nv_key_transform* channel = channel_creator.data();
    2973                        const nv_key_transform* pchannel = pkeys->get_channel(0)->data_cast< nv_key_transform >();
     
    3882        if ( m_data->m_frame_rate == 1 )
    3983        {
    40                 m_data->m_frame_rate = 32;
    41                 m_data->m_duration   = static_cast<float>( max_frames );
     84                m_data->m_frame_rate  = 32;
     85                m_data->m_frame_count = max_frames;
    4286        }
    4387
  • trunk/src/gfx/skeletal_mesh.cc

    r467 r470  
    1313void nv::skeletal_animation_entry::initialize()
    1414{
     15        m_root      = uint32(-1);
    1516        m_prepared  = false;
    1617        m_children  = nullptr;
     
    3031                                m_children[ node->get_parent_id()].push_back( n );
    3132                        }
    32                 }
    33         }
    34 }
    35 
    36 void nv::skeletal_animation_entry::update_skeleton( mat4* data, uint32 time ) const
    37 {
    38         float tick_time = ( time * 0.001f ) * m_frame_rate;
    39         float anim_time = m_start;
    40         if ( m_duration > 0.0f ) anim_time += fmodf( tick_time, m_duration );
     33                        else
     34                        {
     35                                if ( m_root >= 0 )
     36                                {
     37                                        m_root = uint32( -2 );
     38                                }
     39                                else
     40                                        m_root = n;
     41                        }
     42                }
     43                NV_ASSERT( m_root != uint32( -1 ), "Animation without root!" );
     44        }
     45}
     46
     47void nv::skeletal_animation_entry::update_skeleton( mat4* data, uint32 a_ms_time ) const
     48{
     49        float  fframe   = ( a_ms_time * 0.001f ) * m_fps;
     50        uint32 frame    = math::floor( fframe );
     51        float  reminder = fframe - static_cast<float>( frame );
     52        uint32 duration = get_frame_count();
     53        if ( duration == 0 )
     54        {
     55                frame  = get_start_frame();
     56                fframe = static_cast<float>( frame );
     57        }
     58        else if ( frame >= duration )
     59        {
     60                if ( is_looping() )
     61                {
     62                        frame  = frame % duration;
     63                        fframe = static_cast<float>( frame ) + reminder;
     64                }
     65                else
     66                {
     67                        frame  = get_end_frame();
     68                        fframe = static_cast<float>( frame );
     69                }
     70        }
    4171
    4272        if ( !m_node_data->is_flat() )
    4373        {
    44                 animate_rec( data, anim_time, 0, mat4() );
     74                if ( m_root == uint32( -2 ) ) // multi-root
     75                {
     76                        for ( uint32 n = 0; n < m_node_data->size(); ++n )
     77                                if ( ( *m_node_data )[n]->get_parent_id() == -1 )
     78                                        animate_rec( data, fframe, n, mat4() );
     79                }
     80                else
     81                        animate_rec( data, fframe, m_root, mat4() );
    4582                return;
    4683        }
     
    5592                        {
    5693                                raw_channel_interpolator interpolator( node, m_interpolation_key );
    57                                 node_mat = interpolator.get< mat4 >( anim_time );
     94                                node_mat = interpolator.get< mat4 >( fframe );
    5895                        }
    5996
     
    66103{
    67104        if ( m_prepared ) return;
    68         unordered_map< uint64, uint16 > bone_names;
     105        nv::hash_store< shash64, uint16 > bone_names;
    69106        m_offsets = new mat4[ bones->size() ];
    70107        for ( nv::uint16 bi = 0; bi < bones->size(); ++bi )
    71108        {
    72109                const data_channel_set* bone = (*bones)[ bi ];
    73                 bone_names[ bone->get_name().value() ] = bi;
     110                bone_names[ bone->get_name() ] = bi;
    74111                m_offsets[bi] = bone->get_transform();
    75112        }
     
    80117                sint16 bone_id = -1;
    81118
    82                 auto bi = bone_names.find( node->get_name().value() );
     119                auto bi = bone_names.find( node->get_name() );
    83120                if ( bi != bone_names.end() )
    84121                {
     
    129166
    130167nv::skeletal_mesh::skeletal_mesh( context* a_context, const data_channel_set* a_mesh, const mesh_nodes_data* a_bone_data )
    131         : m_context( a_context ), m_bone_data( a_bone_data ), m_index_count( 0 ), m_transform( nullptr )
     168        : m_context( a_context ), m_bone_data( a_bone_data ), m_index_count( 0 ), m_transform( nullptr ), m_parent_id(-1)
    132169{
    133170        if ( a_mesh )
     
    135172                m_va = a_context->create_vertex_array( a_mesh, nv::STATIC_DRAW );
    136173                m_index_count = a_mesh->get_channel_size( slot::INDEX );
     174                m_parent_id = a_mesh->get_parent_id();
    137175        }
    138176        if ( m_bone_data )
  • trunk/src/wx/wx_canvas.cc

    r467 r470  
    66
    77#include <nv/wx/wx_canvas.hh>
     8
     9#include <nv/core/time.hh>
    810
    911wxBEGIN_EVENT_TABLE( nv::wx_gl_canvas, wxWindow )
     
    5153void nv::wx_gl_canvas::on_idle( wxIdleEvent& evt )
    5254{
     55        nv::sleep( 10 );
    5356        if ( m_render )
    5457        {
Note: See TracChangeset for help on using the changeset viewer.