[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 |
|
---|
[470] | 74 | void nv::keyframed_mesh::update_animation( animation_entry* anim, uint32 a_ms_anim_time )
|
---|
[158] | 75 | {
|
---|
| 76 | if ( m_active )
|
---|
| 77 | {
|
---|
[470] | 78 | float fframe = ( static_cast<float>( a_ms_anim_time ) * 0.001f ) * anim->get_fps();
|
---|
| 79 | uint32 frame = uint32( fframe );
|
---|
| 80 | float reminder = fframe - static_cast<float>( frame );
|
---|
| 81 | uint32 duration = anim->is_looping() ? anim->get_frame_count() + 1 : anim->get_frame_count();
|
---|
| 82 |
|
---|
| 83 | if ( frame >= duration )
|
---|
[158] | 84 | {
|
---|
[288] | 85 | if ( anim->is_looping() )
|
---|
[158] | 86 | {
|
---|
[470] | 87 | frame = frame % duration;
|
---|
| 88 | fframe = static_cast<float>( frame ) + reminder;
|
---|
[158] | 89 | }
|
---|
| 90 | else
|
---|
| 91 | {
|
---|
[470] | 92 | m_active = false;
|
---|
| 93 | m_last_frame = anim->get_end_frame();
|
---|
| 94 | m_next_frame = m_last_frame;
|
---|
[288] | 95 | m_interpolation = 0.0f;
|
---|
| 96 | return;
|
---|
[158] | 97 | }
|
---|
| 98 | }
|
---|
[470] | 99 | m_last_frame = frame + anim->get_start_frame();
|
---|
[283] | 100 | m_next_frame = m_last_frame + 1;
|
---|
[470] | 101 | if ( m_next_frame > anim->get_end_frame() ) m_next_frame = anim->get_start_frame();
|
---|
| 102 | m_interpolation = reminder;
|
---|
[158] | 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[283] | 106 |
|
---|
[303] | 107 | void nv::keyframed_mesh::update( program a_program )
|
---|
[158] | 108 | {
|
---|
[303] | 109 | m_context->get_device()->set_opt_uniform( a_program, "nv_interpolate", m_interpolation );
|
---|
[223] | 110 | }
|
---|
| 111 |
|
---|
| 112 | nv::keyframed_mesh::~keyframed_mesh()
|
---|
| 113 | {
|
---|
[313] | 114 | m_context->release( m_va );
|
---|
[223] | 115 | }
|
---|
| 116 |
|
---|
[230] | 117 | void nv::keyframed_mesh::run_animation( animation_entry* a_anim )
|
---|
| 118 | {
|
---|
[252] | 119 | if ( a_anim )
|
---|
| 120 | {
|
---|
[288] | 121 | m_active = true;
|
---|
| 122 | m_last_frame = 0;
|
---|
| 123 | m_next_frame = 0;
|
---|
| 124 | m_interpolation = 0.0f;
|
---|
[252] | 125 | }
|
---|
| 126 | else
|
---|
| 127 | {
|
---|
| 128 | m_active = false;
|
---|
| 129 | }
|
---|
[230] | 130 | }
|
---|
| 131 |
|
---|
[430] | 132 | nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
|
---|
[302] | 133 | : keyframed_mesh( a_context, a_data, a_tag_map )
|
---|
[294] | 134 | , m_loc_next_position( -1 )
|
---|
| 135 | , m_loc_next_normal( -1 )
|
---|
| 136 | , m_loc_next_tangent( -1 )
|
---|
[223] | 137 | , m_gpu_last_frame( 0xFFFFFFFF )
|
---|
| 138 | , m_gpu_next_frame( 0xFFFFFFFF )
|
---|
| 139 | {
|
---|
[313] | 140 | m_va = a_context->create_vertex_array( a_data, STATIC_DRAW );
|
---|
| 141 | m_pbuffer = a_context->find_buffer( m_va, slot::POSITION );
|
---|
[239] | 142 | }
|
---|
[223] | 143 |
|
---|
| 144 |
|
---|
[314] | 145 | void nv::keyframed_mesh_gpu::update_animation( animation_entry* anim, uint32 a_anim_time )
|
---|
[223] | 146 | {
|
---|
[314] | 147 | keyframed_mesh::update_animation( anim, a_anim_time );
|
---|
[296] | 148 | if ( m_loc_next_position == -1 ) return;
|
---|
[158] | 149 | if ( m_gpu_last_frame != m_last_frame )
|
---|
| 150 | {
|
---|
[302] | 151 | uint32 base_offset = m_last_frame * m_vertex_count * m_vsize;
|
---|
[313] | 152 | m_context->update_attribute_offset( m_va, slot::POSITION, base_offset );
|
---|
| 153 | m_context->update_attribute_offset( m_va, slot::NORMAL, base_offset + sizeof( vec3 ) );
|
---|
[294] | 154 | if ( m_has_tangent && m_loc_next_tangent != -1 )
|
---|
| 155 | {
|
---|
[313] | 156 | m_context->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) );
|
---|
[294] | 157 | }
|
---|
[158] | 158 | m_gpu_last_frame = m_last_frame;
|
---|
| 159 | }
|
---|
[296] | 160 | if ( m_loc_next_position != -1 && m_gpu_next_frame != m_next_frame )
|
---|
[158] | 161 | {
|
---|
[302] | 162 | uint32 base_offset = m_next_frame * m_vertex_count * m_vsize;
|
---|
[406] | 163 | m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_position ), base_offset );
|
---|
| 164 | m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_normal ), base_offset + sizeof( vec3 ) );
|
---|
[302] | 165 | if ( m_has_tangent && m_loc_next_tangent != -1 )
|
---|
| 166 | {
|
---|
[406] | 167 | m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_tangent ), base_offset + 2*sizeof( vec3 ) );
|
---|
[302] | 168 | }
|
---|
[158] | 169 | m_gpu_next_frame = m_next_frame;
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[303] | 173 | void nv::keyframed_mesh_gpu::update( program a_program )
|
---|
[296] | 174 | {
|
---|
| 175 | if ( m_loc_next_position == -1 )
|
---|
| 176 | {
|
---|
[303] | 177 | device* dev = m_context->get_device();
|
---|
| 178 | m_loc_next_position = dev->get_attribute_location( a_program, "nv_next_position" );
|
---|
| 179 | m_loc_next_normal = dev->get_attribute_location( a_program, "nv_next_normal" );
|
---|
[296] | 180 | if ( m_has_tangent )
|
---|
[303] | 181 | m_loc_next_tangent = dev->get_attribute_location( a_program, "nv_next_tangent" );
|
---|
[296] | 182 |
|
---|
[406] | 183 | m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_position ), m_pbuffer, FLOAT, 3, 0, m_vsize, false );
|
---|
| 184 | m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_normal ), m_pbuffer, FLOAT, 3, sizeof( vec3 ), m_vsize, false );
|
---|
[296] | 185 | if ( m_has_tangent )
|
---|
[406] | 186 | 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] | 187 | }
|
---|
| 188 | keyframed_mesh::update( a_program );
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[430] | 191 | nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
|
---|
[302] | 192 | : keyframed_mesh( a_context, a_data, a_tag_map )
|
---|
[158] | 193 | {
|
---|
[417] | 194 | const raw_data_channel* vchannel = m_has_tangent ? a_data->get_channel< vertex_pnt >() : a_data->get_channel< vertex_pn >();
|
---|
[313] | 195 | m_va = m_context->create_vertex_array();
|
---|
[417] | 196 | m_pbuffer = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * m_vsize, vchannel->raw_data() );
|
---|
| 197 | m_context->add_vertex_buffers( m_va, m_pbuffer, vchannel->descriptor() );
|
---|
[223] | 198 |
|
---|
[430] | 199 | buffer vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * sizeof( vec2 ), a_data->get_channel_data<vertex_t>() );
|
---|
| 200 | m_context->add_vertex_buffers( m_va, vb, a_data->get_channel<vertex_t>()->descriptor() );
|
---|
[223] | 201 |
|
---|
[430] | 202 | const raw_data_channel* index_channel = a_data->get_channel( slot::INDEX );
|
---|
[412] | 203 | buffer ib = m_context->get_device()->create_buffer( INDEX_BUFFER, STATIC_DRAW, index_channel->raw_size(), index_channel->raw_data() );
|
---|
[416] | 204 | m_context->set_index_buffer( m_va, ib, index_channel->descriptor()[0].etype, true );
|
---|
[223] | 205 |
|
---|
[294] | 206 | m_data = new uint8[ m_vertex_count * m_vsize ];
|
---|
[430] | 207 |
|
---|
| 208 | m_model_data = vchannel->raw_data();
|
---|
[158] | 209 | }
|
---|
[223] | 210 |
|
---|
[314] | 211 | void nv::keyframed_mesh_cpu::update_animation( animation_entry* anim, uint32 a_anim_time )
|
---|
[223] | 212 | {
|
---|
[314] | 213 | keyframed_mesh::update_animation( anim, a_anim_time );
|
---|
[294] | 214 | // TODO: this could be done generic for any data
|
---|
| 215 | if ( m_has_tangent )
|
---|
[223] | 216 | {
|
---|
[430] | 217 | const vertex_pnt* data = reinterpret_cast< const vertex_pnt* >( m_model_data );
|
---|
[294] | 218 | const vertex_pnt* prev = data + m_vertex_count * m_last_frame;
|
---|
| 219 | const vertex_pnt* next = data + m_vertex_count * m_next_frame;
|
---|
[406] | 220 | vertex_pnt* vtx = reinterpret_cast<vertex_pnt*>( m_data );
|
---|
[294] | 221 | for ( size_t i = 0; i < m_vertex_count; ++i )
|
---|
| 222 | {
|
---|
[454] | 223 | vtx[i].position = math::mix( prev[i].position, next[i].position, m_interpolation );
|
---|
| 224 | vtx[i].normal = math::mix( prev[i].normal, next[i].normal, m_interpolation );
|
---|
| 225 | vtx[i].tangent = math::mix( prev[i].tangent, next[i].tangent, m_interpolation );
|
---|
[294] | 226 | }
|
---|
[223] | 227 | }
|
---|
[294] | 228 | else
|
---|
| 229 | {
|
---|
[430] | 230 | const vertex_pn* data = reinterpret_cast< const vertex_pn* >( m_model_data );
|
---|
[294] | 231 | const vertex_pn* prev = data + m_vertex_count * m_last_frame;
|
---|
| 232 | const vertex_pn* next = data + m_vertex_count * m_next_frame;
|
---|
[406] | 233 | vertex_pn* vtx = reinterpret_cast<vertex_pn*>( m_data );
|
---|
[223] | 234 |
|
---|
[294] | 235 | for ( size_t i = 0; i < m_vertex_count; ++i )
|
---|
| 236 | {
|
---|
[454] | 237 | vtx[i].position = math::mix( prev[i].position, next[i].position, m_interpolation );
|
---|
| 238 | vtx[i].normal = math::mix( prev[i].normal, next[i].normal, m_interpolation );
|
---|
[294] | 239 | }
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[302] | 242 | m_context->update( m_pbuffer, m_data, 0, m_vertex_count * m_vsize );
|
---|
[223] | 243 | }
|
---|
[294] | 244 |
|
---|
| 245 | nv::keyframed_mesh_cpu::~keyframed_mesh_cpu()
|
---|
| 246 | {
|
---|
| 247 | delete[] m_data;
|
---|
| 248 | }
|
---|