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 | {
|
---|
17 | m_mesh_data = a_mesh_data->spawn();
|
---|
18 | m_va = a_device->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
|
---|
19 | }
|
---|
20 |
|
---|
21 | void nv::skeletal_mesh::update_animation( animation_entry* a_anim, uint32 a_anim_time )
|
---|
22 | {
|
---|
23 | if ( a_anim )
|
---|
24 | {
|
---|
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 );
|
---|
30 | m_mesh_data->apply( m_transform.data() );
|
---|
31 | vertex_buffer* vb = m_va->find_buffer( nv::slot::POSITION );
|
---|
32 | vb->bind();
|
---|
33 | vb->update( m_mesh_data->data(), 0, m_mesh_data->size() );
|
---|
34 | vb->unbind();
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | nv::skeletal_mesh::~skeletal_mesh()
|
---|
39 | {
|
---|
40 | delete m_va;
|
---|
41 | delete m_mesh_data;
|
---|
42 | }
|
---|
43 |
|
---|
44 | void nv::skeletal_mesh::run_animation( animation_entry* a_anim )
|
---|
45 | {
|
---|
46 | if ( a_anim != nullptr )
|
---|
47 | {
|
---|
48 | skeletal_animation_entry * anim = (skeletal_animation_entry*)(a_anim);
|
---|
49 | m_transform.resize( anim->get_num_joints() );
|
---|
50 | update_animation( a_anim, 0 );
|
---|
51 | }
|
---|
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 )
|
---|
75 | {
|
---|
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 );
|
---|
80 | }
|
---|
81 | }
|
---|
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 | } |
---|