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

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