2014-05-18 08:57:44 +00:00
/*
Open Asset Import Library ( assimp )
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2017-05-09 17:57:36 +00:00
Copyright ( c ) 2006 - 2017 , assimp team
2014-05-18 08:57:44 +00:00
All rights reserved .
2015-05-19 03:52:10 +00:00
Redistribution and use of this software in source and binary forms ,
with or without modification , are permitted provided that the
2014-05-18 08:57:44 +00:00
following conditions are met :
* Redistributions of source code must retain the above
copyright notice , this list of conditions and the
following disclaimer .
* Redistributions in binary form must reproduce the above
copyright notice , this list of conditions and the
following disclaimer in the documentation and / or other
materials provided with the distribution .
* Neither the name of the assimp team , nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team .
2015-05-19 03:52:10 +00:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
" AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
2014-05-18 08:57:44 +00:00
LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2015-05-19 03:52:10 +00:00
A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT
2014-05-18 08:57:44 +00:00
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL ,
2015-05-19 03:52:10 +00:00
SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT
2014-05-18 08:57:44 +00:00
LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
2015-05-19 03:52:10 +00:00
DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
2014-05-18 08:57:44 +00:00
OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
# ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
# include "OgreStructs.h"
# include "TinyFormatter.h"
2016-06-06 20:04:29 +00:00
# include <assimp/scene.h>
# include <assimp/DefaultLogger.hpp>
2015-06-28 19:48:25 +00:00
# include "Exceptional.h"
2015-04-15 23:00:17 +00:00
2014-05-18 08:57:44 +00:00
namespace Assimp
{
namespace Ogre
{
// VertexElement
2015-05-19 03:52:10 +00:00
VertexElement : : VertexElement ( ) :
2015-05-19 03:57:13 +00:00
index ( 0 ) ,
source ( 0 ) ,
offset ( 0 ) ,
type ( VET_FLOAT1 ) ,
semantic ( VES_POSITION )
2014-05-18 08:57:44 +00:00
{
}
size_t VertexElement : : Size ( ) const
{
2015-05-19 03:57:13 +00:00
return TypeSize ( type ) ;
2014-05-18 08:57:44 +00:00
}
size_t VertexElement : : ComponentCount ( ) const
{
2015-05-19 03:57:13 +00:00
return ComponentCount ( type ) ;
2014-05-18 08:57:44 +00:00
}
size_t VertexElement : : ComponentCount ( Type type )
{
2015-05-19 03:57:13 +00:00
switch ( type )
{
case VET_COLOUR :
case VET_COLOUR_ABGR :
case VET_COLOUR_ARGB :
case VET_FLOAT1 :
case VET_DOUBLE1 :
case VET_SHORT1 :
case VET_USHORT1 :
case VET_INT1 :
case VET_UINT1 :
return 1 ;
case VET_FLOAT2 :
case VET_DOUBLE2 :
case VET_SHORT2 :
case VET_USHORT2 :
case VET_INT2 :
case VET_UINT2 :
return 2 ;
case VET_FLOAT3 :
case VET_DOUBLE3 :
case VET_SHORT3 :
case VET_USHORT3 :
case VET_INT3 :
case VET_UINT3 :
return 3 ;
case VET_FLOAT4 :
case VET_DOUBLE4 :
case VET_SHORT4 :
case VET_USHORT4 :
case VET_INT4 :
case VET_UINT4 :
case VET_UBYTE4 :
return 4 ;
}
return 0 ;
2014-05-18 08:57:44 +00:00
}
size_t VertexElement : : TypeSize ( Type type )
{
2015-05-19 03:57:13 +00:00
switch ( type )
{
case VET_COLOUR :
case VET_COLOUR_ABGR :
case VET_COLOUR_ARGB :
return sizeof ( unsigned int ) ;
case VET_FLOAT1 :
return sizeof ( float ) ;
case VET_FLOAT2 :
return sizeof ( float ) * 2 ;
case VET_FLOAT3 :
return sizeof ( float ) * 3 ;
case VET_FLOAT4 :
return sizeof ( float ) * 4 ;
case VET_DOUBLE1 :
return sizeof ( double ) ;
case VET_DOUBLE2 :
return sizeof ( double ) * 2 ;
case VET_DOUBLE3 :
return sizeof ( double ) * 3 ;
case VET_DOUBLE4 :
return sizeof ( double ) * 4 ;
case VET_SHORT1 :
return sizeof ( short ) ;
case VET_SHORT2 :
return sizeof ( short ) * 2 ;
case VET_SHORT3 :
return sizeof ( short ) * 3 ;
case VET_SHORT4 :
return sizeof ( short ) * 4 ;
case VET_USHORT1 :
return sizeof ( unsigned short ) ;
case VET_USHORT2 :
return sizeof ( unsigned short ) * 2 ;
case VET_USHORT3 :
return sizeof ( unsigned short ) * 3 ;
case VET_USHORT4 :
return sizeof ( unsigned short ) * 4 ;
case VET_INT1 :
return sizeof ( int ) ;
case VET_INT2 :
return sizeof ( int ) * 2 ;
case VET_INT3 :
return sizeof ( int ) * 3 ;
case VET_INT4 :
return sizeof ( int ) * 4 ;
case VET_UINT1 :
return sizeof ( unsigned int ) ;
case VET_UINT2 :
return sizeof ( unsigned int ) * 2 ;
case VET_UINT3 :
return sizeof ( unsigned int ) * 3 ;
case VET_UINT4 :
return sizeof ( unsigned int ) * 4 ;
case VET_UBYTE4 :
return sizeof ( unsigned char ) * 4 ;
}
return 0 ;
2014-05-18 08:57:44 +00:00
}
std : : string VertexElement : : TypeToString ( )
{
2015-05-19 03:57:13 +00:00
return TypeToString ( type ) ;
2014-05-18 08:57:44 +00:00
}
std : : string VertexElement : : TypeToString ( Type type )
{
2015-05-19 03:57:13 +00:00
switch ( type )
{
case VET_COLOUR : return " COLOUR " ;
case VET_COLOUR_ABGR : return " COLOUR_ABGR " ;
case VET_COLOUR_ARGB : return " COLOUR_ARGB " ;
case VET_FLOAT1 : return " FLOAT1 " ;
case VET_FLOAT2 : return " FLOAT2 " ;
case VET_FLOAT3 : return " FLOAT3 " ;
case VET_FLOAT4 : return " FLOAT4 " ;
case VET_DOUBLE1 : return " DOUBLE1 " ;
case VET_DOUBLE2 : return " DOUBLE2 " ;
case VET_DOUBLE3 : return " DOUBLE3 " ;
case VET_DOUBLE4 : return " DOUBLE4 " ;
case VET_SHORT1 : return " SHORT1 " ;
case VET_SHORT2 : return " SHORT2 " ;
case VET_SHORT3 : return " SHORT3 " ;
case VET_SHORT4 : return " SHORT4 " ;
case VET_USHORT1 : return " USHORT1 " ;
case VET_USHORT2 : return " USHORT2 " ;
case VET_USHORT3 : return " USHORT3 " ;
case VET_USHORT4 : return " USHORT4 " ;
case VET_INT1 : return " INT1 " ;
case VET_INT2 : return " INT2 " ;
case VET_INT3 : return " INT3 " ;
case VET_INT4 : return " INT4 " ;
case VET_UINT1 : return " UINT1 " ;
case VET_UINT2 : return " UINT2 " ;
case VET_UINT3 : return " UINT3 " ;
case VET_UINT4 : return " UINT4 " ;
case VET_UBYTE4 : return " UBYTE4 " ;
}
return " Uknown_VertexElement::Type " ;
2014-05-18 08:57:44 +00:00
}
std : : string VertexElement : : SemanticToString ( )
{
2015-05-19 03:57:13 +00:00
return SemanticToString ( semantic ) ;
2014-05-18 08:57:44 +00:00
}
std : : string VertexElement : : SemanticToString ( Semantic semantic )
{
2015-05-19 03:57:13 +00:00
switch ( semantic )
{
case VES_POSITION : return " POSITION " ;
case VES_BLEND_WEIGHTS : return " BLEND_WEIGHTS " ;
case VES_BLEND_INDICES : return " BLEND_INDICES " ;
case VES_NORMAL : return " NORMAL " ;
case VES_DIFFUSE : return " DIFFUSE " ;
case VES_SPECULAR : return " SPECULAR " ;
case VES_TEXTURE_COORDINATES : return " TEXTURE_COORDINATES " ;
case VES_BINORMAL : return " BINORMAL " ;
case VES_TANGENT : return " TANGENT " ;
}
return " Uknown_VertexElement::Semantic " ;
2014-05-18 08:57:44 +00:00
}
2014-05-20 01:52:53 +00:00
// IVertexData
2014-05-18 08:57:44 +00:00
2014-05-20 01:52:53 +00:00
IVertexData : : IVertexData ( ) :
2015-05-19 03:57:13 +00:00
count ( 0 )
2014-05-18 08:57:44 +00:00
{
}
2014-05-20 01:52:53 +00:00
bool IVertexData : : HasBoneAssignments ( ) const
{
2015-05-19 03:57:13 +00:00
return ! boneAssignments . empty ( ) ;
2014-05-20 01:52:53 +00:00
}
void IVertexData : : AddVertexMapping ( uint32_t oldIndex , uint32_t newIndex )
{
2015-05-19 03:57:13 +00:00
BoneAssignmentsForVertex ( oldIndex , newIndex , boneAssignmentsMap [ newIndex ] ) ;
vertexIndexMapping [ oldIndex ] . push_back ( newIndex ) ;
2014-05-20 01:52:53 +00:00
}
void IVertexData : : BoneAssignmentsForVertex ( uint32_t currentIndex , uint32_t newIndex , VertexBoneAssignmentList & dest ) const
{
2016-05-21 22:44:37 +00:00
for ( const auto & boneAssign : boneAssignments )
2015-05-19 03:57:13 +00:00
{
2016-05-21 22:44:37 +00:00
if ( boneAssign . vertexIndex = = currentIndex )
2015-05-19 03:57:13 +00:00
{
2016-05-21 22:44:37 +00:00
VertexBoneAssignment a = boneAssign ;
2015-05-19 03:57:13 +00:00
a . vertexIndex = newIndex ;
dest . push_back ( a ) ;
}
}
2014-05-20 01:52:53 +00:00
}
AssimpVertexBoneWeightList IVertexData : : AssimpBoneWeights ( size_t vertices )
{
2015-05-19 03:57:13 +00:00
AssimpVertexBoneWeightList weights ;
for ( size_t vi = 0 ; vi < vertices ; + + vi )
{
2016-11-20 01:49:33 +00:00
VertexBoneAssignmentList & vertexWeights = boneAssignmentsMap [ static_cast < unsigned int > ( vi ) ] ;
2015-05-19 03:57:13 +00:00
for ( VertexBoneAssignmentList : : const_iterator iter = vertexWeights . begin ( ) , end = vertexWeights . end ( ) ;
iter ! = end ; + + iter )
{
std : : vector < aiVertexWeight > & boneWeights = weights [ iter - > boneIndex ] ;
2016-11-20 01:49:33 +00:00
boneWeights . push_back ( aiVertexWeight ( static_cast < unsigned int > ( vi ) , iter - > weight ) ) ;
2015-05-19 03:57:13 +00:00
}
}
return weights ;
2014-05-20 01:52:53 +00:00
}
std : : set < uint16_t > IVertexData : : ReferencedBonesByWeights ( ) const
{
2015-05-19 03:57:13 +00:00
std : : set < uint16_t > referenced ;
2016-05-21 22:44:37 +00:00
for ( const auto & boneAssign : boneAssignments )
2015-05-19 03:57:13 +00:00
{
2016-05-21 22:44:37 +00:00
referenced . insert ( boneAssign . boneIndex ) ;
2015-05-19 03:57:13 +00:00
}
return referenced ;
2014-05-20 01:52:53 +00:00
}
// VertexData
VertexData : : VertexData ( )
{
}
2014-05-18 08:57:44 +00:00
VertexData : : ~ VertexData ( )
{
2015-05-19 03:57:13 +00:00
Reset ( ) ;
2014-05-18 08:57:44 +00:00
}
void VertexData : : Reset ( )
{
2015-05-19 03:57:13 +00:00
// Releases shared ptr memory streams.
vertexBindings . clear ( ) ;
vertexElements . clear ( ) ;
2014-05-18 08:57:44 +00:00
}
uint32_t VertexData : : VertexSize ( uint16_t source ) const
{
2015-05-19 03:57:13 +00:00
uint32_t size = 0 ;
2016-05-21 22:44:37 +00:00
for ( const auto & element : vertexElements )
2015-05-19 03:57:13 +00:00
{
2016-05-21 22:44:37 +00:00
if ( element . source = = source )
2016-11-20 01:49:33 +00:00
size + = static_cast < uint32_t > ( element . Size ( ) ) ;
2015-05-19 03:57:13 +00:00
}
return size ;
2014-05-18 08:57:44 +00:00
}
MemoryStream * VertexData : : VertexBuffer ( uint16_t source )
{
2015-05-19 03:57:13 +00:00
if ( vertexBindings . find ( source ) ! = vertexBindings . end ( ) )
return vertexBindings [ source ] . get ( ) ;
return 0 ;
2014-05-18 08:57:44 +00:00
}
VertexElement * VertexData : : GetVertexElement ( VertexElement : : Semantic semantic , uint16_t index )
{
2016-05-21 22:44:37 +00:00
for ( auto & element : vertexElements )
2015-05-19 03:57:13 +00:00
{
if ( element . semantic = = semantic & & element . index = = index )
return & element ;
}
return 0 ;
2014-05-18 08:57:44 +00:00
}
2014-05-20 01:52:53 +00:00
// VertexDataXml
VertexDataXml : : VertexDataXml ( )
{
}
2014-05-21 01:37:45 +00:00
bool VertexDataXml : : HasPositions ( ) const
{
2015-05-19 03:57:13 +00:00
return ! positions . empty ( ) ;
2014-05-21 01:37:45 +00:00
}
2014-05-20 01:52:53 +00:00
bool VertexDataXml : : HasNormals ( ) const
{
2015-05-19 03:57:13 +00:00
return ! normals . empty ( ) ;
2014-05-20 01:52:53 +00:00
}
bool VertexDataXml : : HasTangents ( ) const
{
2015-05-19 03:57:13 +00:00
return ! tangents . empty ( ) ;
2014-05-20 01:52:53 +00:00
}
bool VertexDataXml : : HasUvs ( ) const
{
2015-05-19 03:57:13 +00:00
return ! uvs . empty ( ) ;
2014-05-20 01:52:53 +00:00
}
size_t VertexDataXml : : NumUvs ( ) const
{
2015-05-19 03:57:13 +00:00
return uvs . size ( ) ;
2014-05-20 01:52:53 +00:00
}
2014-05-18 08:57:44 +00:00
// IndexData
IndexData : : IndexData ( ) :
2015-05-19 03:57:13 +00:00
count ( 0 ) ,
faceCount ( 0 ) ,
is32bit ( false )
2014-05-18 08:57:44 +00:00
{
}
IndexData : : ~ IndexData ( )
{
2015-05-19 03:57:13 +00:00
Reset ( ) ;
2014-05-18 08:57:44 +00:00
}
void IndexData : : Reset ( )
{
2015-05-19 03:57:13 +00:00
// Release shared ptr memory stream.
buffer . reset ( ) ;
2014-05-18 08:57:44 +00:00
}
size_t IndexData : : IndexSize ( ) const
{
2015-05-19 03:57:13 +00:00
return ( is32bit ? sizeof ( uint32_t ) : sizeof ( uint16_t ) ) ;
2014-05-18 08:57:44 +00:00
}
size_t IndexData : : FaceSize ( ) const
{
2015-05-19 03:57:13 +00:00
return IndexSize ( ) * 3 ;
2014-05-18 08:57:44 +00:00
}
// Mesh
2016-04-23 07:43:20 +00:00
Mesh : : Mesh ( )
: hasSkeletalAnimations ( false )
, skeleton ( NULL )
, sharedVertexData ( NULL )
, subMeshes ( )
, animations ( )
, poses ( )
2014-05-18 08:57:44 +00:00
{
}
Mesh : : ~ Mesh ( )
{
2015-05-19 03:57:13 +00:00
Reset ( ) ;
2014-05-18 08:57:44 +00:00
}
void Mesh : : Reset ( )
{
2015-05-19 03:57:13 +00:00
OGRE_SAFE_DELETE ( skeleton )
OGRE_SAFE_DELETE ( sharedVertexData )
2014-05-18 09:30:16 +00:00
2016-05-21 22:44:37 +00:00
for ( auto & mesh : subMeshes ) {
OGRE_SAFE_DELETE ( mesh )
2015-05-19 03:57:13 +00:00
}
subMeshes . clear ( ) ;
2016-05-21 22:44:37 +00:00
for ( auto & anim : animations ) {
OGRE_SAFE_DELETE ( anim )
2015-05-19 03:57:13 +00:00
}
animations . clear ( ) ;
2016-05-21 22:44:37 +00:00
for ( auto & pose : poses ) {
OGRE_SAFE_DELETE ( pose )
2015-05-19 03:57:13 +00:00
}
poses . clear ( ) ;
2014-05-18 08:57:44 +00:00
}
size_t Mesh : : NumSubMeshes ( ) const
{
2015-05-19 03:57:13 +00:00
return subMeshes . size ( ) ;
2014-05-18 08:57:44 +00:00
}
2016-04-23 07:43:20 +00:00
SubMesh * Mesh : : GetSubMesh ( size_t index ) const
2014-05-18 08:57:44 +00:00
{
2016-04-23 07:43:20 +00:00
for ( size_t i = 0 ; i < subMeshes . size ( ) ; + + i ) {
if ( subMeshes [ i ] - > index = = index ) {
return subMeshes [ i ] ;
}
}
2015-05-19 03:57:13 +00:00
return 0 ;
2014-05-18 08:57:44 +00:00
}
void Mesh : : ConvertToAssimpScene ( aiScene * dest )
{
2016-04-23 07:43:20 +00:00
if ( nullptr = = dest ) {
return ;
}
2015-05-19 03:57:13 +00:00
// Setup
2016-11-20 01:49:33 +00:00
dest - > mNumMeshes = static_cast < unsigned int > ( NumSubMeshes ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mMeshes = new aiMesh * [ dest - > mNumMeshes ] ;
// Create root node
dest - > mRootNode = new aiNode ( ) ;
dest - > mRootNode - > mNumMeshes = dest - > mNumMeshes ;
dest - > mRootNode - > mMeshes = new unsigned int [ dest - > mRootNode - > mNumMeshes ] ;
// Export meshes
2016-04-23 07:43:20 +00:00
for ( size_t i = 0 ; i < dest - > mNumMeshes ; + + i ) {
2015-05-19 03:57:13 +00:00
dest - > mMeshes [ i ] = subMeshes [ i ] - > ConvertToAssimpMesh ( this ) ;
2016-11-20 01:49:33 +00:00
dest - > mRootNode - > mMeshes [ i ] = static_cast < unsigned int > ( i ) ;
2015-05-19 03:57:13 +00:00
}
// Export skeleton
if ( skeleton )
{
// Bones
if ( ! skeleton - > bones . empty ( ) )
{
BoneList rootBones = skeleton - > RootBones ( ) ;
2016-11-20 01:49:33 +00:00
dest - > mRootNode - > mNumChildren = static_cast < unsigned int > ( rootBones . size ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mRootNode - > mChildren = new aiNode * [ dest - > mRootNode - > mNumChildren ] ;
for ( size_t i = 0 , len = rootBones . size ( ) ; i < len ; + + i )
{
dest - > mRootNode - > mChildren [ i ] = rootBones [ i ] - > ConvertToAssimpNode ( skeleton , dest - > mRootNode ) ;
}
}
// Animations
if ( ! skeleton - > animations . empty ( ) )
{
2016-11-20 01:49:33 +00:00
dest - > mNumAnimations = static_cast < unsigned int > ( skeleton - > animations . size ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mAnimations = new aiAnimation * [ dest - > mNumAnimations ] ;
for ( size_t i = 0 , len = skeleton - > animations . size ( ) ; i < len ; + + i )
{
dest - > mAnimations [ i ] = skeleton - > animations [ i ] - > ConvertToAssimpAnimation ( ) ;
}
}
}
2014-05-18 08:57:44 +00:00
}
2014-05-20 01:52:53 +00:00
// ISubMesh
2014-05-18 08:57:44 +00:00
2014-05-20 01:52:53 +00:00
ISubMesh : : ISubMesh ( ) :
2015-05-19 03:57:13 +00:00
index ( 0 ) ,
materialIndex ( - 1 ) ,
usesSharedVertexData ( false ) ,
operationType ( OT_POINT_LIST )
2014-05-20 01:52:53 +00:00
{
}
// SubMesh
SubMesh : : SubMesh ( ) :
2015-05-19 03:57:13 +00:00
vertexData ( 0 ) ,
indexData ( new IndexData ( ) )
2014-05-18 08:57:44 +00:00
{
}
2014-05-20 01:52:53 +00:00
SubMesh : : ~ SubMesh ( )
2014-05-18 08:57:44 +00:00
{
2015-05-19 03:57:13 +00:00
Reset ( ) ;
2014-05-18 08:57:44 +00:00
}
2014-05-20 01:52:53 +00:00
void SubMesh : : Reset ( )
2014-05-18 08:57:44 +00:00
{
2015-05-19 03:57:13 +00:00
OGRE_SAFE_DELETE ( vertexData )
OGRE_SAFE_DELETE ( indexData )
2014-05-18 08:57:44 +00:00
}
2014-05-20 01:52:53 +00:00
aiMesh * SubMesh : : ConvertToAssimpMesh ( Mesh * parent )
2014-05-18 08:57:44 +00:00
{
2015-05-19 03:57:13 +00:00
if ( operationType ! = OT_TRIANGLE_LIST ) {
throw DeadlyImportError ( Formatter : : format ( ) < < " Only mesh operation type OT_TRIANGLE_LIST is supported. Found " < < operationType ) ;
}
aiMesh * dest = new aiMesh ( ) ;
dest - > mPrimitiveTypes = aiPrimitiveType_TRIANGLE ;
if ( ! name . empty ( ) )
dest - > mName = name ;
// Material index
if ( materialIndex ! = - 1 )
dest - > mMaterialIndex = materialIndex ;
// Pick source vertex data from shader geometry or from internal geometry.
VertexData * src = ( ! usesSharedVertexData ? vertexData : parent - > sharedVertexData ) ;
VertexElement * positionsElement = src - > GetVertexElement ( VertexElement : : VES_POSITION ) ;
VertexElement * normalsElement = src - > GetVertexElement ( VertexElement : : VES_NORMAL ) ;
VertexElement * uv1Element = src - > GetVertexElement ( VertexElement : : VES_TEXTURE_COORDINATES , 0 ) ;
VertexElement * uv2Element = src - > GetVertexElement ( VertexElement : : VES_TEXTURE_COORDINATES , 1 ) ;
// Sanity checks
if ( ! positionsElement ) {
throw DeadlyImportError ( " Failed to import Ogre VertexElement::VES_POSITION. Mesh does not have vertex positions! " ) ;
} else if ( positionsElement - > type ! = VertexElement : : VET_FLOAT3 ) {
throw DeadlyImportError ( " Ogre Mesh position vertex element type != VertexElement::VET_FLOAT3. This is not supported. " ) ;
} else if ( normalsElement & & normalsElement - > type ! = VertexElement : : VET_FLOAT3 ) {
throw DeadlyImportError ( " Ogre Mesh normal vertex element type != VertexElement::VET_FLOAT3. This is not supported. " ) ;
}
// Faces
dest - > mNumFaces = indexData - > faceCount ;
dest - > mFaces = new aiFace [ dest - > mNumFaces ] ;
// Assimp required unique vertices, we need to convert from Ogres shared indexing.
size_t uniqueVertexCount = dest - > mNumFaces * 3 ;
2016-11-20 01:49:33 +00:00
dest - > mNumVertices = static_cast < unsigned int > ( uniqueVertexCount ) ;
2015-05-19 03:57:13 +00:00
dest - > mVertices = new aiVector3D [ dest - > mNumVertices ] ;
// Source streams
MemoryStream * positions = src - > VertexBuffer ( positionsElement - > source ) ;
MemoryStream * normals = ( normalsElement ? src - > VertexBuffer ( normalsElement - > source ) : 0 ) ;
MemoryStream * uv1 = ( uv1Element ? src - > VertexBuffer ( uv1Element - > source ) : 0 ) ;
MemoryStream * uv2 = ( uv2Element ? src - > VertexBuffer ( uv2Element - > source ) : 0 ) ;
// Element size
const size_t sizePosition = positionsElement - > Size ( ) ;
const size_t sizeNormal = ( normalsElement ? normalsElement - > Size ( ) : 0 ) ;
const size_t sizeUv1 = ( uv1Element ? uv1Element - > Size ( ) : 0 ) ;
const size_t sizeUv2 = ( uv2Element ? uv2Element - > Size ( ) : 0 ) ;
// Vertex width
const size_t vWidthPosition = src - > VertexSize ( positionsElement - > source ) ;
const size_t vWidthNormal = ( normalsElement ? src - > VertexSize ( normalsElement - > source ) : 0 ) ;
const size_t vWidthUv1 = ( uv1Element ? src - > VertexSize ( uv1Element - > source ) : 0 ) ;
const size_t vWidthUv2 = ( uv2Element ? src - > VertexSize ( uv2Element - > source ) : 0 ) ;
bool boneAssignments = src - > HasBoneAssignments ( ) ;
// Prepare normals
if ( normals )
dest - > mNormals = new aiVector3D [ dest - > mNumVertices ] ;
// Prepare UVs, ignoring incompatible UVs.
if ( uv1 )
{
if ( uv1Element - > type = = VertexElement : : VET_FLOAT2 | | uv1Element - > type = = VertexElement : : VET_FLOAT3 )
{
2016-11-20 01:49:33 +00:00
dest - > mNumUVComponents [ 0 ] = static_cast < unsigned int > ( uv1Element - > ComponentCount ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mTextureCoords [ 0 ] = new aiVector3D [ dest - > mNumVertices ] ;
}
else
{
DefaultLogger : : get ( ) - > warn ( Formatter : : format ( ) < < " Ogre imported UV0 type " < < uv1Element - > TypeToString ( ) < < " is not compatible with Assimp. Ignoring UV. " ) ;
uv1 = 0 ;
}
}
if ( uv2 )
{
if ( uv2Element - > type = = VertexElement : : VET_FLOAT2 | | uv2Element - > type = = VertexElement : : VET_FLOAT3 )
{
2016-11-20 01:49:33 +00:00
dest - > mNumUVComponents [ 1 ] = static_cast < unsigned int > ( uv2Element - > ComponentCount ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mTextureCoords [ 1 ] = new aiVector3D [ dest - > mNumVertices ] ;
}
else
{
DefaultLogger : : get ( ) - > warn ( Formatter : : format ( ) < < " Ogre imported UV0 type " < < uv2Element - > TypeToString ( ) < < " is not compatible with Assimp. Ignoring UV. " ) ;
uv2 = 0 ;
}
}
aiVector3D * uv1Dest = ( uv1 ? dest - > mTextureCoords [ 0 ] : 0 ) ;
aiVector3D * uv2Dest = ( uv2 ? dest - > mTextureCoords [ 1 ] : 0 ) ;
MemoryStream * faces = indexData - > buffer . get ( ) ;
for ( size_t fi = 0 , isize = indexData - > IndexSize ( ) , fsize = indexData - > FaceSize ( ) ;
fi < dest - > mNumFaces ; + + fi )
{
// Source Ogre face
aiFace ogreFace ;
ogreFace . mNumIndices = 3 ;
ogreFace . mIndices = new unsigned int [ 3 ] ;
faces - > Seek ( fi * fsize , aiOrigin_SET ) ;
if ( indexData - > is32bit )
{
faces - > Read ( & ogreFace . mIndices [ 0 ] , isize , 3 ) ;
}
else
{
uint16_t iout = 0 ;
for ( size_t ii = 0 ; ii < 3 ; + + ii )
{
faces - > Read ( & iout , isize , 1 ) ;
ogreFace . mIndices [ ii ] = static_cast < unsigned int > ( iout ) ;
}
}
// Destination Assimp face
aiFace & face = dest - > mFaces [ fi ] ;
face . mNumIndices = 3 ;
face . mIndices = new unsigned int [ 3 ] ;
const size_t pos = fi * 3 ;
for ( size_t v = 0 ; v < 3 ; + + v )
{
const size_t newIndex = pos + v ;
// Write face index
2016-11-20 01:49:33 +00:00
face . mIndices [ v ] = static_cast < unsigned int > ( newIndex ) ;
2015-05-19 03:57:13 +00:00
// Ogres vertex index to ref into the source buffers.
const size_t ogreVertexIndex = ogreFace . mIndices [ v ] ;
2016-11-20 01:49:33 +00:00
src - > AddVertexMapping ( static_cast < uint32_t > ( ogreVertexIndex ) , static_cast < uint32_t > ( newIndex ) ) ;
2015-05-19 03:57:13 +00:00
// Position
positions - > Seek ( ( vWidthPosition * ogreVertexIndex ) + positionsElement - > offset , aiOrigin_SET ) ;
positions - > Read ( & dest - > mVertices [ newIndex ] , sizePosition , 1 ) ;
// Normal
if ( normals )
{
normals - > Seek ( ( vWidthNormal * ogreVertexIndex ) + normalsElement - > offset , aiOrigin_SET ) ;
normals - > Read ( & dest - > mNormals [ newIndex ] , sizeNormal , 1 ) ;
}
// UV0
if ( uv1 & & uv1Dest )
{
uv1 - > Seek ( ( vWidthUv1 * ogreVertexIndex ) + uv1Element - > offset , aiOrigin_SET ) ;
uv1 - > Read ( & uv1Dest [ newIndex ] , sizeUv1 , 1 ) ;
uv1Dest [ newIndex ] . y = ( uv1Dest [ newIndex ] . y * - 1 ) + 1 ; // Flip UV from Ogre to Assimp form
}
// UV1
if ( uv2 & & uv2Dest )
{
uv2 - > Seek ( ( vWidthUv2 * ogreVertexIndex ) + uv2Element - > offset , aiOrigin_SET ) ;
uv2 - > Read ( & uv2Dest [ newIndex ] , sizeUv2 , 1 ) ;
uv2Dest [ newIndex ] . y = ( uv2Dest [ newIndex ] . y * - 1 ) + 1 ; // Flip UV from Ogre to Assimp form
}
}
}
// Bones and bone weights
if ( parent - > skeleton & & boneAssignments )
{
AssimpVertexBoneWeightList weights = src - > AssimpBoneWeights ( dest - > mNumVertices ) ;
std : : set < uint16_t > referencedBones = src - > ReferencedBonesByWeights ( ) ;
2016-11-20 01:49:33 +00:00
dest - > mNumBones = static_cast < unsigned int > ( referencedBones . size ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mBones = new aiBone * [ dest - > mNumBones ] ;
size_t assimpBoneIndex = 0 ;
for ( std : : set < uint16_t > : : const_iterator rbIter = referencedBones . begin ( ) , rbEnd = referencedBones . end ( ) ; rbIter ! = rbEnd ; + + rbIter , + + assimpBoneIndex )
{
Bone * bone = parent - > skeleton - > BoneById ( ( * rbIter ) ) ;
dest - > mBones [ assimpBoneIndex ] = bone - > ConvertToAssimpBone ( parent - > skeleton , weights [ bone - > id ] ) ;
}
}
return dest ;
2014-05-20 01:52:53 +00:00
}
// MeshXml
MeshXml : : MeshXml ( ) :
2015-05-19 03:57:13 +00:00
skeleton ( 0 ) ,
sharedVertexData ( 0 )
2014-05-20 01:52:53 +00:00
{
}
MeshXml : : ~ MeshXml ( )
{
2015-05-19 03:57:13 +00:00
Reset ( ) ;
2014-05-20 01:52:53 +00:00
}
void MeshXml : : Reset ( )
{
2015-05-19 03:57:13 +00:00
OGRE_SAFE_DELETE ( skeleton )
OGRE_SAFE_DELETE ( sharedVertexData )
2014-05-20 01:52:53 +00:00
2016-05-21 22:44:37 +00:00
for ( auto & mesh : subMeshes ) {
OGRE_SAFE_DELETE ( mesh )
2015-05-19 03:57:13 +00:00
}
subMeshes . clear ( ) ;
2014-05-20 01:52:53 +00:00
}
size_t MeshXml : : NumSubMeshes ( ) const
{
2015-05-19 03:57:13 +00:00
return subMeshes . size ( ) ;
2014-05-20 01:52:53 +00:00
}
SubMeshXml * MeshXml : : GetSubMesh ( uint16_t index ) const
{
2015-05-19 03:57:13 +00:00
for ( size_t i = 0 ; i < subMeshes . size ( ) ; + + i )
if ( subMeshes [ i ] - > index = = index )
return subMeshes [ i ] ;
return 0 ;
2014-05-20 01:52:53 +00:00
}
void MeshXml : : ConvertToAssimpScene ( aiScene * dest )
{
2015-05-19 03:57:13 +00:00
// Setup
2016-11-20 01:49:33 +00:00
dest - > mNumMeshes = static_cast < unsigned int > ( NumSubMeshes ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mMeshes = new aiMesh * [ dest - > mNumMeshes ] ;
// Create root node
dest - > mRootNode = new aiNode ( ) ;
dest - > mRootNode - > mNumMeshes = dest - > mNumMeshes ;
dest - > mRootNode - > mMeshes = new unsigned int [ dest - > mRootNode - > mNumMeshes ] ;
// Export meshes
for ( size_t i = 0 ; i < dest - > mNumMeshes ; + + i )
{
dest - > mMeshes [ i ] = subMeshes [ i ] - > ConvertToAssimpMesh ( this ) ;
2016-11-20 01:49:33 +00:00
dest - > mRootNode - > mMeshes [ i ] = static_cast < unsigned int > ( i ) ;
2015-05-19 03:57:13 +00:00
}
// Export skeleton
if ( skeleton )
{
// Bones
if ( ! skeleton - > bones . empty ( ) )
{
BoneList rootBones = skeleton - > RootBones ( ) ;
2016-11-20 01:49:33 +00:00
dest - > mRootNode - > mNumChildren = static_cast < unsigned int > ( rootBones . size ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mRootNode - > mChildren = new aiNode * [ dest - > mRootNode - > mNumChildren ] ;
for ( size_t i = 0 , len = rootBones . size ( ) ; i < len ; + + i )
{
dest - > mRootNode - > mChildren [ i ] = rootBones [ i ] - > ConvertToAssimpNode ( skeleton , dest - > mRootNode ) ;
}
}
// Animations
if ( ! skeleton - > animations . empty ( ) )
{
2016-11-20 01:49:33 +00:00
dest - > mNumAnimations = static_cast < unsigned int > ( skeleton - > animations . size ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mAnimations = new aiAnimation * [ dest - > mNumAnimations ] ;
for ( size_t i = 0 , len = skeleton - > animations . size ( ) ; i < len ; + + i )
{
dest - > mAnimations [ i ] = skeleton - > animations [ i ] - > ConvertToAssimpAnimation ( ) ;
}
}
}
2014-05-20 01:52:53 +00:00
}
// SubMeshXml
SubMeshXml : : SubMeshXml ( ) :
2015-05-19 03:57:13 +00:00
indexData ( new IndexDataXml ( ) ) ,
vertexData ( 0 )
2014-05-20 01:52:53 +00:00
{
}
SubMeshXml : : ~ SubMeshXml ( )
{
2015-05-19 03:57:13 +00:00
Reset ( ) ;
2014-05-20 01:52:53 +00:00
}
void SubMeshXml : : Reset ( )
{
2015-05-19 03:57:13 +00:00
OGRE_SAFE_DELETE ( indexData )
OGRE_SAFE_DELETE ( vertexData )
2014-05-20 01:52:53 +00:00
}
aiMesh * SubMeshXml : : ConvertToAssimpMesh ( MeshXml * parent )
{
2015-05-19 03:57:13 +00:00
aiMesh * dest = new aiMesh ( ) ;
dest - > mPrimitiveTypes = aiPrimitiveType_TRIANGLE ;
if ( ! name . empty ( ) )
dest - > mName = name ;
// Material index
if ( materialIndex ! = - 1 )
dest - > mMaterialIndex = materialIndex ;
// Faces
dest - > mNumFaces = indexData - > faceCount ;
dest - > mFaces = new aiFace [ dest - > mNumFaces ] ;
// Assimp required unique vertices, we need to convert from Ogres shared indexing.
size_t uniqueVertexCount = dest - > mNumFaces * 3 ;
2016-11-20 01:49:33 +00:00
dest - > mNumVertices = static_cast < unsigned int > ( uniqueVertexCount ) ;
2015-05-19 03:57:13 +00:00
dest - > mVertices = new aiVector3D [ dest - > mNumVertices ] ;
VertexDataXml * src = ( ! usesSharedVertexData ? vertexData : parent - > sharedVertexData ) ;
bool boneAssignments = src - > HasBoneAssignments ( ) ;
bool normals = src - > HasNormals ( ) ;
size_t uvs = src - > NumUvs ( ) ;
// Prepare normals
if ( normals )
dest - > mNormals = new aiVector3D [ dest - > mNumVertices ] ;
// Prepare UVs
for ( size_t uvi = 0 ; uvi < uvs ; + + uvi )
{
dest - > mNumUVComponents [ uvi ] = 2 ;
dest - > mTextureCoords [ uvi ] = new aiVector3D [ dest - > mNumVertices ] ;
}
for ( size_t fi = 0 ; fi < dest - > mNumFaces ; + + fi )
{
// Source Ogre face
aiFace & ogreFace = indexData - > faces [ fi ] ;
// Destination Assimp face
aiFace & face = dest - > mFaces [ fi ] ;
face . mNumIndices = 3 ;
face . mIndices = new unsigned int [ 3 ] ;
const size_t pos = fi * 3 ;
for ( size_t v = 0 ; v < 3 ; + + v )
{
const size_t newIndex = pos + v ;
// Write face index
2016-11-20 01:49:33 +00:00
face . mIndices [ v ] = static_cast < unsigned int > ( newIndex ) ;
2015-05-19 03:57:13 +00:00
// Ogres vertex index to ref into the source buffers.
const size_t ogreVertexIndex = ogreFace . mIndices [ v ] ;
2016-11-20 01:49:33 +00:00
src - > AddVertexMapping ( static_cast < uint32_t > ( ogreVertexIndex ) , static_cast < uint32_t > ( newIndex ) ) ;
2015-05-19 03:57:13 +00:00
// Position
dest - > mVertices [ newIndex ] = src - > positions [ ogreVertexIndex ] ;
// Normal
if ( normals )
dest - > mNormals [ newIndex ] = src - > normals [ ogreVertexIndex ] ;
// UVs
for ( size_t uvi = 0 ; uvi < uvs ; + + uvi )
{
aiVector3D * uvDest = dest - > mTextureCoords [ uvi ] ;
std : : vector < aiVector3D > & uvSrc = src - > uvs [ uvi ] ;
uvDest [ newIndex ] = uvSrc [ ogreVertexIndex ] ;
}
}
}
// Bones and bone weights
if ( parent - > skeleton & & boneAssignments )
{
AssimpVertexBoneWeightList weights = src - > AssimpBoneWeights ( dest - > mNumVertices ) ;
std : : set < uint16_t > referencedBones = src - > ReferencedBonesByWeights ( ) ;
2016-11-20 01:49:33 +00:00
dest - > mNumBones = static_cast < unsigned int > ( referencedBones . size ( ) ) ;
2015-05-19 03:57:13 +00:00
dest - > mBones = new aiBone * [ dest - > mNumBones ] ;
size_t assimpBoneIndex = 0 ;
for ( std : : set < uint16_t > : : const_iterator rbIter = referencedBones . begin ( ) , rbEnd = referencedBones . end ( ) ; rbIter ! = rbEnd ; + + rbIter , + + assimpBoneIndex )
{
Bone * bone = parent - > skeleton - > BoneById ( ( * rbIter ) ) ;
dest - > mBones [ assimpBoneIndex ] = bone - > ConvertToAssimpBone ( parent - > skeleton , weights [ bone - > id ] ) ;
}
}
return dest ;
2014-05-18 08:57:44 +00:00
}
2014-05-20 01:52:53 +00:00
// Animation
Animation : : Animation ( Skeleton * parent ) :
2015-05-19 03:57:13 +00:00
parentMesh ( NULL ) ,
parentSkeleton ( parent ) ,
length ( 0.0f ) ,
baseTime ( - 1.0f )
2014-05-20 01:52:53 +00:00
{
}
2014-05-18 08:57:44 +00:00
2015-05-19 03:52:10 +00:00
Animation : : Animation ( Mesh * parent ) :
2015-05-19 03:57:13 +00:00
parentMesh ( parent ) ,
parentSkeleton ( 0 ) ,
length ( 0.0f ) ,
baseTime ( - 1.0f )
2014-05-18 08:57:44 +00:00
{
}
2014-05-20 01:52:53 +00:00
VertexData * Animation : : AssociatedVertexData ( VertexAnimationTrack * track ) const
2014-05-18 08:57:44 +00:00
{
2015-05-19 03:57:13 +00:00
if ( ! parentMesh )
return 0 ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
bool sharedGeom = ( track - > target = = 0 ) ;
if ( sharedGeom )
return parentMesh - > sharedVertexData ;
else
return parentMesh - > GetSubMesh ( track - > target - 1 ) - > vertexData ;
2014-05-20 01:52:53 +00:00
}
aiAnimation * Animation : : ConvertToAssimpAnimation ( )
{
2015-05-19 03:57:13 +00:00
aiAnimation * anim = new aiAnimation ( ) ;
anim - > mName = name ;
anim - > mDuration = static_cast < double > ( length ) ;
anim - > mTicksPerSecond = 1.0 ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
// Tracks
if ( ! tracks . empty ( ) )
{
2016-11-20 01:49:33 +00:00
anim - > mNumChannels = static_cast < unsigned int > ( tracks . size ( ) ) ;
2015-05-19 03:57:13 +00:00
anim - > mChannels = new aiNodeAnim * [ anim - > mNumChannels ] ;
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
for ( size_t i = 0 , len = tracks . size ( ) ; i < len ; + + i )
{
anim - > mChannels [ i ] = tracks [ i ] . ConvertToAssimpAnimationNode ( parentSkeleton ) ;
}
}
return anim ;
2014-05-20 01:52:53 +00:00
}
// Skeleton
2014-05-21 01:00:11 +00:00
Skeleton : : Skeleton ( ) :
2015-05-19 03:57:13 +00:00
bones ( ) ,
animations ( ) ,
blendMode ( ANIMBLEND_AVERAGE )
2014-05-20 01:52:53 +00:00
{
}
Skeleton : : ~ Skeleton ( )
{
2015-05-19 03:57:13 +00:00
Reset ( ) ;
2014-05-20 01:52:53 +00:00
}
void Skeleton : : Reset ( )
{
2016-05-21 22:44:37 +00:00
for ( auto & bone : bones ) {
OGRE_SAFE_DELETE ( bone )
2015-05-19 03:57:13 +00:00
}
bones . clear ( ) ;
2016-05-21 22:44:37 +00:00
for ( auto & anim : animations ) {
OGRE_SAFE_DELETE ( anim )
2015-05-19 03:57:13 +00:00
}
animations . clear ( ) ;
2014-05-20 01:52:53 +00:00
}
BoneList Skeleton : : RootBones ( ) const
{
2015-05-19 03:57:13 +00:00
BoneList rootBones ;
for ( BoneList : : const_iterator iter = bones . begin ( ) ; iter ! = bones . end ( ) ; + + iter )
{
if ( ! ( * iter ) - > IsParented ( ) )
rootBones . push_back ( ( * iter ) ) ;
}
return rootBones ;
2014-05-20 01:52:53 +00:00
}
size_t Skeleton : : NumRootBones ( ) const
{
2015-05-19 03:57:13 +00:00
size_t num = 0 ;
for ( BoneList : : const_iterator iter = bones . begin ( ) ; iter ! = bones . end ( ) ; + + iter )
{
if ( ! ( * iter ) - > IsParented ( ) )
num + + ;
}
return num ;
2014-05-20 01:52:53 +00:00
}
Bone * Skeleton : : BoneByName ( const std : : string & name ) const
{
2015-05-19 03:57:13 +00:00
for ( BoneList : : const_iterator iter = bones . begin ( ) ; iter ! = bones . end ( ) ; + + iter )
{
if ( ( * iter ) - > name = = name )
return ( * iter ) ;
}
return 0 ;
2014-05-20 01:52:53 +00:00
}
Bone * Skeleton : : BoneById ( uint16_t id ) const
{
2015-05-19 03:57:13 +00:00
for ( BoneList : : const_iterator iter = bones . begin ( ) ; iter ! = bones . end ( ) ; + + iter )
{
if ( ( * iter ) - > id = = id )
return ( * iter ) ;
}
return 0 ;
2014-05-20 01:52:53 +00:00
}
// Bone
Bone : : Bone ( ) :
2015-05-19 03:57:13 +00:00
id ( 0 ) ,
parent ( 0 ) ,
parentId ( - 1 ) ,
scale ( 1.0f , 1.0f , 1.0f )
2014-05-20 01:52:53 +00:00
{
}
bool Bone : : IsParented ( ) const
{
2015-05-19 03:57:13 +00:00
return ( parentId ! = - 1 & & parent ! = 0 ) ;
2014-05-20 01:52:53 +00:00
}
uint16_t Bone : : ParentId ( ) const
{
2015-05-19 03:57:13 +00:00
return static_cast < uint16_t > ( parentId ) ;
2014-05-20 01:52:53 +00:00
}
void Bone : : AddChild ( Bone * bone )
{
2015-05-19 03:57:13 +00:00
if ( ! bone )
return ;
if ( bone - > IsParented ( ) )
throw DeadlyImportError ( " Attaching child Bone that is already parented: " + bone - > name ) ;
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
bone - > parent = this ;
bone - > parentId = id ;
children . push_back ( bone - > id ) ;
2014-05-20 01:52:53 +00:00
}
void Bone : : CalculateWorldMatrixAndDefaultPose ( Skeleton * skeleton )
{
2015-05-19 03:57:13 +00:00
if ( ! IsParented ( ) )
worldMatrix = aiMatrix4x4 ( scale , rotation , position ) . Inverse ( ) ;
else
worldMatrix = aiMatrix4x4 ( scale , rotation , position ) . Inverse ( ) * parent - > worldMatrix ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
defaultPose = aiMatrix4x4 ( scale , rotation , position ) ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
// Recursively for all children now that the parent matrix has been calculated.
2016-05-21 22:44:37 +00:00
for ( auto boneId : children )
2015-05-19 03:57:13 +00:00
{
2016-05-21 22:44:37 +00:00
Bone * child = skeleton - > BoneById ( boneId ) ;
2015-05-19 03:57:13 +00:00
if ( ! child ) {
2016-05-21 22:44:37 +00:00
throw DeadlyImportError ( Formatter : : format ( ) < < " CalculateWorldMatrixAndDefaultPose: Failed to find child bone " < < boneId < < " for parent " < < id < < " " < < name ) ;
2015-05-19 03:57:13 +00:00
}
child - > CalculateWorldMatrixAndDefaultPose ( skeleton ) ;
}
2014-05-20 01:52:53 +00:00
}
aiNode * Bone : : ConvertToAssimpNode ( Skeleton * skeleton , aiNode * parentNode )
{
2015-05-19 03:57:13 +00:00
// Bone node
aiNode * node = new aiNode ( name ) ;
node - > mParent = parentNode ;
node - > mTransformation = defaultPose ;
// Children
if ( ! children . empty ( ) )
{
2016-11-20 01:49:33 +00:00
node - > mNumChildren = static_cast < unsigned int > ( children . size ( ) ) ;
2015-05-19 03:57:13 +00:00
node - > mChildren = new aiNode * [ node - > mNumChildren ] ;
for ( size_t i = 0 , len = children . size ( ) ; i < len ; + + i )
{
Bone * child = skeleton - > BoneById ( children [ i ] ) ;
if ( ! child ) {
throw DeadlyImportError ( Formatter : : format ( ) < < " ConvertToAssimpNode: Failed to find child bone " < < children [ i ] < < " for parent " < < id < < " " < < name ) ;
}
node - > mChildren [ i ] = child - > ConvertToAssimpNode ( skeleton , node ) ;
}
}
return node ;
2014-05-20 01:52:53 +00:00
}
2014-08-18 00:09:06 +00:00
aiBone * Bone : : ConvertToAssimpBone ( Skeleton * /*parent*/ , const std : : vector < aiVertexWeight > & boneWeights )
2014-05-20 01:52:53 +00:00
{
2015-05-19 03:57:13 +00:00
aiBone * bone = new aiBone ( ) ;
bone - > mName = name ;
bone - > mOffsetMatrix = worldMatrix ;
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
if ( ! boneWeights . empty ( ) )
{
2016-11-20 01:49:33 +00:00
bone - > mNumWeights = static_cast < unsigned int > ( boneWeights . size ( ) ) ;
2015-05-19 03:57:13 +00:00
bone - > mWeights = new aiVertexWeight [ boneWeights . size ( ) ] ;
memcpy ( bone - > mWeights , & boneWeights [ 0 ] , boneWeights . size ( ) * sizeof ( aiVertexWeight ) ) ;
}
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
return bone ;
2014-05-20 01:52:53 +00:00
}
// VertexAnimationTrack
VertexAnimationTrack : : VertexAnimationTrack ( ) :
2015-05-19 03:57:13 +00:00
type ( VAT_NONE ) ,
target ( 0 )
2014-05-20 01:52:53 +00:00
{
}
aiNodeAnim * VertexAnimationTrack : : ConvertToAssimpAnimationNode ( Skeleton * skeleton )
{
2015-05-19 03:57:13 +00:00
if ( boneName . empty ( ) | | type ! = VAT_TRANSFORM ) {
throw DeadlyImportError ( " VertexAnimationTrack::ConvertToAssimpAnimationNode: Cannot convert track that has no target bone name or is not type of VAT_TRANSFORM " ) ;
}
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
aiNodeAnim * nodeAnim = new aiNodeAnim ( ) ;
nodeAnim - > mNodeName = boneName ;
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
Bone * bone = skeleton - > BoneByName ( boneName ) ;
if ( ! bone ) {
throw DeadlyImportError ( " VertexAnimationTrack::ConvertToAssimpAnimationNode: Failed to find bone " + boneName + " from parent Skeleton " ) ;
}
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
// Keyframes
size_t numKeyframes = transformKeyFrames . size ( ) ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
nodeAnim - > mPositionKeys = new aiVectorKey [ numKeyframes ] ;
nodeAnim - > mRotationKeys = new aiQuatKey [ numKeyframes ] ;
nodeAnim - > mScalingKeys = new aiVectorKey [ numKeyframes ] ;
2016-11-20 01:49:33 +00:00
nodeAnim - > mNumPositionKeys = static_cast < unsigned int > ( numKeyframes ) ;
nodeAnim - > mNumRotationKeys = static_cast < unsigned int > ( numKeyframes ) ;
nodeAnim - > mNumScalingKeys = static_cast < unsigned int > ( numKeyframes ) ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
for ( size_t kfi = 0 ; kfi < numKeyframes ; + + kfi )
{
TransformKeyFrame & kfSource = transformKeyFrames [ kfi ] ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
// Calculate the complete transformation from world space to bone space
aiVector3D pos ; aiQuaternion rot ; aiVector3D scale ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
aiMatrix4x4 finalTransform = bone - > defaultPose * kfSource . Transform ( ) ;
finalTransform . Decompose ( scale , rot , pos ) ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
double t = static_cast < double > ( kfSource . timePos ) ;
nodeAnim - > mPositionKeys [ kfi ] . mTime = t ;
nodeAnim - > mRotationKeys [ kfi ] . mTime = t ;
nodeAnim - > mScalingKeys [ kfi ] . mTime = t ;
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
nodeAnim - > mPositionKeys [ kfi ] . mValue = pos ;
nodeAnim - > mRotationKeys [ kfi ] . mValue = rot ;
nodeAnim - > mScalingKeys [ kfi ] . mValue = scale ;
}
2014-05-20 01:52:53 +00:00
2015-05-19 03:57:13 +00:00
return nodeAnim ;
2014-05-18 08:57:44 +00:00
}
2014-05-21 01:00:11 +00:00
// TransformKeyFrame
2015-05-19 03:52:10 +00:00
TransformKeyFrame : : TransformKeyFrame ( ) :
2015-05-19 03:57:13 +00:00
timePos ( 0.0f ) ,
scale ( 1.0f , 1.0f , 1.0f )
2014-05-21 01:00:11 +00:00
{
}
aiMatrix4x4 TransformKeyFrame : : Transform ( )
{
2015-05-19 03:57:13 +00:00
return aiMatrix4x4 ( scale , rotation , position ) ;
2014-05-21 01:00:11 +00:00
}
2014-05-18 08:57:44 +00:00
} // Ogre
} // Assimp
# endif // ASSIMP_BUILD_NO_OGRE_IMPORTER