2008-10-15 16:12:18 +00:00
|
|
|
/** Implementation of the BVH loader */
|
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2008-10-15 16:12:18 +00:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
Copyright (c) 2006-2012, assimp team
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
2008-10-19 21:44:12 +00:00
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
2008-10-19 21:44:12 +00:00
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
2008-10-15 16:12:18 +00:00
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2008-10-19 21:44:12 +00:00
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
2012-02-03 03:38:30 +00:00
|
|
|
written permission of the assimp team.
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
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
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AssimpPCH.h"
|
2009-01-18 23:48:25 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_BVH_IMPORTER
|
|
|
|
|
2008-10-15 16:12:18 +00:00
|
|
|
#include "BVHLoader.h"
|
|
|
|
#include "fast_atof.h"
|
2008-10-19 21:44:12 +00:00
|
|
|
#include "SkeletonMeshBuilder.h"
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
2012-04-22 22:26:26 +00:00
|
|
|
static const aiImporterDesc desc = {
|
|
|
|
"BVH Importer (MoCap)",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportTextFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"bvh"
|
|
|
|
};
|
|
|
|
|
2008-10-15 16:12:18 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
BVHLoader::BVHLoader()
|
2012-06-21 15:24:50 +00:00
|
|
|
: noSkeletonMesh()
|
2009-01-10 17:58:06 +00:00
|
|
|
{}
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
BVHLoader::~BVHLoader()
|
2009-01-10 17:58:06 +00:00
|
|
|
{}
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the class can handle the format of the given file.
|
General
- Added format auto-detection to most loaders
- Simplified BaseImporter::CanRead() with some utility methods
- improved fast_atof -> no overruns anymore. Fuck you, irrlicht.
- added assimp_cmd tool to allow command line model processing. Mainly adebugging tool for internal purposes, but others might find it useful, too.
- vc8/vc9: revision number is now written to DLL version header
- mkutil: some batch scripts to simplify tagging & building of release versions
- some API cleanup
- fixing some doxygen markup (+now explicit use of @file <filename>)
- Icon for assimp_view and assimp_cmd
3DS
- Normal vectors are not anymore inverted in some cases
- Improved pivot handling
- Improved handling of x-flipped meshes
Collada
- fixed a minor bug (visual_scene element)
LWS
- WIP implementation. No animations yet, some bugs and crashes.
- Animation system remains disabled, WIP code
- many test files for LWS, but most of them test the anim support, which is, read above, currently disabled.
STL
- fixing a log warning which appears for every model
- added binary&ascii test spider, exported from truespace
MD3
- Cleaning up output tags for automatically joined player models.
IRR
- Fixing coordinate system issues.
- Instance handling improved.
- Some of the reported crashes not yet fixed.
PretransformVertices
- Numerous performance improvements.
- Added config option to preserve the hierarchy during the step.
RemoveRedundantMaterials
- Added config option to specify a list of materials which are kept in every case.
UNREAL
- Added support for the old unreal data format (*.a,*.d,*.uc)
- tested only with exports from Milkshape
- more Unreal stuff to come soon
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@356 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2009-03-05 22:32:13 +00:00
|
|
|
bool BVHLoader::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool cs) const
|
2008-10-15 16:12:18 +00:00
|
|
|
{
|
|
|
|
// check file extension
|
General
- Added format auto-detection to most loaders
- Simplified BaseImporter::CanRead() with some utility methods
- improved fast_atof -> no overruns anymore. Fuck you, irrlicht.
- added assimp_cmd tool to allow command line model processing. Mainly adebugging tool for internal purposes, but others might find it useful, too.
- vc8/vc9: revision number is now written to DLL version header
- mkutil: some batch scripts to simplify tagging & building of release versions
- some API cleanup
- fixing some doxygen markup (+now explicit use of @file <filename>)
- Icon for assimp_view and assimp_cmd
3DS
- Normal vectors are not anymore inverted in some cases
- Improved pivot handling
- Improved handling of x-flipped meshes
Collada
- fixed a minor bug (visual_scene element)
LWS
- WIP implementation. No animations yet, some bugs and crashes.
- Animation system remains disabled, WIP code
- many test files for LWS, but most of them test the anim support, which is, read above, currently disabled.
STL
- fixing a log warning which appears for every model
- added binary&ascii test spider, exported from truespace
MD3
- Cleaning up output tags for automatically joined player models.
IRR
- Fixing coordinate system issues.
- Instance handling improved.
- Some of the reported crashes not yet fixed.
PretransformVertices
- Numerous performance improvements.
- Added config option to preserve the hierarchy during the step.
RemoveRedundantMaterials
- Added config option to specify a list of materials which are kept in every case.
UNREAL
- Added support for the old unreal data format (*.a,*.d,*.uc)
- tested only with exports from Milkshape
- more Unreal stuff to come soon
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@356 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2009-03-05 22:32:13 +00:00
|
|
|
const std::string extension = GetExtension(pFile);
|
|
|
|
|
|
|
|
if( extension == "bvh")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ((!extension.length() || cs) && pIOHandler) {
|
|
|
|
const char* tokens[] = {"HIERARCHY"};
|
|
|
|
return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
|
|
|
|
}
|
|
|
|
return false;
|
2008-10-15 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
2012-06-21 15:24:50 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BVHLoader::SetupProperties(const Importer* pImp)
|
|
|
|
{
|
|
|
|
noSkeletonMesh = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_NO_SKELETON_MESHES,0) != 0;
|
|
|
|
}
|
|
|
|
|
2012-04-22 22:26:26 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Loader meta information
|
|
|
|
const aiImporterDesc* BVHLoader::GetInfo () const
|
|
|
|
{
|
|
|
|
return &desc;
|
|
|
|
}
|
|
|
|
|
2008-10-15 16:12:18 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Imports the given file into the given scene structure.
|
|
|
|
void BVHLoader::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
|
|
|
|
{
|
|
|
|
mFileName = pFile;
|
|
|
|
|
|
|
|
// read file into memory
|
|
|
|
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
|
|
|
|
if( file.get() == NULL)
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "Failed to open file " + pFile + ".");
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
size_t fileSize = file->FileSize();
|
|
|
|
if( fileSize == 0)
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "File is too small.");
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
mBuffer.resize( fileSize);
|
|
|
|
file->Read( &mBuffer.front(), 1, fileSize);
|
|
|
|
|
|
|
|
// start reading
|
|
|
|
mReader = mBuffer.begin();
|
|
|
|
mLine = 1;
|
|
|
|
ReadStructure( pScene);
|
2008-10-19 21:44:12 +00:00
|
|
|
|
2012-06-21 15:24:50 +00:00
|
|
|
if (!noSkeletonMesh) {
|
|
|
|
// build a dummy mesh for the skeleton so that we see something at least
|
|
|
|
SkeletonMeshBuilder meshBuilder( pScene);
|
|
|
|
}
|
2008-10-19 21:44:12 +00:00
|
|
|
|
|
|
|
// construct an animation from all the motion data we read
|
|
|
|
CreateAnimation( pScene);
|
2008-10-15 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the file
|
|
|
|
void BVHLoader::ReadStructure( aiScene* pScene)
|
|
|
|
{
|
|
|
|
// first comes hierarchy
|
|
|
|
std::string header = GetNextToken();
|
|
|
|
if( header != "HIERARCHY")
|
|
|
|
ThrowException( "Expected header string \"HIERARCHY\".");
|
|
|
|
ReadHierarchy( pScene);
|
|
|
|
|
|
|
|
// then comes the motion data
|
|
|
|
std::string motion = GetNextToken();
|
|
|
|
if( motion != "MOTION")
|
|
|
|
ThrowException( "Expected beginning of motion data \"MOTION\".");
|
|
|
|
ReadMotion( pScene);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the hierarchy
|
|
|
|
void BVHLoader::ReadHierarchy( aiScene* pScene)
|
|
|
|
{
|
|
|
|
std::string root = GetNextToken();
|
|
|
|
if( root != "ROOT")
|
|
|
|
ThrowException( "Expected root node \"ROOT\".");
|
|
|
|
|
|
|
|
// Go read the hierarchy from here
|
|
|
|
pScene->mRootNode = ReadNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a node and recursively its childs and returns the created node;
|
|
|
|
aiNode* BVHLoader::ReadNode()
|
|
|
|
{
|
|
|
|
// first token is name
|
|
|
|
std::string nodeName = GetNextToken();
|
|
|
|
if( nodeName.empty() || nodeName == "{")
|
|
|
|
ThrowException( boost::str( boost::format( "Expected node name, but found \"%s\".") % nodeName));
|
|
|
|
|
|
|
|
// then an opening brace should follow
|
|
|
|
std::string openBrace = GetNextToken();
|
|
|
|
if( openBrace != "{")
|
|
|
|
ThrowException( boost::str( boost::format( "Expected opening brace \"{\", but found \"%s\".") % openBrace));
|
|
|
|
|
|
|
|
// Create a node
|
|
|
|
aiNode* node = new aiNode( nodeName);
|
|
|
|
std::vector<aiNode*> childNodes;
|
|
|
|
|
2008-10-19 21:44:12 +00:00
|
|
|
// and create an bone entry for it
|
|
|
|
mNodes.push_back( Node( node));
|
|
|
|
Node& internNode = mNodes.back();
|
|
|
|
|
2008-10-15 16:12:18 +00:00
|
|
|
// now read the node's contents
|
|
|
|
while( 1)
|
|
|
|
{
|
|
|
|
std::string token = GetNextToken();
|
2008-10-19 21:44:12 +00:00
|
|
|
|
2008-10-15 16:12:18 +00:00
|
|
|
// node offset to parent node
|
|
|
|
if( token == "OFFSET")
|
|
|
|
ReadNodeOffset( node);
|
|
|
|
else if( token == "CHANNELS")
|
2008-10-19 21:44:12 +00:00
|
|
|
ReadNodeChannels( internNode);
|
2008-10-15 16:12:18 +00:00
|
|
|
else if( token == "JOINT")
|
|
|
|
{
|
|
|
|
// child node follows
|
|
|
|
aiNode* child = ReadNode();
|
2008-10-19 21:44:12 +00:00
|
|
|
child->mParent = node;
|
2008-10-15 16:12:18 +00:00
|
|
|
childNodes.push_back( child);
|
2008-10-19 21:44:12 +00:00
|
|
|
}
|
|
|
|
else if( token == "End")
|
2008-10-15 16:12:18 +00:00
|
|
|
{
|
2008-10-19 21:44:12 +00:00
|
|
|
// The real symbol is "End Site". Second part comes in a separate token
|
|
|
|
std::string siteToken = GetNextToken();
|
|
|
|
if( siteToken != "Site")
|
|
|
|
ThrowException( boost::str( boost::format( "Expected \"End Site\" keyword, but found \"%s %s\".") % token % siteToken));
|
|
|
|
|
|
|
|
aiNode* child = ReadEndSite( nodeName);
|
|
|
|
child->mParent = node;
|
2008-10-15 16:12:18 +00:00
|
|
|
childNodes.push_back( child);
|
2008-10-19 21:44:12 +00:00
|
|
|
}
|
|
|
|
else if( token == "}")
|
2008-10-15 16:12:18 +00:00
|
|
|
{
|
|
|
|
// we're done with that part of the hierarchy
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
// everything else is a parse error
|
|
|
|
ThrowException( boost::str( boost::format( "Unknown keyword \"%s\".") % token));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the child nodes if there are any
|
|
|
|
if( childNodes.size() > 0)
|
|
|
|
{
|
|
|
|
node->mNumChildren = childNodes.size();
|
|
|
|
node->mChildren = new aiNode*[node->mNumChildren];
|
|
|
|
std::copy( childNodes.begin(), childNodes.end(), node->mChildren);
|
|
|
|
}
|
|
|
|
|
|
|
|
// and return the sub-hierarchy we built here
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2008-10-19 21:44:12 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads an end node and returns the created node.
|
|
|
|
aiNode* BVHLoader::ReadEndSite( const std::string& pParentName)
|
|
|
|
{
|
|
|
|
// check opening brace
|
|
|
|
std::string openBrace = GetNextToken();
|
|
|
|
if( openBrace != "{")
|
|
|
|
ThrowException( boost::str( boost::format( "Expected opening brace \"{\", but found \"%s\".") % openBrace));
|
|
|
|
|
|
|
|
// Create a node
|
|
|
|
aiNode* node = new aiNode( "EndSite_" + pParentName);
|
|
|
|
|
|
|
|
// now read the node's contents. Only possible entry is "OFFSET"
|
|
|
|
while( 1)
|
|
|
|
{
|
|
|
|
std::string token = GetNextToken();
|
|
|
|
|
|
|
|
// end node's offset
|
|
|
|
if( token == "OFFSET")
|
|
|
|
{
|
|
|
|
ReadNodeOffset( node);
|
|
|
|
}
|
|
|
|
else if( token == "}")
|
|
|
|
{
|
|
|
|
// we're done with the end node
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
// everything else is a parse error
|
|
|
|
ThrowException( boost::str( boost::format( "Unknown keyword \"%s\".") % token));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// and return the sub-hierarchy we built here
|
|
|
|
return node;
|
|
|
|
}
|
2008-10-15 16:12:18 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads a node offset for the given node
|
|
|
|
void BVHLoader::ReadNodeOffset( aiNode* pNode)
|
|
|
|
{
|
|
|
|
// Offset consists of three floats to read
|
|
|
|
aiVector3D offset;
|
|
|
|
offset.x = GetNextTokenAsFloat();
|
|
|
|
offset.y = GetNextTokenAsFloat();
|
|
|
|
offset.z = GetNextTokenAsFloat();
|
|
|
|
|
|
|
|
// build a transformation matrix from it
|
|
|
|
pNode->mTransformation = aiMatrix4x4( 1.0f, 0.0f, 0.0f, offset.x, 0.0f, 1.0f, 0.0f, offset.y,
|
|
|
|
0.0f, 0.0f, 1.0f, offset.z, 0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the animation channels for the given node
|
2008-10-19 21:44:12 +00:00
|
|
|
void BVHLoader::ReadNodeChannels( BVHLoader::Node& pNode)
|
2008-10-15 16:12:18 +00:00
|
|
|
{
|
|
|
|
// number of channels. Use the float reader because we're lazy
|
|
|
|
float numChannelsFloat = GetNextTokenAsFloat();
|
|
|
|
unsigned int numChannels = (unsigned int) numChannelsFloat;
|
|
|
|
|
|
|
|
for( unsigned int a = 0; a < numChannels; a++)
|
2008-10-19 21:44:12 +00:00
|
|
|
{
|
|
|
|
std::string channelToken = GetNextToken();
|
|
|
|
|
|
|
|
if( channelToken == "Xposition")
|
|
|
|
pNode.mChannels.push_back( Channel_PositionX);
|
|
|
|
else if( channelToken == "Yposition")
|
|
|
|
pNode.mChannels.push_back( Channel_PositionY);
|
|
|
|
else if( channelToken == "Zposition")
|
|
|
|
pNode.mChannels.push_back( Channel_PositionZ);
|
|
|
|
else if( channelToken == "Xrotation")
|
|
|
|
pNode.mChannels.push_back( Channel_RotationX);
|
|
|
|
else if( channelToken == "Yrotation")
|
|
|
|
pNode.mChannels.push_back( Channel_RotationY);
|
|
|
|
else if( channelToken == "Zrotation")
|
|
|
|
pNode.mChannels.push_back( Channel_RotationZ);
|
|
|
|
else
|
|
|
|
ThrowException( boost::str( boost::format( "Invalid channel specifier \"%s\".") % channelToken));
|
|
|
|
}
|
2008-10-15 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the motion data
|
2011-06-09 14:29:32 +00:00
|
|
|
void BVHLoader::ReadMotion( aiScene* /*pScene*/)
|
2008-10-15 16:12:18 +00:00
|
|
|
{
|
|
|
|
// Read number of frames
|
|
|
|
std::string tokenFrames = GetNextToken();
|
|
|
|
if( tokenFrames != "Frames:")
|
|
|
|
ThrowException( boost::str( boost::format( "Expected frame count \"Frames:\", but found \"%s\".") % tokenFrames));
|
|
|
|
|
|
|
|
float numFramesFloat = GetNextTokenAsFloat();
|
2008-10-19 21:44:12 +00:00
|
|
|
mAnimNumFrames = (unsigned int) numFramesFloat;
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
// Read frame duration
|
|
|
|
std::string tokenDuration1 = GetNextToken();
|
|
|
|
std::string tokenDuration2 = GetNextToken();
|
|
|
|
if( tokenDuration1 != "Frame" || tokenDuration2 != "Time:")
|
|
|
|
ThrowException( boost::str( boost::format( "Expected frame duration \"Frame Time:\", but found \"%s %s\".") % tokenDuration1 % tokenDuration2));
|
|
|
|
|
2008-10-19 21:44:12 +00:00
|
|
|
mAnimTickDuration = GetNextTokenAsFloat();
|
|
|
|
|
|
|
|
// resize value vectors for each node
|
|
|
|
for( std::vector<Node>::iterator it = mNodes.begin(); it != mNodes.end(); ++it)
|
|
|
|
it->mChannelValues.reserve( it->mChannels.size() * mAnimNumFrames);
|
2008-10-15 16:12:18 +00:00
|
|
|
|
2008-10-19 21:44:12 +00:00
|
|
|
// now read all the data and store it in the corresponding node's value vector
|
|
|
|
for( unsigned int frame = 0; frame < mAnimNumFrames; ++frame)
|
|
|
|
{
|
|
|
|
// on each line read the values for all nodes
|
|
|
|
for( std::vector<Node>::iterator it = mNodes.begin(); it != mNodes.end(); ++it)
|
|
|
|
{
|
|
|
|
// get as many values as the node has channels
|
|
|
|
for( unsigned int c = 0; c < it->mChannels.size(); ++c)
|
|
|
|
it->mChannelValues.push_back( GetNextTokenAsFloat());
|
|
|
|
}
|
|
|
|
|
|
|
|
// after one frame worth of values for all nodes there should be a newline, but we better don't rely on it
|
|
|
|
}
|
2008-10-15 16:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Retrieves the next token
|
|
|
|
std::string BVHLoader::GetNextToken()
|
|
|
|
{
|
|
|
|
// skip any preceeding whitespace
|
|
|
|
while( mReader != mBuffer.end())
|
|
|
|
{
|
|
|
|
if( !isspace( *mReader))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// count lines
|
|
|
|
if( *mReader == '\n')
|
|
|
|
mLine++;
|
|
|
|
|
|
|
|
++mReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect all chars till the next whitespace. BVH is easy in respect to that.
|
|
|
|
std::string token;
|
|
|
|
while( mReader != mBuffer.end())
|
|
|
|
{
|
|
|
|
if( isspace( *mReader))
|
|
|
|
break;
|
|
|
|
|
|
|
|
token.push_back( *mReader);
|
|
|
|
++mReader;
|
|
|
|
|
|
|
|
// little extra logic to make sure braces are counted correctly
|
|
|
|
if( token == "{" || token == "}")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// empty token means end of file, which is just fine
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Reads the next token as a float
|
|
|
|
float BVHLoader::GetNextTokenAsFloat()
|
|
|
|
{
|
|
|
|
std::string token = GetNextToken();
|
|
|
|
if( token.empty())
|
|
|
|
ThrowException( "Unexpected end of file while trying to read a float");
|
|
|
|
|
|
|
|
// check if the float is valid by testing if the atof() function consumed every char of the token
|
|
|
|
const char* ctoken = token.c_str();
|
|
|
|
float result = 0.0f;
|
2012-01-31 16:18:32 +00:00
|
|
|
ctoken = fast_atoreal_move<float>( ctoken, result);
|
2008-10-15 16:12:18 +00:00
|
|
|
|
|
|
|
if( ctoken != token.c_str() + token.length())
|
|
|
|
ThrowException( boost::str( boost::format( "Expected a floating point number, but found \"%s\".") % token));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Aborts the file reading with an exception
|
2015-02-13 12:45:36 +00:00
|
|
|
AI_WONT_RETURN void BVHLoader::ThrowException( const std::string& pError)
|
2008-10-15 16:12:18 +00:00
|
|
|
{
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( boost::str( boost::format( "%s:%d - %s") % mFileName % mLine % pError));
|
2008-10-15 16:12:18 +00:00
|
|
|
}
|
2008-10-19 21:44:12 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructs an animation for the motion data and stores it in the given scene
|
|
|
|
void BVHLoader::CreateAnimation( aiScene* pScene)
|
|
|
|
{
|
|
|
|
// create the animation
|
|
|
|
pScene->mNumAnimations = 1;
|
|
|
|
pScene->mAnimations = new aiAnimation*[1];
|
|
|
|
aiAnimation* anim = new aiAnimation;
|
|
|
|
pScene->mAnimations[0] = anim;
|
|
|
|
|
|
|
|
// put down the basic parameters
|
|
|
|
anim->mName.Set( "Motion");
|
|
|
|
anim->mTicksPerSecond = 1.0 / double( mAnimTickDuration);
|
|
|
|
anim->mDuration = double( mAnimNumFrames - 1);
|
|
|
|
|
|
|
|
// now generate the tracks for all nodes
|
|
|
|
anim->mNumChannels = mNodes.size();
|
|
|
|
anim->mChannels = new aiNodeAnim*[anim->mNumChannels];
|
2009-01-10 17:58:06 +00:00
|
|
|
|
|
|
|
// FIX: set the array elements to NULL to ensure proper deletion if an exception is thrown
|
|
|
|
for (unsigned int i = 0; i < anim->mNumChannels;++i)
|
|
|
|
anim->mChannels[i] = NULL;
|
|
|
|
|
2008-10-19 21:44:12 +00:00
|
|
|
for( unsigned int a = 0; a < anim->mNumChannels; a++)
|
|
|
|
{
|
|
|
|
const Node& node = mNodes[a];
|
2009-01-10 17:58:06 +00:00
|
|
|
const std::string nodeName = std::string( node.mNode->mName.data );
|
2008-10-19 21:44:12 +00:00
|
|
|
aiNodeAnim* nodeAnim = new aiNodeAnim;
|
|
|
|
anim->mChannels[a] = nodeAnim;
|
2009-01-10 17:58:06 +00:00
|
|
|
nodeAnim->mNodeName.Set( nodeName);
|
2008-10-19 21:44:12 +00:00
|
|
|
|
|
|
|
// translational part, if given
|
|
|
|
if( node.mChannels.size() == 6)
|
|
|
|
{
|
|
|
|
nodeAnim->mNumPositionKeys = mAnimNumFrames;
|
|
|
|
nodeAnim->mPositionKeys = new aiVectorKey[mAnimNumFrames];
|
|
|
|
aiVectorKey* poskey = nodeAnim->mPositionKeys;
|
|
|
|
for( unsigned int fr = 0; fr < mAnimNumFrames; ++fr)
|
|
|
|
{
|
|
|
|
poskey->mTime = double( fr);
|
2009-01-10 17:58:06 +00:00
|
|
|
|
|
|
|
// Now compute all translations in the right order
|
2009-03-08 15:29:34 +00:00
|
|
|
for( unsigned int channel = 0; channel < 3; ++channel)
|
|
|
|
{
|
|
|
|
switch( node.mChannels[channel])
|
|
|
|
{
|
|
|
|
case Channel_PositionX: poskey->mValue.x = node.mChannelValues[fr * node.mChannels.size() + channel]; break;
|
|
|
|
case Channel_PositionY: poskey->mValue.y = node.mChannelValues[fr * node.mChannels.size() + channel]; break;
|
|
|
|
case Channel_PositionZ: poskey->mValue.z = node.mChannelValues[fr * node.mChannels.size() + channel]; break;
|
2010-03-18 17:00:12 +00:00
|
|
|
default: throw DeadlyImportError( "Unexpected animation channel setup at node " + nodeName );
|
2009-03-08 15:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-19 21:44:12 +00:00
|
|
|
++poskey;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
// if no translation part is given, put a default sequence
|
|
|
|
aiVector3D nodePos( node.mNode->mTransformation.a4, node.mNode->mTransformation.b4, node.mNode->mTransformation.c4);
|
2008-11-03 17:59:04 +00:00
|
|
|
nodeAnim->mNumPositionKeys = 1;
|
|
|
|
nodeAnim->mPositionKeys = new aiVectorKey[1];
|
2008-10-19 21:44:12 +00:00
|
|
|
nodeAnim->mPositionKeys[0].mTime = 0.0;
|
|
|
|
nodeAnim->mPositionKeys[0].mValue = nodePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// rotation part. Always present. First find value offsets
|
|
|
|
{
|
2009-01-10 17:58:06 +00:00
|
|
|
unsigned int rotOffset = 0;
|
2008-10-19 21:44:12 +00:00
|
|
|
if( node.mChannels.size() == 6)
|
|
|
|
{
|
2009-01-10 17:58:06 +00:00
|
|
|
// Offset all further calculations
|
2008-10-19 21:44:12 +00:00
|
|
|
rotOffset = 3;
|
2009-01-10 17:58:06 +00:00
|
|
|
}
|
2008-10-19 21:44:12 +00:00
|
|
|
|
|
|
|
// Then create the number of rotation keys
|
|
|
|
nodeAnim->mNumRotationKeys = mAnimNumFrames;
|
|
|
|
nodeAnim->mRotationKeys = new aiQuatKey[mAnimNumFrames];
|
|
|
|
aiQuatKey* rotkey = nodeAnim->mRotationKeys;
|
|
|
|
for( unsigned int fr = 0; fr < mAnimNumFrames; ++fr)
|
|
|
|
{
|
2009-03-08 15:29:34 +00:00
|
|
|
aiMatrix4x4 temp;
|
|
|
|
aiMatrix3x3 rotMatrix;
|
|
|
|
|
|
|
|
for( unsigned int channel = 0; channel < 3; ++channel)
|
|
|
|
{
|
|
|
|
// translate ZXY euler angels into a quaternion
|
|
|
|
const float angle = node.mChannelValues[fr * node.mChannels.size() + rotOffset + channel] * float( AI_MATH_PI) / 180.0f;
|
|
|
|
|
|
|
|
// Compute rotation transformations in the right order
|
|
|
|
switch (node.mChannels[rotOffset+channel])
|
|
|
|
{
|
|
|
|
case Channel_RotationX: aiMatrix4x4::RotationX( angle, temp); rotMatrix *= aiMatrix3x3( temp); break;
|
|
|
|
case Channel_RotationY: aiMatrix4x4::RotationY( angle, temp); rotMatrix *= aiMatrix3x3( temp); break;
|
|
|
|
case Channel_RotationZ: aiMatrix4x4::RotationZ( angle, temp); rotMatrix *= aiMatrix3x3( temp); break;
|
2010-03-18 17:00:12 +00:00
|
|
|
default: throw DeadlyImportError( "Unexpected animation channel setup at node " + nodeName );
|
2009-03-08 15:29:34 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-19 21:44:12 +00:00
|
|
|
|
|
|
|
rotkey->mTime = double( fr);
|
|
|
|
rotkey->mValue = aiQuaternion( rotMatrix);
|
|
|
|
++rotkey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scaling part. Always just a default track
|
|
|
|
{
|
2008-11-03 17:59:04 +00:00
|
|
|
nodeAnim->mNumScalingKeys = 1;
|
|
|
|
nodeAnim->mScalingKeys = new aiVectorKey[1];
|
2008-10-19 21:44:12 +00:00
|
|
|
nodeAnim->mScalingKeys[0].mTime = 0.0;
|
|
|
|
nodeAnim->mScalingKeys[0].mValue.Set( 1.0f, 1.0f, 1.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_BVH_IMPORTER
|