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

Last change on this file since 296 was 296, checked in by epyon, 11 years ago
  • lua::table_guard is_* utility functions
  • minor tweaks
File size: 6.9 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_cpu::skeletal_mesh_cpu( device* a_device, const mesh_data* a_mesh_data, const mesh_nodes_data* bones )
14        : skeletal_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_cpu::update_animation( animation_entry* a_anim, uint32 a_anim_time )
32{
33        if ( a_anim )
34        {
35                skeletal_animation_entry_cpu * anim = (skeletal_animation_entry_cpu*)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_cpu::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_cpu::~skeletal_mesh_cpu()
88{
89        delete m_va;
90}
91
92void nv::skeletal_animation_entry_gpu::initialize()
93{
94        m_prepared  = false;
95        m_children  = nullptr;
96        m_offsets   = nullptr;
97        uint32 node_count = m_node_data->get_count();
98        m_bone_ids  = new sint16[ node_count ];
99
100        if ( !m_node_data->is_flat() )
101        {
102                m_children = new std::vector< uint32 >[ node_count ];
103                for ( uint32 n = 0; n < node_count; ++n )
104                {
105                        const mesh_node_data* node = m_node_data->get_node(n);
106                        if ( node->parent_id != -1 )
107                        {
108                                m_children[ node->parent_id ].push_back( n );
109                        }
110                }
111        }
112}
113
114void nv::skeletal_animation_entry_gpu::update_skeleton( mat4* data, uint32 time ) const
115{
116        float tick_time = ( time * 0.001f ) * m_frame_rate;
117        float anim_time = m_start;
118        if ( m_duration > 0.0f ) anim_time += fmodf( tick_time, m_duration );
119
120        if ( !m_node_data->is_flat() )
121        {
122                animate_rec( data, anim_time, 0, mat4() );
123                return;
124        }
125
126        for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
127                if ( m_bone_ids[n] >= 0 )
128                {
129                        const mesh_node_data* node = m_node_data->get_node(n);
130                        nv::mat4 node_mat( node->transform );
131
132                        if ( node->data )
133                        {
134                                node_mat = node->data->get_matrix( anim_time );
135                        }
136
137                        sint16 bone_id = m_bone_ids[n];
138                        data[ bone_id ] = node_mat * m_offsets[ bone_id ];
139                }
140}
141
142void nv::skeletal_animation_entry_gpu::prepare( const mesh_nodes_data* bones )
143{
144        if ( m_prepared ) return;
145        std::unordered_map< std::string, nv::uint16 > bone_names;
146        m_offsets = new mat4[ bones->get_count() ];
147        for ( nv::uint16 bi = 0; bi < bones->get_count(); ++bi )
148        {
149                const mesh_node_data* bone = bones->get_node(bi);
150                bone_names[ bone->name ] = bi;
151                m_offsets[bi] = bone->transform;
152        }
153
154        for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
155        {
156                const mesh_node_data* node = m_node_data->get_node(n);
157                sint16 bone_id = -1;
158
159                auto bi = bone_names.find( node->name );
160                if ( bi != bone_names.end() )
161                {
162                        bone_id = bi->second;
163                }
164                m_bone_ids[n] = bone_id;
165        }
166        m_prepared = true;
167}
168
169void nv::skeletal_animation_entry_gpu::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) const
170{
171        // TODO: fix transforms, which are now embedded,
172        //       see note in assimp_loader.cc:load_node
173        const mesh_node_data* node = m_node_data->get_node( node_id );
174        mat4 node_mat( node->transform );
175
176        if ( node->data )
177        {
178                node_mat = node->data->get_matrix( time );
179        }
180
181        mat4 global_mat = parent_mat * node_mat;
182
183        sint16 bone_id = m_bone_ids[ node_id ];
184        if ( bone_id >= 0 )
185        {
186                data[ bone_id ] = global_mat * m_offsets[ bone_id ];
187        }
188
189        for ( auto child : m_children[ node_id ] )
190        {
191                animate_rec( data, time, child, global_mat );
192        }
193}
194
195nv::skeletal_animation_entry_gpu::~skeletal_animation_entry_gpu()
196{
197        delete[] m_offsets;
198        delete[] m_children;
199        delete[] m_bone_ids;
200}
201
202nv::skeletal_mesh_gpu::skeletal_mesh_gpu( device* a_device, const mesh_data* a_mesh, const mesh_nodes_data* a_bone_data )
203        : skeletal_mesh(), m_bone_data( a_bone_data ), m_transform( nullptr )
204{
205        m_va          = a_device->create_vertex_array( a_mesh, nv::STATIC_DRAW );
206        m_index_count = a_mesh->get_count();
207        if ( m_bone_data )
208        {
209                m_transform = new mat4[ m_bone_data->get_count() ];
210        }
211}
212
213void nv::skeletal_mesh_gpu::update_animation( animation_entry* a_anim, uint32 a_anim_time )
214{
215        if ( m_bone_data && a_anim )
216        {
217                skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)a_anim;
218                anim->prepare( m_bone_data );
219                anim->update_skeleton( m_transform, a_anim_time );
220        }
221}
222
223void nv::skeletal_mesh_gpu::update( program* a_program )
224{
225        if ( m_bone_data )
226                a_program->set_opt_uniform_array( "nv_m_bones", m_transform, m_bone_data->get_count() );
227}
228
229nv::transform nv::skeletal_mesh_gpu::get_node_transform( uint32 node_id ) const
230{
231        if ( node_id == 0 ) return transform();
232        return transform( m_transform[ node_id ] );
233}
234
235nv::mat4 nv::skeletal_mesh_gpu::get_node_matrix( uint32 node_id ) const
236{
237        return m_transform[ node_id ];
238}
Note: See TracBrowser for help on using the repository browser.