2008-08-06 23:01:38 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (ASSIMP)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2008, ASSIMP Development Team
|
|
|
|
|
|
|
|
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
|
|
|
|
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 Development Team.
|
|
|
|
|
|
|
|
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.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
/** @file MD5Loader.cpp
|
|
|
|
* @brief Implementation of the MD5 importer class
|
|
|
|
*/
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
#include "AssimpPCH.h"
|
2009-01-18 23:48:25 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_MD5_IMPORTER
|
2008-10-13 16:45:48 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// internal headers
|
|
|
|
#include "MaterialSystem.h"
|
|
|
|
#include "RemoveComments.h"
|
|
|
|
#include "MD5Loader.h"
|
|
|
|
#include "StringComparison.h"
|
|
|
|
#include "fast_atof.h"
|
2009-03-08 15:29:34 +00:00
|
|
|
#include "SkeletonMeshBuilder.h"
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// Minimum weight value. Weights inside [-n ... n] are ignored
|
|
|
|
#define AI_MD5_WEIGHT_EPSILON 1e-5f
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
MD5Importer::MD5Importer()
|
2009-03-08 15:29:34 +00:00
|
|
|
: configNoAutoLoad (false)
|
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
|
|
|
{}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
MD5Importer::~MD5Importer()
|
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
|
|
|
{}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the class can handle the format of the given file.
|
|
|
|
bool MD5Importer::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
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);
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
if (extension == "md5anim" || extension == "md5mesh" || extension == "md5camera")
|
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
|
|
|
return true;
|
|
|
|
else if (!extension.length() || checkSig) {
|
|
|
|
if (!pIOHandler)
|
|
|
|
return true;
|
|
|
|
const char* tokens[] = {"MD5Version"};
|
|
|
|
return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
|
|
|
|
}
|
|
|
|
return false;
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
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
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
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
|
|
|
// Get list of all supported extensions
|
2010-03-02 17:38:01 +00:00
|
|
|
void MD5Importer::GetExtensionList(std::set<std::string>& extensions)
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
2010-03-02 17:38:01 +00:00
|
|
|
extensions.insert("md5anim");
|
|
|
|
extensions.insert("md5mesh");
|
|
|
|
extensions.insert("md5camera");
|
2009-03-08 15:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Setup import properties
|
|
|
|
void MD5Importer::SetupProperties(const Importer* pImp)
|
|
|
|
{
|
|
|
|
// AI_CONFIG_IMPORT_MD5_NO_ANIM_AUTOLOAD
|
|
|
|
configNoAutoLoad = (0 != pImp->GetPropertyInteger(AI_CONFIG_IMPORT_MD5_NO_ANIM_AUTOLOAD,0));
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Imports the given file into the given scene structure.
|
2009-03-08 15:29:34 +00:00
|
|
|
void MD5Importer::InternReadFile( const std::string& pFile,
|
|
|
|
aiScene* _pScene, IOSystem* _pIOHandler)
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
2009-03-08 15:29:34 +00:00
|
|
|
pIOHandler = _pIOHandler;
|
|
|
|
pScene = _pScene;
|
|
|
|
bHadMD5Mesh = bHadMD5Anim = bHadMD5Camera = false;
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// remove the file extension
|
|
|
|
std::string::size_type pos = pFile.find_last_of('.');
|
2009-03-08 15:29:34 +00:00
|
|
|
mFile = (std::string::npos == pos ? pFile : pFile.substr(0,pos+1));
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
const std::string extension = GetExtension(pFile);
|
|
|
|
try {
|
|
|
|
if (extension == "md5camera") {
|
|
|
|
LoadMD5CameraFile();
|
|
|
|
}
|
|
|
|
else if (configNoAutoLoad || extension == "md5anim") {
|
|
|
|
// determine file extension and process just *one* file
|
|
|
|
if (extension.length() == 0) {
|
|
|
|
/* fixme */
|
|
|
|
}
|
|
|
|
if (extension == "md5anim") {
|
|
|
|
LoadMD5AnimFile();
|
|
|
|
}
|
|
|
|
else if (extension == "md5mesh") {
|
|
|
|
LoadMD5MeshFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LoadMD5MeshFile();
|
|
|
|
LoadMD5AnimFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( ImportErrorException* ex) {
|
|
|
|
UnloadFileFromMemory();
|
|
|
|
throw ex;
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// make sure we have at least one file
|
2009-03-08 20:12:43 +00:00
|
|
|
if (!bHadMD5Mesh && !bHadMD5Anim && !bHadMD5Camera)
|
2009-03-15 00:40:30 +00:00
|
|
|
throw new ImportErrorException("Failed to read valid contents from this MD5* file");
|
|
|
|
|
|
|
|
// Now rotate the whole scene 90 degrees around the x axis to convert to internal coordinate system
|
|
|
|
pScene->mRootNode->mTransformation = aiMatrix4x4(1.f,0.f,0.f,0.f,
|
|
|
|
0.f,0.f,1.f,0.f,0.f,-1.f,0.f,0.f,0.f,0.f,0.f,1.f);
|
2009-03-08 15:29:34 +00:00
|
|
|
|
|
|
|
// the output scene wouldn't pass the validation without this flag
|
|
|
|
if (!bHadMD5Mesh)
|
|
|
|
pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-08 15:29:34 +00:00
|
|
|
// Load a file into a memory buffer
|
2008-08-06 23:01:38 +00:00
|
|
|
void MD5Importer::LoadFileIntoMemory (IOStream* file)
|
|
|
|
{
|
|
|
|
ai_assert(NULL != file);
|
2009-03-08 15:29:34 +00:00
|
|
|
fileSize = (unsigned int)file->FileSize();
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// allocate storage and copy the contents of the file to a memory buffer
|
2009-03-08 15:29:34 +00:00
|
|
|
pScene = pScene;
|
|
|
|
mBuffer = new char[fileSize+1];
|
|
|
|
file->Read( (void*)mBuffer, 1, fileSize);
|
|
|
|
iLineNumber = 1;
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// append a terminal 0
|
2009-03-08 15:29:34 +00:00
|
|
|
mBuffer[fileSize] = '\0';
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// now remove all line comments from the file
|
2009-03-08 15:29:34 +00:00
|
|
|
CommentRemover::RemoveLineComments("//",mBuffer,' ');
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-08 15:29:34 +00:00
|
|
|
// Unload the current memory buffer
|
2008-08-06 23:01:38 +00:00
|
|
|
void MD5Importer::UnloadFileFromMemory ()
|
|
|
|
{
|
|
|
|
// delete the file buffer
|
2009-03-08 15:29:34 +00:00
|
|
|
delete[] mBuffer;
|
|
|
|
mBuffer = NULL;
|
|
|
|
fileSize = 0;
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-08 15:29:34 +00:00
|
|
|
// Build unique vertices
|
|
|
|
void MD5Importer::MakeDataUnique (MD5::MeshDesc& meshSrc)
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
|
|
|
std::vector<bool> abHad(meshSrc.mVertices.size(),false);
|
|
|
|
|
|
|
|
// allocate enough storage to keep the output structures
|
2009-03-08 15:29:34 +00:00
|
|
|
const unsigned int iNewNum = meshSrc.mFaces.size()*3;
|
|
|
|
unsigned int iNewIndex = meshSrc.mVertices.size();
|
2008-08-06 23:01:38 +00:00
|
|
|
meshSrc.mVertices.resize(iNewNum);
|
|
|
|
|
|
|
|
// try to guess how much storage we'll need for new weights
|
|
|
|
const float fWeightsPerVert = meshSrc.mWeights.size() / (float)iNewIndex;
|
|
|
|
const unsigned int guess = (unsigned int)(fWeightsPerVert*iNewNum);
|
|
|
|
meshSrc.mWeights.reserve(guess + (guess >> 3)); // + 12.5% as buffer
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
for (FaceList::const_iterator iter = meshSrc.mFaces.begin(),iterEnd = meshSrc.mFaces.end();iter != iterEnd;++iter){
|
2008-08-06 23:01:38 +00:00
|
|
|
const aiFace& face = *iter;
|
2009-03-11 22:56:16 +00:00
|
|
|
for (unsigned int i = 0; i < 3;++i) {
|
|
|
|
if (face.mIndices[0] >= meshSrc.mVertices.size())
|
|
|
|
throw new ImportErrorException("MD5MESH: Invalid vertex index");
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
if (abHad[face.mIndices[i]]) {
|
2008-08-06 23:01:38 +00:00
|
|
|
// generate a new vertex
|
|
|
|
meshSrc.mVertices[iNewIndex] = meshSrc.mVertices[face.mIndices[i]];
|
|
|
|
face.mIndices[i] = iNewIndex++;
|
|
|
|
}
|
|
|
|
else abHad[face.mIndices[i]] = true;
|
|
|
|
}
|
2009-03-15 00:40:30 +00:00
|
|
|
// swap face order
|
|
|
|
std::swap(face.mIndices[0],face.mIndices[2]);
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-08 15:29:34 +00:00
|
|
|
// Recursive node graph construction from a MD5MESH
|
|
|
|
void MD5Importer::AttachChilds_Mesh(int iParentID,aiNode* piParent, BoneList& bones)
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
|
|
|
ai_assert(NULL != piParent && !piParent->mNumChildren);
|
2009-03-08 15:29:34 +00:00
|
|
|
|
|
|
|
// First find out how many children we'll have
|
|
|
|
for (int i = 0; i < (int)bones.size();++i) {
|
|
|
|
if (iParentID != i && bones[i].mParentIndex == iParentID) {
|
2008-08-06 23:01:38 +00:00
|
|
|
++piParent->mNumChildren;
|
|
|
|
}
|
|
|
|
}
|
2009-03-08 15:29:34 +00:00
|
|
|
if (piParent->mNumChildren) {
|
2008-08-06 23:01:38 +00:00
|
|
|
piParent->mChildren = new aiNode*[piParent->mNumChildren];
|
2009-03-08 15:29:34 +00:00
|
|
|
for (int i = 0; i < (int)bones.size();++i) {
|
2008-08-06 23:01:38 +00:00
|
|
|
// (avoid infinite recursion)
|
2009-03-08 15:29:34 +00:00
|
|
|
if (iParentID != i && bones[i].mParentIndex == iParentID) {
|
2008-08-06 23:01:38 +00:00
|
|
|
aiNode* pc;
|
2009-03-08 15:29:34 +00:00
|
|
|
// setup a new node
|
2008-08-06 23:01:38 +00:00
|
|
|
*piParent->mChildren++ = pc = new aiNode();
|
2009-03-08 15:29:34 +00:00
|
|
|
pc->mName = aiString(bones[i].mName);
|
2008-08-06 23:01:38 +00:00
|
|
|
pc->mParent = piParent;
|
|
|
|
|
|
|
|
// get the transformation matrix from rotation and translational components
|
2009-03-08 15:29:34 +00:00
|
|
|
aiQuaternion quat;
|
|
|
|
MD5::ConvertQuaternion ( bones[i].mRotationQuat, quat );
|
|
|
|
|
2009-11-19 23:22:53 +00:00
|
|
|
// FIX to get to Assimp's quaternion conventions
|
|
|
|
quat.w *= -1.f;
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
bones[i].mTransform = aiMatrix4x4 ( quat.GetMatrix());
|
|
|
|
bones[i].mTransform.a4 = bones[i].mPositionXYZ.x;
|
|
|
|
bones[i].mTransform.b4 = bones[i].mPositionXYZ.y;
|
|
|
|
bones[i].mTransform.c4 = bones[i].mPositionXYZ.z;
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// store it for later use
|
2009-03-08 15:29:34 +00:00
|
|
|
pc->mTransformation = bones[i].mInvTransform = bones[i].mTransform;
|
2008-08-06 23:01:38 +00:00
|
|
|
bones[i].mInvTransform.Inverse();
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// the transformations for each bone are absolute, so we need to multiply them
|
|
|
|
// with the inverse of the absolute matrix of the parent joint
|
|
|
|
if (-1 != iParentID) {
|
|
|
|
pc->mTransformation = bones[iParentID].mInvTransform * pc->mTransformation;
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add children to this node, too
|
2009-03-08 15:29:34 +00:00
|
|
|
AttachChilds_Mesh( i, pc, bones);
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-08 15:29:34 +00:00
|
|
|
// undo offset computations
|
2008-08-06 23:01:38 +00:00
|
|
|
piParent->mChildren -= piParent->mNumChildren;
|
|
|
|
}
|
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-08 15:29:34 +00:00
|
|
|
// Recursive node graph construction from a MD5ANIM
|
|
|
|
void MD5Importer::AttachChilds_Anim(int iParentID,aiNode* piParent, AnimBoneList& bones,const aiNodeAnim** node_anims)
|
|
|
|
{
|
|
|
|
ai_assert(NULL != piParent && !piParent->mNumChildren);
|
|
|
|
|
|
|
|
// First find out how many children we'll have
|
|
|
|
for (int i = 0; i < (int)bones.size();++i) {
|
|
|
|
if (iParentID != i && bones[i].mParentIndex == iParentID) {
|
|
|
|
++piParent->mNumChildren;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (piParent->mNumChildren) {
|
|
|
|
piParent->mChildren = new aiNode*[piParent->mNumChildren];
|
|
|
|
for (int i = 0; i < (int)bones.size();++i) {
|
|
|
|
// (avoid infinite recursion)
|
|
|
|
if (iParentID != i && bones[i].mParentIndex == iParentID)
|
|
|
|
{
|
|
|
|
aiNode* pc;
|
|
|
|
// setup a new node
|
|
|
|
*piParent->mChildren++ = pc = new aiNode();
|
|
|
|
pc->mName = aiString(bones[i].mName);
|
|
|
|
pc->mParent = piParent;
|
|
|
|
|
|
|
|
// get the corresponding animation channel and its first frame
|
|
|
|
const aiNodeAnim** cur = node_anims;
|
|
|
|
while ((**cur).mNodeName != pc->mName)++cur;
|
|
|
|
|
|
|
|
aiMatrix4x4::Translation((**cur).mPositionKeys[0].mValue,pc->mTransformation);
|
|
|
|
pc->mTransformation = pc->mTransformation * aiMatrix4x4((**cur).mRotationKeys[0].mValue.GetMatrix()) ;
|
|
|
|
|
|
|
|
// add children to this node, too
|
|
|
|
AttachChilds_Anim( i, pc, bones,node_anims);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// undo offset computations
|
|
|
|
piParent->mChildren -= piParent->mNumChildren;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Load a MD5MESH file
|
2008-08-06 23:01:38 +00:00
|
|
|
void MD5Importer::LoadMD5MeshFile ()
|
|
|
|
{
|
2009-03-08 15:29:34 +00:00
|
|
|
std::string pFile = mFile + "md5mesh";
|
2008-08-06 23:01:38 +00:00
|
|
|
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
|
|
|
|
|
|
|
|
// Check whether we can read from the file
|
2009-03-08 15:29:34 +00:00
|
|
|
if( file.get() == NULL) {
|
|
|
|
DefaultLogger::get()->warn("Failed to read MD5MESH file: " + pFile);
|
2008-08-06 23:01:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bHadMD5Mesh = true;
|
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
|
|
|
LoadFileIntoMemory(file.get());
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// now construct a parser and parse the file
|
|
|
|
MD5::MD5Parser parser(mBuffer,fileSize);
|
|
|
|
|
|
|
|
// load the mesh information from it
|
|
|
|
MD5::MD5MeshParser meshParser(parser.mSections);
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// create the bone hierarchy - first the root node and dummy nodes for all meshes
|
|
|
|
pScene->mRootNode = new aiNode("<MD5_Root>");
|
2008-08-06 23:01:38 +00:00
|
|
|
pScene->mRootNode->mNumChildren = 2;
|
|
|
|
pScene->mRootNode->mChildren = new aiNode*[2];
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// build the hierarchy from the MD5MESH file
|
2008-08-28 17:35:36 +00:00
|
|
|
aiNode* pcNode = pScene->mRootNode->mChildren[1] = new aiNode();
|
2009-03-08 15:29:34 +00:00
|
|
|
pcNode->mName.Set("<MD5_Hierarchy>");
|
2008-08-06 23:01:38 +00:00
|
|
|
pcNode->mParent = pScene->mRootNode;
|
2009-03-08 15:29:34 +00:00
|
|
|
AttachChilds_Mesh(-1,pcNode,meshParser.mJoints);
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
pcNode = pScene->mRootNode->mChildren[0] = new aiNode();
|
2009-03-08 15:29:34 +00:00
|
|
|
pcNode->mName.Set("<MD5_Mesh>");
|
2008-08-28 17:35:36 +00:00
|
|
|
pcNode->mParent = pScene->mRootNode;
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
#if 0
|
|
|
|
if (pScene->mRootNode->mChildren[1]->mNumChildren) /* start at the right hierarchy level */
|
|
|
|
SkeletonMeshBuilder skeleton_maker(pScene,pScene->mRootNode->mChildren[1]->mChildren[0]);
|
|
|
|
#else
|
2008-08-28 17:35:36 +00:00
|
|
|
std::vector<MD5::MeshDesc>::const_iterator end = meshParser.mMeshes.end();
|
|
|
|
|
|
|
|
// FIX: MD5 files exported from Blender can have empty meshes
|
2009-03-08 15:29:34 +00:00
|
|
|
for (std::vector<MD5::MeshDesc>::const_iterator it = meshParser.mMeshes.begin(),end = meshParser.mMeshes.end(); it != end;++it) {
|
2008-08-28 17:35:36 +00:00
|
|
|
if (!(*it).mFaces.empty() && !(*it).mVertices.empty())
|
|
|
|
++pScene->mNumMaterials;
|
|
|
|
}
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// generate all meshes
|
2008-08-28 17:35:36 +00:00
|
|
|
pScene->mNumMeshes = pScene->mNumMaterials;
|
2008-08-06 23:01:38 +00:00
|
|
|
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
|
|
|
|
pScene->mMaterials = new aiMaterial*[pScene->mNumMeshes];
|
2008-08-28 17:35:36 +00:00
|
|
|
|
|
|
|
// storage for node mesh indices
|
|
|
|
pcNode->mNumMeshes = pScene->mNumMeshes;
|
|
|
|
pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
|
|
|
|
for (unsigned int m = 0; m < pcNode->mNumMeshes;++m)
|
|
|
|
pcNode->mMeshes[m] = m;
|
|
|
|
|
|
|
|
unsigned int n = 0;
|
2009-03-08 15:29:34 +00:00
|
|
|
for (std::vector<MD5::MeshDesc>::iterator it = meshParser.mMeshes.begin(),end = meshParser.mMeshes.end(); it != end;++it) {
|
2008-08-28 17:35:36 +00:00
|
|
|
MD5::MeshDesc& meshSrc = *it;
|
|
|
|
if (meshSrc.mFaces.empty() || meshSrc.mVertices.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
aiMesh* mesh = pScene->mMeshes[n] = new aiMesh();
|
2008-09-30 20:20:56 +00:00
|
|
|
mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// generate unique vertices in our internal verbose format
|
|
|
|
MakeDataUnique(meshSrc);
|
|
|
|
|
|
|
|
mesh->mNumVertices = (unsigned int) meshSrc.mVertices.size();
|
|
|
|
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
|
|
|
|
mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
|
|
|
|
mesh->mNumUVComponents[0] = 2;
|
|
|
|
|
|
|
|
// copy texture coordinates
|
|
|
|
aiVector3D* pv = mesh->mTextureCoords[0];
|
2009-03-08 15:29:34 +00:00
|
|
|
for (MD5::VertexList::const_iterator iter = meshSrc.mVertices.begin();iter != meshSrc.mVertices.end();++iter,++pv) {
|
2008-08-06 23:01:38 +00:00
|
|
|
pv->x = (*iter).mUV.x;
|
|
|
|
pv->y = 1.0f-(*iter).mUV.y; // D3D to OpenGL
|
|
|
|
pv->z = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort all bone weights - per bone
|
|
|
|
unsigned int* piCount = new unsigned int[meshParser.mJoints.size()];
|
|
|
|
::memset(piCount,0,sizeof(unsigned int)*meshParser.mJoints.size());
|
|
|
|
|
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
|
|
|
for (MD5::VertexList::const_iterator iter = meshSrc.mVertices.begin();iter != meshSrc.mVertices.end();++iter,++pv) {
|
2008-08-06 23:01:38 +00:00
|
|
|
for (unsigned int jub = (*iter).mFirstWeight, w = jub; w < jub + (*iter).mNumWeights;++w)
|
|
|
|
{
|
|
|
|
MD5::WeightDesc& desc = meshSrc.mWeights[w];
|
2009-03-08 15:29:34 +00:00
|
|
|
/* FIX for some invalid exporters */
|
|
|
|
if (!(desc.mWeight < AI_MD5_WEIGHT_EPSILON && desc.mWeight >= -AI_MD5_WEIGHT_EPSILON ))
|
|
|
|
++piCount[desc.mBone];
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check how many we will need
|
|
|
|
for (unsigned int p = 0; p < meshParser.mJoints.size();++p)
|
|
|
|
if (piCount[p])mesh->mNumBones++;
|
|
|
|
|
|
|
|
if (mesh->mNumBones) // just for safety
|
|
|
|
{
|
|
|
|
mesh->mBones = new aiBone*[mesh->mNumBones];
|
|
|
|
for (unsigned int q = 0,h = 0; q < meshParser.mJoints.size();++q)
|
|
|
|
{
|
|
|
|
if (!piCount[q])continue;
|
|
|
|
aiBone* p = mesh->mBones[h] = new aiBone();
|
|
|
|
p->mNumWeights = piCount[q];
|
|
|
|
p->mWeights = new aiVertexWeight[p->mNumWeights];
|
|
|
|
p->mName = aiString(meshParser.mJoints[q].mName);
|
2009-03-08 15:29:34 +00:00
|
|
|
p->mOffsetMatrix = meshParser.mJoints[q].mInvTransform;
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// store the index for later use
|
2009-03-08 15:29:34 +00:00
|
|
|
MD5::BoneDesc& boneSrc = meshParser.mJoints[q];
|
|
|
|
boneSrc.mMap = h++;
|
|
|
|
|
|
|
|
// compute w-component of quaternion
|
|
|
|
MD5::ConvertQuaternion( boneSrc.mRotationQuat, boneSrc.mRotationQuatConverted );
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
|
2008-09-12 20:25:11 +00:00
|
|
|
//unsigned int g = 0;
|
2008-08-06 23:01:38 +00:00
|
|
|
pv = mesh->mVertices;
|
2009-03-08 15:29:34 +00:00
|
|
|
for (MD5::VertexList::const_iterator iter = meshSrc.mVertices.begin();iter != meshSrc.mVertices.end();++iter,++pv) {
|
2008-08-06 23:01:38 +00:00
|
|
|
// compute the final vertex position from all single weights
|
|
|
|
*pv = aiVector3D();
|
|
|
|
|
|
|
|
// there are models which have weights which don't sum to 1 ...
|
|
|
|
float fSum = 0.0f;
|
|
|
|
for (unsigned int jub = (*iter).mFirstWeight, w = jub; w < jub + (*iter).mNumWeights;++w)
|
|
|
|
fSum += meshSrc.mWeights[w].mWeight;
|
2009-03-08 15:29:34 +00:00
|
|
|
if (!fSum) {
|
|
|
|
DefaultLogger::get()->error("MD5MESH: The sum of all vertex bone weights is 0");
|
|
|
|
continue;
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// process bone weights
|
2009-03-15 00:40:30 +00:00
|
|
|
for (unsigned int jub = (*iter).mFirstWeight, w = jub; w < jub + (*iter).mNumWeights;++w) {
|
2009-03-11 22:56:16 +00:00
|
|
|
if (w >= meshSrc.mWeights.size())
|
|
|
|
throw new ImportErrorException("MD5MESH: Invalid weight index");
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
MD5::WeightDesc& desc = meshSrc.mWeights[w];
|
2009-03-08 15:29:34 +00:00
|
|
|
if ( desc.mWeight < AI_MD5_WEIGHT_EPSILON && desc.mWeight >= -AI_MD5_WEIGHT_EPSILON)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const float fNewWeight = desc.mWeight / fSum;
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// transform the local position into worldspace
|
|
|
|
MD5::BoneDesc& boneSrc = meshParser.mJoints[desc.mBone];
|
2009-03-08 15:29:34 +00:00
|
|
|
const aiVector3D v = boneSrc.mRotationQuatConverted.Rotate (desc.vOffsetPosition);
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// use the original weight to compute the vertex position
|
|
|
|
// (some MD5s seem to depend on the invalid weight values ...)
|
2009-03-08 15:29:34 +00:00
|
|
|
*pv += ((boneSrc.mPositionXYZ+v)* desc.mWeight);
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
aiBone* bone = mesh->mBones[boneSrc.mMap];
|
|
|
|
*bone->mWeights++ = aiVertexWeight((unsigned int)(pv-mesh->mVertices),fNewWeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// undo our nice offset tricks ...
|
|
|
|
for (unsigned int p = 0; p < mesh->mNumBones;++p)
|
|
|
|
mesh->mBones[p]->mWeights -= mesh->mBones[p]->mNumWeights;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] piCount;
|
|
|
|
|
|
|
|
// now setup all faces - we can directly copy the list
|
|
|
|
// (however, take care that the aiFace destructor doesn't delete the mIndices array)
|
|
|
|
mesh->mNumFaces = (unsigned int)meshSrc.mFaces.size();
|
|
|
|
mesh->mFaces = new aiFace[mesh->mNumFaces];
|
2009-03-15 00:40:30 +00:00
|
|
|
for (unsigned int c = 0; c < mesh->mNumFaces;++c) {
|
2008-08-06 23:01:38 +00:00
|
|
|
mesh->mFaces[c].mNumIndices = 3;
|
|
|
|
mesh->mFaces[c].mIndices = meshSrc.mFaces[c].mIndices;
|
|
|
|
meshSrc.mFaces[c].mIndices = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate a material for the mesh
|
|
|
|
MaterialHelper* mat = new MaterialHelper();
|
2008-08-28 17:35:36 +00:00
|
|
|
pScene->mMaterials[n] = mat;
|
2009-03-08 15:29:34 +00:00
|
|
|
|
|
|
|
// insert the typical doom3 textures:
|
|
|
|
// nnn_local.tga - normal map
|
|
|
|
// nnn_h.tga - height map
|
|
|
|
// nnn_s.tga - specular map
|
|
|
|
// nnn_d.tga - diffuse map
|
|
|
|
if (meshSrc.mShader.length && !strchr(meshSrc.mShader.data,'.')) {
|
|
|
|
|
|
|
|
aiString temp(meshSrc.mShader);
|
|
|
|
temp.Append("_local.tga");
|
|
|
|
mat->AddProperty(&temp,AI_MATKEY_TEXTURE_NORMALS(0));
|
|
|
|
|
|
|
|
temp = aiString(meshSrc.mShader);
|
|
|
|
temp.Append("_s.tga");
|
|
|
|
mat->AddProperty(&temp,AI_MATKEY_TEXTURE_SPECULAR(0));
|
|
|
|
|
|
|
|
temp = aiString(meshSrc.mShader);
|
|
|
|
temp.Append("_d.tga");
|
|
|
|
mat->AddProperty(&temp,AI_MATKEY_TEXTURE_DIFFUSE(0));
|
|
|
|
|
|
|
|
temp = aiString(meshSrc.mShader);
|
|
|
|
temp.Append("_h.tga");
|
|
|
|
mat->AddProperty(&temp,AI_MATKEY_TEXTURE_HEIGHT(0));
|
|
|
|
|
|
|
|
// set this also as material name
|
|
|
|
mat->AddProperty(&meshSrc.mShader,AI_MATKEY_NAME);
|
|
|
|
}
|
|
|
|
else mat->AddProperty(&meshSrc.mShader,AI_MATKEY_TEXTURE_DIFFUSE(0));
|
2008-08-28 17:35:36 +00:00
|
|
|
mesh->mMaterialIndex = n++;
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
2009-03-08 15:29:34 +00:00
|
|
|
#endif
|
2008-08-06 23:01:38 +00:00
|
|
|
// delete the file again
|
2009-01-18 23:48:25 +00:00
|
|
|
UnloadFileFromMemory();
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-03-08 15:29:34 +00:00
|
|
|
// Load an MD5ANIM file
|
2008-08-06 23:01:38 +00:00
|
|
|
void MD5Importer::LoadMD5AnimFile ()
|
|
|
|
{
|
2009-03-08 15:29:34 +00:00
|
|
|
std::string pFile = mFile + "md5anim";
|
2008-08-06 23:01:38 +00:00
|
|
|
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
|
|
|
|
|
|
|
|
// Check whether we can read from the file
|
2009-03-08 15:29:34 +00:00
|
|
|
if( file.get() == NULL) {
|
|
|
|
DefaultLogger::get()->warn("Failed to read MD5ANIM file: " + pFile);
|
2008-08-06 23:01:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
LoadFileIntoMemory(file.get());
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// parse the basic file structure
|
2008-08-06 23:01:38 +00:00
|
|
|
MD5::MD5Parser parser(mBuffer,fileSize);
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// load the animation information from the parse tree
|
2008-08-06 23:01:38 +00:00
|
|
|
MD5::MD5AnimParser animParser(parser.mSections);
|
|
|
|
|
|
|
|
// generate and fill the output animation
|
2009-03-11 22:56:16 +00:00
|
|
|
if (animParser.mAnimatedBones.empty() || animParser.mFrames.empty() ||
|
|
|
|
animParser.mBaseFrames.size() != animParser.mAnimatedBones.size()) {
|
|
|
|
|
|
|
|
DefaultLogger::get()->error("MD5ANIM: No frames or animated bones loaded");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bHadMD5Anim = true;
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
pScene->mAnimations = new aiAnimation*[pScene->mNumAnimations = 1];
|
2008-08-06 23:01:38 +00:00
|
|
|
aiAnimation* anim = pScene->mAnimations[0] = new aiAnimation();
|
2008-09-30 13:05:48 +00:00
|
|
|
anim->mNumChannels = (unsigned int)animParser.mAnimatedBones.size();
|
|
|
|
anim->mChannels = new aiNodeAnim*[anim->mNumChannels];
|
2009-03-08 15:29:34 +00:00
|
|
|
for (unsigned int i = 0; i < anim->mNumChannels;++i) {
|
2008-09-30 13:05:48 +00:00
|
|
|
aiNodeAnim* node = anim->mChannels[i] = new aiNodeAnim();
|
|
|
|
node->mNodeName = aiString( animParser.mAnimatedBones[i].mName );
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// allocate storage for the keyframes
|
2009-03-11 22:56:16 +00:00
|
|
|
node->mPositionKeys = new aiVectorKey[animParser.mFrames.size()];
|
|
|
|
node->mRotationKeys = new aiQuatKey[animParser.mFrames.size()];
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1 tick == 1 frame
|
|
|
|
anim->mTicksPerSecond = animParser.fFrameRate;
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
for (FrameList::const_iterator iter = animParser.mFrames.begin(), iterEnd = animParser.mFrames.end();iter != iterEnd;++iter){
|
2008-08-06 23:01:38 +00:00
|
|
|
double dTime = (double)(*iter).iIndex;
|
2009-03-11 22:56:16 +00:00
|
|
|
aiNodeAnim** pcAnimNode = anim->mChannels;
|
|
|
|
if (!(*iter).mValues.empty() || iter == animParser.mFrames.begin()) /* be sure we have at least one frame */
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
|
|
|
// now process all values in there ... read all joints
|
|
|
|
MD5::BaseFrameDesc* pcBaseFrame = &animParser.mBaseFrames[0];
|
2009-03-08 15:29:34 +00:00
|
|
|
for (AnimBoneList::const_iterator iter2 = animParser.mAnimatedBones.begin(); iter2 != animParser.mAnimatedBones.end();++iter2,
|
|
|
|
++pcAnimNode,++pcBaseFrame)
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
2009-03-08 15:29:34 +00:00
|
|
|
if((*iter2).iFirstKeyIndex >= (*iter).mValues.size()) {
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
// Allow for empty frames
|
|
|
|
if ((*iter2).iFlags != 0) {
|
|
|
|
throw new ImportErrorException("MD5: Keyframe index is out of range");
|
2010-02-06 23:52:41 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
}
|
2010-02-06 23:52:41 +00:00
|
|
|
continue;
|
2009-03-11 22:56:16 +00:00
|
|
|
}
|
2010-02-06 23:52:41 +00:00
|
|
|
const float* fpCur = &(*iter).mValues[(*iter2).iFirstKeyIndex];
|
2008-09-30 13:05:48 +00:00
|
|
|
aiNodeAnim* pcCurAnimBone = *pcAnimNode;
|
2009-03-11 22:56:16 +00:00
|
|
|
|
|
|
|
aiVectorKey* vKey = &pcCurAnimBone->mPositionKeys[pcCurAnimBone->mNumPositionKeys++];
|
|
|
|
aiQuatKey* qKey = &pcCurAnimBone->mRotationKeys [pcCurAnimBone->mNumRotationKeys++];
|
2009-03-08 15:29:34 +00:00
|
|
|
aiVector3D vTemp;
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// translational component
|
|
|
|
for (unsigned int i = 0; i < 3; ++i) {
|
|
|
|
if ((*iter2).iFlags & (1u << i))
|
|
|
|
vKey->mValue[i] = *fpCur++;
|
|
|
|
else vKey->mValue[i] = pcBaseFrame->vPositionXYZ[i];
|
|
|
|
}
|
2009-03-11 22:56:16 +00:00
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// orientation component
|
|
|
|
for (unsigned int i = 0; i < 3; ++i) {
|
|
|
|
if ((*iter2).iFlags & (8u << i))
|
|
|
|
vTemp[i] = *fpCur++;
|
|
|
|
else vTemp[i] = pcBaseFrame->vRotationQuat[i];
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
MD5::ConvertQuaternion(vTemp, qKey->mValue);
|
|
|
|
qKey->mTime = vKey->mTime = dTime;
|
2009-11-19 23:22:53 +00:00
|
|
|
|
|
|
|
// we need this to get to Assimp quaternion conventions
|
|
|
|
qKey->mValue.w *= -1.f;
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-11 22:56:16 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// compute the duration of the animation
|
|
|
|
anim->mDuration = std::max(dTime,anim->mDuration);
|
|
|
|
}
|
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// If we didn't build the hierarchy yet (== we didn't load a MD5MESH),
|
|
|
|
// construct it now from the data given in the MD5ANIM.
|
|
|
|
if (!pScene->mRootNode) {
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
pScene->mRootNode->mName.Set("<MD5_Hierarchy>");
|
2009-03-15 00:40:30 +00:00
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
AttachChilds_Anim(-1,pScene->mRootNode,animParser.mAnimatedBones,(const aiNodeAnim**)anim->mChannels);
|
|
|
|
|
|
|
|
// Call SkeletonMeshBuilder to construct a mesh to represent the shape
|
|
|
|
if (pScene->mRootNode->mNumChildren) {
|
|
|
|
SkeletonMeshBuilder skeleton_maker(pScene,pScene->mRootNode->mChildren[0]);
|
|
|
|
}
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
// delete the file again
|
2009-01-18 23:48:25 +00:00
|
|
|
UnloadFileFromMemory();
|
2008-09-12 20:25:11 +00:00
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
2009-03-08 15:29:34 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Load an MD5CAMERA file
|
|
|
|
void MD5Importer::LoadMD5CameraFile ()
|
|
|
|
{
|
|
|
|
std::string pFile = mFile + "md5camera";
|
|
|
|
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
|
|
|
|
|
|
|
|
// Check whether we can read from the file
|
|
|
|
if( file.get() == NULL) {
|
2009-03-08 20:12:43 +00:00
|
|
|
throw new ImportErrorException("Failed to read MD5CAMERA file: " + pFile);
|
2009-03-08 15:29:34 +00:00
|
|
|
}
|
|
|
|
bHadMD5Camera = true;
|
|
|
|
LoadFileIntoMemory(file.get());
|
|
|
|
|
|
|
|
// parse the basic file structure
|
|
|
|
MD5::MD5Parser parser(mBuffer,fileSize);
|
|
|
|
|
|
|
|
// load the camera animation data from the parse tree
|
|
|
|
MD5::MD5CameraParser cameraParser(parser.mSections);
|
2009-03-08 20:12:43 +00:00
|
|
|
|
|
|
|
if (cameraParser.frames.empty())
|
|
|
|
throw new ImportErrorException("MD5CAMERA: No frames parsed");
|
|
|
|
|
|
|
|
std::vector<unsigned int>& cuts = cameraParser.cuts;
|
|
|
|
std::vector<MD5::CameraAnimFrameDesc>& frames = cameraParser.frames;
|
|
|
|
|
2009-03-15 00:40:30 +00:00
|
|
|
// Construct output graph - a simple root with a dummy child.
|
|
|
|
// The root node performs the coordinate system conversion
|
|
|
|
aiNode* root = pScene->mRootNode = new aiNode("<MD5CameraRoot>");
|
|
|
|
root->mChildren = new aiNode*[root->mNumChildren = 1];
|
|
|
|
root->mChildren[0] = new aiNode("<MD5Camera>");
|
|
|
|
root->mChildren[0]->mParent = root;
|
2009-03-08 20:12:43 +00:00
|
|
|
|
|
|
|
// ... but with one camera assigned to it
|
|
|
|
pScene->mCameras = new aiCamera*[pScene->mNumCameras = 1];
|
|
|
|
aiCamera* cam = pScene->mCameras[0] = new aiCamera();
|
2009-03-15 00:40:30 +00:00
|
|
|
cam->mName = "<MD5Camera>";
|
2009-03-08 20:12:43 +00:00
|
|
|
|
|
|
|
// FIXME: Fov is currently set to the first frame's value
|
|
|
|
cam->mHorizontalFOV = AI_DEG_TO_RAD( frames.front().fFOV );
|
|
|
|
|
|
|
|
// every cut is written to a separate aiAnimation
|
|
|
|
if (!cuts.size()) {
|
|
|
|
cuts.push_back(0);
|
|
|
|
cuts.push_back(frames.size()-1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cuts.insert(cuts.begin(),0);
|
|
|
|
|
|
|
|
if (cuts.back() < frames.size()-1)
|
|
|
|
cuts.push_back(frames.size()-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pScene->mNumAnimations = cuts.size()-1;
|
|
|
|
aiAnimation** tmp = pScene->mAnimations = new aiAnimation*[pScene->mNumAnimations];
|
|
|
|
for (std::vector<unsigned int>::const_iterator it = cuts.begin(); it != cuts.end()-1; ++it) {
|
|
|
|
|
|
|
|
aiAnimation* anim = *tmp++ = new aiAnimation();
|
2010-02-06 23:52:41 +00:00
|
|
|
anim->mName.length = ::sprintf(anim->mName.data,"anim%u_from_%u_to_%u",(unsigned int)(it-cuts.begin()),(*it),*(it+1));
|
2009-03-08 20:12:43 +00:00
|
|
|
|
|
|
|
anim->mTicksPerSecond = cameraParser.fFrameRate;
|
|
|
|
anim->mChannels = new aiNodeAnim*[anim->mNumChannels = 1];
|
|
|
|
aiNodeAnim* nd = anim->mChannels[0] = new aiNodeAnim();
|
|
|
|
nd->mNodeName.Set("<MD5Camera>");
|
|
|
|
|
|
|
|
nd->mNumPositionKeys = nd->mNumRotationKeys = *(it+1) - (*it);
|
|
|
|
nd->mPositionKeys = new aiVectorKey[nd->mNumPositionKeys];
|
|
|
|
nd->mRotationKeys = new aiQuatKey [nd->mNumRotationKeys];
|
|
|
|
for (unsigned int i = 0; i < nd->mNumPositionKeys; ++i) {
|
|
|
|
|
|
|
|
nd->mPositionKeys[i].mValue = frames[*it+i].vPositionXYZ;
|
|
|
|
MD5::ConvertQuaternion(frames[*it+i].vRotationQuat,nd->mRotationKeys[i].mValue);
|
|
|
|
nd->mRotationKeys[i].mTime = nd->mPositionKeys[i].mTime = *it+i;
|
|
|
|
}
|
|
|
|
}
|
2009-03-08 15:29:34 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
#endif // !! ASSIMP_BUILD_NO_MD5_IMPORTER
|