[227] | 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 |
|
---|
[275] | 13 | nv::skeletal_mesh::skeletal_mesh( device* a_device, md5_mesh_data* a_mesh_data )
|
---|
[231] | 14 | : animated_mesh()
|
---|
[241] | 15 | , m_mesh_data( nullptr )
|
---|
[227] | 16 | , m_animation( nullptr )
|
---|
[241] | 17 | , m_animation_time( 0 )
|
---|
[227] | 18 | {
|
---|
[241] | 19 | m_mesh_data = a_mesh_data->spawn();
|
---|
[275] | 20 | m_va = a_device->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
|
---|
[239] | 21 | }
|
---|
[227] | 22 |
|
---|
| 23 |
|
---|
| 24 | void nv::skeletal_mesh::setup_animation( md5_animation* a_anim )
|
---|
| 25 | {
|
---|
[241] | 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 | }
|
---|
[227] | 33 | }
|
---|
| 34 |
|
---|
| 35 | void nv::skeletal_mesh::update( uint32 ms )
|
---|
| 36 | {
|
---|
[241] | 37 | if ( m_animation )
|
---|
[227] | 38 | {
|
---|
[241] | 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;
|
---|
[261] | 43 | m_animation->update_skeleton( m_transform.data(), (float)m_animation_time * 0.001f );
|
---|
| 44 | m_mesh_data->apply( m_transform.data() );
|
---|
[281] | 45 | vertex_buffer* vb = m_va->find_buffer( nv::slot::POSITION );
|
---|
[239] | 46 | vb->bind();
|
---|
[241] | 47 | vb->update( m_mesh_data->data(), 0, m_mesh_data->size() );
|
---|
[239] | 48 | vb->unbind();
|
---|
[227] | 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[230] | 52 | nv::skeletal_mesh::~skeletal_mesh()
|
---|
[227] | 53 | {
|
---|
[230] | 54 | delete m_va;
|
---|
[241] | 55 | delete m_mesh_data;
|
---|
[227] | 56 | }
|
---|
| 57 |
|
---|
[230] | 58 | void nv::skeletal_mesh::run_animation( animation_entry* a_anim )
|
---|
[227] | 59 | {
|
---|
[252] | 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 | }
|
---|
[227] | 69 | }
|
---|