[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 | {
|
---|
[241] | 17 | m_mesh_data = a_mesh_data->spawn();
|
---|
[275] | 18 | m_va = a_device->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
|
---|
[239] | 19 | }
|
---|
[227] | 20 |
|
---|
[283] | 21 | void nv::skeletal_mesh::update_animation( animation_entry* a_anim, uint32 a_anim_time )
|
---|
[227] | 22 | {
|
---|
[283] | 23 | if ( a_anim )
|
---|
[241] | 24 | {
|
---|
[283] | 25 | skeletal_animation_entry * anim = (skeletal_animation_entry*)a_anim;
|
---|
| 26 | float frame_duration = 1000.f / (float)anim->get_frame_rate();
|
---|
| 27 | uint32 anim_duration = uint32( frame_duration * (float)anim->get_frame_count() );
|
---|
| 28 | uint32 new_time = a_anim_time % anim_duration;
|
---|
| 29 | anim->update_skeleton( m_transform.data(), (float)new_time * 0.001f );
|
---|
[261] | 30 | m_mesh_data->apply( m_transform.data() );
|
---|
[281] | 31 | vertex_buffer* vb = m_va->find_buffer( nv::slot::POSITION );
|
---|
[239] | 32 | vb->bind();
|
---|
[241] | 33 | vb->update( m_mesh_data->data(), 0, m_mesh_data->size() );
|
---|
[239] | 34 | vb->unbind();
|
---|
[227] | 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[230] | 38 | nv::skeletal_mesh::~skeletal_mesh()
|
---|
[227] | 39 | {
|
---|
[230] | 40 | delete m_va;
|
---|
[241] | 41 | delete m_mesh_data;
|
---|
[227] | 42 | }
|
---|
| 43 |
|
---|
[230] | 44 | void nv::skeletal_mesh::run_animation( animation_entry* a_anim )
|
---|
[227] | 45 | {
|
---|
[252] | 46 | if ( a_anim != nullptr )
|
---|
| 47 | {
|
---|
[283] | 48 | skeletal_animation_entry * anim = (skeletal_animation_entry*)(a_anim);
|
---|
| 49 | m_transform.resize( anim->get_num_joints() );
|
---|
| 50 | update_animation( a_anim, 0 );
|
---|
[252] | 51 | }
|
---|
[283] | 52 | }
|
---|
| 53 |
|
---|
| 54 | void nv::skeletal_animation_entry_gpu::update_skeleton( mat4* data, uint32 time )
|
---|
| 55 | {
|
---|
| 56 | m_animation->animate( data, time );
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | void nv::skeletal_animation_entry_gpu::prepare( const nmd_temp_model* m_model )
|
---|
| 60 | {
|
---|
| 61 | m_animation->prepare( m_model );
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | nv::skeletal_mesh_gpu::skeletal_mesh_gpu( device* a_device, const nmd_temp_model* a_model, uint32 index, bool primary )
|
---|
| 65 | : animated_mesh(), m_primary( primary ), m_model( a_model )
|
---|
| 66 | {
|
---|
| 67 | const mesh_data* data = a_model->get_data( index );
|
---|
| 68 | m_va = a_device->create_vertex_array( data, nv::STATIC_DRAW );
|
---|
| 69 | m_index_count = data->get_count();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void nv::skeletal_mesh_gpu::run_animation( animation_entry* a_anim )
|
---|
| 73 | {
|
---|
| 74 | if ( m_primary && a_anim != nullptr )
|
---|
[252] | 75 | {
|
---|
[283] | 76 | skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)(a_anim);
|
---|
| 77 | m_transform.resize( m_model->get_bone_count() );
|
---|
| 78 | anim->prepare( m_model );
|
---|
| 79 | update_animation( a_anim, 0 );
|
---|
[252] | 80 | }
|
---|
[227] | 81 | }
|
---|
[283] | 82 |
|
---|
| 83 | void nv::skeletal_mesh_gpu::update_animation( animation_entry* a_anim, uint32 a_anim_time )
|
---|
| 84 | {
|
---|
| 85 | if ( m_primary && a_anim )
|
---|
| 86 | {
|
---|
| 87 | skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)a_anim;
|
---|
| 88 | anim->update_skeleton( m_transform.data(), a_anim_time );
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | void nv::skeletal_mesh_gpu::update( program* a_program ) const
|
---|
| 93 | {
|
---|
| 94 | if (m_primary)
|
---|
| 95 | a_program->set_uniform_array( "nv_m_bones", m_transform );
|
---|
| 96 | } |
---|