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

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