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

Last change on this file since 392 was 392, checked in by epyon, 10 years ago
  • massive shift towards nova STL
  • include cleanups


File size: 6.8 KB
Line 
1// Copyright (C) 2011-2014 ChaosForge Ltd
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 "nv/interface/context.hh"
8#include "nv/interface/device.hh"
9#include "nv/stl/unordered_map.hh"
10
11nv::skeletal_mesh_cpu::skeletal_mesh_cpu( context* a_context, const mesh_data* a_mesh_data, const mesh_nodes_data* bones )
12        : skeletal_mesh( a_context )
13        , m_data( a_mesh_data )
14{
15        const mesh_raw_channel* pnt_chan = a_mesh_data->get_channel<md5_vtx_pnt>();
16        m_pntdata.assign( (const md5_vtx_pnt*)pnt_chan->data, pnt_chan->count );
17        m_bone_offset.resize( bones->get_count() );
18        m_transform.resize( bones->get_count() );
19
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_context->create_vertex_array( a_mesh_data,
27STREAM_DRAW );
28        m_pbuffer   = a_context->find_buffer( m_va, slot::POSITION );
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                        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                                        unsigned index = (unsigned)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                m_context->update( m_pbuffer, m_pntdata.data(), 0, m_pntdata.raw_size() );
65        }
66}
67
68
69void nv::skeletal_animation_entry_cpu::update_skeleton( transform* skeleton, float time ) const
70{
71        float frame_duration = 1000.f / (float)m_node_data->get_frame_rate();
72        float anim_duration = frame_duration * m_node_data->get_duration();
73        float new_time = fmodf( time, anim_duration ) * 0.001f;
74
75        float frame_num = new_time * m_node_data->get_frame_rate();
76        for ( size_t i = 0; i < m_node_data->get_count(); ++i )
77        {
78                skeleton[i] = m_node_data->get_node(i)->data->get_transform( frame_num );
79        }
80}
81
82void nv::skeletal_animation_entry_gpu::initialize()
83{
84        m_prepared  = false;
85        m_children  = nullptr;
86        m_offsets   = nullptr;
87        uint32 node_count = m_node_data->get_count();
88        m_bone_ids  = new sint16[ node_count ];
89
90        if ( !m_node_data->is_flat() )
91        {
92                m_children = new vector< uint32 >[ node_count ];
93                for ( uint32 n = 0; n < node_count; ++n )
94                {
95                        const mesh_node_data* node = m_node_data->get_node(n);
96                        if ( node->parent_id != -1 )
97                        {
98                                m_children[ node->parent_id ].push_back( n );
99                        }
100                }
101        }
102}
103
104void nv::skeletal_animation_entry_gpu::update_skeleton( mat4* data, uint32 time ) const
105{
106        float tick_time = ( time * 0.001f ) * m_frame_rate;
107        float anim_time = m_start;
108        if ( m_duration > 0.0f ) anim_time += fmodf( tick_time, m_duration );
109
110        if ( !m_node_data->is_flat() )
111        {
112                animate_rec( data, anim_time, 0, mat4() );
113                return;
114        }
115
116        for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
117                if ( m_bone_ids[n] >= 0 )
118                {
119                        const mesh_node_data* node = m_node_data->get_node(n);
120                        nv::mat4 node_mat( node->transform );
121
122                        if ( node->data )
123                        {
124                                node_mat = node->data->get_matrix( anim_time );
125                        }
126
127                        sint16 bone_id = m_bone_ids[n];
128                        data[ bone_id ] = node_mat * m_offsets[ bone_id ];
129                }
130}
131
132void nv::skeletal_animation_entry_gpu::prepare( const mesh_nodes_data* bones )
133{
134        if ( m_prepared ) return;
135        unordered_map< std::string, nv::uint16 > bone_names;
136        m_offsets = new mat4[ bones->get_count() ];
137        for ( nv::uint16 bi = 0; bi < bones->get_count(); ++bi )
138        {
139                const mesh_node_data* bone = bones->get_node(bi);
140                bone_names[ bone->name ] = bi;
141                m_offsets[bi] = bone->transform;
142        }
143
144        for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
145        {
146                const mesh_node_data* node = m_node_data->get_node(n);
147                sint16 bone_id = -1;
148
149                auto bi = bone_names.find( node->name );
150                if ( bi != bone_names.end() )
151                {
152                        bone_id = (sint16)bi->second;
153                }
154                m_bone_ids[n] = bone_id;
155        }
156        m_prepared = true;
157}
158
159void nv::skeletal_animation_entry_gpu::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) const
160{
161        // TODO: fix transforms, which are now embedded,
162        //       see note in assimp_loader.cc:load_node
163        const mesh_node_data* node = m_node_data->get_node( node_id );
164        mat4 node_mat( node->transform );
165
166        if ( node->data )
167        {
168                node_mat = node->data->get_matrix( time );
169        }
170
171        mat4 global_mat = parent_mat * node_mat;
172
173        sint16 bone_id = m_bone_ids[ node_id ];
174        if ( bone_id >= 0 )
175        {
176                data[ bone_id ] = global_mat * m_offsets[ bone_id ];
177        }
178
179        for ( auto child : m_children[ node_id ] )
180        {
181                animate_rec( data, time, child, global_mat );
182        }
183}
184
185nv::skeletal_animation_entry_gpu::~skeletal_animation_entry_gpu()
186{
187        delete[] m_offsets;
188        delete[] m_children;
189        delete[] m_bone_ids;
190}
191
192nv::skeletal_mesh_gpu::skeletal_mesh_gpu( context* a_context, const mesh_data* a_mesh, const mesh_nodes_data* a_bone_data )
193        : skeletal_mesh( a_context ), m_bone_data( a_bone_data ), m_transform( nullptr )
194{
195        m_va          = a_context->create_vertex_array( a_mesh, nv::STATIC_DRAW );
196        m_index_count = a_mesh->get_count();
197        if ( m_bone_data )
198        {
199                m_transform = new mat4[ m_bone_data->get_count() ];
200        }
201}
202
203void nv::skeletal_mesh_gpu::update_animation( animation_entry* a_anim, uint32 a_anim_time )
204{
205        if ( m_bone_data && a_anim )
206        {
207                skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)a_anim;
208                anim->prepare( m_bone_data );
209                anim->update_skeleton( m_transform, a_anim_time );
210        }
211}
212
213void nv::skeletal_mesh_gpu::update( program a_program )
214{
215        if ( m_bone_data )
216                m_context->get_device()->set_opt_uniform_array( a_program, "nv_m_bones", m_transform, m_bone_data->get_count() );
217}
218
219nv::transform nv::skeletal_mesh_gpu::get_node_transform( uint32 node_id ) const
220{
221        if ( node_id == 0 ) return transform();
222        return transform( m_transform[ node_id ] );
223}
224
225nv::mat4 nv::skeletal_mesh_gpu::get_node_matrix( uint32 node_id ) const
226{
227        return m_transform[ node_id ];
228}
Note: See TracBrowser for help on using the repository browser.