[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 |
|
---|
[239] | 17 | nv::keyframed_mesh::keyframed_mesh( context* a_context, mesh_data* a_data, tag_map* a_tag_map )
|
---|
[231] | 18 | : animated_mesh()
|
---|
[239] | 19 | , m_mesh_data( a_data )
|
---|
| 20 | , m_tag_map( a_tag_map )
|
---|
[223] | 21 | , m_start_frame( false )
|
---|
| 22 | , m_stop_frame( false )
|
---|
[158] | 23 | , m_last_frame( 0 )
|
---|
| 24 | , m_next_frame( 0 )
|
---|
[223] | 25 | , m_time( 0 )
|
---|
| 26 | , m_fps( 0 )
|
---|
[158] | 27 | , m_interpolation( 0.0f )
|
---|
| 28 | , m_looping( false )
|
---|
| 29 | , m_active( false )
|
---|
| 30 | {
|
---|
[231] | 31 | m_va = a_context->get_device()->create_vertex_array();
|
---|
[239] | 32 |
|
---|
| 33 | m_index_count = m_mesh_data->get_index_channel()->count;
|
---|
| 34 | m_vertex_count = m_mesh_data->get_channel_data()[1]->count;
|
---|
| 35 | m_frame_count = m_mesh_data->get_channel_data()[0]->count / m_vertex_count;
|
---|
[158] | 36 | }
|
---|
| 37 |
|
---|
[204] | 38 | size_t keyframed_mesh::get_max_frames() const
|
---|
[158] | 39 | {
|
---|
[239] | 40 | return m_frame_count;
|
---|
[158] | 41 | }
|
---|
| 42 |
|
---|
[230] | 43 | transform keyframed_mesh::get_tag( const std::string& tag ) const
|
---|
[158] | 44 | {
|
---|
[239] | 45 | NV_ASSERT( m_tag_map, "TAGMAP FAIL" );
|
---|
| 46 | const std::vector< transform >* transforms = m_tag_map->get_tag( tag );
|
---|
| 47 | NV_ASSERT( transforms, "TAG FAIL" );
|
---|
| 48 | return interpolate( (*transforms)[ m_last_frame ], (*transforms)[ m_next_frame ], m_interpolation );
|
---|
[158] | 49 | }
|
---|
| 50 |
|
---|
[159] | 51 | void keyframed_mesh::setup_animation( uint32 start, uint32 count, uint32 fps, bool loop )
|
---|
[158] | 52 | {
|
---|
| 53 | m_start_frame = start;
|
---|
[159] | 54 | m_stop_frame = start+count-1;
|
---|
[158] | 55 | m_looping = loop;
|
---|
| 56 | m_fps = fps;
|
---|
[159] | 57 | m_active = count > 1;
|
---|
[223] | 58 | m_time = 0;
|
---|
[158] | 59 | m_last_frame = start;
|
---|
[159] | 60 | m_next_frame = (count > 1 ? start + 1 : start );
|
---|
[158] | 61 | m_interpolation = 0.0f;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[223] | 64 | void nv::keyframed_mesh::set_frame( uint32 frame )
|
---|
| 65 | {
|
---|
| 66 | m_last_frame = frame;
|
---|
| 67 | m_next_frame = frame;
|
---|
| 68 | m_active = false;
|
---|
| 69 | m_interpolation = 0.0f;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[158] | 72 | void keyframed_mesh::update( uint32 ms )
|
---|
| 73 | {
|
---|
| 74 | if ( m_active )
|
---|
| 75 | {
|
---|
| 76 | m_time += ms;
|
---|
| 77 | uint32 f_diff = (m_stop_frame - m_start_frame);
|
---|
| 78 | float f_time = 1000 / (float)m_fps;
|
---|
| 79 | float f_max = ( m_looping ? ( f_diff + 1 ) : f_diff ) * f_time;
|
---|
| 80 | float f_pos = m_time / f_time;
|
---|
| 81 |
|
---|
| 82 | m_last_frame = (uint32)glm::floor( f_pos ) + m_start_frame;
|
---|
| 83 | m_next_frame = m_last_frame + 1;
|
---|
| 84 | if ( m_next_frame > m_stop_frame )
|
---|
| 85 | {
|
---|
| 86 | m_next_frame = m_start_frame;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if ( m_time >= f_max )
|
---|
| 90 | {
|
---|
| 91 | if ( m_looping )
|
---|
| 92 | {
|
---|
[183] | 93 | uint32 left = m_time - static_cast< uint32 >( f_max );
|
---|
[158] | 94 | m_time = 0;
|
---|
| 95 | update( left );
|
---|
| 96 | }
|
---|
| 97 | else
|
---|
| 98 | {
|
---|
| 99 | m_active = false;
|
---|
| 100 | m_last_frame = m_stop_frame;
|
---|
| 101 | m_next_frame = m_stop_frame;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | m_interpolation = f_pos - glm::floor( f_pos );
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[230] | 108 | void nv::keyframed_mesh::update( program* a_program )
|
---|
[158] | 109 | {
|
---|
[230] | 110 | a_program->set_opt_uniform( "nv_interpolate", m_interpolation );
|
---|
[223] | 111 | }
|
---|
| 112 |
|
---|
| 113 | nv::keyframed_mesh::~keyframed_mesh()
|
---|
| 114 | {
|
---|
| 115 | delete m_va;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[230] | 118 | void nv::keyframed_mesh::run_animation( animation_entry* a_anim )
|
---|
| 119 | {
|
---|
| 120 | keyframed_animation_entry * anim = down_cast<keyframed_animation_entry>(a_anim);
|
---|
| 121 | setup_animation( anim->m_start, anim->m_frames, anim->m_fps, anim->m_looping );
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[239] | 124 | nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, mesh_data* a_data, tag_map* a_tag_map, program* a_program )
|
---|
| 125 | : keyframed_mesh( a_context, a_data, a_tag_map )
|
---|
[223] | 126 | , m_loc_next_position( 0 )
|
---|
| 127 | , m_loc_next_normal( 0 )
|
---|
| 128 | , m_gpu_last_frame( 0xFFFFFFFF )
|
---|
| 129 | , m_gpu_next_frame( 0xFFFFFFFF )
|
---|
| 130 | {
|
---|
[230] | 131 | m_loc_next_position = a_program->get_attribute( "nv_next_position" )->get_location();
|
---|
| 132 | m_loc_next_normal = a_program->get_attribute( "nv_next_normal" )->get_location();
|
---|
[239] | 133 | m_va = a_context->get_device()->create_vertex_array( a_data, nv::STATIC_DRAW );
|
---|
| 134 | vertex_buffer* vb = m_va->find_buffer( nv::POSITION );
|
---|
| 135 | m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, sizeof( vertex_pn ), false );
|
---|
| 136 | m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, sizeof( vec3 ), sizeof( vertex_pn ), false );
|
---|
| 137 | }
|
---|
[223] | 138 |
|
---|
| 139 |
|
---|
[230] | 140 | void nv::keyframed_mesh_gpu::update( uint32 ms )
|
---|
[223] | 141 | {
|
---|
[230] | 142 | keyframed_mesh::update( ms );
|
---|
| 143 |
|
---|
[158] | 144 | if ( m_gpu_last_frame != m_last_frame )
|
---|
| 145 | {
|
---|
[239] | 146 | m_va->update_vertex_buffer( slot::POSITION, m_last_frame * m_vertex_count * sizeof( vertex_pn ) );
|
---|
| 147 | m_va->update_vertex_buffer( slot::NORMAL, m_last_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
|
---|
[158] | 148 | m_gpu_last_frame = m_last_frame;
|
---|
| 149 | }
|
---|
| 150 | if ( m_gpu_next_frame != m_next_frame )
|
---|
| 151 | {
|
---|
[239] | 152 | m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * m_vertex_count * sizeof( vertex_pn ) );
|
---|
| 153 | m_va->update_vertex_buffer( m_loc_next_normal, m_next_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
|
---|
[158] | 154 | m_gpu_next_frame = m_next_frame;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[239] | 158 | nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, mesh_data* a_data, tag_map* a_tag_map )
|
---|
| 159 | : keyframed_mesh( a_context, a_data, a_tag_map )
|
---|
[158] | 160 | {
|
---|
[239] | 161 | m_vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( vertex_pn ), (void*)m_mesh_data->get_channel_data()[0]->data );
|
---|
| 162 | m_va->add_vertex_buffers( m_vb, m_mesh_data->get_channel_data()[0] );
|
---|
[223] | 163 |
|
---|
[239] | 164 | nv::vertex_buffer* vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( nv::vec2 ), (void*)m_mesh_data->get_channel_data()[1]->data );
|
---|
| 165 | m_va->add_vertex_buffers( vb, m_mesh_data->get_channel_data()[1] );
|
---|
[223] | 166 |
|
---|
[239] | 167 | nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_mesh_data->get_index_channel()->size, (void*)m_mesh_data->get_index_channel()->data );
|
---|
| 168 | m_va->set_index_buffer( ib, m_mesh_data->get_index_channel()->etype, true );
|
---|
[223] | 169 |
|
---|
[239] | 170 | m_vertex.resize( m_vertex_count );
|
---|
[158] | 171 | }
|
---|
[223] | 172 |
|
---|
| 173 | void nv::keyframed_mesh_cpu::update( uint32 ms )
|
---|
| 174 | {
|
---|
| 175 | keyframed_mesh::update( ms );
|
---|
| 176 |
|
---|
[239] | 177 | const vertex_pn* data = (const vertex_pn*)(m_mesh_data->get_channel_data()[0]->data);
|
---|
| 178 | const vertex_pn* prev = data + m_vertex_count * m_last_frame;
|
---|
| 179 | const vertex_pn* next = data + m_vertex_count * m_next_frame;
|
---|
[223] | 180 |
|
---|
[239] | 181 | for ( size_t i = 0; i < m_vertex_count; ++i )
|
---|
[223] | 182 | {
|
---|
[239] | 183 | m_vertex[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
|
---|
| 184 | m_vertex[i].normal = glm::mix( prev[i].normal, next[i].normal, m_interpolation );
|
---|
[223] | 185 | }
|
---|
| 186 |
|
---|
[239] | 187 | m_vb->bind();
|
---|
| 188 | m_vb->update( m_vertex.data(), 0, m_vertex_count * sizeof( vertex_pn ) );
|
---|
| 189 | m_vb->unbind();
|
---|
[223] | 190 | }
|
---|