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

Last change on this file since 288 was 288, checked in by epyon, 11 years ago
  • unified animation_entry class
  • virtuals removed from animation_entry
  • lua_table - added missing get_float
  • lua_table - fixed has_field
File size: 7.0 KB
Line 
1// Copyright (C) 2011 Kornel Kisielewicz
2// This file is part of NV Libraries.
3// For conditions of distribution and use, see copyright notice in nv.hh
4
5#include "nv/gfx/skeletal_mesh.hh"
6
7#include <glm/gtc/matrix_access.hpp>
8#include <glm/gtx/matrix_interpolation.hpp>
9#include "nv/interface/context.hh"
10#include "nv/interface/device.hh"
11
12
13nv::skeletal_mesh::skeletal_mesh( device* a_device, const mesh_data* a_mesh_data, const mesh_nodes_data* bones )
14        : animated_mesh()
15        , m_data( a_mesh_data )
16{
17        const mesh_raw_channel* pnt_chan = a_mesh_data->get_channel<md5_vtx_pnt>();
18        m_pntdata.assign( (const md5_vtx_pnt*)pnt_chan->data, pnt_chan->count );
19        m_bone_offset.resize( bones->get_count() );
20        for ( uint32 i = 0; i < bones->get_count(); ++i )
21        {
22                m_bone_offset[i] = transform( bones->get_node(i)->transform );
23        }
24        m_vtx_data  = a_mesh_data->get_channel_data<md5_vtx_pntiw>();
25        m_indices   = a_mesh_data->get_count();
26        m_va        = a_device->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
27}
28
29void nv::skeletal_mesh::update_animation( animation_entry* a_anim, uint32 a_anim_time )
30{
31        if ( a_anim )
32        {
33                skeletal_animation_entry * anim = (skeletal_animation_entry*)a_anim;
34                float frame_duration = 1000.f / (float)anim->get_frame_rate();
35                float anim_duration = frame_duration * anim->get_duration();
36                float new_time = fmodf( (float)a_anim_time, anim_duration );
37                anim->update_skeleton( m_transform.data(), new_time * 0.001f );
38
39                //m_mesh_data->apply( m_transform.data() );
40                {
41                        size_t skeleton_size = m_bone_offset.size();
42                        size_t vertex_count  = m_pntdata.size();
43                        m_pos_offset.resize( skeleton_size );
44                        for ( unsigned int i = 0; i < skeleton_size; ++i )
45                        {
46                                m_pos_offset[i] = m_transform[i] * m_bone_offset[i];
47                        }
48
49                        std::fill( m_pntdata.raw_data(), m_pntdata.raw_data() + m_pntdata.raw_size(), 0 );
50                        for ( unsigned int i = 0; i < vertex_count; ++i )
51                        {
52                                const md5_vtx_pntiw& vert = m_vtx_data[i];
53
54                                for ( size_t j = 0; j < 4; ++j )
55                                {
56                                        int   index  = vert.boneindex[j];
57                                        float weight = vert.boneweight[j];
58                                        const quat& orient      = m_transform[index].get_orientation();
59                                        const transform& offset = m_pos_offset[index];
60                                        m_pntdata[i].position += offset.transformed( vert.position )        * weight;
61                                        m_pntdata[i].normal   += ( orient * vert.normal  ) * weight;
62                                        m_pntdata[i].tangent  += ( orient * vert.tangent ) * weight;
63                                }
64                        }
65                }
66
67                vertex_buffer* vb = m_va->find_buffer( nv::slot::POSITION );
68                vb->bind();
69                vb->update( m_pntdata.data(), 0, m_pntdata.raw_size() );
70                vb->unbind();
71        }
72}
73
74nv::skeletal_mesh::~skeletal_mesh()
75{
76        delete m_va;
77}
78
79void nv::skeletal_mesh::run_animation( animation_entry* a_anim )
80{
81        if ( a_anim != nullptr )
82        {
83                skeletal_animation_entry * anim = (skeletal_animation_entry*)(a_anim);
84                m_transform.resize( anim->get_num_joints() );
85                update_animation( a_anim, 0 );
86        }
87}
88
89void nv::skeletal_animation_entry_gpu::initialize()
90{
91        m_prepared  = false;
92        m_children  = nullptr;
93        m_offsets   = nullptr;
94        uint32 node_count = m_node_data->get_count();
95        m_bone_ids  = new sint16[ node_count ];
96
97        if ( !m_node_data->is_flat() )
98        {
99                m_children = new std::vector< uint32 >[ node_count ];
100                for ( uint32 n = 0; n < node_count; ++n )
101                {
102                        const mesh_node_data* node = m_node_data->get_node(n);
103                        if ( node->parent_id != -1 )
104                        {
105                                m_children[ node->parent_id ].push_back( n );
106                        }
107                }
108        }
109}
110
111void nv::skeletal_animation_entry_gpu::update_skeleton( mat4* data, uint32 time ) const
112{
113        float tick_time = ( time * 0.001f ) * m_node_data->get_frame_rate();
114        float anim_time = fmodf( tick_time, m_node_data->get_duration() );
115
116        if ( !m_node_data->is_flat() )
117        {
118                animate_rec( data, anim_time, 0, mat4() );
119                return;
120        }
121
122        for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
123                if ( m_bone_ids[n] >= 0 )
124                {
125                        const mesh_node_data* node = m_node_data->get_node(n);
126                        nv::mat4 node_mat( node->transform );
127
128                        if ( node->data )
129                        {
130                                node_mat = node->data->get_matrix( anim_time );
131                        }
132
133                        sint16 bone_id = m_bone_ids[n];
134                        data[ bone_id ] = node_mat * m_offsets[ bone_id ];
135                }
136}
137
138void nv::skeletal_animation_entry_gpu::prepare( const mesh_nodes_data* bones )
139{
140        if ( m_prepared ) return;
141        std::unordered_map< std::string, nv::uint16 > bone_names;
142        m_offsets = new mat4[ bones->get_count() ];
143        for ( nv::uint16 bi = 0; bi < bones->get_count(); ++bi )
144        {
145                const mesh_node_data* bone = bones->get_node(bi);
146                bone_names[ bone->name ] = bi;
147                m_offsets[bi] = bone->transform;
148        }
149
150        for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
151        {
152                const mesh_node_data* node = m_node_data->get_node(n);
153                sint16 bone_id = -1;
154
155                auto bi = bone_names.find( node->name );
156                if ( bi != bone_names.end() )
157                {
158                        bone_id = bi->second;
159                }
160                m_bone_ids[n] = bone_id;
161        }
162        m_prepared = true;
163}
164
165void nv::skeletal_animation_entry_gpu::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) const
166{
167        // TODO: fix transforms, which are now embedded,
168        //       see note in assimp_loader.cc:load_node
169        const mesh_node_data* node = m_node_data->get_node( node_id );
170        mat4 node_mat( node->transform );
171
172        if ( node->data )
173        {
174                node_mat = node->data->get_matrix( time );
175        }
176
177        mat4 global_mat = parent_mat * node_mat;
178
179        sint16 bone_id = m_bone_ids[ node_id ];
180        if ( bone_id >= 0 )
181        {
182                data[ bone_id ] = global_mat * m_offsets[ bone_id ];
183        }
184
185        for ( auto child : m_children[ node_id ] )
186        {
187                animate_rec( data, time, child, global_mat );
188        }
189}
190
191nv::skeletal_animation_entry_gpu::~skeletal_animation_entry_gpu()
192{
193        delete[] m_offsets;
194        delete[] m_children;
195        delete[] m_bone_ids;
196}
197
198nv::skeletal_mesh_gpu::skeletal_mesh_gpu( device* a_device, const mesh_data* a_mesh, const mesh_nodes_data* a_bone_data )
199        : animated_mesh(), m_bone_data( a_bone_data ), m_transform( nullptr )
200{
201        m_va          = a_device->create_vertex_array( a_mesh, nv::STATIC_DRAW );
202        m_index_count = a_mesh->get_count();
203        if ( m_bone_data )
204        {
205                m_transform = new mat4[ m_bone_data->get_count() ];
206        }
207}
208
209void nv::skeletal_mesh_gpu::run_animation( animation_entry* a_anim )
210{
211        if ( m_bone_data && a_anim != nullptr )
212        {
213                skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)(a_anim);
214                anim->prepare( m_bone_data );
215                update_animation( a_anim, 0 );
216        }
217}
218
219void nv::skeletal_mesh_gpu::update_animation( animation_entry* a_anim, uint32 a_anim_time )
220{
221        if ( m_bone_data && a_anim )
222        {
223                skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)a_anim;
224                anim->update_skeleton( m_transform, a_anim_time );
225        }
226}
227
228void nv::skeletal_mesh_gpu::update( program* a_program ) const
229{
230        if ( m_bone_data )
231                a_program->set_uniform_array( "nv_m_bones", m_transform, m_bone_data->get_count() );
232}
233
234nv::transform nv::skeletal_mesh_gpu::get_node_transform( uint32 node_id ) const
235{
236        return transform( m_transform[ node_id ] );
237}
238
239nv::mat4 nv::skeletal_mesh_gpu::get_node_matrix( uint32 node_id ) const
240{
241        return m_transform[ node_id ];
242}
Note: See TracBrowser for help on using the repository browser.