Changeset 293
- Timestamp:
- 07/26/14 09:55:54 (11 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/formats/assimp_loader.hh
r291 r293 20 20 { 21 21 public: 22 assimp_loader( const string& a_ext, const mat3& a_rotate_transform, float a_scale, bool pre_transform, uint32 a_assimp_flags = 0 ); 23 assimp_loader( const string& a_ext, bool pre_transform, uint32 a_assimp_flags = 0 ); 22 explicit assimp_loader( const string& a_ext, uint32 a_assimp_flags = 0 ); 24 23 virtual bool load( stream& source ); 25 24 virtual mesh_data* release_mesh_data( size_t index = 0 ); … … 35 34 bool load_bones( size_t index, std::vector< mesh_node_data >& bones ); 36 35 void load_mesh_data( mesh_data* data, size_t index ); 37 void initialize( const string& a_ext, const mat3& a_rotate_transform, float a_scale, uint32 a_assimp_flags ); 38 sint16 load_node( uint32 anim_id, mesh_node_data* nodes, const void* vnode, sint16 this_id, sint16 parent_id, uint32& max_frames ); 36 sint16 load_node( uint32 anim_id, mesh_node_data* nodes, const void* vnode, sint16 this_id, sint16 parent_id ); 39 37 uint32 count_nodes( const void* node ) const; 40 void create_transformed_keys( mesh_node_data* data, const void* vnode, const mesh_node_data* parent ); 41 void create_direct_keys( mesh_node_data* data, const void* vnode ); 38 void create_keys( mesh_node_data* data, const void* vnode ); 42 39 43 40 string_table_creator m_strings; 44 41 string m_ext; 45 mat3 m_r33;46 mat3 m_ri33;47 float m_scale;48 42 uint32 m_assimp_flags; 49 43 const void* m_scene; 50 44 size_t m_mesh_count; 51 bool m_flat;52 };53 54 struct assimp_plain_vtx55 {56 vec3 position;57 vec3 normal;58 vec2 texcoord;59 vec4 tangent;60 61 assimp_plain_vtx() {}62 assimp_plain_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g )63 {64 position = v;65 texcoord = t;66 normal = n;67 tangent = g;68 }69 };70 71 struct assimp_skinned_vtx72 {73 vec3 position;74 vec3 normal;75 vec2 texcoord;76 vec4 tangent;77 ivec4 boneindex;78 vec4 boneweight;79 80 assimp_skinned_vtx() {}81 assimp_skinned_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g )82 {83 position = v;84 texcoord = t;85 normal = n;86 tangent = g;87 }88 45 }; 89 46 -
trunk/nv/gfx/animation.hh
r287 r293 135 135 if ( ksi.vslot != animation_slot::TIME ) 136 136 { 137 uint32 index = final_key.count;138 final_key.slots[ index ].offset =final_key.size;139 final_key.slots[ index ].etype = ksi.etype;140 final_key.slots[ index ].vslot = ksi.vslot;141 final_key.size += get_datatype_info( ksi.etype ).size;142 final_key.count++;137 uint32 index = m_final_key.count; 138 m_final_key.slots[ index ].offset = m_final_key.size; 139 m_final_key.slots[ index ].etype = ksi.etype; 140 m_final_key.slots[ index ].vslot = ksi.vslot; 141 m_final_key.size += get_datatype_info( ksi.etype ).size; 142 m_final_key.count++; 143 143 } 144 144 } … … 153 153 pkey += m_channels[i]->get_raw( index, pkey ); 154 154 } 155 return extract_matrix_raw( final_key, key );155 return extract_matrix_raw( m_final_key, key ); 156 156 }; 157 157 … … 164 164 pkey += m_channels[i]->get_raw( index, pkey ); 165 165 } 166 return extract_transform_raw( final_key, key );166 return extract_transform_raw( m_final_key, key ); 167 167 }; 168 168 … … 175 175 pkey += m_channels[i]->interpolate_raw( time, pkey ); 176 176 } 177 return extract_matrix_raw( final_key, key );177 return extract_matrix_raw( m_final_key, key ); 178 178 }; 179 179 … … 186 186 pkey += m_channels[i]->interpolate_raw( time, pkey ); 187 187 } 188 return extract_transform_raw( final_key, key );188 return extract_transform_raw( m_final_key, key ); 189 189 }; 190 190 191 191 size_t get_channel_count() const { return m_channels.size(); } 192 192 const key_raw_channel* get_channel( size_t index ) const { return m_channels[ index ]; } 193 const key_descriptor& get_final_key() const { return m_final_key; } 193 194 194 195 virtual ~key_data() … … 197 198 } 198 199 private: 199 key_descriptor final_key;200 key_descriptor m_final_key; 200 201 std::vector< key_raw_channel* > m_channels; 201 202 }; -
trunk/nv/interface/interpolation_raw.hh
r286 r293 137 137 } 138 138 139 inline void transform_key_position( uint8* data, float scale, const mat3& r33 ) 140 { 141 vec3& p = *((vec3*)(data)); 142 p = r33 * p * scale; 143 } 144 inline void transform_key_rotation( uint8* data, const mat3& r33, const mat3& ri33 ) 145 { 146 quat& r = *((quat*)(data)); 147 r = glm::quat_cast( r33 * glm::mat3_cast( r ) * ri33 ); 148 } 149 inline void transform_key_scale( void* data, float scale ) 150 { 151 vec3& s = *((vec3*)(data)); 152 s = s * scale; 153 } 154 inline void transform_key_transform( uint8* data, float scale, const mat3& r33, const mat3& ri33 ) 155 { 156 transform& t = *((transform*)(data)); 157 t = transform( 158 r33 * t.get_position() * scale, 159 glm::quat_cast( r33 * glm::mat3_cast( t.get_orientation() ) * ri33 ) 160 ); 161 } 162 163 inline void transform_key_raw( const key_descriptor& desc, uint8* data, float scale, const mat3& r33, const mat3& ri33 ) 164 { 165 for ( uint32 i = 0; i < desc.count; ++i ) 166 { 167 uint32 offset = desc.slots[i].offset; 168 switch ( desc.slots[i].vslot ) 169 { 170 case animation_slot::TIME: break; 171 case animation_slot::POSITION: transform_key_position( data + offset, scale, r33 ); break; 172 case animation_slot::ROTATION: transform_key_rotation( data + offset, r33, ri33 ); break; 173 case animation_slot::SCALE: transform_key_scale( data + offset, scale ); break; 174 case animation_slot::TFORM: transform_key_transform( data + offset, scale, r33, ri33 ); break; 175 default: 176 break; 177 } 178 } 179 } 139 180 140 181 } -
trunk/nv/interface/mesh_data.hh
r287 r293 1 // Copyright (C) 2012-201 3 ChaosForge / Kornel Kisielewicz1 // Copyright (C) 2012-2014 ChaosForge Ltd 2 2 // http://chaosforge.org/ 3 3 // … … 9 9 10 10 #include <nv/common.hh> 11 #include <vector>12 11 #include <nv/math.hh> 12 #include <nv/string.hh> 13 #include <nv/array.hh> 14 #include <nv/gfx/animation.hh> 13 15 #include <nv/interface/vertex.hh> 14 16 … … 22 24 struct mesh_raw_channel 23 25 { 26 friend class mesh_creator; 27 24 28 vertex_descriptor desc; 25 29 uint8* data; … … 78 82 class mesh_data 79 83 { 84 friend class mesh_creator; 80 85 public: 81 86 explicit mesh_data() : m_index_channel( nullptr ) {} … … 95 100 const std::vector< mesh_raw_channel* >& get_raw_channels() const { return m_channels; } 96 101 const mesh_raw_channel* get_index_channel() const { return m_index_channel; } 102 size_t get_channel_count() const { return m_channels.size(); } 103 const mesh_raw_channel* get_channel( size_t index ) const 104 { 105 if ( m_channels.size() > index ) return m_channels[index]; 106 return nullptr; 107 } 97 108 98 109 size_t get_count() const … … 164 175 }; 165 176 177 struct mesh_node_data 178 { 179 string name; 180 sint16 target_id; 181 sint16 parent_id; 182 mat4 transform; 183 key_data* data; 184 }; 185 186 class mesh_nodes_data 187 { 188 friend class mesh_creator; 189 friend class mesh_nodes_creator; 190 public: 191 explicit mesh_nodes_data( const string& name, uint32 count, mesh_node_data* nodes ) 192 : m_name( name ), m_count( count ), m_nodes( nodes ), m_frame_rate(0), m_duration(0.0f), m_flat( false ) 193 { 194 } 195 196 explicit mesh_nodes_data( const string& name, uint32 count, mesh_node_data* nodes, 197 uint16 a_fps, float a_frames, bool a_flat ) 198 : m_name( name ), m_count( count ), m_nodes( nodes ), m_frame_rate(a_fps), m_duration(a_frames), m_flat( a_flat ) 199 { 200 } 201 202 size_t get_count() const { return m_count; } 203 204 const mesh_node_data* get_node( size_t i ) const 205 { 206 if ( i >= m_count ) return nullptr; 207 return &m_nodes[i]; 208 } 209 210 key_data* release_node_data( size_t i ) 211 { 212 key_data* result = m_nodes[i].data; 213 m_nodes[i].data = nullptr; 214 return result; 215 } 216 217 bool is_flat() const { return m_flat; } 218 uint16 get_frame_rate() const { return m_frame_rate; } 219 float get_duration() const { return m_duration; } 220 void set_name( const std::string& name ) { m_name = name; } 221 const string& get_name() const { return m_name; } 222 223 ~mesh_nodes_data() 224 { 225 if ( m_count > 0 ) 226 for ( uint32 i = 0; i < m_count; ++i ) delete m_nodes[i].data; 227 delete[] m_nodes; 228 } 229 230 private: 231 string m_name; 232 uint32 m_count; 233 mesh_node_data* m_nodes; 234 uint16 m_frame_rate; 235 float m_duration; 236 bool m_flat; 237 }; 238 239 class mesh_data_pack 240 { 241 friend class mesh_creator; 242 public: 243 explicit mesh_data_pack( uint32 a_count, mesh_data* a_meshes, mesh_nodes_data* a_nodes = nullptr ) 244 { 245 m_count = a_count; 246 m_meshes = a_meshes; 247 m_nodes = a_nodes; 248 } 249 const mesh_data* get_mesh( uint32 index ) const 250 { 251 if ( index >= m_count ) return nullptr; 252 return &m_meshes[ index ]; 253 } 254 uint32 get_count() const { return m_count; } 255 const mesh_nodes_data* get_nodes() const { return m_nodes; } 256 ~mesh_data_pack() 257 { 258 delete[] m_meshes; 259 delete m_nodes; 260 } 261 private: 262 uint32 m_count; 263 mesh_data* m_meshes; 264 mesh_nodes_data* m_nodes; 265 }; 166 266 } 167 267 -
trunk/nv/interface/mesh_loader.hh
r291 r293 1 // Copyright (C) 2012-201 3 ChaosForge / Kornel Kisielewicz1 // Copyright (C) 2012-2014 ChaosForge Ltd 2 2 // http://chaosforge.org/ 3 3 // … … 26 26 { 27 27 28 struct mesh_node_data29 {30 string name;31 sint16 target_id;32 sint16 parent_id;33 mat4 transform;34 key_data* data;35 };36 37 class mesh_nodes_data38 {39 public:40 explicit mesh_nodes_data( const string& name, uint32 count, mesh_node_data* nodes )41 : m_name( name ), m_count( count ), m_nodes( nodes ), m_frame_rate(0), m_duration(0.0f), m_flat( false )42 {43 }44 45 explicit mesh_nodes_data( const string& name, uint32 count, mesh_node_data* nodes,46 uint16 a_fps, float a_frames, bool a_flat )47 : m_name( name ), m_count( count ), m_nodes( nodes ), m_frame_rate(a_fps), m_duration(a_frames), m_flat( a_flat )48 {49 }50 51 size_t get_count() const { return m_count; }52 53 const mesh_node_data* get_node( size_t i ) const54 {55 if ( i >= m_count ) return nullptr;56 return &m_nodes[i];57 }58 59 key_data* release_node_data( size_t i )60 {61 key_data* result = m_nodes[i].data;62 m_nodes[i].data = nullptr;63 return result;64 }65 66 bool is_flat() const { return m_flat; }67 uint16 get_frame_rate() const { return m_frame_rate; }68 float get_duration() const { return m_duration; }69 void set_name( const std::string& name ) { m_name = name; }70 const string& get_name() const { return m_name; }71 72 ~mesh_nodes_data()73 {74 if ( m_count > 0 )75 for ( uint32 i = 0; i < m_count; ++i ) delete m_nodes[i].data;76 delete[] m_nodes;77 }78 79 private:80 string m_name;81 uint32 m_count;82 mesh_node_data* m_nodes;83 uint16 m_frame_rate;84 float m_duration;85 bool m_flat;86 };87 88 class mesh_data_pack89 {90 public:91 explicit mesh_data_pack( uint32 a_count, mesh_data* a_meshes, mesh_nodes_data* a_nodes = nullptr )92 {93 m_count = a_count;94 m_meshes = a_meshes;95 m_nodes = a_nodes;96 }97 const mesh_data* get_mesh( uint32 index ) const98 {99 if ( index >= m_count ) return nullptr;100 return &m_meshes[ index ];101 }102 uint32 get_count() const { return m_count; }103 const mesh_nodes_data* get_nodes() const { return m_nodes; }104 ~mesh_data_pack()105 {106 delete[] m_meshes;107 delete m_nodes;108 }109 private:110 uint32 m_count;111 mesh_data* m_meshes;112 mesh_nodes_data* m_nodes;113 };114 115 116 28 class mesh_loader 117 29 { -
trunk/src/formats/assimp_loader.cc
r292 r293 9 9 #include <glm/gtx/transform.hpp> 10 10 #include "nv/io/std_stream.hh" 11 #include "nv/gfx/mesh_creator.hh" 11 12 #include "nv/lib/assimp.hh" 12 13 … … 14 15 15 16 const int MAX_BONES = 64; 17 18 struct assimp_plain_vtx 19 { 20 vec3 position; 21 vec3 normal; 22 vec2 texcoord; 23 vec4 tangent; 24 25 assimp_plain_vtx() {} 26 assimp_plain_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g ) 27 { 28 position = v; 29 texcoord = t; 30 normal = n; 31 tangent = g; 32 } 33 }; 34 35 struct assimp_skinned_vtx 36 { 37 vec3 position; 38 vec3 normal; 39 vec2 texcoord; 40 vec4 tangent; 41 ivec4 boneindex; 42 vec4 boneweight; 43 44 assimp_skinned_vtx() {} 45 assimp_skinned_vtx( const vec3& v, const vec2& t, const vec3& n, const vec4& g ) 46 { 47 position = v; 48 texcoord = t; 49 normal = n; 50 tangent = g; 51 } 52 }; 16 53 17 54 struct assimp_key_p { float time; vec3 position; }; … … 21 58 22 59 23 nv::assimp_loader::assimp_loader( const string& a_ext, const mat3& a_rotate_transform, float a_scale, bool pre_transform, uint32 a_assimp_flags /*= 0 */ ) 24 : m_scene( nullptr ), m_flat( pre_transform ), m_mesh_count(0) 25 { 26 initialize( a_ext, a_rotate_transform, a_scale, a_assimp_flags ); 27 } 28 29 nv::assimp_loader::assimp_loader( const string& a_ext, bool pre_transform, uint32 a_assimp_flags /*= 0 */ ) 30 : m_scene( nullptr ), m_flat( pre_transform ), m_mesh_count(0) 31 { 32 initialize( a_ext, mat3(), 1.0f, a_assimp_flags ); 33 } 34 35 36 void nv::assimp_loader::initialize( const string& a_ext, const mat3& a_rotate_transform, float a_scale, uint32 a_assimp_flags ) 60 nv::assimp_loader::assimp_loader( const string& a_ext, uint32 a_assimp_flags /*= 0 */ ) 61 : m_scene( nullptr ), m_mesh_count(0) 37 62 { 38 63 m_ext = a_ext; 39 m_r33 = a_rotate_transform;40 m_ri33 = glm::transpose( m_r33 );41 m_scale = a_scale;42 64 m_assimp_flags = a_assimp_flags; 43 65 if ( m_assimp_flags == 0 ) … … 97 119 data->set_name( mesh->mName.data ); 98 120 99 vec3 vertex_offset = glm::vec3();100 mat3 vertex_transform = m_scale * m_r33;101 mat3 normal_transform = m_r33;102 103 121 bool skinned = mesh->mNumBones > 0; 104 122 mesh_raw_channel* channel = nullptr; … … 111 129 for (unsigned int i=0; i<mesh->mNumVertices; i++) 112 130 { 113 vec3 v = vertex_transform * assimp_vec3_cast( mesh->mVertices[ i ] ) + vertex_offset;114 vec3 n = glm::normalize( normal_transform *assimp_vec3_cast( mesh->mNormals[ i ] ) );115 vec3 t = glm::normalize( normal_transform *assimp_vec3_cast( mesh->mTangents[ i ] ) );116 vec3 b = glm::normalize( normal_transform *assimp_vec3_cast( mesh->mBitangents[ i ] ) );131 vec3 v = assimp_vec3_cast( mesh->mVertices[ i ] ); 132 vec3 n = glm::normalize( assimp_vec3_cast( mesh->mNormals[ i ] ) ); 133 vec3 t = glm::normalize( assimp_vec3_cast( mesh->mTangents[ i ] ) ); 134 vec3 b = glm::normalize( assimp_vec3_cast( mesh->mBitangents[ i ] ) ); 117 135 vec2 s = assimp_st_cast( mesh->mTextureCoords[ 0 ][ i ] ); 118 136 … … 176 194 const aiMesh* mesh = scene->mMeshes[ index ]; 177 195 178 mat4 bone_transform = mat4( ( 1.f/m_scale * m_ri33 ) );179 mat4 bone_pre_transform = mat4( m_scale * m_r33 );180 181 196 for (unsigned int m=0; m<mesh->mNumBones; m++) 182 197 { 183 198 aiBone* bone = mesh->mBones[m]; 184 mat4 offset = bone_pre_transform * assimp_mat4_cast( bone->mOffsetMatrix ) * bone_transform;199 mat4 offset = assimp_mat4_cast( bone->mOffsetMatrix ); 185 200 bones[m].name = bone->mName.data; 186 201 bones[m].data = nullptr; … … 338 353 uint16 frame_rate = (uint16)anim->mTicksPerSecond; 339 354 float duration = (float)anim->mDuration; 340 bool flat = m_flat; 341 uint32 max_frames = 0; 342 343 load_node( index, data, root, 0, -1, max_frames ); 344 345 // DAE pre_transform hack 346 if ( m_flat && frame_rate == 1 ) 347 { 348 frame_rate = 32; 349 duration = (float)max_frames; 350 } 351 352 return new mesh_nodes_data( anim->mName.data, count, data, frame_rate, duration, flat ) ; 355 bool flat = false; 356 357 load_node( index, data, root, 0, -1 ); 358 359 return new mesh_nodes_data( anim->mName.data, count, data, frame_rate, duration, flat ); 353 360 } 354 361 … … 364 371 } 365 372 366 nv::sint16 nv::assimp_loader::load_node( uint32 anim_id, mesh_node_data* nodes, const void* vnode, sint16 this_id, sint16 parent_id , uint32& max_frames)373 nv::sint16 nv::assimp_loader::load_node( uint32 anim_id, mesh_node_data* nodes, const void* vnode, sint16 this_id, sint16 parent_id ) 367 374 { 368 375 const aiScene* scene = (const aiScene*)m_scene; … … 393 400 a_data.data = nullptr; 394 401 395 if (anode) 396 { 397 if ( m_flat ) 398 { 399 create_transformed_keys( &a_data, anode, parent_id >= 0 ? &(nodes[ parent_id ]) : nullptr ); 400 } 401 else 402 { 403 create_direct_keys( &a_data, anode ); 404 } 405 max_frames = glm::max<uint32>( a_data.data->get_channel(0)->count, max_frames ); 406 } 402 if (anode) create_keys( &a_data, anode ); 407 403 408 404 nv::sint16 next = this_id + 1; 409 405 for ( unsigned i = 0; i < node->mNumChildren; ++i ) 410 406 { 411 next = load_node( anim_id, nodes, node->mChildren[i], next, this_id , max_frames);407 next = load_node( anim_id, nodes, node->mChildren[i], next, this_id ); 412 408 } 413 409 … … 415 411 } 416 412 417 void nv::assimp_loader::create_transformed_keys( mesh_node_data* data, const void* vnode, const mesh_node_data* parent ) 418 { 419 const aiNodeAnim* node = (const aiNodeAnim*)vnode; 420 size_t max_keys = glm::max( node->mNumPositionKeys, node->mNumRotationKeys ); 421 422 key_raw_channel* raw_channel = key_raw_channel::create<assimp_key_tr>( max_keys ); 423 data->data = new key_data; 424 data->data->add_channel( raw_channel ); 425 assimp_key_tr* channel = ((assimp_key_tr*)(raw_channel->data)); 426 427 for ( unsigned n = 0; n < max_keys; ++n ) 428 { 429 size_t pn = glm::min( node->mNumPositionKeys - 1, n ); 430 size_t rn = glm::min( node->mNumRotationKeys - 1, n ); 431 nv::vec3 pos = m_r33 * nv::assimp_vec3_cast(node->mPositionKeys[pn].mValue) * m_scale; 432 nv::quat rot = glm::quat_cast( m_r33 * glm::mat3_cast( assimp_quat_cast(node->mRotationKeys[rn].mValue ) ) * m_ri33 ); 433 // TODO: only do the calculation when a rotate transform is present! 434 nv::transform ptr; 435 if ( parent && parent->data ) 436 { 437 const key_raw_channel* pchannel = parent->data->get_channel(0); 438 if ( pchannel && pchannel->count > 0 ) 439 { 440 ptr = ((assimp_key_tr*)pchannel->data)[ glm::min( n, pchannel->count-1 ) ].tform; 441 } 442 } 443 444 nv::transform key( ptr * nv::transform( pos, rot ) ); 445 channel[n].tform = key; 446 } 447 } 448 449 void nv::assimp_loader::create_direct_keys( mesh_node_data* data, const void* vnode ) 413 void nv::assimp_loader::create_keys( mesh_node_data* data, const void* vnode ) 450 414 { 451 415 const aiNodeAnim* node = (const aiNodeAnim*)vnode; … … 469 433 { 470 434 pchannel[np].time = (float)node->mPositionKeys[np].mTime; 471 pchannel[np].position = m_r33 * assimp_vec3_cast(node->mPositionKeys[np].mValue) * m_scale;435 pchannel[np].position = assimp_vec3_cast(node->mPositionKeys[np].mValue); 472 436 } 473 437 for ( unsigned np = 0; np < node->mNumRotationKeys; ++np ) 474 438 { 475 439 rchannel[np].time = (float)node->mRotationKeys[np].mTime; 476 rchannel[np].rotation = glm::quat_cast( m_r33 * glm::mat3_cast( assimp_quat_cast(node->mRotationKeys[np].mValue ) ) * m_ri33);440 rchannel[np].rotation = assimp_quat_cast(node->mRotationKeys[np].mValue ); 477 441 } 478 442 // if ( node->mNumScalingKeys > 0 )
Note: See TracChangeset
for help on using the changeset viewer.