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