2008-05-09 17:24:28 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2008-05-09 17:24:28 +00:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
Copyright (c) 2006-2012, assimp team
|
2008-05-09 17:24:28 +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
|
|
|
|
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.
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2008-05-09 17:24:28 +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-05-09 17:24:28 +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.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
#include "AssimpPCH.h"
|
2009-01-18 23:48:25 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_MD2_IMPORTER
|
2008-10-13 16:45:48 +00:00
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
/** @file Implementation of the MD2 importer class */
|
|
|
|
#include "MD2Loader.h"
|
2008-08-08 23:58:40 +00:00
|
|
|
#include "ByteSwap.h"
|
2008-06-22 10:09:26 +00:00
|
|
|
#include "MD2NormalTable.h" // shouldn't be included by other units
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
2008-06-22 10:09:26 +00:00
|
|
|
using namespace Assimp::MD2;
|
|
|
|
|
|
|
|
|
|
|
|
// helper macro to determine the size of an array
|
|
|
|
#if (!defined ARRAYSIZE)
|
|
|
|
# define ARRAYSIZE(_array) (int(sizeof(_array) / sizeof(_array[0])))
|
|
|
|
#endif
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-06-22 10:09:26 +00:00
|
|
|
// Helper function to lookup a normal in Quake 2's precalculated table
|
|
|
|
void MD2::LookupNormalIndex(uint8_t iNormalIndex,aiVector3D& vOut)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-06-22 10:09:26 +00:00
|
|
|
// make sure the normal index has a valid value
|
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
|
|
|
if (iNormalIndex >= ARRAYSIZE(g_avNormals)) {
|
|
|
|
DefaultLogger::get()->warn("Index overflow in Quake II normal vector list");
|
2008-06-22 10:09:26 +00:00
|
|
|
iNormalIndex = ARRAYSIZE(g_avNormals) - 1;
|
|
|
|
}
|
|
|
|
vOut = *((const aiVector3D*)(&g_avNormals[iNormalIndex]));
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
2008-06-22 10:09:26 +00:00
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
MD2Importer::MD2Importer()
|
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-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
MD2Importer::~MD2Importer()
|
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 MD2Importer::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
|
2008-05-05 12:36:31 +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);
|
|
|
|
if (extension == "md2")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// if check for extension is not enough, check for the magic tokens
|
|
|
|
if (!extension.length() || checkSig) {
|
|
|
|
uint32_t tokens[1];
|
|
|
|
tokens[0] = AI_MD2_MAGIC_NUMBER_LE;
|
|
|
|
return CheckMagicToken(pIOHandler,pFile,tokens,1);
|
|
|
|
}
|
|
|
|
return false;
|
2008-05-05 12:36:31 +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 a list of all extensions supported by this loader
|
2010-03-02 17:38:01 +00:00
|
|
|
void MD2Importer::GetExtensionList(std::set<std::string>& extensions)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2010-03-02 17:38:01 +00:00
|
|
|
extensions.insert("md2");
|
2008-06-22 10:09:26 +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-06-22 10:09:26 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-08-08 23:58:40 +00:00
|
|
|
// Setup configuration properties
|
|
|
|
void MD2Importer::SetupProperties(const Importer* pImp)
|
|
|
|
{
|
2009-02-12 16:44:12 +00:00
|
|
|
// The
|
|
|
|
// AI_CONFIG_IMPORT_MD2_KEYFRAME option overrides the
|
2008-08-08 23:58:40 +00:00
|
|
|
// AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option.
|
2011-04-22 21:29:18 +00:00
|
|
|
configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_MD2_KEYFRAME,-1);
|
|
|
|
if(static_cast<unsigned int>(-1) == configFrameID){
|
2008-10-13 16:45:48 +00:00
|
|
|
configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME,0);
|
2008-08-08 23:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-06-22 10:09:26 +00:00
|
|
|
// Validate the file header
|
|
|
|
void MD2Importer::ValidateHeader( )
|
|
|
|
{
|
2008-08-08 18:29:05 +00:00
|
|
|
// check magic number
|
2008-10-13 16:45:48 +00:00
|
|
|
if (m_pcHeader->magic != AI_MD2_MAGIC_NUMBER_BE &&
|
|
|
|
m_pcHeader->magic != AI_MD2_MAGIC_NUMBER_LE)
|
2008-08-08 18:29:05 +00:00
|
|
|
{
|
|
|
|
char szBuffer[5];
|
2008-10-13 16:45:48 +00:00
|
|
|
szBuffer[0] = ((char*)&m_pcHeader->magic)[0];
|
|
|
|
szBuffer[1] = ((char*)&m_pcHeader->magic)[1];
|
|
|
|
szBuffer[2] = ((char*)&m_pcHeader->magic)[2];
|
|
|
|
szBuffer[3] = ((char*)&m_pcHeader->magic)[3];
|
2008-08-08 18:29:05 +00:00
|
|
|
szBuffer[4] = '\0';
|
|
|
|
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("Invalid MD2 magic word: should be IDP2, the "
|
2008-08-08 18:29:05 +00:00
|
|
|
"magic word found is " + std::string(szBuffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
// check file format version
|
2008-10-13 16:45:48 +00:00
|
|
|
if (m_pcHeader->version != 8)
|
2008-08-08 18:29:05 +00:00
|
|
|
DefaultLogger::get()->warn( "Unsupported md2 file version. Continuing happily ...");
|
|
|
|
|
2008-08-08 23:58:40 +00:00
|
|
|
// check some values whether they are valid
|
2008-10-13 16:45:48 +00:00
|
|
|
if (0 == m_pcHeader->numFrames)
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "Invalid md2 file: NUM_FRAMES is 0");
|
2008-08-08 23:58:40 +00:00
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
if (m_pcHeader->offsetEnd > (uint32_t)fileSize)
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "Invalid md2 file: File is too small");
|
2008-08-08 23:58:40 +00:00
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
if (m_pcHeader->offsetSkins + m_pcHeader->numSkins * sizeof (MD2::Skin) >= fileSize ||
|
|
|
|
m_pcHeader->offsetTexCoords + m_pcHeader->numTexCoords * sizeof (MD2::TexCoord) >= fileSize ||
|
|
|
|
m_pcHeader->offsetTriangles + m_pcHeader->numTriangles * sizeof (MD2::Triangle) >= fileSize ||
|
|
|
|
m_pcHeader->offsetFrames + m_pcHeader->numFrames * sizeof (MD2::Frame) >= fileSize ||
|
|
|
|
m_pcHeader->offsetEnd > fileSize)
|
2008-06-22 10:09:26 +00:00
|
|
|
{
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("Invalid MD2 header: some offsets are outside the file");
|
2008-06-22 10:09:26 +00:00
|
|
|
}
|
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
if (m_pcHeader->numSkins > AI_MD2_MAX_SKINS)
|
2008-06-22 10:09:26 +00:00
|
|
|
DefaultLogger::get()->warn("The model contains more skins than Quake 2 supports");
|
2008-10-13 16:45:48 +00:00
|
|
|
if ( m_pcHeader->numFrames > AI_MD2_MAX_FRAMES)
|
2008-06-22 10:09:26 +00:00
|
|
|
DefaultLogger::get()->warn("The model contains more frames than Quake 2 supports");
|
2008-10-13 16:45:48 +00:00
|
|
|
if (m_pcHeader->numVertices > AI_MD2_MAX_VERTS)
|
2008-06-22 10:09:26 +00:00
|
|
|
DefaultLogger::get()->warn("The model contains more vertices than Quake 2 supports");
|
2008-08-08 23:58:40 +00:00
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
if (m_pcHeader->numFrames <= configFrameID )
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("The requested frame is not existing the file");
|
2008-05-05 12:36:31 +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-05-05 12:36:31 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Imports the given file into the given scene structure.
|
2008-08-08 23:58:40 +00:00
|
|
|
void MD2Importer::InternReadFile( const std::string& pFile,
|
|
|
|
aiScene* pScene, IOSystem* pIOHandler)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
|
|
|
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
|
|
|
|
|
|
|
|
// Check whether we can read from the file
|
|
|
|
if( file.get() == NULL)
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "Failed to open MD2 file " + pFile + "");
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// check whether the md3 file is large enough to contain
|
|
|
|
// at least the file header
|
2008-06-22 10:09:26 +00:00
|
|
|
fileSize = (unsigned int)file->FileSize();
|
2008-05-05 12:36:31 +00:00
|
|
|
if( fileSize < sizeof(MD2::Header))
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "MD2 File is too small");
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-10-19 11:32:33 +00:00
|
|
|
std::vector<uint8_t> mBuffer2(fileSize);
|
2008-08-28 17:35:36 +00:00
|
|
|
file->Read(&mBuffer2[0], 1, fileSize);
|
2008-10-13 16:45:48 +00:00
|
|
|
mBuffer = &mBuffer2[0];
|
2008-08-28 17:35:36 +00:00
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-10-19 11:32:33 +00:00
|
|
|
m_pcHeader = (BE_NCONST MD2::Header*)mBuffer;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-08 23:58:40 +00:00
|
|
|
#ifdef AI_BUILD_BIG_ENDIAN
|
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
ByteSwap::Swap4(&m_pcHeader->frameSize);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->magic);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->numFrames);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->numGlCommands);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->numSkins);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->numTexCoords);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->numTriangles);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->numVertices);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->offsetEnd);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->offsetFrames);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->offsetGlCommands);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->offsetSkins);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->offsetTexCoords);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->offsetTriangles);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->skinHeight);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->skinWidth);
|
|
|
|
ByteSwap::Swap4(&m_pcHeader->version);
|
2008-08-08 23:58:40 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
ValidateHeader();
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// there won't be more than one mesh inside the file
|
|
|
|
pScene->mNumMaterials = 1;
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
pScene->mRootNode->mNumMeshes = 1;
|
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[1];
|
|
|
|
pScene->mRootNode->mMeshes[0] = 0;
|
|
|
|
pScene->mMaterials = new aiMaterial*[1];
|
2011-08-22 20:22:51 +00:00
|
|
|
pScene->mMaterials[0] = new aiMaterial();
|
2008-08-28 17:35:36 +00:00
|
|
|
pScene->mNumMeshes = 1;
|
|
|
|
pScene->mMeshes = new aiMesh*[1];
|
2008-09-30 20:20:56 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
aiMesh* pcMesh = pScene->mMeshes[0] = new aiMesh();
|
2008-09-30 20:20:56 +00:00
|
|
|
pcMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// navigate to the begin of the frame data
|
2008-10-19 11:32:33 +00:00
|
|
|
BE_NCONST MD2::Frame* pcFrame = (BE_NCONST MD2::Frame*) ((uint8_t*)
|
2008-10-13 16:45:48 +00:00
|
|
|
m_pcHeader + m_pcHeader->offsetFrames);
|
2008-09-30 20:20:56 +00:00
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
pcFrame += configFrameID;
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// navigate to the begin of the triangle data
|
|
|
|
MD2::Triangle* pcTriangles = (MD2::Triangle*) ((uint8_t*)
|
2008-10-13 16:45:48 +00:00
|
|
|
m_pcHeader + m_pcHeader->offsetTriangles);
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// navigate to the begin of the tex coords data
|
2008-10-19 11:32:33 +00:00
|
|
|
BE_NCONST MD2::TexCoord* pcTexCoords = (BE_NCONST MD2::TexCoord*) ((uint8_t*)
|
2008-10-13 16:45:48 +00:00
|
|
|
m_pcHeader + m_pcHeader->offsetTexCoords);
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// navigate to the begin of the vertex data
|
2008-10-19 11:32:33 +00:00
|
|
|
BE_NCONST MD2::Vertex* pcVerts = (BE_NCONST MD2::Vertex*) (pcFrame->vertices);
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-08-08 23:58:40 +00:00
|
|
|
#ifdef AI_BUILD_BIG_ENDIAN
|
2008-10-28 23:18:06 +00:00
|
|
|
for (uint32_t i = 0; i< m_pcHeader->numTriangles; ++i)
|
2008-08-28 17:35:36 +00:00
|
|
|
{
|
|
|
|
for (unsigned int p = 0; p < 3;++p)
|
2008-08-08 23:58:40 +00:00
|
|
|
{
|
2008-08-28 17:35:36 +00:00
|
|
|
ByteSwap::Swap2(& pcTriangles[i].textureIndices[p]);
|
|
|
|
ByteSwap::Swap2(& pcTriangles[i].vertexIndices[p]);
|
2008-08-08 23:58:40 +00:00
|
|
|
}
|
2008-08-28 17:35:36 +00:00
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < m_pcHeader->offsetTexCoords;++i)
|
|
|
|
{
|
|
|
|
ByteSwap::Swap2(& pcTexCoords[i].s);
|
|
|
|
ByteSwap::Swap2(& pcTexCoords[i].t);
|
|
|
|
}
|
|
|
|
ByteSwap::Swap4( & pcFrame->scale[0] );
|
|
|
|
ByteSwap::Swap4( & pcFrame->scale[1] );
|
|
|
|
ByteSwap::Swap4( & pcFrame->scale[2] );
|
|
|
|
ByteSwap::Swap4( & pcFrame->translate[0] );
|
|
|
|
ByteSwap::Swap4( & pcFrame->translate[1] );
|
|
|
|
ByteSwap::Swap4( & pcFrame->translate[2] );
|
2008-08-08 23:58:40 +00:00
|
|
|
#endif
|
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
pcMesh->mNumFaces = m_pcHeader->numTriangles;
|
|
|
|
pcMesh->mFaces = new aiFace[m_pcHeader->numTriangles];
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// allocate output storage
|
|
|
|
pcMesh->mNumVertices = (unsigned int)pcMesh->mNumFaces*3;
|
|
|
|
pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices];
|
|
|
|
pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices];
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2009-02-12 15:58:55 +00:00
|
|
|
// Not sure whether there are MD2 files without texture coordinates
|
2008-08-28 17:35:36 +00:00
|
|
|
// NOTE: texture coordinates can be there without a texture,
|
|
|
|
// but a texture can't be there without a valid UV channel
|
2011-08-22 20:22:51 +00:00
|
|
|
aiMaterial* pcHelper = (aiMaterial*)pScene->mMaterials[0];
|
2009-02-12 15:58:55 +00:00
|
|
|
const int iMode = (int)aiShadingMode_Gouraud;
|
|
|
|
pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
|
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
if (m_pcHeader->numTexCoords && m_pcHeader->numSkins)
|
2008-08-28 17:35:36 +00:00
|
|
|
{
|
|
|
|
// navigate to the first texture associated with the mesh
|
2008-10-13 16:45:48 +00:00
|
|
|
const MD2::Skin* pcSkins = (const MD2::Skin*) ((unsigned char*)m_pcHeader +
|
|
|
|
m_pcHeader->offsetSkins);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
aiColor3D clr;
|
|
|
|
clr.b = clr.g = clr.r = 1.0f;
|
|
|
|
pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
clr.b = clr.g = clr.r = 0.05f;
|
|
|
|
pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
if (pcSkins->name[0])
|
|
|
|
{
|
|
|
|
aiString szString;
|
|
|
|
const size_t iLen = ::strlen(pcSkins->name);
|
|
|
|
::memcpy(szString.data,pcSkins->name,iLen);
|
|
|
|
szString.data[iLen] = '\0';
|
|
|
|
szString.length = iLen;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
pcHelper->AddProperty(&szString,AI_MATKEY_TEXTURE_DIFFUSE(0));
|
2008-06-22 10:09:26 +00:00
|
|
|
}
|
2009-02-12 15:58:55 +00:00
|
|
|
else{
|
2008-08-28 17:35:36 +00:00
|
|
|
DefaultLogger::get()->warn("Texture file name has zero length. It will be skipped.");
|
2008-06-22 10:09:26 +00:00
|
|
|
}
|
2008-08-28 17:35:36 +00:00
|
|
|
}
|
2009-03-18 22:01:11 +00:00
|
|
|
else {
|
2008-08-28 17:35:36 +00:00
|
|
|
// apply a default material
|
|
|
|
aiColor3D clr;
|
|
|
|
clr.b = clr.g = clr.r = 0.6f;
|
|
|
|
pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
|
|
|
|
|
|
|
|
clr.b = clr.g = clr.r = 0.05f;
|
|
|
|
pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
|
|
|
|
|
|
|
|
aiString szName;
|
2011-10-15 10:28:36 +00:00
|
|
|
szName.Set(AI_DEFAULT_MATERIAL_NAME);
|
2008-08-28 17:35:36 +00:00
|
|
|
pcHelper->AddProperty(&szName,AI_MATKEY_NAME);
|
2009-02-12 15:58:55 +00:00
|
|
|
|
|
|
|
aiString sz;
|
|
|
|
|
|
|
|
// TODO: Try to guess the name of the texture file from the model file name
|
|
|
|
|
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
|
|
|
sz.Set("$texture_dummy.bmp");
|
2009-02-12 15:58:55 +00:00
|
|
|
pcHelper->AddProperty(&sz,AI_MATKEY_TEXTURE_DIFFUSE(0));
|
2008-08-28 17:35:36 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// now read all triangles of the first frame, apply scaling and translation
|
|
|
|
unsigned int iCurrent = 0;
|
2008-06-22 10:09:26 +00:00
|
|
|
|
2009-02-12 15:58:55 +00:00
|
|
|
float fDivisorU = 1.0f,fDivisorV = 1.0f;
|
2009-03-18 22:01:11 +00:00
|
|
|
if (m_pcHeader->numTexCoords) {
|
2008-08-28 17:35:36 +00:00
|
|
|
// allocate storage for texture coordinates, too
|
|
|
|
pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices];
|
|
|
|
pcMesh->mNumUVComponents[0] = 2;
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// check whether the skin width or height are zero (this would
|
|
|
|
// cause a division through zero)
|
2009-02-12 15:58:55 +00:00
|
|
|
if (!m_pcHeader->skinWidth) {
|
|
|
|
DefaultLogger::get()->error("MD2: No valid skin width given");
|
2008-08-28 17:35:36 +00:00
|
|
|
}
|
2008-10-13 16:45:48 +00:00
|
|
|
else fDivisorU = (float)m_pcHeader->skinWidth;
|
2009-02-12 15:58:55 +00:00
|
|
|
if (!m_pcHeader->skinHeight){
|
|
|
|
DefaultLogger::get()->error("MD2: No valid skin height given");
|
2008-06-22 10:09:26 +00:00
|
|
|
}
|
2008-10-13 16:45:48 +00:00
|
|
|
else fDivisorV = (float)m_pcHeader->skinHeight;
|
2008-08-28 17:35:36 +00:00
|
|
|
}
|
2008-05-25 22:29:05 +00:00
|
|
|
|
2009-03-18 22:01:11 +00:00
|
|
|
for (unsigned int i = 0; i < (unsigned int)m_pcHeader->numTriangles;++i) {
|
2009-02-12 15:58:55 +00:00
|
|
|
// Allocate the face
|
2008-08-28 17:35:36 +00:00
|
|
|
pScene->mMeshes[0]->mFaces[i].mIndices = new unsigned int[3];
|
|
|
|
pScene->mMeshes[0]->mFaces[i].mNumIndices = 3;
|
|
|
|
|
|
|
|
// copy texture coordinates
|
|
|
|
// check whether they are different from the previous value at this index.
|
|
|
|
// In this case, create a full separate set of vertices/normals/texcoords
|
2009-03-18 22:01:11 +00:00
|
|
|
for (unsigned int c = 0; c < 3;++c,++iCurrent) {
|
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// validate vertex indices
|
2008-10-13 16:45:48 +00:00
|
|
|
register unsigned int iIndex = (unsigned int)pcTriangles[i].vertexIndices[c];
|
2009-03-18 22:01:11 +00:00
|
|
|
if (iIndex >= m_pcHeader->numVertices) {
|
2008-10-13 16:45:48 +00:00
|
|
|
DefaultLogger::get()->error("MD2: Vertex index is outside the allowed range");
|
|
|
|
iIndex = m_pcHeader->numVertices-1;
|
2008-08-28 17:35:36 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// read x,y, and z component of the vertex
|
|
|
|
aiVector3D& vec = pcMesh->mVertices[iCurrent];
|
2008-05-09 17:24:28 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
vec.x = (float)pcVerts[iIndex].vertex[0] * pcFrame->scale[0];
|
|
|
|
vec.x += pcFrame->translate[0];
|
2008-05-09 17:24:28 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
vec.y = (float)pcVerts[iIndex].vertex[1] * pcFrame->scale[1];
|
|
|
|
vec.y += pcFrame->translate[1];
|
2008-05-09 17:24:28 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
vec.z = (float)pcVerts[iIndex].vertex[2] * pcFrame->scale[2];
|
|
|
|
vec.z += pcFrame->translate[2];
|
2008-05-09 17:24:28 +00:00
|
|
|
|
2008-08-28 17:35:36 +00:00
|
|
|
// read the normal vector from the precalculated normal table
|
|
|
|
aiVector3D& vNormal = pcMesh->mNormals[iCurrent];
|
|
|
|
LookupNormalIndex(pcVerts[iIndex].lightNormalIndex,vNormal);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-18 22:01:11 +00:00
|
|
|
// flip z and y to become right-handed
|
|
|
|
std::swap((float&)vNormal.z,(float&)vNormal.y);
|
|
|
|
std::swap((float&)vec.z,(float&)vec.y);
|
2009-02-12 15:58:55 +00:00
|
|
|
|
|
|
|
if (m_pcHeader->numTexCoords) {
|
2008-08-28 17:35:36 +00:00
|
|
|
// validate texture coordinates
|
2009-02-12 15:58:55 +00:00
|
|
|
iIndex = pcTriangles[i].textureIndices[c];
|
|
|
|
if (iIndex >= m_pcHeader->numTexCoords) {
|
2008-10-13 16:45:48 +00:00
|
|
|
DefaultLogger::get()->error("MD2: UV index is outside the allowed range");
|
|
|
|
iIndex = m_pcHeader->numTexCoords-1;
|
2008-06-22 10:09:26 +00:00
|
|
|
}
|
2008-08-28 17:35:36 +00:00
|
|
|
|
|
|
|
aiVector3D& pcOut = pcMesh->mTextureCoords[0][iCurrent];
|
|
|
|
|
|
|
|
// the texture coordinates are absolute values but we
|
|
|
|
// need relative values between 0 and 1
|
2009-02-12 15:58:55 +00:00
|
|
|
pcOut.x = pcTexCoords[iIndex].s / fDivisorU;
|
|
|
|
pcOut.y = 1.f-pcTexCoords[iIndex].t / fDivisorV;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-02-12 15:58:55 +00:00
|
|
|
pScene->mMeshes[0]->mFaces[i].mIndices[c] = iCurrent;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-08 18:29:05 +00:00
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_MD2_IMPORTER
|