[395] | 1 | // Copyright (C) 2011-2015 ChaosForge Ltd
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[227] | 6 |
|
---|
| 7 | #include "nv/gfx/skeletal_mesh.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/interface/context.hh"
|
---|
| 10 | #include "nv/interface/device.hh"
|
---|
[392] | 11 | #include "nv/stl/unordered_map.hh"
|
---|
[227] | 12 |
|
---|
[475] | 13 | #include "nv/core/logging.hh"
|
---|
[287] | 14 |
|
---|
[470] | 15 | void nv::skeletal_animation_entry::update_skeleton( mat4* data, uint32 a_ms_time ) const
|
---|
[283] | 16 | {
|
---|
[470] | 17 | float fframe = ( a_ms_time * 0.001f ) * m_fps;
|
---|
[471] | 18 | uint32 frame = uint32( math::floor( fframe ) );
|
---|
[470] | 19 | float reminder = fframe - static_cast<float>( frame );
|
---|
| 20 | uint32 duration = get_frame_count();
|
---|
| 21 | if ( duration == 0 )
|
---|
| 22 | {
|
---|
| 23 | frame = get_start_frame();
|
---|
| 24 | fframe = static_cast<float>( frame );
|
---|
| 25 | }
|
---|
| 26 | else if ( frame >= duration )
|
---|
| 27 | {
|
---|
| 28 | if ( is_looping() )
|
---|
| 29 | {
|
---|
| 30 | frame = frame % duration;
|
---|
| 31 | fframe = static_cast<float>( frame ) + reminder;
|
---|
| 32 | }
|
---|
| 33 | else
|
---|
| 34 | {
|
---|
| 35 | frame = get_end_frame();
|
---|
| 36 | fframe = static_cast<float>( frame );
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
[287] | 39 |
|
---|
[475] | 40 | m_data.animate( data, fframe );
|
---|
[283] | 41 | }
|
---|
| 42 |
|
---|
[467] | 43 | void nv::skeletal_animation_entry::prepare( const mesh_nodes_data* bones )
|
---|
[283] | 44 | {
|
---|
[475] | 45 | m_data.prepare( bones );
|
---|
[283] | 46 | }
|
---|
| 47 |
|
---|
[467] | 48 | nv::skeletal_mesh::skeletal_mesh( context* a_context, const data_channel_set* a_mesh, const mesh_nodes_data* a_bone_data )
|
---|
[470] | 49 | : m_context( a_context ), m_bone_data( a_bone_data ), m_index_count( 0 ), m_transform( nullptr ), m_parent_id(-1)
|
---|
[287] | 50 | {
|
---|
[458] | 51 | if ( a_mesh )
|
---|
| 52 | {
|
---|
| 53 | m_va = a_context->create_vertex_array( a_mesh, nv::STATIC_DRAW );
|
---|
| 54 | m_index_count = a_mesh->get_channel_size( slot::INDEX );
|
---|
[470] | 55 | m_parent_id = a_mesh->get_parent_id();
|
---|
[458] | 56 | }
|
---|
[287] | 57 | if ( m_bone_data )
|
---|
| 58 | {
|
---|
[428] | 59 | m_transform = new mat4[ m_bone_data->size() ];
|
---|
[287] | 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[467] | 63 | void nv::skeletal_mesh::update_animation( animation_entry* a_anim, uint32 a_anim_time )
|
---|
[283] | 64 | {
|
---|
[287] | 65 | if ( m_bone_data && a_anim )
|
---|
[283] | 66 | {
|
---|
[467] | 67 | skeletal_animation_entry * anim = static_cast<skeletal_animation_entry*>( a_anim );
|
---|
[296] | 68 | anim->prepare( m_bone_data );
|
---|
[287] | 69 | anim->update_skeleton( m_transform, a_anim_time );
|
---|
[283] | 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[467] | 73 | void nv::skeletal_mesh::update( program a_program )
|
---|
[283] | 74 | {
|
---|
[287] | 75 | if ( m_bone_data )
|
---|
[428] | 76 | m_context->get_device()->set_opt_uniform_array( a_program, "nv_m_bones", m_transform, m_bone_data->size() );
|
---|
[287] | 77 | }
|
---|
| 78 |
|
---|
[467] | 79 | nv::transform nv::skeletal_mesh::get_node_transform( uint32 node_id ) const
|
---|
[287] | 80 | {
|
---|
| 81 | return transform( m_transform[ node_id ] );
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[467] | 84 | nv::mat4 nv::skeletal_mesh::get_node_matrix( uint32 node_id ) const
|
---|
[287] | 85 | {
|
---|
| 86 | return m_transform[ node_id ];
|
---|
| 87 | }
|
---|