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