[158] | 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/keyframed_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 | #include "nv/logging.hh"
|
---|
| 14 |
|
---|
| 15 | using namespace nv;
|
---|
| 16 |
|
---|
| 17 | keyframed_mesh::keyframed_mesh( context* a_context, keyframed_mesh_data* a_data, program* a_program )
|
---|
| 18 | : m_context( a_context )
|
---|
| 19 | , m_data( a_data )
|
---|
| 20 | , m_program( a_program )
|
---|
| 21 | , m_va( nullptr )
|
---|
| 22 | , m_loc_next_position( 0 )
|
---|
| 23 | , m_loc_next_normal( 0 )
|
---|
| 24 | , m_last_frame( 0 )
|
---|
| 25 | , m_next_frame( 0 )
|
---|
| 26 | , m_gpu_last_frame( 0xFFFFFFFF )
|
---|
| 27 | , m_gpu_next_frame( 0xFFFFFFFF )
|
---|
| 28 | , m_interpolation( 0.0f )
|
---|
| 29 | , m_start_frame( false )
|
---|
| 30 | , m_stop_frame( false )
|
---|
| 31 | , m_looping( false )
|
---|
| 32 | , m_active( false )
|
---|
| 33 | {
|
---|
| 34 | m_va = m_context->get_device()->create_vertex_array();
|
---|
| 35 |
|
---|
| 36 | nv::vertex_buffer* vb;
|
---|
[161] | 37 | m_loc_next_position = m_program->get_attribute( "nv_next_position" )->get_location();
|
---|
| 38 | m_loc_next_normal = m_program->get_attribute( "nv_next_normal" )->get_location();
|
---|
[158] | 39 |
|
---|
| 40 | vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_positions().data() );
|
---|
| 41 | m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, 0, false );
|
---|
[161] | 42 | m_va->add_vertex_buffer( nv::POSITION, vb, nv::FLOAT, 3 );
|
---|
[158] | 43 |
|
---|
| 44 | vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_normals().data() );
|
---|
| 45 | m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, 0, 0, false );
|
---|
[161] | 46 | m_va->add_vertex_buffer( nv::NORMAL, vb, nv::FLOAT, 3 );
|
---|
[158] | 47 |
|
---|
| 48 | vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
|
---|
[161] | 49 | m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
|
---|
[158] | 50 | nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint16 ), (void*)m_data->get_indices().data() );
|
---|
| 51 | m_va->set_index_buffer( ib, nv::USHORT, true );
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | mat4 keyframed_mesh::get_tag( const std::string& tag ) const
|
---|
| 55 | {
|
---|
| 56 | const std::vector< nv::mat4 >& transforms = m_data->get_tag_map().at( tag );
|
---|
| 57 | return glm::interpolate( transforms[ m_last_frame ], transforms[ m_next_frame ], m_interpolation );
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | uint32 keyframed_mesh::get_max_frames() const
|
---|
| 61 | {
|
---|
| 62 | return m_data->get_frame_count();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void keyframed_mesh::set_frame( uint32 frame )
|
---|
| 66 | {
|
---|
| 67 | m_last_frame = frame;
|
---|
| 68 | m_next_frame = frame;
|
---|
| 69 | m_interpolation = 0.0f;
|
---|
| 70 | m_active = false;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[159] | 73 | void keyframed_mesh::setup_animation( uint32 start, uint32 count, uint32 fps, bool loop )
|
---|
[158] | 74 | {
|
---|
| 75 | m_start_frame = start;
|
---|
[159] | 76 | m_stop_frame = start+count-1;
|
---|
[158] | 77 | m_looping = loop;
|
---|
| 78 | m_fps = fps;
|
---|
[159] | 79 | m_active = count > 1;
|
---|
[158] | 80 | m_last_frame = start;
|
---|
[159] | 81 | m_next_frame = (count > 1 ? start + 1 : start );
|
---|
[158] | 82 | m_interpolation = 0.0f;
|
---|
| 83 | m_time = 0;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void keyframed_mesh::update( uint32 ms )
|
---|
| 87 | {
|
---|
| 88 | if ( m_active )
|
---|
| 89 | {
|
---|
| 90 | m_time += ms;
|
---|
| 91 | uint32 f_diff = (m_stop_frame - m_start_frame);
|
---|
| 92 | float f_time = 1000 / (float)m_fps;
|
---|
| 93 | float f_max = ( m_looping ? ( f_diff + 1 ) : f_diff ) * f_time;
|
---|
| 94 | float f_pos = m_time / f_time;
|
---|
| 95 |
|
---|
| 96 | m_last_frame = (uint32)glm::floor( f_pos ) + m_start_frame;
|
---|
| 97 | m_next_frame = m_last_frame + 1;
|
---|
| 98 | if ( m_next_frame > m_stop_frame )
|
---|
| 99 | {
|
---|
| 100 | m_next_frame = m_start_frame;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | if ( m_time >= f_max )
|
---|
| 104 | {
|
---|
| 105 | if ( m_looping )
|
---|
| 106 | {
|
---|
| 107 | uint32 left = m_time - f_max;
|
---|
| 108 | m_time = 0;
|
---|
| 109 | update( left );
|
---|
| 110 | }
|
---|
| 111 | else
|
---|
| 112 | {
|
---|
| 113 | m_active = false;
|
---|
| 114 | m_last_frame = m_stop_frame;
|
---|
| 115 | m_next_frame = m_stop_frame;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | m_interpolation = f_pos - glm::floor( f_pos );
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void nv::keyframed_mesh::draw( render_state& rstate )
|
---|
| 123 | {
|
---|
| 124 | nv::uint32 vtx_count = m_data->get_vertex_count();
|
---|
| 125 | if ( m_gpu_last_frame != m_last_frame )
|
---|
| 126 | {
|
---|
[161] | 127 | m_va->update_vertex_buffer( slot::POSITION, m_last_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
| 128 | m_va->update_vertex_buffer( slot::NORMAL, m_last_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
[158] | 129 | m_gpu_last_frame = m_last_frame;
|
---|
| 130 | }
|
---|
| 131 | if ( m_gpu_next_frame != m_next_frame )
|
---|
| 132 | {
|
---|
| 133 | m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
| 134 | m_va->update_vertex_buffer( m_loc_next_normal, m_next_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
| 135 | m_gpu_next_frame = m_next_frame;
|
---|
| 136 | }
|
---|
[161] | 137 | m_program->set_uniform( "nv_interpolate", m_interpolation );
|
---|
[158] | 138 | m_context->draw( nv::TRIANGLES, rstate, m_program, m_va, m_data->get_index_count() );
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | nv::keyframed_mesh::~keyframed_mesh()
|
---|
| 142 | {
|
---|
| 143 | delete m_va;
|
---|
| 144 | }
|
---|