2008-11-04 20:41:11 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (ASSIMP)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2010-04-10 15:30:22 +00:00
|
|
|
Copyright (c) 2006-2010, ASSIMP Development Team
|
2008-11-04 20:41:11 +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.
|
|
|
|
|
|
|
|
* 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 B3DImporter.cpp
|
|
|
|
* @brief Implementation of the b3d importer class
|
|
|
|
*/
|
2008-11-04 20:41:11 +00:00
|
|
|
|
|
|
|
#include "AssimpPCH.h"
|
2009-01-18 23:48:25 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_B3D_IMPORTER
|
2008-11-04 20:41:11 +00:00
|
|
|
|
|
|
|
// internal headers
|
|
|
|
#include "B3DImporter.h"
|
|
|
|
#include "TextureTransform.h"
|
2009-03-15 00:40:30 +00:00
|
|
|
#include "ConvertToLHProcess.h"
|
2008-11-04 20:41:11 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
using namespace std;
|
|
|
|
|
2009-08-21 11:26:54 +00:00
|
|
|
// (fixme, Aramis) quick workaround to get rid of all those signed to unsigned warnings
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# pragma warning (disable: 4018)
|
|
|
|
#endif
|
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
//#define DEBUG_B3D
|
|
|
|
|
2009-01-18 23:48:25 +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
|
|
|
bool B3DImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const{
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2010-02-06 23:52:41 +00:00
|
|
|
size_t pos=pFile.find_last_of( '.' );
|
2008-11-04 20:41:11 +00:00
|
|
|
if( pos==string::npos ) return false;
|
|
|
|
|
|
|
|
string ext=pFile.substr( pos+1 );
|
|
|
|
if( ext.size()!=3 ) return false;
|
|
|
|
|
|
|
|
return (ext[0]=='b' || ext[0]=='B') && (ext[1]=='3') && (ext[2]=='d' || ext[2]=='D');
|
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-03-02 17:38:01 +00:00
|
|
|
void B3DImporter::GetExtensionList( std::set<std::string>& extensions ){
|
|
|
|
extensions.insert("b3d");
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
#ifdef DEBUG_B3D
|
|
|
|
extern "C"{ void _stdcall AllocConsole(); }
|
|
|
|
#endif
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
void B3DImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler){
|
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
#ifdef DEBUG_B3D
|
|
|
|
AllocConsole();
|
|
|
|
freopen( "conin$","r",stdin );
|
|
|
|
freopen( "conout$","w",stdout );
|
|
|
|
freopen( "conout$","w",stderr );
|
|
|
|
cout<<"Hello world from the B3DImporter!"<<endl;
|
|
|
|
#endif
|
|
|
|
|
2008-11-04 20:41:11 +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 B3D file " + pFile + ".");
|
2008-11-04 20:41:11 +00:00
|
|
|
|
|
|
|
// check whether the .b3d file is large enough to contain
|
|
|
|
// at least one chunk.
|
|
|
|
size_t fileSize = file->FileSize();
|
2010-03-18 17:00:12 +00:00
|
|
|
if( fileSize<8 ) throw DeadlyImportError( "B3D File is too small.");
|
2008-11-04 20:41:11 +00:00
|
|
|
|
|
|
|
_pos=0;
|
|
|
|
_buf.resize( fileSize );
|
|
|
|
file->Read( &_buf[0],1,fileSize );
|
|
|
|
_stack.clear();
|
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
ReadBB3D( pScene );
|
|
|
|
}
|
2009-03-15 00:40:30 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::Oops(){
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "B3D Importer - INTERNAL ERROR" );
|
2009-06-02 05:08:24 +00:00
|
|
|
}
|
2009-04-02 15:16:01 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::Fail( string str ){
|
|
|
|
#ifdef DEBUG_B3D
|
|
|
|
cout<<"Error in B3D file data: "<<str<<endl;
|
|
|
|
#endif
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "B3D Importer - error in B3D file data: "+str );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
int B3DImporter::ReadByte(){
|
|
|
|
if( _pos<_buf.size() ) return _buf[_pos++];
|
2009-06-02 05:08:24 +00:00
|
|
|
Fail( "EOF" );
|
2009-08-01 13:57:29 +00:00
|
|
|
return 0;
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
int B3DImporter::ReadInt(){
|
|
|
|
if( _pos+4<=_buf.size() ){
|
|
|
|
int n=*(int*)&_buf[_pos];
|
|
|
|
_pos+=4;
|
|
|
|
return n;
|
|
|
|
}
|
2009-06-02 05:08:24 +00:00
|
|
|
Fail( "EOF" );
|
2009-08-01 13:57:29 +00:00
|
|
|
return 0;
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
float B3DImporter::ReadFloat(){
|
|
|
|
if( _pos+4<=_buf.size() ){
|
|
|
|
float n=*(float*)&_buf[_pos];
|
|
|
|
_pos+=4;
|
|
|
|
return n;
|
|
|
|
}
|
2009-06-02 05:08:24 +00:00
|
|
|
Fail( "EOF" );
|
2009-08-01 13:57:29 +00:00
|
|
|
return 0.0f;
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-06-02 05:08:24 +00:00
|
|
|
aiVector2D B3DImporter::ReadVec2(){
|
|
|
|
float x=ReadFloat();
|
|
|
|
float y=ReadFloat();
|
|
|
|
return aiVector2D( x,y );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-06-02 05:08:24 +00:00
|
|
|
aiVector3D B3DImporter::ReadVec3(){
|
|
|
|
float x=ReadFloat();
|
|
|
|
float y=ReadFloat();
|
|
|
|
float z=ReadFloat();
|
|
|
|
return aiVector3D( x,y,z );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-06-02 05:08:24 +00:00
|
|
|
aiQuaternion B3DImporter::ReadQuat(){
|
2010-04-24 00:18:34 +00:00
|
|
|
// (aramis_acg) Fix to adapt the loader to changed quat orientation
|
|
|
|
float w=-ReadFloat();
|
2009-06-02 05:08:24 +00:00
|
|
|
float x=ReadFloat();
|
|
|
|
float y=ReadFloat();
|
|
|
|
float z=ReadFloat();
|
|
|
|
return aiQuaternion( w,x,y,z );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
string B3DImporter::ReadString(){
|
|
|
|
string str;
|
|
|
|
while( _pos<_buf.size() ){
|
|
|
|
char c=(char)ReadByte();
|
|
|
|
if( !c ) return str;
|
|
|
|
str+=c;
|
|
|
|
}
|
2009-06-02 05:08:24 +00:00
|
|
|
Fail( "EOF" );
|
2009-08-01 13:57:29 +00:00
|
|
|
return string();
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
string B3DImporter::ReadChunk(){
|
|
|
|
string tag;
|
|
|
|
for( int i=0;i<4;++i ){
|
|
|
|
tag+=char( ReadByte() );
|
|
|
|
}
|
2009-06-02 05:08:24 +00:00
|
|
|
#ifdef DEBUG_B3D
|
2008-11-04 20:41:11 +00:00
|
|
|
// cout<<"ReadChunk:"<<tag<<endl;
|
2009-06-02 05:08:24 +00:00
|
|
|
#endif
|
2008-11-04 20:41:11 +00:00
|
|
|
unsigned sz=(unsigned)ReadInt();
|
|
|
|
_stack.push_back( _pos+sz );
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
void B3DImporter::ExitChunk(){
|
|
|
|
_pos=_stack.back();
|
|
|
|
_stack.pop_back();
|
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
unsigned B3DImporter::ChunkSize(){
|
|
|
|
return _stack.back()-_pos;
|
|
|
|
}
|
2009-06-02 05:08:24 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T *B3DImporter::to_array( const vector<T> &v ){
|
|
|
|
if( !v.size() ) return 0;
|
|
|
|
T *p=new T[v.size()];
|
2010-02-06 23:52:41 +00:00
|
|
|
for( size_t i=0;i<v.size();++i ){
|
2009-06-02 05:08:24 +00:00
|
|
|
p[i]=v[i];
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
void B3DImporter::ReadTEXS(){
|
|
|
|
while( ChunkSize() ){
|
|
|
|
string name=ReadString();
|
2010-02-06 23:52:41 +00:00
|
|
|
/*int flags=*/ReadInt();
|
|
|
|
/*int blend=*/ReadInt();
|
|
|
|
/*aiVector2D pos=*/ReadVec2();
|
|
|
|
/*aiVector2D scale=*/ReadVec2();
|
|
|
|
/*float rot=*/ReadFloat();
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
_textures.push_back( name );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
void B3DImporter::ReadBRUS(){
|
|
|
|
int n_texs=ReadInt();
|
2009-06-02 05:08:24 +00:00
|
|
|
if( n_texs<0 || n_texs>8 ){
|
|
|
|
Fail( "Bad texture count" );
|
|
|
|
}
|
2008-11-04 20:41:11 +00:00
|
|
|
while( ChunkSize() ){
|
|
|
|
string name=ReadString();
|
2009-06-02 05:08:24 +00:00
|
|
|
aiVector3D color=ReadVec3();
|
|
|
|
float alpha=ReadFloat();
|
2008-11-04 20:41:11 +00:00
|
|
|
float shiny=ReadFloat();
|
2010-02-06 23:52:41 +00:00
|
|
|
/*int blend=**/ReadInt();
|
2008-11-04 20:41:11 +00:00
|
|
|
int fx=ReadInt();
|
|
|
|
|
|
|
|
MaterialHelper *mat=new MaterialHelper;
|
|
|
|
_materials.push_back( mat );
|
2009-01-11 21:51:45 +00:00
|
|
|
|
|
|
|
// Name
|
|
|
|
aiString ainame( name );
|
|
|
|
mat->AddProperty( &ainame,AI_MATKEY_NAME );
|
|
|
|
|
|
|
|
// Diffuse color
|
2009-06-02 05:08:24 +00:00
|
|
|
mat->AddProperty( &color,1,AI_MATKEY_COLOR_DIFFUSE );
|
2009-01-11 21:51:45 +00:00
|
|
|
|
|
|
|
// Opacity
|
2009-06-02 05:08:24 +00:00
|
|
|
mat->AddProperty( &alpha,1,AI_MATKEY_OPACITY );
|
2009-01-11 21:51:45 +00:00
|
|
|
|
|
|
|
// Specular color
|
|
|
|
aiColor3D speccolor( shiny,shiny,shiny );
|
|
|
|
mat->AddProperty( &speccolor,1,AI_MATKEY_COLOR_SPECULAR );
|
|
|
|
|
|
|
|
// Specular power
|
|
|
|
float specpow=shiny*128;
|
|
|
|
mat->AddProperty( &specpow,1,AI_MATKEY_SHININESS );
|
|
|
|
|
|
|
|
// Double sided
|
|
|
|
if( fx & 0x10 ){
|
|
|
|
int i=1;
|
|
|
|
mat->AddProperty( &i,1,AI_MATKEY_TWOSIDED );
|
|
|
|
}
|
|
|
|
|
|
|
|
//Textures
|
2008-11-04 20:41:11 +00:00
|
|
|
for( int i=0;i<n_texs;++i ){
|
|
|
|
int texid=ReadInt();
|
2010-02-06 23:52:41 +00:00
|
|
|
if( texid<-1 || (texid>=0 && texid>=static_cast<int>(_textures.size())) ){
|
2009-06-02 05:08:24 +00:00
|
|
|
Fail( "Bad texture id" );
|
|
|
|
}
|
|
|
|
if( i==0 && texid>=0 ){
|
|
|
|
aiString texname( _textures[texid] );
|
2009-01-11 21:51:45 +00:00
|
|
|
mat->AddProperty( &texname,AI_MATKEY_TEXTURE_DIFFUSE(0) );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
void B3DImporter::ReadVRTS(){
|
2009-06-02 05:08:24 +00:00
|
|
|
_vflags=ReadInt();
|
|
|
|
_tcsets=ReadInt();
|
|
|
|
_tcsize=ReadInt();
|
|
|
|
if( _tcsets<0 || _tcsets>4 || _tcsize<0 || _tcsize>4 ){
|
|
|
|
Fail( "Bad texcoord data" );
|
|
|
|
}
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
int sz=12+(_vflags&1?12:0)+(_vflags&2?16:0)+(_tcsets*_tcsize*4);
|
|
|
|
int n_verts=ChunkSize()/sz;
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-06-08 23:32:16 +00:00
|
|
|
int v0=_vertices.size();
|
|
|
|
_vertices.resize( v0+n_verts );
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
for( int i=0;i<n_verts;++i ){
|
2009-06-08 23:32:16 +00:00
|
|
|
Vertex &v=_vertices[v0+i];
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
memset( v.bones,0,sizeof(v.bones) );
|
|
|
|
memset( v.weights,0,sizeof(v.weights) );
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
v.vertex=ReadVec3();
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
if( _vflags & 1 ) v.normal=ReadVec3();
|
|
|
|
|
|
|
|
if( _vflags & 2 ) ReadQuat(); //skip v 4bytes...
|
|
|
|
|
|
|
|
for( int i=0;i<_tcsets;++i ){
|
|
|
|
float t[4]={0,0,0,0};
|
|
|
|
for( int j=0;j<_tcsize;++j ){
|
|
|
|
t[j]=ReadFloat();
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
2009-06-02 05:08:24 +00:00
|
|
|
t[1]=1-t[1];
|
|
|
|
if( !i ) v.texcoords=aiVector3D( t[0],t[1],t[2] );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-06-02 05:08:24 +00:00
|
|
|
void B3DImporter::ReadTRIS( int v0 ){
|
2008-11-04 20:41:11 +00:00
|
|
|
int matid=ReadInt();
|
2009-06-02 05:08:24 +00:00
|
|
|
if( matid==-1 ){
|
|
|
|
matid=0;
|
2010-02-06 23:52:41 +00:00
|
|
|
}else if( matid<0 || matid>=(int)_materials.size() ){
|
2009-06-02 05:08:24 +00:00
|
|
|
#ifdef DEBUG_B3D
|
|
|
|
cout<<"material id="<<matid<<endl;
|
|
|
|
#endif
|
|
|
|
Fail( "Bad material id" );
|
|
|
|
}
|
|
|
|
|
2008-11-04 20:41:11 +00:00
|
|
|
aiMesh *mesh=new aiMesh;
|
2008-11-11 19:02:51 +00:00
|
|
|
_meshes.push_back( mesh );
|
2008-11-04 20:41:11 +00:00
|
|
|
|
|
|
|
mesh->mMaterialIndex=matid;
|
2009-06-08 23:32:16 +00:00
|
|
|
mesh->mNumFaces=0;
|
2008-11-11 19:02:51 +00:00
|
|
|
mesh->mPrimitiveTypes=aiPrimitiveType_TRIANGLE;
|
|
|
|
|
2009-06-08 23:32:16 +00:00
|
|
|
int n_tris=ChunkSize()/12;
|
2008-11-11 19:02:51 +00:00
|
|
|
aiFace *face=mesh->mFaces=new aiFace[n_tris];
|
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
for( int i=0;i<n_tris;++i ){
|
|
|
|
int i0=ReadInt()+v0;
|
|
|
|
int i1=ReadInt()+v0;
|
|
|
|
int i2=ReadInt()+v0;
|
2010-02-06 23:52:41 +00:00
|
|
|
if( i0<0 || i0>=(int)_vertices.size() || i1<0 || i1>=(int)_vertices.size() || i2<0 || i2>=(int)_vertices.size() ){
|
2009-06-02 05:08:24 +00:00
|
|
|
#ifdef DEBUG_B3D
|
2009-06-08 23:32:16 +00:00
|
|
|
cout<<"Bad triangle index: i0="<<i0<<", i1="<<i1<<", i2="<<i2<<endl;
|
2009-06-02 05:08:24 +00:00
|
|
|
#endif
|
|
|
|
Fail( "Bad triangle index" );
|
2009-06-08 23:32:16 +00:00
|
|
|
continue;
|
2008-11-11 19:02:51 +00:00
|
|
|
}
|
2009-06-02 05:08:24 +00:00
|
|
|
face->mNumIndices=3;
|
|
|
|
face->mIndices=new unsigned[3];
|
|
|
|
face->mIndices[0]=i0;
|
|
|
|
face->mIndices[1]=i1;
|
|
|
|
face->mIndices[2]=i2;
|
2009-06-08 23:32:16 +00:00
|
|
|
++mesh->mNumFaces;
|
2008-11-11 19:02:51 +00:00
|
|
|
++face;
|
|
|
|
}
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-04 20:41:11 +00:00
|
|
|
void B3DImporter::ReadMESH(){
|
2010-02-06 23:52:41 +00:00
|
|
|
/*int matid=*/ReadInt();
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
int v0=_vertices.size();
|
|
|
|
|
2008-11-04 20:41:11 +00:00
|
|
|
while( ChunkSize() ){
|
|
|
|
string t=ReadChunk();
|
|
|
|
if( t=="VRTS" ){
|
|
|
|
ReadVRTS();
|
|
|
|
}else if( t=="TRIS" ){
|
2009-06-02 05:08:24 +00:00
|
|
|
ReadTRIS( v0 );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
ExitChunk();
|
|
|
|
}
|
2009-06-02 05:08:24 +00:00
|
|
|
}
|
2008-11-04 20:41:11 +00:00
|
|
|
|
2009-06-02 05:08:24 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadBONE( int id ){
|
|
|
|
while( ChunkSize() ){
|
|
|
|
int vertex=ReadInt();
|
|
|
|
float weight=ReadFloat();
|
2010-02-06 23:52:41 +00:00
|
|
|
if( vertex<0 || vertex>=(int)_vertices.size() ){
|
2009-06-02 05:08:24 +00:00
|
|
|
Fail( "Bad vertex index" );
|
|
|
|
}
|
|
|
|
|
|
|
|
Vertex &v=_vertices[vertex];
|
|
|
|
int i;
|
|
|
|
for( i=0;i<4;++i ){
|
|
|
|
if( !v.weights[i] ){
|
|
|
|
v.bones[i]=id;
|
|
|
|
v.weights[i]=weight;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_B3D
|
|
|
|
if( i==4 ){
|
|
|
|
cout<<"Too many bone weights"<<endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadKEYS( aiNodeAnim *nodeAnim ){
|
|
|
|
vector<aiVectorKey> trans,scale;
|
|
|
|
vector<aiQuatKey> rot;
|
|
|
|
int flags=ReadInt();
|
|
|
|
while( ChunkSize() ){
|
|
|
|
int frame=ReadInt();
|
|
|
|
if( flags & 1 ){
|
|
|
|
trans.push_back( aiVectorKey( frame,ReadVec3() ) );
|
|
|
|
}
|
|
|
|
if( flags & 2 ){
|
|
|
|
scale.push_back( aiVectorKey( frame,ReadVec3() ) );
|
|
|
|
}
|
|
|
|
if( flags & 4 ){
|
|
|
|
rot.push_back( aiQuatKey( frame,ReadQuat() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( flags & 1 ){
|
|
|
|
nodeAnim->mNumPositionKeys=trans.size();
|
|
|
|
nodeAnim->mPositionKeys=to_array( trans );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( flags & 2 ){
|
|
|
|
nodeAnim->mNumScalingKeys=scale.size();
|
|
|
|
nodeAnim->mScalingKeys=to_array( scale );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( flags & 4 ){
|
|
|
|
nodeAnim->mNumRotationKeys=rot.size();
|
|
|
|
nodeAnim->mRotationKeys=to_array( rot );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadANIM(){
|
2010-02-06 23:52:41 +00:00
|
|
|
/*int flags=*/ReadInt();
|
2009-06-02 05:08:24 +00:00
|
|
|
int frames=ReadInt();
|
|
|
|
float fps=ReadFloat();
|
|
|
|
|
|
|
|
aiAnimation *anim=new aiAnimation;
|
|
|
|
_animations.push_back( anim );
|
|
|
|
|
|
|
|
anim->mDuration=frames;
|
|
|
|
anim->mTicksPerSecond=fps;
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-06-02 05:08:24 +00:00
|
|
|
aiNode *B3DImporter::ReadNODE( aiNode *parent ){
|
2008-11-04 20:41:11 +00:00
|
|
|
|
|
|
|
string name=ReadString();
|
2009-06-02 05:08:24 +00:00
|
|
|
aiVector3D t=ReadVec3();
|
|
|
|
aiVector3D s=ReadVec3();
|
|
|
|
aiQuaternion r=ReadQuat();
|
|
|
|
|
|
|
|
aiMatrix4x4 trans,scale,rot;
|
|
|
|
|
|
|
|
aiMatrix4x4::Translation( t,trans );
|
|
|
|
aiMatrix4x4::Scaling( s,scale );
|
|
|
|
rot=aiMatrix4x4( r.GetMatrix() );
|
|
|
|
|
|
|
|
aiMatrix4x4 tform=trans * rot * scale;
|
|
|
|
|
|
|
|
int nodeid=_nodes.size();
|
|
|
|
|
|
|
|
aiNode *node=new aiNode( name );
|
|
|
|
_nodes.push_back( node );
|
|
|
|
|
|
|
|
node->mParent=parent;
|
|
|
|
node->mTransformation=tform;
|
|
|
|
|
|
|
|
aiNodeAnim *nodeAnim=0;
|
|
|
|
vector<unsigned> meshes;
|
|
|
|
vector<aiNode*> children;
|
2008-11-04 20:41:11 +00:00
|
|
|
|
|
|
|
while( ChunkSize() ){
|
|
|
|
string t=ReadChunk();
|
|
|
|
if( t=="MESH" ){
|
2009-06-02 05:08:24 +00:00
|
|
|
int n=_meshes.size();
|
2008-11-04 20:41:11 +00:00
|
|
|
ReadMESH();
|
2010-02-06 23:52:41 +00:00
|
|
|
for( int i=n;i<(int)_meshes.size();++i ){
|
2009-06-02 05:08:24 +00:00
|
|
|
meshes.push_back( i );
|
|
|
|
}
|
|
|
|
}else if( t=="BONE" ){
|
|
|
|
ReadBONE( nodeid );
|
|
|
|
}else if( t=="ANIM" ){
|
|
|
|
ReadANIM();
|
|
|
|
}else if( t=="KEYS" ){
|
|
|
|
if( !nodeAnim ){
|
|
|
|
nodeAnim=new aiNodeAnim;
|
|
|
|
_nodeAnims.push_back( nodeAnim );
|
|
|
|
nodeAnim->mNodeName=node->mName;
|
|
|
|
}
|
|
|
|
ReadKEYS( nodeAnim );
|
|
|
|
}else if( t=="NODE" ){
|
|
|
|
aiNode *child=ReadNODE( node );
|
|
|
|
children.push_back( child );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
ExitChunk();
|
|
|
|
}
|
2009-06-02 05:08:24 +00:00
|
|
|
|
|
|
|
node->mNumMeshes=meshes.size();
|
|
|
|
node->mMeshes=to_array( meshes );
|
|
|
|
|
|
|
|
node->mNumChildren=children.size();
|
|
|
|
node->mChildren=to_array( children );
|
|
|
|
|
|
|
|
return node;
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-06-02 05:08:24 +00:00
|
|
|
void B3DImporter::ReadBB3D( aiScene *scene ){
|
|
|
|
|
|
|
|
_textures.clear();
|
|
|
|
_materials.size();
|
|
|
|
|
|
|
|
_vertices.clear();
|
|
|
|
_meshes.clear();
|
|
|
|
|
|
|
|
_nodes.clear();
|
|
|
|
_nodeAnims.clear();
|
|
|
|
_animations.clear();
|
|
|
|
|
2008-11-04 20:41:11 +00:00
|
|
|
string t=ReadChunk();
|
|
|
|
if( t=="BB3D" ){
|
|
|
|
int version=ReadInt();
|
2010-02-06 23:52:41 +00:00
|
|
|
|
|
|
|
if (!DefaultLogger::isNullLogger()) {
|
|
|
|
char dmp[128];
|
|
|
|
sprintf(dmp,"B3D file format version: %i",version);
|
|
|
|
DefaultLogger::get()->info(dmp);
|
|
|
|
}
|
|
|
|
|
2008-11-04 20:41:11 +00:00
|
|
|
while( ChunkSize() ){
|
|
|
|
string t=ReadChunk();
|
|
|
|
if( t=="TEXS" ){
|
|
|
|
ReadTEXS();
|
|
|
|
}else if( t=="BRUS" ){
|
|
|
|
ReadBRUS();
|
|
|
|
}else if( t=="NODE" ){
|
2009-06-02 05:08:24 +00:00
|
|
|
ReadNODE( 0 );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
|
|
|
ExitChunk();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ExitChunk();
|
2009-06-02 05:08:24 +00:00
|
|
|
|
|
|
|
if( !_nodes.size() ) Fail( "No nodes" );
|
|
|
|
|
|
|
|
if( !_meshes.size() ) Fail( "No meshes" );
|
|
|
|
|
|
|
|
//Fix nodes/meshes/bones
|
2010-02-06 23:52:41 +00:00
|
|
|
for(size_t i=0;i<_nodes.size();++i ){
|
2009-06-02 05:08:24 +00:00
|
|
|
aiNode *node=_nodes[i];
|
|
|
|
|
2010-02-06 23:52:41 +00:00
|
|
|
for( size_t j=0;j<node->mNumMeshes;++j ){
|
2009-06-02 05:08:24 +00:00
|
|
|
aiMesh *mesh=_meshes[node->mMeshes[j]];
|
|
|
|
|
|
|
|
int n_tris=mesh->mNumFaces;
|
|
|
|
int n_verts=mesh->mNumVertices=n_tris * 3;
|
|
|
|
|
|
|
|
aiVector3D *mv=mesh->mVertices=new aiVector3D[ n_verts ],*mn=0,*mc=0;
|
|
|
|
if( _vflags & 1 ) mn=mesh->mNormals=new aiVector3D[ n_verts ];
|
|
|
|
if( _tcsets ) mc=mesh->mTextureCoords[0]=new aiVector3D[ n_verts ];
|
|
|
|
|
|
|
|
aiFace *face=mesh->mFaces;
|
|
|
|
|
|
|
|
vector< vector<aiVertexWeight> > vweights( _nodes.size() );
|
|
|
|
|
|
|
|
for( int i=0;i<n_verts;i+=3 ){
|
|
|
|
for( int j=0;j<3;++j ){
|
|
|
|
Vertex &v=_vertices[face->mIndices[j]];
|
|
|
|
|
|
|
|
*mv++=v.vertex;
|
|
|
|
if( mn ) *mn++=v.normal;
|
|
|
|
if( mc ) *mc++=v.texcoords;
|
|
|
|
|
|
|
|
face->mIndices[j]=i+j;
|
|
|
|
|
|
|
|
for( int k=0;k<4;++k ){
|
|
|
|
if( !v.weights[k] ) break;
|
|
|
|
|
|
|
|
int bone=v.bones[k];
|
|
|
|
float weight=v.weights[k];
|
|
|
|
|
|
|
|
vweights[bone].push_back( aiVertexWeight(i+j,weight) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++face;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<aiBone*> bones;
|
2010-02-06 23:52:41 +00:00
|
|
|
for(size_t i=0;i<vweights.size();++i ){
|
2009-06-02 05:08:24 +00:00
|
|
|
vector<aiVertexWeight> &weights=vweights[i];
|
|
|
|
if( !weights.size() ) continue;
|
|
|
|
|
|
|
|
aiBone *bone=new aiBone;
|
|
|
|
bones.push_back( bone );
|
|
|
|
|
|
|
|
aiNode *bnode=_nodes[i];
|
|
|
|
|
|
|
|
bone->mName=bnode->mName;
|
|
|
|
bone->mNumWeights=weights.size();
|
|
|
|
bone->mWeights=to_array( weights );
|
|
|
|
|
|
|
|
aiMatrix4x4 mat=bnode->mTransformation;
|
|
|
|
while( bnode->mParent ){
|
|
|
|
bnode=bnode->mParent;
|
|
|
|
mat=bnode->mTransformation * mat;
|
|
|
|
}
|
|
|
|
bone->mOffsetMatrix=mat.Inverse();
|
|
|
|
}
|
|
|
|
mesh->mNumBones=bones.size();
|
|
|
|
mesh->mBones=to_array( bones );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//nodes
|
|
|
|
scene->mRootNode=_nodes[0];
|
|
|
|
|
2010-02-06 23:52:41 +00:00
|
|
|
//material
|
2009-06-02 05:08:24 +00:00
|
|
|
if( !_materials.size() ){
|
|
|
|
_materials.push_back( new MaterialHelper );
|
|
|
|
}
|
|
|
|
scene->mNumMaterials=_materials.size();
|
|
|
|
scene->mMaterials=to_array( _materials );
|
|
|
|
|
|
|
|
//meshes
|
|
|
|
scene->mNumMeshes=_meshes.size();
|
|
|
|
scene->mMeshes=to_array( _meshes );
|
|
|
|
|
|
|
|
//animations
|
|
|
|
if( _animations.size()==1 && _nodeAnims.size() ){
|
|
|
|
|
|
|
|
aiAnimation *anim=_animations.back();
|
|
|
|
anim->mNumChannels=_nodeAnims.size();
|
|
|
|
anim->mChannels=to_array( _nodeAnims );
|
|
|
|
|
|
|
|
scene->mNumAnimations=_animations.size();
|
|
|
|
scene->mAnimations=to_array( _animations );
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert to RH
|
|
|
|
MakeLeftHandedProcess makeleft;
|
|
|
|
makeleft.Execute( scene );
|
|
|
|
|
|
|
|
FlipWindingOrderProcess flip;
|
|
|
|
flip.Execute( scene );
|
2008-11-04 20:41:11 +00:00
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_B3D_IMPORTER
|