2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2019-01-30 08:41:39 +00:00
|
|
|
Copyright (c) 2006-2019, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
2015-05-19 03:48:29 +00:00
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2016-04-05 21:23:53 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2016-04-05 21:23:53 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2016-04-05 21:23:53 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file B3DImporter.cpp
|
|
|
|
* @brief Implementation of the b3d importer class
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_B3D_IMPORTER
|
|
|
|
|
|
|
|
// internal headers
|
2019-06-10 21:26:00 +00:00
|
|
|
#include "B3D/B3DImporter.h"
|
|
|
|
#include "PostProcessing/TextureTransform.h"
|
|
|
|
#include "PostProcessing/ConvertToLHProcess.h"
|
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/IOSystem.hpp>
|
|
|
|
#include <assimp/anim.h>
|
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
2017-02-22 16:20:26 +00:00
|
|
|
#include <assimp/importerdesc.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-06-10 21:26:00 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
using namespace Assimp;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
static const aiImporterDesc desc = {
|
2015-07-03 18:40:07 +00:00
|
|
|
"BlitzBasic 3D Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"http://www.blitzbasic.com/",
|
|
|
|
aiImporterFlags_SupportBinaryFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2016-04-05 21:23:53 +00:00
|
|
|
"b3d"
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
#ifdef _MSC_VER
|
2015-07-01 18:43:37 +00:00
|
|
|
# pragma warning (disable: 4018)
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//#define DEBUG_B3D
|
|
|
|
|
2017-10-01 12:16:21 +00:00
|
|
|
template<typename T>
|
2019-12-04 20:25:17 +00:00
|
|
|
void DeleteAllBarePointers(std::vector<T>& x) {
|
|
|
|
for(auto p : x) {
|
2017-10-01 12:16:21 +00:00
|
|
|
delete p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
B3DImporter::~B3DImporter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool B3DImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const{
|
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
size_t pos=pFile.find_last_of( '.' );
|
2019-12-04 20:25:17 +00:00
|
|
|
if( pos==string::npos ) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
string ext=pFile.substr( pos+1 );
|
2019-12-04 20:25:17 +00:00
|
|
|
if( ext.size()!=3 ) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
return (ext[0]=='b' || ext[0]=='B') && (ext[1]=='3') && (ext[2]=='d' || ext[2]=='D');
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Loader meta information
|
|
|
|
const aiImporterDesc* B3DImporter::GetInfo () const
|
|
|
|
{
|
2015-07-03 18:40:07 +00:00
|
|
|
return &desc;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler){
|
2016-04-05 21:23:53 +00:00
|
|
|
std::unique_ptr<IOStream> file( pIOHandler->Open( pFile));
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
// Check whether we can read from the file
|
2019-12-04 20:25:17 +00:00
|
|
|
if( file.get() == nullptr) {
|
2015-07-03 18:40:07 +00:00
|
|
|
throw DeadlyImportError( "Failed to open B3D file " + pFile + ".");
|
2019-12-04 20:25:17 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
// check whether the .b3d file is large enough to contain
|
|
|
|
// at least one chunk.
|
|
|
|
size_t fileSize = file->FileSize();
|
2019-12-04 20:25:17 +00:00
|
|
|
if( fileSize<8 ) {
|
|
|
|
throw DeadlyImportError( "B3D File is too small.");
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
_pos=0;
|
|
|
|
_buf.resize( fileSize );
|
|
|
|
file->Read( &_buf[0],1,fileSize );
|
|
|
|
_stack.clear();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
ReadBB3D( pScene );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
AI_WONT_RETURN void B3DImporter::Oops(){
|
2015-07-03 18:40:07 +00:00
|
|
|
throw DeadlyImportError( "B3D Importer - INTERNAL ERROR" );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
AI_WONT_RETURN void B3DImporter::Fail( string str ){
|
|
|
|
#ifdef DEBUG_B3D
|
2019-12-03 13:35:53 +00:00
|
|
|
ASSIMP_LOG_ERROR_F("Error in B3D file data: ", str);
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
2015-07-03 18:40:07 +00:00
|
|
|
throw DeadlyImportError( "B3D Importer - error in B3D file data: "+str );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
int B3DImporter::ReadByte(){
|
2019-12-04 20:25:17 +00:00
|
|
|
if( _pos<_buf.size() ) {
|
|
|
|
return _buf[_pos++];
|
|
|
|
}
|
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
Fail( "EOF" );
|
|
|
|
return 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
int B3DImporter::ReadInt(){
|
2015-07-03 18:40:07 +00:00
|
|
|
if( _pos+4<=_buf.size() ){
|
2017-10-07 15:36:09 +00:00
|
|
|
int n;
|
|
|
|
memcpy(&n, &_buf[_pos], 4);
|
2015-07-03 18:40:07 +00:00
|
|
|
_pos+=4;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
Fail( "EOF" );
|
|
|
|
return 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
float B3DImporter::ReadFloat(){
|
2015-07-03 18:40:07 +00:00
|
|
|
if( _pos+4<=_buf.size() ){
|
2017-10-07 15:36:09 +00:00
|
|
|
float n;
|
|
|
|
memcpy(&n, &_buf[_pos], 4);
|
2015-07-03 18:40:07 +00:00
|
|
|
_pos+=4;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
Fail( "EOF" );
|
|
|
|
return 0.0f;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiVector2D B3DImporter::ReadVec2(){
|
2015-07-03 18:40:07 +00:00
|
|
|
float x=ReadFloat();
|
|
|
|
float y=ReadFloat();
|
|
|
|
return aiVector2D( x,y );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiVector3D B3DImporter::ReadVec3(){
|
2015-07-03 18:40:07 +00:00
|
|
|
float x=ReadFloat();
|
|
|
|
float y=ReadFloat();
|
|
|
|
float z=ReadFloat();
|
|
|
|
return aiVector3D( x,y,z );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiQuaternion B3DImporter::ReadQuat(){
|
2015-07-03 18:40:07 +00:00
|
|
|
// (aramis_acg) Fix to adapt the loader to changed quat orientation
|
|
|
|
float w=-ReadFloat();
|
|
|
|
float x=ReadFloat();
|
|
|
|
float y=ReadFloat();
|
|
|
|
float z=ReadFloat();
|
|
|
|
return aiQuaternion( w,x,y,z );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
string B3DImporter::ReadString(){
|
2015-07-03 18:40:07 +00:00
|
|
|
string str;
|
|
|
|
while( _pos<_buf.size() ){
|
|
|
|
char c=(char)ReadByte();
|
2019-12-04 20:25:17 +00:00
|
|
|
if( !c ) {
|
|
|
|
return str;
|
|
|
|
}
|
2015-07-03 18:40:07 +00:00
|
|
|
str+=c;
|
|
|
|
}
|
|
|
|
Fail( "EOF" );
|
|
|
|
return string();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
string B3DImporter::ReadChunk(){
|
2015-07-03 18:40:07 +00:00
|
|
|
string tag;
|
|
|
|
for( int i=0;i<4;++i ){
|
|
|
|
tag+=char( ReadByte() );
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifdef DEBUG_B3D
|
2019-12-03 13:35:53 +00:00
|
|
|
ASSIMP_LOG_DEBUG_F("ReadChunk: ", tag);
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
2015-07-03 18:40:07 +00:00
|
|
|
unsigned sz=(unsigned)ReadInt();
|
|
|
|
_stack.push_back( _pos+sz );
|
|
|
|
return tag;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ExitChunk(){
|
2015-07-03 18:40:07 +00:00
|
|
|
_pos=_stack.back();
|
|
|
|
_stack.pop_back();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
unsigned B3DImporter::ChunkSize(){
|
2015-07-03 18:40:07 +00:00
|
|
|
return _stack.back()-_pos;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T *B3DImporter::to_array( const vector<T> &v ){
|
|
|
|
if( v.empty() ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-03 18:40:07 +00:00
|
|
|
T *p=new T[ v.size() ];
|
|
|
|
for( size_t i=0;i<v.size();++i ){
|
|
|
|
p[i]=v[i];
|
|
|
|
}
|
|
|
|
return p;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 16:40:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
template<class T>
|
|
|
|
T **unique_to_array( vector<std::unique_ptr<T> > &v ){
|
|
|
|
if( v.empty() ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
T **p = new T*[ v.size() ];
|
|
|
|
for( size_t i = 0; i < v.size(); ++i ){
|
|
|
|
p[i] = v[i].release();
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadTEXS(){
|
2015-07-03 18:40:07 +00:00
|
|
|
while( ChunkSize() ){
|
|
|
|
string name=ReadString();
|
|
|
|
/*int flags=*/ReadInt();
|
|
|
|
/*int blend=*/ReadInt();
|
|
|
|
/*aiVector2D pos=*/ReadVec2();
|
|
|
|
/*aiVector2D scale=*/ReadVec2();
|
|
|
|
/*float rot=*/ReadFloat();
|
|
|
|
|
|
|
|
_textures.push_back( name );
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadBRUS(){
|
2015-07-03 18:40:07 +00:00
|
|
|
int n_texs=ReadInt();
|
|
|
|
if( n_texs<0 || n_texs>8 ){
|
|
|
|
Fail( "Bad texture count" );
|
|
|
|
}
|
|
|
|
while( ChunkSize() ){
|
|
|
|
string name=ReadString();
|
|
|
|
aiVector3D color=ReadVec3();
|
|
|
|
float alpha=ReadFloat();
|
|
|
|
float shiny=ReadFloat();
|
|
|
|
/*int blend=**/ReadInt();
|
|
|
|
int fx=ReadInt();
|
|
|
|
|
2017-12-12 17:16:06 +00:00
|
|
|
std::unique_ptr<aiMaterial> mat(new aiMaterial);
|
2016-04-05 21:23:53 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
// Name
|
|
|
|
aiString ainame( name );
|
|
|
|
mat->AddProperty( &ainame,AI_MATKEY_NAME );
|
2016-04-05 21:23:53 +00:00
|
|
|
|
|
|
|
// Diffuse color
|
2015-07-03 18:40:07 +00:00
|
|
|
mat->AddProperty( &color,1,AI_MATKEY_COLOR_DIFFUSE );
|
|
|
|
|
|
|
|
// Opacity
|
|
|
|
mat->AddProperty( &alpha,1,AI_MATKEY_OPACITY );
|
|
|
|
|
|
|
|
// Specular color
|
|
|
|
aiColor3D speccolor( shiny,shiny,shiny );
|
|
|
|
mat->AddProperty( &speccolor,1,AI_MATKEY_COLOR_SPECULAR );
|
2016-04-05 21:23:53 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
// Specular power
|
|
|
|
float specpow=shiny*128;
|
|
|
|
mat->AddProperty( &specpow,1,AI_MATKEY_SHININESS );
|
2016-04-05 21:23:53 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
// Double sided
|
|
|
|
if( fx & 0x10 ){
|
2016-04-05 21:23:53 +00:00
|
|
|
int i=1;
|
2015-07-03 18:40:07 +00:00
|
|
|
mat->AddProperty( &i,1,AI_MATKEY_TWOSIDED );
|
2016-04-05 21:23:53 +00:00
|
|
|
}
|
2015-07-03 18:40:07 +00:00
|
|
|
|
|
|
|
//Textures
|
|
|
|
for( int i=0;i<n_texs;++i ){
|
|
|
|
int texid=ReadInt();
|
|
|
|
if( texid<-1 || (texid>=0 && texid>=static_cast<int>(_textures.size())) ){
|
|
|
|
Fail( "Bad texture id" );
|
|
|
|
}
|
|
|
|
if( i==0 && texid>=0 ){
|
|
|
|
aiString texname( _textures[texid] );
|
|
|
|
mat->AddProperty( &texname,AI_MATKEY_TEXTURE_DIFFUSE(0) );
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 17:16:06 +00:00
|
|
|
_materials.emplace_back( std::move(mat) );
|
2015-07-03 18:40:07 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadVRTS(){
|
2015-07-03 18:40:07 +00:00
|
|
|
_vflags=ReadInt();
|
|
|
|
_tcsets=ReadInt();
|
|
|
|
_tcsize=ReadInt();
|
|
|
|
if( _tcsets<0 || _tcsets>4 || _tcsize<0 || _tcsize>4 ){
|
|
|
|
Fail( "Bad texcoord data" );
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
int sz=12+(_vflags&1?12:0)+(_vflags&2?16:0)+(_tcsets*_tcsize*4);
|
|
|
|
int n_verts=ChunkSize()/sz;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-11-18 14:21:59 +00:00
|
|
|
int v0=static_cast<int>(_vertices.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
_vertices.resize( v0+n_verts );
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
for( int i=0;i<n_verts;++i ){
|
|
|
|
Vertex &v=_vertices[v0+i];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
memset( v.bones,0,sizeof(v.bones) );
|
|
|
|
memset( v.weights,0,sizeof(v.weights) );
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
v.vertex=ReadVec3();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-12-04 20:25:17 +00:00
|
|
|
if( _vflags & 1 ) {
|
|
|
|
v.normal=ReadVec3();
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-12-04 20:25:17 +00:00
|
|
|
if( _vflags & 2 ) {
|
|
|
|
ReadQuat(); //skip v 4bytes...
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
for( int i=0;i<_tcsets;++i ){
|
|
|
|
float t[4]={0,0,0,0};
|
|
|
|
for( int j=0;j<_tcsize;++j ){
|
|
|
|
t[j]=ReadFloat();
|
|
|
|
}
|
|
|
|
t[1]=1-t[1];
|
2019-12-04 20:25:17 +00:00
|
|
|
if( !i ) {
|
|
|
|
v.texcoords=aiVector3D( t[0],t[1],t[2] );
|
|
|
|
}
|
2015-07-03 18:40:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-12-02 11:56:55 +00:00
|
|
|
void B3DImporter::ReadTRIS(int v0) {
|
|
|
|
int matid = ReadInt();
|
|
|
|
if (matid == -1) {
|
|
|
|
matid = 0;
|
|
|
|
} else if (matid < 0 || matid >= (int)_materials.size()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifdef DEBUG_B3D
|
2019-12-03 13:35:53 +00:00
|
|
|
ASSIMP_LOG_ERROR_F("material id=", matid);
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
2019-12-02 11:56:55 +00:00
|
|
|
Fail("Bad material id");
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-12-02 11:56:55 +00:00
|
|
|
std::unique_ptr<aiMesh> mesh(new aiMesh);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-12-02 11:56:55 +00:00
|
|
|
mesh->mMaterialIndex = matid;
|
|
|
|
mesh->mNumFaces = 0;
|
|
|
|
mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-12-02 11:56:55 +00:00
|
|
|
int n_tris = ChunkSize() / 12;
|
|
|
|
aiFace *face = mesh->mFaces = new aiFace[n_tris];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-12-02 11:56:55 +00:00
|
|
|
for (int i = 0; i < n_tris; ++i) {
|
|
|
|
int i0 = ReadInt() + v0;
|
|
|
|
int i1 = ReadInt() + v0;
|
|
|
|
int i2 = ReadInt() + v0;
|
|
|
|
if (i0 < 0 || i0 >= (int)_vertices.size() || i1 < 0 || i1 >= (int)_vertices.size() || i2 < 0 || i2 >= (int)_vertices.size()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifdef DEBUG_B3D
|
2019-12-03 13:35:53 +00:00
|
|
|
ASSIMP_LOG_ERROR_F("Bad triangle index: i0=", i0, ", i1=", i1, ", i2=", i2);
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
2019-12-02 11:56:55 +00:00
|
|
|
Fail("Bad triangle index");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
face->mNumIndices = 3;
|
|
|
|
face->mIndices = new unsigned[3];
|
|
|
|
face->mIndices[0] = i0;
|
|
|
|
face->mIndices[1] = i1;
|
|
|
|
face->mIndices[2] = i2;
|
|
|
|
++mesh->mNumFaces;
|
|
|
|
++face;
|
|
|
|
}
|
|
|
|
|
|
|
|
_meshes.emplace_back(std::move(mesh));
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadMESH(){
|
2015-07-03 18:40:07 +00:00
|
|
|
/*int matid=*/ReadInt();
|
|
|
|
|
2016-11-18 14:21:59 +00:00
|
|
|
int v0= static_cast<int>(_vertices.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
|
|
|
|
while( ChunkSize() ){
|
|
|
|
string t=ReadChunk();
|
|
|
|
if( t=="VRTS" ){
|
|
|
|
ReadVRTS();
|
|
|
|
}else if( t=="TRIS" ){
|
|
|
|
ReadTRIS( v0 );
|
|
|
|
}
|
|
|
|
ExitChunk();
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2019-12-02 11:56:55 +00:00
|
|
|
void B3DImporter::ReadBONE(int id) {
|
|
|
|
while (ChunkSize()) {
|
|
|
|
int vertex = ReadInt();
|
|
|
|
float weight = ReadFloat();
|
|
|
|
if (vertex < 0 || vertex >= (int)_vertices.size()) {
|
|
|
|
Fail("Bad vertex index");
|
|
|
|
}
|
|
|
|
|
|
|
|
Vertex &v = _vertices[vertex];
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
if (!v.weights[i]) {
|
|
|
|
v.bones[i] = id;
|
|
|
|
v.weights[i] = weight;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadKEYS( aiNodeAnim *nodeAnim ){
|
2015-07-03 18:40:07 +00:00
|
|
|
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 ){
|
2016-11-18 14:21:59 +00:00
|
|
|
nodeAnim->mNumPositionKeys=static_cast<unsigned int>(trans.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
nodeAnim->mPositionKeys=to_array( trans );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( flags & 2 ){
|
2016-11-18 14:21:59 +00:00
|
|
|
nodeAnim->mNumScalingKeys=static_cast<unsigned int>(scale.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
nodeAnim->mScalingKeys=to_array( scale );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( flags & 4 ){
|
2016-11-18 14:21:59 +00:00
|
|
|
nodeAnim->mNumRotationKeys=static_cast<unsigned int>(rot.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
nodeAnim->mRotationKeys=to_array( rot );
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadANIM(){
|
2015-07-03 18:40:07 +00:00
|
|
|
/*int flags=*/ReadInt();
|
|
|
|
int frames=ReadInt();
|
|
|
|
float fps=ReadFloat();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2017-12-12 16:42:14 +00:00
|
|
|
std::unique_ptr<aiAnimation> anim(new aiAnimation);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
anim->mDuration=frames;
|
|
|
|
anim->mTicksPerSecond=fps;
|
2017-12-12 16:42:14 +00:00
|
|
|
_animations.emplace_back( std::move(anim) );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiNode *B3DImporter::ReadNODE( aiNode *parent ){
|
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
string name=ReadString();
|
|
|
|
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;
|
|
|
|
|
2016-11-18 14:21:59 +00:00
|
|
|
int nodeid=static_cast<int>(_nodes.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
|
|
|
|
aiNode *node=new aiNode( name );
|
|
|
|
_nodes.push_back( node );
|
|
|
|
|
|
|
|
node->mParent=parent;
|
|
|
|
node->mTransformation=tform;
|
|
|
|
|
2017-12-12 17:39:41 +00:00
|
|
|
std::unique_ptr<aiNodeAnim> nodeAnim;
|
2015-07-03 18:40:07 +00:00
|
|
|
vector<unsigned> meshes;
|
|
|
|
vector<aiNode*> children;
|
|
|
|
|
|
|
|
while( ChunkSize() ){
|
|
|
|
string t=ReadChunk();
|
|
|
|
if( t=="MESH" ){
|
2016-11-18 14:21:59 +00:00
|
|
|
unsigned int n= static_cast<unsigned int>(_meshes.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
ReadMESH();
|
2016-11-18 14:21:59 +00:00
|
|
|
for( unsigned int i=n;i<static_cast<unsigned int>(_meshes.size());++i ){
|
2015-07-03 18:40:07 +00:00
|
|
|
meshes.push_back( i );
|
|
|
|
}
|
|
|
|
}else if( t=="BONE" ){
|
|
|
|
ReadBONE( nodeid );
|
|
|
|
}else if( t=="ANIM" ){
|
|
|
|
ReadANIM();
|
|
|
|
}else if( t=="KEYS" ){
|
|
|
|
if( !nodeAnim ){
|
2017-12-12 17:39:41 +00:00
|
|
|
nodeAnim.reset(new aiNodeAnim);
|
2015-07-03 18:40:07 +00:00
|
|
|
nodeAnim->mNodeName=node->mName;
|
|
|
|
}
|
2017-12-12 17:39:41 +00:00
|
|
|
ReadKEYS( nodeAnim.get() );
|
2015-07-03 18:40:07 +00:00
|
|
|
}else if( t=="NODE" ){
|
|
|
|
aiNode *child=ReadNODE( node );
|
|
|
|
children.push_back( child );
|
|
|
|
}
|
|
|
|
ExitChunk();
|
|
|
|
}
|
|
|
|
|
2017-12-12 17:39:41 +00:00
|
|
|
if (nodeAnim) {
|
|
|
|
_nodeAnims.emplace_back( std::move(nodeAnim) );
|
|
|
|
}
|
|
|
|
|
2016-11-18 14:21:59 +00:00
|
|
|
node->mNumMeshes= static_cast<unsigned int>(meshes.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
node->mMeshes=to_array( meshes );
|
|
|
|
|
2016-11-18 14:21:59 +00:00
|
|
|
node->mNumChildren=static_cast<unsigned int>(children.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
node->mChildren=to_array( children );
|
|
|
|
|
|
|
|
return node;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void B3DImporter::ReadBB3D( aiScene *scene ){
|
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
_textures.clear();
|
2017-10-01 12:16:21 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
_materials.clear();
|
|
|
|
|
|
|
|
_vertices.clear();
|
2017-10-01 12:16:21 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
_meshes.clear();
|
|
|
|
|
2017-10-01 12:16:21 +00:00
|
|
|
DeleteAllBarePointers(_nodes);
|
2015-07-03 18:40:07 +00:00
|
|
|
_nodes.clear();
|
2017-10-01 12:16:21 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
_nodeAnims.clear();
|
2017-10-01 12:16:21 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
_animations.clear();
|
|
|
|
|
|
|
|
string t=ReadChunk();
|
|
|
|
if( t=="BB3D" ){
|
|
|
|
int version=ReadInt();
|
2016-04-05 21:23:53 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
if (!DefaultLogger::isNullLogger()) {
|
|
|
|
char dmp[128];
|
2016-01-06 14:35:25 +00:00
|
|
|
ai_snprintf(dmp, 128, "B3D file format version: %i",version);
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_INFO(dmp);
|
2015-07-03 18:40:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while( ChunkSize() ){
|
|
|
|
string t=ReadChunk();
|
|
|
|
if( t=="TEXS" ){
|
|
|
|
ReadTEXS();
|
|
|
|
}else if( t=="BRUS" ){
|
|
|
|
ReadBRUS();
|
|
|
|
}else if( t=="NODE" ){
|
|
|
|
ReadNODE( 0 );
|
|
|
|
}
|
|
|
|
ExitChunk();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ExitChunk();
|
|
|
|
|
2019-12-04 20:25:17 +00:00
|
|
|
if( !_nodes.size() ) {
|
|
|
|
Fail( "No nodes" );
|
|
|
|
}
|
2015-07-03 18:40:07 +00:00
|
|
|
|
2019-12-04 20:25:17 +00:00
|
|
|
if( !_meshes.size() ) {
|
|
|
|
Fail( "No meshes" );
|
|
|
|
}
|
2015-07-03 18:40:07 +00:00
|
|
|
|
2019-12-04 20:25:17 +00:00
|
|
|
// Fix nodes/meshes/bones
|
2015-07-03 18:40:07 +00:00
|
|
|
for(size_t i=0;i<_nodes.size();++i ){
|
|
|
|
aiNode *node=_nodes[i];
|
|
|
|
|
|
|
|
for( size_t j=0;j<node->mNumMeshes;++j ){
|
2017-12-12 16:54:41 +00:00
|
|
|
aiMesh *mesh = _meshes[node->mMeshes[j]].get();
|
2015-07-03 18:40:07 +00:00
|
|
|
|
|
|
|
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;
|
2019-12-04 20:25:17 +00:00
|
|
|
if( _vflags & 1 ) {
|
|
|
|
mn=mesh->mNormals=new aiVector3D[ n_verts ];
|
|
|
|
}
|
|
|
|
if( _tcsets ) {
|
|
|
|
mc=mesh->mTextureCoords[0]=new aiVector3D[ n_verts ];
|
|
|
|
}
|
2015-07-03 18:40:07 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
for(size_t i=0;i<vweights.size();++i ){
|
|
|
|
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;
|
2016-11-18 14:21:59 +00:00
|
|
|
bone->mNumWeights= static_cast<unsigned int>(weights.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
bone->mWeights=to_array( weights );
|
|
|
|
|
|
|
|
aiMatrix4x4 mat=bnode->mTransformation;
|
|
|
|
while( bnode->mParent ){
|
|
|
|
bnode=bnode->mParent;
|
|
|
|
mat=bnode->mTransformation * mat;
|
|
|
|
}
|
|
|
|
bone->mOffsetMatrix=mat.Inverse();
|
|
|
|
}
|
2016-11-18 14:21:59 +00:00
|
|
|
mesh->mNumBones= static_cast<unsigned int>(bones.size());
|
2015-07-03 18:40:07 +00:00
|
|
|
mesh->mBones=to_array( bones );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//nodes
|
|
|
|
scene->mRootNode=_nodes[0];
|
2017-12-12 18:03:16 +00:00
|
|
|
_nodes.clear(); // node ownership now belongs to scene
|
2015-07-03 18:40:07 +00:00
|
|
|
|
|
|
|
//material
|
|
|
|
if( !_materials.size() ){
|
2017-12-12 17:16:06 +00:00
|
|
|
_materials.emplace_back( std::unique_ptr<aiMaterial>(new aiMaterial) );
|
2015-07-03 18:40:07 +00:00
|
|
|
}
|
2016-11-18 14:21:59 +00:00
|
|
|
scene->mNumMaterials= static_cast<unsigned int>(_materials.size());
|
2017-12-12 17:16:06 +00:00
|
|
|
scene->mMaterials = unique_to_array( _materials );
|
2016-04-05 21:23:53 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
//meshes
|
2016-11-18 14:21:59 +00:00
|
|
|
scene->mNumMeshes= static_cast<unsigned int>(_meshes.size());
|
2017-12-12 16:54:41 +00:00
|
|
|
scene->mMeshes = unique_to_array( _meshes );
|
2015-07-03 18:40:07 +00:00
|
|
|
|
|
|
|
//animations
|
|
|
|
if( _animations.size()==1 && _nodeAnims.size() ){
|
|
|
|
|
2017-12-12 16:42:14 +00:00
|
|
|
aiAnimation *anim = _animations.back().get();
|
2016-11-18 14:21:59 +00:00
|
|
|
anim->mNumChannels=static_cast<unsigned int>(_nodeAnims.size());
|
2017-12-12 17:39:41 +00:00
|
|
|
anim->mChannels = unique_to_array( _nodeAnims );
|
2015-07-03 18:40:07 +00:00
|
|
|
|
2016-11-18 14:21:59 +00:00
|
|
|
scene->mNumAnimations=static_cast<unsigned int>(_animations.size());
|
2017-12-12 16:42:14 +00:00
|
|
|
scene->mAnimations=unique_to_array( _animations );
|
2015-07-03 18:40:07 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
// convert to RH
|
|
|
|
MakeLeftHandedProcess makeleft;
|
|
|
|
makeleft.Execute( scene );
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-07-03 18:40:07 +00:00
|
|
|
FlipWindingOrderProcess flip;
|
|
|
|
flip.Execute( scene );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_B3D_IMPORTER
|