[319] | 1 | // Copyright (C) 2011-2014 ChaosForge Ltd
|
---|
[227] | 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/skeletal_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 |
|
---|
[299] | 13 | nv::skeletal_mesh_cpu::skeletal_mesh_cpu( context* a_context, const mesh_data* a_mesh_data, const mesh_nodes_data* bones )
|
---|
[302] | 14 | : skeletal_mesh( a_context )
|
---|
[287] | 15 | , m_data( a_mesh_data )
|
---|
[227] | 16 | {
|
---|
[287] | 17 | const mesh_raw_channel* pnt_chan = a_mesh_data->get_channel<md5_vtx_pnt>();
|
---|
| 18 | m_pntdata.assign( (const md5_vtx_pnt*)pnt_chan->data, pnt_chan->count );
|
---|
| 19 | m_bone_offset.resize( bones->get_count() );
|
---|
[289] | 20 | m_transform.resize( bones->get_count() );
|
---|
| 21 |
|
---|
[287] | 22 | for ( uint32 i = 0; i < bones->get_count(); ++i )
|
---|
| 23 | {
|
---|
| 24 | m_bone_offset[i] = transform( bones->get_node(i)->transform );
|
---|
| 25 | }
|
---|
| 26 | m_vtx_data = a_mesh_data->get_channel_data<md5_vtx_pntiw>();
|
---|
| 27 | m_indices = a_mesh_data->get_count();
|
---|
[313] | 28 | m_va = a_context->create_vertex_array( a_mesh_data,
|
---|
[302] | 29 | STREAM_DRAW );
|
---|
[313] | 30 | m_pbuffer = a_context->find_buffer( m_va, slot::POSITION );
|
---|
[239] | 31 | }
|
---|
[227] | 32 |
|
---|
[296] | 33 | void nv::skeletal_mesh_cpu::update_animation( animation_entry* a_anim, uint32 a_anim_time )
|
---|
[227] | 34 | {
|
---|
[283] | 35 | if ( a_anim )
|
---|
[241] | 36 | {
|
---|
[296] | 37 | skeletal_animation_entry_cpu * anim = (skeletal_animation_entry_cpu*)a_anim;
|
---|
[289] | 38 | anim->update_skeleton( m_transform.data(), (float)a_anim_time );
|
---|
[287] | 39 | {
|
---|
| 40 | size_t skeleton_size = m_bone_offset.size();
|
---|
| 41 | size_t vertex_count = m_pntdata.size();
|
---|
| 42 | m_pos_offset.resize( skeleton_size );
|
---|
| 43 | for ( unsigned int i = 0; i < skeleton_size; ++i )
|
---|
| 44 | {
|
---|
| 45 | m_pos_offset[i] = m_transform[i] * m_bone_offset[i];
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | std::fill( m_pntdata.raw_data(), m_pntdata.raw_data() + m_pntdata.raw_size(), 0 );
|
---|
| 49 | for ( unsigned int i = 0; i < vertex_count; ++i )
|
---|
| 50 | {
|
---|
| 51 | const md5_vtx_pntiw& vert = m_vtx_data[i];
|
---|
| 52 |
|
---|
| 53 | for ( size_t j = 0; j < 4; ++j )
|
---|
| 54 | {
|
---|
[323] | 55 | unsigned index = (unsigned)vert.boneindex[j];
|
---|
| 56 | float weight = vert.boneweight[j];
|
---|
[287] | 57 | const quat& orient = m_transform[index].get_orientation();
|
---|
| 58 | const transform& offset = m_pos_offset[index];
|
---|
| 59 | m_pntdata[i].position += offset.transformed( vert.position ) * weight;
|
---|
| 60 | m_pntdata[i].normal += ( orient * vert.normal ) * weight;
|
---|
| 61 | m_pntdata[i].tangent += ( orient * vert.tangent ) * weight;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[302] | 66 | m_context->update( m_pbuffer, m_pntdata.data(), 0, m_pntdata.raw_size() );
|
---|
[227] | 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[289] | 70 |
|
---|
[296] | 71 | void nv::skeletal_animation_entry_cpu::update_skeleton( transform* skeleton, float time ) const
|
---|
[289] | 72 | {
|
---|
| 73 | float frame_duration = 1000.f / (float)m_node_data->get_frame_rate();
|
---|
| 74 | float anim_duration = frame_duration * m_node_data->get_duration();
|
---|
| 75 | float new_time = fmodf( time, anim_duration ) * 0.001f;
|
---|
| 76 |
|
---|
| 77 | float frame_num = new_time * m_node_data->get_frame_rate();
|
---|
| 78 | for ( size_t i = 0; i < m_node_data->get_count(); ++i )
|
---|
| 79 | {
|
---|
| 80 | skeleton[i] = m_node_data->get_node(i)->data->get_transform( frame_num );
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[288] | 84 | void nv::skeletal_animation_entry_gpu::initialize()
|
---|
[283] | 85 | {
|
---|
[287] | 86 | m_prepared = false;
|
---|
| 87 | m_children = nullptr;
|
---|
| 88 | m_offsets = nullptr;
|
---|
[288] | 89 | uint32 node_count = m_node_data->get_count();
|
---|
[287] | 90 | m_bone_ids = new sint16[ node_count ];
|
---|
| 91 |
|
---|
| 92 | if ( !m_node_data->is_flat() )
|
---|
| 93 | {
|
---|
| 94 | m_children = new std::vector< uint32 >[ node_count ];
|
---|
| 95 | for ( uint32 n = 0; n < node_count; ++n )
|
---|
| 96 | {
|
---|
| 97 | const mesh_node_data* node = m_node_data->get_node(n);
|
---|
| 98 | if ( node->parent_id != -1 )
|
---|
| 99 | {
|
---|
| 100 | m_children[ node->parent_id ].push_back( n );
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
[283] | 104 | }
|
---|
| 105 |
|
---|
[287] | 106 | void nv::skeletal_animation_entry_gpu::update_skeleton( mat4* data, uint32 time ) const
|
---|
[283] | 107 | {
|
---|
[290] | 108 | float tick_time = ( time * 0.001f ) * m_frame_rate;
|
---|
| 109 | float anim_time = m_start;
|
---|
| 110 | if ( m_duration > 0.0f ) anim_time += fmodf( tick_time, m_duration );
|
---|
[287] | 111 |
|
---|
| 112 | if ( !m_node_data->is_flat() )
|
---|
| 113 | {
|
---|
| 114 | animate_rec( data, anim_time, 0, mat4() );
|
---|
| 115 | return;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
|
---|
| 119 | if ( m_bone_ids[n] >= 0 )
|
---|
| 120 | {
|
---|
| 121 | const mesh_node_data* node = m_node_data->get_node(n);
|
---|
| 122 | nv::mat4 node_mat( node->transform );
|
---|
| 123 |
|
---|
| 124 | if ( node->data )
|
---|
| 125 | {
|
---|
| 126 | node_mat = node->data->get_matrix( anim_time );
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | sint16 bone_id = m_bone_ids[n];
|
---|
| 130 | data[ bone_id ] = node_mat * m_offsets[ bone_id ];
|
---|
| 131 | }
|
---|
[283] | 132 | }
|
---|
| 133 |
|
---|
[287] | 134 | void nv::skeletal_animation_entry_gpu::prepare( const mesh_nodes_data* bones )
|
---|
[283] | 135 | {
|
---|
[287] | 136 | if ( m_prepared ) return;
|
---|
| 137 | std::unordered_map< std::string, nv::uint16 > bone_names;
|
---|
| 138 | m_offsets = new mat4[ bones->get_count() ];
|
---|
| 139 | for ( nv::uint16 bi = 0; bi < bones->get_count(); ++bi )
|
---|
| 140 | {
|
---|
| 141 | const mesh_node_data* bone = bones->get_node(bi);
|
---|
| 142 | bone_names[ bone->name ] = bi;
|
---|
| 143 | m_offsets[bi] = bone->transform;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | for ( uint32 n = 0; n < m_node_data->get_count(); ++n )
|
---|
| 147 | {
|
---|
| 148 | const mesh_node_data* node = m_node_data->get_node(n);
|
---|
| 149 | sint16 bone_id = -1;
|
---|
| 150 |
|
---|
| 151 | auto bi = bone_names.find( node->name );
|
---|
| 152 | if ( bi != bone_names.end() )
|
---|
| 153 | {
|
---|
[323] | 154 | bone_id = (sint16)bi->second;
|
---|
[287] | 155 | }
|
---|
| 156 | m_bone_ids[n] = bone_id;
|
---|
| 157 | }
|
---|
| 158 | m_prepared = true;
|
---|
[283] | 159 | }
|
---|
| 160 |
|
---|
[287] | 161 | void nv::skeletal_animation_entry_gpu::animate_rec( mat4* data, float time, uint32 node_id, const mat4& parent_mat ) const
|
---|
| 162 | {
|
---|
| 163 | // TODO: fix transforms, which are now embedded,
|
---|
| 164 | // see note in assimp_loader.cc:load_node
|
---|
| 165 | const mesh_node_data* node = m_node_data->get_node( node_id );
|
---|
| 166 | mat4 node_mat( node->transform );
|
---|
| 167 |
|
---|
| 168 | if ( node->data )
|
---|
| 169 | {
|
---|
| 170 | node_mat = node->data->get_matrix( time );
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | mat4 global_mat = parent_mat * node_mat;
|
---|
| 174 |
|
---|
| 175 | sint16 bone_id = m_bone_ids[ node_id ];
|
---|
| 176 | if ( bone_id >= 0 )
|
---|
| 177 | {
|
---|
| 178 | data[ bone_id ] = global_mat * m_offsets[ bone_id ];
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | for ( auto child : m_children[ node_id ] )
|
---|
| 182 | {
|
---|
| 183 | animate_rec( data, time, child, global_mat );
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | nv::skeletal_animation_entry_gpu::~skeletal_animation_entry_gpu()
|
---|
| 188 | {
|
---|
| 189 | delete[] m_offsets;
|
---|
| 190 | delete[] m_children;
|
---|
| 191 | delete[] m_bone_ids;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[299] | 194 | nv::skeletal_mesh_gpu::skeletal_mesh_gpu( context* a_context, const mesh_data* a_mesh, const mesh_nodes_data* a_bone_data )
|
---|
[302] | 195 | : skeletal_mesh( a_context ), m_bone_data( a_bone_data ), m_transform( nullptr )
|
---|
[287] | 196 | {
|
---|
[313] | 197 | m_va = a_context->create_vertex_array( a_mesh, nv::STATIC_DRAW );
|
---|
[287] | 198 | m_index_count = a_mesh->get_count();
|
---|
| 199 | if ( m_bone_data )
|
---|
| 200 | {
|
---|
| 201 | m_transform = new mat4[ m_bone_data->get_count() ];
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[283] | 205 | void nv::skeletal_mesh_gpu::update_animation( animation_entry* a_anim, uint32 a_anim_time )
|
---|
| 206 | {
|
---|
[287] | 207 | if ( m_bone_data && a_anim )
|
---|
[283] | 208 | {
|
---|
| 209 | skeletal_animation_entry_gpu * anim = (skeletal_animation_entry_gpu*)a_anim;
|
---|
[296] | 210 | anim->prepare( m_bone_data );
|
---|
[287] | 211 | anim->update_skeleton( m_transform, a_anim_time );
|
---|
[283] | 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[303] | 215 | void nv::skeletal_mesh_gpu::update( program a_program )
|
---|
[283] | 216 | {
|
---|
[287] | 217 | if ( m_bone_data )
|
---|
[303] | 218 | m_context->get_device()->set_opt_uniform_array( a_program, "nv_m_bones", m_transform, m_bone_data->get_count() );
|
---|
[287] | 219 | }
|
---|
| 220 |
|
---|
| 221 | nv::transform nv::skeletal_mesh_gpu::get_node_transform( uint32 node_id ) const
|
---|
| 222 | {
|
---|
[290] | 223 | if ( node_id == 0 ) return transform();
|
---|
[287] | 224 | return transform( m_transform[ node_id ] );
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | nv::mat4 nv::skeletal_mesh_gpu::get_node_matrix( uint32 node_id ) const
|
---|
| 228 | {
|
---|
| 229 | return m_transform[ node_id ];
|
---|
| 230 | }
|
---|