// Copyright (C) 2011-2015 ChaosForge Ltd // http://chaosforge.org/ // // This file is part of Nova libraries. // For conditions of distribution and use, see copying.txt file in root folder. #include "nv/gfx/skeletal_mesh.hh" #include "nv/interface/context.hh" #include "nv/interface/device.hh" #include "nv/stl/unordered_map.hh" #include "nv/core/logging.hh" void nv::skeletal_animation_entry::update_skeleton( skeleton_instance& data, uint32 a_ms_time ) const { float fframe = ( a_ms_time * 0.001f ) * m_fps; float nframe = nv::floor( fframe ); uint32 duration = get_frame_count(); if ( duration == 0 ) { fframe = static_cast( get_start_frame() ); } else if ( nframe >= duration ) { if ( is_looping() ) fframe = nv::fmodf( fframe, nv::f32( duration ) ); else fframe = static_cast( get_end_frame() ); } if ( data.size() == 0 ) data.initialize( m_temp_anim->size() ); data.animate( m_temp_anim, m_data, fframe ); } void nv::skeletal_animation_entry::prepare( const mesh_nodes_data* bones ) { m_data.prepare( m_temp_anim, bones ? bones : m_temp_anim ); } nv::skeletal_mesh::skeletal_mesh( context* a_context, const data_channel_set* a_mesh, const mesh_nodes_data* a_bone_data ) : m_skeleton( a_bone_data ? a_bone_data->size() : 0 ), m_context( a_context ), m_bone_data( a_bone_data ), m_index_count( 0 ), m_parent_id(-1) { if ( a_mesh ) { m_va = a_context->create_vertex_array( a_mesh, nv::STATIC_DRAW ); m_index_count = a_mesh->get_channel_size( slot::INDEX ); m_parent_id = a_mesh->get_parent_id(); } } void nv::skeletal_mesh::update_animation( animation_entry& a_anim, uint32 a_anim_time ) { skeletal_animation_entry& anim = static_cast( a_anim ); anim.prepare( m_bone_data ); anim.update_skeleton( m_skeleton, a_anim_time ); } void nv::skeletal_mesh::update_program( program a_program ) { m_context->get_device()->set_opt_uniform_array( a_program, "nv_m_bones", m_skeleton.transforms(), m_skeleton.size() ); } nv::transform nv::skeletal_mesh::get_node_transform( uint32 node_id ) const { return transform( get_node_matrix( node_id ) ); } nv::mat4 nv::skeletal_mesh::get_node_matrix( uint32 node_id ) const { return m_skeleton.transforms()[ node_id ]; }