1 | // Copyright (C) 2011 Kornel Kisielewicz
|
---|
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 |
|
---|
13 | nv::skeletal_mesh::skeletal_mesh( device* a_device, md5_mesh_data* a_mesh_data )
|
---|
14 | : animated_mesh()
|
---|
15 | , m_mesh_data( nullptr )
|
---|
16 | , m_animation( nullptr )
|
---|
17 | , m_animation_time( 0 )
|
---|
18 | {
|
---|
19 | m_mesh_data = a_mesh_data->spawn();
|
---|
20 | m_va = a_device->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | void nv::skeletal_mesh::setup_animation( md5_animation* a_anim )
|
---|
25 | {
|
---|
26 | m_animation = a_anim;
|
---|
27 | m_animation_time = 0;
|
---|
28 | if ( m_animation )
|
---|
29 | {
|
---|
30 | m_transform.resize( m_animation->get_num_joints() );
|
---|
31 | update( 0 );
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | void nv::skeletal_mesh::update( uint32 ms )
|
---|
36 | {
|
---|
37 | if ( m_animation )
|
---|
38 | {
|
---|
39 | m_animation_time += ms;
|
---|
40 | float frame_duration = 1000.f / (float)m_animation->get_frame_rate();
|
---|
41 | uint32 anim_duration = uint32( frame_duration * (float)m_animation->get_frame_count() );
|
---|
42 | while ( m_animation_time >= anim_duration ) m_animation_time -= anim_duration;
|
---|
43 | m_animation->update_skeleton( m_transform.data(), (float)m_animation_time * 0.001f );
|
---|
44 | m_mesh_data->apply( m_transform.data() );
|
---|
45 | vertex_buffer* vb = m_va->find_buffer( nv::POSITION );
|
---|
46 | vb->bind();
|
---|
47 | vb->update( m_mesh_data->data(), 0, m_mesh_data->size() );
|
---|
48 | vb->unbind();
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | nv::skeletal_mesh::~skeletal_mesh()
|
---|
53 | {
|
---|
54 | delete m_va;
|
---|
55 | delete m_mesh_data;
|
---|
56 | }
|
---|
57 |
|
---|
58 | void nv::skeletal_mesh::run_animation( animation_entry* a_anim )
|
---|
59 | {
|
---|
60 | if ( a_anim != nullptr )
|
---|
61 | {
|
---|
62 | skeletal_animation_entry * anim = down_cast<skeletal_animation_entry>(a_anim);
|
---|
63 | setup_animation( anim->m_animation );
|
---|
64 | }
|
---|
65 | else
|
---|
66 | {
|
---|
67 | setup_animation( nullptr );
|
---|
68 | }
|
---|
69 | }
|
---|