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

Last change on this file since 479 was 479, checked in by epyon, 10 years ago
  • resource system cleanups
File size: 2.3 KB
Line 
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.
6
7#include "nv/gfx/skeletal_mesh.hh"
8
9#include "nv/interface/context.hh"
10#include "nv/interface/device.hh"
11#include "nv/stl/unordered_map.hh"
12
13#include "nv/core/logging.hh"
14
15void nv::skeletal_animation_entry::update_skeleton( skeleton_instance& data, uint32 a_ms_time ) const
16{
17        float  fframe = ( a_ms_time * 0.001f ) * m_fps;
18        float  nframe = nv::floor( fframe );
19        uint32 duration = get_frame_count();
20        if ( duration == 0 )
21        {
22                fframe = static_cast<float>( get_start_frame() );
23        }
24        else if ( nframe >= duration )
25        {
26                if ( is_looping() )
27                        fframe = nv::fmodf( fframe, nv::f32( duration ) );
28                else
29                        fframe = static_cast<float>( get_end_frame() );
30        }
31
32        if ( data.size() == 0 )
33                data.initialize( m_temp_anim->size() );
34        data.animate( m_temp_anim, m_data, fframe );
35}
36
37void nv::skeletal_animation_entry::prepare( const mesh_nodes_data* bones )
38{
39        m_data.prepare( m_temp_anim, bones ? bones : m_temp_anim );
40}
41
42nv::skeletal_mesh::skeletal_mesh( context* a_context, const data_channel_set* a_mesh, const mesh_nodes_data* a_bone_data )
43        : 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)
44{
45        if ( a_mesh )
46        {
47                m_va = a_context->create_vertex_array( a_mesh, nv::STATIC_DRAW );
48                m_index_count = a_mesh->get_channel_size( slot::INDEX );
49                m_parent_id = a_mesh->get_parent_id();
50        }
51}
52
53void nv::skeletal_mesh::update_animation( animation_entry& a_anim, uint32 a_anim_time )
54{
55        skeletal_animation_entry& anim = static_cast<skeletal_animation_entry&>( a_anim );
56        anim.prepare( m_bone_data );
57        anim.update_skeleton( m_skeleton, a_anim_time );
58}
59
60void nv::skeletal_mesh::update_program( program a_program )
61{
62        m_context->get_device()->set_opt_uniform_array( a_program, "nv_m_bones", m_skeleton.transforms(), m_skeleton.size() );
63}
64
65nv::transform nv::skeletal_mesh::get_node_transform( uint32 node_id ) const
66{
67        return transform( get_node_matrix( node_id ) );
68}
69
70nv::mat4 nv::skeletal_mesh::get_node_matrix( uint32 node_id ) const
71{
72        return m_skeleton.transforms()[ node_id ];
73}
Note: See TracBrowser for help on using the repository browser.