source: trunk/src/gfx/skeletal_mesh.cc @ 467

Last change on this file since 467 was 467, checked in by epyon, 10 years ago
  • wx_canvas - color logging
  • string - _sh64/_sh32 literal support
  • skeletal_mesh - removed obsolete CPU support
File size: 4.7 KB
Line 
1// Copyright (C) 2011-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/gfx/skeletal_mesh.hh"
8
9#include "nv/interface/context.hh"
10#include "nv/interface/device.hh"
11#include "nv/stl/unordered_map.hh"
12
13void nv::skeletal_animation_entry::initialize()
14{
15        m_prepared  = false;
16        m_children  = nullptr;
17        m_offsets   = nullptr;
18        m_bone_ids  = new sint16[m_node_data->size()];
19
20        NV_ASSERT( m_node_data, "node data empty!" );
21
22        if ( !m_node_data->is_flat() )
23        {
24                m_children = new vector< uint32 >[m_node_data->size()];
25                for ( uint32 n = 0; n < m_node_data->size(); ++n )
26                {
27                        const data_channel_set* node = (*m_node_data)[n];
28                        if ( node->get_parent_id() != -1 )
29                        {
30                                m_children[ node->get_parent_id()].push_back( n );
31                        }
32                }
33        }
34}
35
36void 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 );
41
42        if ( !m_node_data->is_flat() )
43        {
44                animate_rec( data, anim_time, 0, mat4() );
45                return;
46        }
47
48        for ( uint32 n = 0; n < m_node_data->size(); ++n )
49                if ( m_bone_ids[n] >= 0 )
50                {
51                        const data_channel_set* node = (*m_node_data)[n];
52                        nv::mat4 node_mat( node->get_transform() );
53
54                        if ( node->size() > 0 )
55                        {
56                                raw_channel_interpolator interpolator( node, m_interpolation_key );
57                                node_mat = interpolator.get< mat4 >( anim_time );
58                        }
59
60                        sint16 bone_id = m_bone_ids[n];
61                        data[ bone_id ] = node_mat * m_offsets[ bone_id ];
62                }
63}
64
65void nv::skeletal_animation_entry::prepare( const mesh_nodes_data* bones )
66{
67        if ( m_prepared ) return;
68        unordered_map< uint64, uint16 > bone_names;
69        m_offsets = new mat4[ bones->size() ];
70        for ( nv::uint16 bi = 0; bi < bones->size(); ++bi )
71        {
72                const data_channel_set* bone = (*bones)[ bi ];
73                bone_names[ bone->get_name().value() ] = bi;
74                m_offsets[bi] = bone->get_transform();
75        }
76
77        for ( uint32 n = 0; n < m_node_data->size(); ++n )
78        {
79                const data_channel_set* node = (*m_node_data)[ n ];
80                sint16 bone_id = -1;
81
82                auto bi = bone_names.find( node->get_name().value() );
83                if ( bi != bone_names.end() )
84                {
85                        bone_id = sint16( bi->second );
86                }
87                m_bone_ids[n] = bone_id;
88
89                if ( m_interpolation_key.size() == 0 && node->size() > 0 )
90                        m_interpolation_key = node->get_interpolation_key();
91
92        }
93        m_prepared = true;
94}
95
96void nv::skeletal_animation_entry::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) const
97{
98        // TODO: fix transforms, which are now embedded,
99        //       see note in assimp_loader.cc:load_node
100        const data_channel_set* node = ( *m_node_data )[ node_id ];
101        mat4 node_mat( node->get_transform() );
102
103        if ( node->size() > 0 )
104        {
105                raw_channel_interpolator interpolator( node, m_interpolation_key );
106                node_mat = interpolator.get< mat4 >( time );
107        }
108
109        mat4 global_mat = parent_mat * node_mat;
110
111        sint16 bone_id = m_bone_ids[ node_id ];
112        if ( bone_id >= 0 )
113        {
114                data[ bone_id ] = global_mat * m_offsets[ bone_id ];
115        }
116
117        for ( auto child : m_children[ node_id ] )
118        {
119                animate_rec( data, time, child, global_mat );
120        }
121}
122
123nv::skeletal_animation_entry::~skeletal_animation_entry()
124{
125        delete[] m_offsets;
126        delete[] m_children;
127        delete[] m_bone_ids;
128}
129
130nv::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 )
132{
133        if ( a_mesh )
134        {
135                m_va = a_context->create_vertex_array( a_mesh, nv::STATIC_DRAW );
136                m_index_count = a_mesh->get_channel_size( slot::INDEX );
137        }
138        if ( m_bone_data )
139        {
140                m_transform = new mat4[ m_bone_data->size() ];
141        }
142}
143
144void nv::skeletal_mesh::update_animation( animation_entry* a_anim, uint32 a_anim_time )
145{
146        if ( m_bone_data && a_anim )
147        {
148                skeletal_animation_entry * anim = static_cast<skeletal_animation_entry*>( a_anim );
149                anim->prepare( m_bone_data );
150                anim->update_skeleton( m_transform, a_anim_time );
151        }
152}
153
154void nv::skeletal_mesh::update( program a_program )
155{
156        if ( m_bone_data )
157                m_context->get_device()->set_opt_uniform_array( a_program, "nv_m_bones", m_transform, m_bone_data->size() );
158}
159
160nv::transform nv::skeletal_mesh::get_node_transform( uint32 node_id ) const
161{
162        if ( node_id == 0 ) return transform();
163        if ( node_id == uint32(-1) ) return transform( m_transform[0] );
164        return transform( m_transform[ node_id ] );
165}
166
167nv::mat4 nv::skeletal_mesh::get_node_matrix( uint32 node_id ) const
168{
169        return m_transform[ node_id ];
170}
Note: See TracBrowser for help on using the repository browser.