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