[395] | 1 | // Copyright (C) 2011-2015 ChaosForge Ltd
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[158] | 6 |
|
---|
| 7 | #include "nv/gfx/keyframed_mesh.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/interface/context.hh"
|
---|
| 10 | #include "nv/interface/device.hh"
|
---|
[319] | 11 | #include "nv/core/logging.hh"
|
---|
[158] | 12 |
|
---|
| 13 | using namespace nv;
|
---|
| 14 |
|
---|
[302] | 15 | nv::keyframed_mesh::keyframed_mesh( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
|
---|
[231] | 16 | : animated_mesh()
|
---|
[302] | 17 | , m_context( a_context )
|
---|
[239] | 18 | , m_mesh_data( a_data )
|
---|
| 19 | , m_tag_map( a_tag_map )
|
---|
[158] | 20 | , m_last_frame( 0 )
|
---|
| 21 | , m_next_frame( 0 )
|
---|
| 22 | , m_interpolation( 0.0f )
|
---|
| 23 | , m_active( false )
|
---|
| 24 | {
|
---|
[417] | 25 | m_index_count = m_mesh_data->get_channel_size( slot::INDEX );
|
---|
| 26 | m_vertex_count = m_mesh_data->get_channel_size<vertex_t>();
|
---|
| 27 | uint32 pos_size = m_mesh_data->get_channel_size<vertex_pnt>();
|
---|
| 28 | if ( pos_size == 0 )
|
---|
[294] | 29 | {
|
---|
[417] | 30 | pos_size = m_mesh_data->get_channel_size<vertex_pn>();
|
---|
| 31 | m_has_tangent = false;
|
---|
| 32 | m_vsize = sizeof( vertex_pn );
|
---|
[294] | 33 | }
|
---|
[417] | 34 | else
|
---|
| 35 | {
|
---|
| 36 | m_has_tangent = true;
|
---|
| 37 | m_vsize = sizeof( vertex_pnt );
|
---|
| 38 | }
|
---|
| 39 | m_frame_count = pos_size / m_vertex_count;
|
---|
[302] | 40 | m_pbuffer = buffer();
|
---|
[158] | 41 | }
|
---|
| 42 |
|
---|
[376] | 43 | nv::size_t keyframed_mesh::get_max_frames() const
|
---|
[158] | 44 | {
|
---|
[239] | 45 | return m_frame_count;
|
---|
[158] | 46 | }
|
---|
| 47 |
|
---|
[287] | 48 | transform keyframed_mesh::get_node_transform( uint32 node_id ) const
|
---|
[158] | 49 | {
|
---|
[304] | 50 | if ( !m_tag_map ) return transform();
|
---|
[287] | 51 | NV_ASSERT( node_id < m_tag_map->get_count(), "TAGMAP FAIL" );
|
---|
[417] | 52 | const key_channel_set* data = m_tag_map->get_node( node_id )->data;
|
---|
[285] | 53 | NV_ASSERT( data, "TAG FAIL" );
|
---|
| 54 | transform last = data->get_raw_transform( m_last_frame );
|
---|
| 55 | transform next = data->get_raw_transform( m_next_frame );
|
---|
| 56 | return interpolate( last, next, m_interpolation );
|
---|
[158] | 57 | }
|
---|
| 58 |
|
---|
[287] | 59 | mat4 keyframed_mesh::get_node_matrix( uint32 node_id ) const
|
---|
| 60 | {
|
---|
| 61 | return get_node_transform( node_id ).extract();
|
---|
| 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 |
|
---|
[288] | 72 | void nv::keyframed_mesh::update_animation( animation_entry* anim, uint32 a_anim_time )
|
---|
[158] | 73 | {
|
---|
| 74 | if ( m_active )
|
---|
| 75 | {
|
---|
[406] | 76 | float tick_time = ( static_cast<float>( a_anim_time ) * 0.001f ) * anim->get_frame_rate();
|
---|
[288] | 77 | float duration = anim->is_looping() ? anim->get_duration() + 1.0f : anim->get_duration();
|
---|
| 78 | if ( tick_time >= duration )
|
---|
[158] | 79 | {
|
---|
[288] | 80 | if ( anim->is_looping() )
|
---|
[158] | 81 | {
|
---|
[288] | 82 | tick_time = fmodf( tick_time, duration );
|
---|
[158] | 83 | }
|
---|
| 84 | else
|
---|
| 85 | {
|
---|
| 86 | m_active = false;
|
---|
[406] | 87 | m_last_frame = static_cast<uint32>( anim->get_end() );
|
---|
[288] | 88 | m_next_frame = m_last_frame;
|
---|
| 89 | m_interpolation = 0.0f;
|
---|
| 90 | return;
|
---|
[158] | 91 | }
|
---|
| 92 | }
|
---|
[406] | 93 | m_last_frame = static_cast<uint32>( glm::floor( tick_time ) + anim->get_start() );
|
---|
[283] | 94 | m_next_frame = m_last_frame + 1;
|
---|
[406] | 95 | if ( m_next_frame > static_cast<uint32>( anim->get_end() ) ) m_next_frame = static_cast<uint32>( anim->get_start() );
|
---|
[288] | 96 | m_interpolation = tick_time - glm::floor( tick_time );
|
---|
[158] | 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[283] | 100 |
|
---|
[303] | 101 | void nv::keyframed_mesh::update( program a_program )
|
---|
[158] | 102 | {
|
---|
[303] | 103 | m_context->get_device()->set_opt_uniform( a_program, "nv_interpolate", m_interpolation );
|
---|
[223] | 104 | }
|
---|
| 105 |
|
---|
| 106 | nv::keyframed_mesh::~keyframed_mesh()
|
---|
| 107 | {
|
---|
[313] | 108 | m_context->release( m_va );
|
---|
[223] | 109 | }
|
---|
| 110 |
|
---|
[230] | 111 | void nv::keyframed_mesh::run_animation( animation_entry* a_anim )
|
---|
| 112 | {
|
---|
[252] | 113 | if ( a_anim )
|
---|
| 114 | {
|
---|
[288] | 115 | m_active = true;
|
---|
| 116 | m_last_frame = 0;
|
---|
| 117 | m_next_frame = 0;
|
---|
| 118 | m_interpolation = 0.0f;
|
---|
[252] | 119 | }
|
---|
| 120 | else
|
---|
| 121 | {
|
---|
| 122 | m_active = false;
|
---|
| 123 | }
|
---|
[230] | 124 | }
|
---|
| 125 |
|
---|
[299] | 126 | nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
|
---|
[302] | 127 | : keyframed_mesh( a_context, a_data, a_tag_map )
|
---|
[294] | 128 | , m_loc_next_position( -1 )
|
---|
| 129 | , m_loc_next_normal( -1 )
|
---|
| 130 | , m_loc_next_tangent( -1 )
|
---|
[223] | 131 | , m_gpu_last_frame( 0xFFFFFFFF )
|
---|
| 132 | , m_gpu_next_frame( 0xFFFFFFFF )
|
---|
| 133 | {
|
---|
[313] | 134 | m_va = a_context->create_vertex_array( a_data, STATIC_DRAW );
|
---|
| 135 | m_pbuffer = a_context->find_buffer( m_va, slot::POSITION );
|
---|
[239] | 136 | }
|
---|
[223] | 137 |
|
---|
| 138 |
|
---|
[314] | 139 | void nv::keyframed_mesh_gpu::update_animation( animation_entry* anim, uint32 a_anim_time )
|
---|
[223] | 140 | {
|
---|
[314] | 141 | keyframed_mesh::update_animation( anim, a_anim_time );
|
---|
[296] | 142 | if ( m_loc_next_position == -1 ) return;
|
---|
[158] | 143 | if ( m_gpu_last_frame != m_last_frame )
|
---|
| 144 | {
|
---|
[302] | 145 | uint32 base_offset = m_last_frame * m_vertex_count * m_vsize;
|
---|
[313] | 146 | m_context->update_attribute_offset( m_va, slot::POSITION, base_offset );
|
---|
| 147 | m_context->update_attribute_offset( m_va, slot::NORMAL, base_offset + sizeof( vec3 ) );
|
---|
[294] | 148 | if ( m_has_tangent && m_loc_next_tangent != -1 )
|
---|
| 149 | {
|
---|
[313] | 150 | m_context->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) );
|
---|
[294] | 151 | }
|
---|
[158] | 152 | m_gpu_last_frame = m_last_frame;
|
---|
| 153 | }
|
---|
[296] | 154 | if ( m_loc_next_position != -1 && m_gpu_next_frame != m_next_frame )
|
---|
[158] | 155 | {
|
---|
[302] | 156 | uint32 base_offset = m_next_frame * m_vertex_count * m_vsize;
|
---|
[406] | 157 | m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_position ), base_offset );
|
---|
| 158 | m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_normal ), base_offset + sizeof( vec3 ) );
|
---|
[302] | 159 | if ( m_has_tangent && m_loc_next_tangent != -1 )
|
---|
| 160 | {
|
---|
[406] | 161 | m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_tangent ), base_offset + 2*sizeof( vec3 ) );
|
---|
[302] | 162 | }
|
---|
[158] | 163 | m_gpu_next_frame = m_next_frame;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[303] | 167 | void nv::keyframed_mesh_gpu::update( program a_program )
|
---|
[296] | 168 | {
|
---|
| 169 | if ( m_loc_next_position == -1 )
|
---|
| 170 | {
|
---|
[303] | 171 | device* dev = m_context->get_device();
|
---|
| 172 | m_loc_next_position = dev->get_attribute_location( a_program, "nv_next_position" );
|
---|
| 173 | m_loc_next_normal = dev->get_attribute_location( a_program, "nv_next_normal" );
|
---|
[296] | 174 | if ( m_has_tangent )
|
---|
[303] | 175 | m_loc_next_tangent = dev->get_attribute_location( a_program, "nv_next_tangent" );
|
---|
[296] | 176 |
|
---|
[406] | 177 | m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_position ), m_pbuffer, FLOAT, 3, 0, m_vsize, false );
|
---|
| 178 | m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_normal ), m_pbuffer, FLOAT, 3, sizeof( vec3 ), m_vsize, false );
|
---|
[296] | 179 | if ( m_has_tangent )
|
---|
[406] | 180 | m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_tangent ), m_pbuffer, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );
|
---|
[296] | 181 | }
|
---|
| 182 | keyframed_mesh::update( a_program );
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[299] | 185 | nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
|
---|
[302] | 186 | : keyframed_mesh( a_context, a_data, a_tag_map )
|
---|
[158] | 187 | {
|
---|
[417] | 188 | const raw_data_channel* vchannel = m_has_tangent ? a_data->get_channel< vertex_pnt >() : a_data->get_channel< vertex_pn >();
|
---|
[313] | 189 | m_va = m_context->create_vertex_array();
|
---|
[417] | 190 | m_pbuffer = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * m_vsize, vchannel->raw_data() );
|
---|
| 191 | m_context->add_vertex_buffers( m_va, m_pbuffer, vchannel->descriptor() );
|
---|
[223] | 192 |
|
---|
[413] | 193 | buffer vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * sizeof( vec2 ), m_mesh_data->get_channel_data<vertex_t>() );
|
---|
[417] | 194 | m_context->add_vertex_buffers( m_va, vb, m_mesh_data->get_channel<vertex_t>()->descriptor() );
|
---|
[223] | 195 |
|
---|
[416] | 196 | const raw_data_channel* index_channel = m_mesh_data->get_channel( slot::INDEX );
|
---|
[412] | 197 | buffer ib = m_context->get_device()->create_buffer( INDEX_BUFFER, STATIC_DRAW, index_channel->raw_size(), index_channel->raw_data() );
|
---|
[416] | 198 | m_context->set_index_buffer( m_va, ib, index_channel->descriptor()[0].etype, true );
|
---|
[223] | 199 |
|
---|
[294] | 200 | m_data = new uint8[ m_vertex_count * m_vsize ];
|
---|
[158] | 201 | }
|
---|
[223] | 202 |
|
---|
[314] | 203 | void nv::keyframed_mesh_cpu::update_animation( animation_entry* anim, uint32 a_anim_time )
|
---|
[223] | 204 | {
|
---|
[314] | 205 | keyframed_mesh::update_animation( anim, a_anim_time );
|
---|
[294] | 206 | // TODO: this could be done generic for any data
|
---|
| 207 | if ( m_has_tangent )
|
---|
[223] | 208 | {
|
---|
[294] | 209 | const vertex_pnt* data = m_mesh_data->get_channel_data<vertex_pnt>();
|
---|
| 210 | const vertex_pnt* prev = data + m_vertex_count * m_last_frame;
|
---|
| 211 | const vertex_pnt* next = data + m_vertex_count * m_next_frame;
|
---|
[406] | 212 | vertex_pnt* vtx = reinterpret_cast<vertex_pnt*>( m_data );
|
---|
[294] | 213 | for ( size_t i = 0; i < m_vertex_count; ++i )
|
---|
| 214 | {
|
---|
| 215 | vtx[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
|
---|
| 216 | vtx[i].normal = glm::mix( prev[i].normal, next[i].normal, m_interpolation );
|
---|
| 217 | vtx[i].tangent = glm::mix( prev[i].tangent, next[i].tangent, m_interpolation );
|
---|
| 218 | }
|
---|
[223] | 219 | }
|
---|
[294] | 220 | else
|
---|
| 221 | {
|
---|
| 222 | const vertex_pn* data = m_mesh_data->get_channel_data<vertex_pn>();
|
---|
| 223 | const vertex_pn* prev = data + m_vertex_count * m_last_frame;
|
---|
| 224 | const vertex_pn* next = data + m_vertex_count * m_next_frame;
|
---|
[406] | 225 | vertex_pn* vtx = reinterpret_cast<vertex_pn*>( m_data );
|
---|
[223] | 226 |
|
---|
[294] | 227 | for ( size_t i = 0; i < m_vertex_count; ++i )
|
---|
| 228 | {
|
---|
| 229 | vtx[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
|
---|
| 230 | vtx[i].normal = glm::mix( prev[i].normal, next[i].normal, m_interpolation );
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[302] | 234 | m_context->update( m_pbuffer, m_data, 0, m_vertex_count * m_vsize );
|
---|
[223] | 235 | }
|
---|
[294] | 236 |
|
---|
| 237 | nv::keyframed_mesh_cpu::~keyframed_mesh_cpu()
|
---|
| 238 | {
|
---|
| 239 | delete[] m_data;
|
---|
| 240 | }
|
---|