diff --git a/code/B3DImporter.cpp b/code/B3DImporter.cpp index 72da7cac4..63344a876 100644 --- a/code/B3DImporter.cpp +++ b/code/B3DImporter.cpp @@ -80,7 +80,7 @@ void B3DImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOS // check whether the .b3d file is large enough to contain // at least one chunk. size_t fileSize = file->FileSize(); - if( fileSize < 8) throw new ImportErrorException( "B3D File is too small."); + if( fileSize<8 ) throw new ImportErrorException( "B3D File is too small."); _pos=0; _buf.resize( fileSize ); @@ -92,24 +92,30 @@ void B3DImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOS _meshes.clear(); ReadBB3D(); - + //materials - aiMaterial **mats=new aiMaterial*[_materials.size()]; - for( unsigned i=0;i<_materials.size();++i ){ - mats[i]=_materials[i]; + if( _materials.size() ){ + aiMaterial **mats=new aiMaterial*[_materials.size()]; + for( unsigned i=0;i<_materials.size();++i ){ + mats[i]=_materials[i]; + } + pScene->mNumMaterials=_materials.size(); + pScene->mMaterials=mats; } - pScene->mNumMaterials=_materials.size(); - pScene->mMaterials=mats; - + //meshes - aiMesh **meshes=new aiMesh*[_meshes.size()]; - for( unsigned i=0;i<_meshes.size();++i ){ - meshes[i]=_meshes[i]; + if( _meshes.size() ){ + aiMesh **meshes=new aiMesh*[_meshes.size()]; + for( unsigned i=0;i<_meshes.size();++i ){ + meshes[i]=_meshes[i]; + } + pScene->mNumMeshes=_meshes.size(); + pScene->mMeshes=meshes; + }else{ + throw new ImportErrorException( "B3D: No meshes loaded" ); } - pScene->mNumMeshes=_meshes.size(); - pScene->mMeshes=meshes; - //nodes - NOTE: Have to create mMeshes array here or crash 'n' burn. + //create root node aiNode *node=new aiNode( "root" ); node->mNumMeshes=_meshes.size(); node->mMeshes=new unsigned[_meshes.size()]; @@ -275,11 +281,11 @@ void B3DImporter::ReadBRUS(){ // ------------------------------------------------------------------------------------------------ void B3DImporter::ReadVRTS(){ - int vertFlags=ReadInt(); - int tc_sets=ReadInt(); - int tc_size=ReadInt(); + _vertFlags=ReadInt(); + _tcSets=ReadInt(); + _tcSize=ReadInt(); - if( tc_sets<0 || tc_sets>4 || tc_size<0 || tc_size>4 ) throw new ImportErrorException( "B3D Param Error" ); + if( _tcSets<0 || _tcSets>4 || _tcSize<0 || _tcSize>4 ) throw new ImportErrorException( "B3D Param Error" ); while( ChunkSize() ){ Vertex vert; @@ -287,18 +293,18 @@ void B3DImporter::ReadVRTS(){ vert.position=ReadVec3(); std::swap( vert.position.y,vert.position.z ); - if( vertFlags & 1 ){ + if( _vertFlags & 1 ){ vert.normal=ReadVec3(); std::swap( vert.normal.y,vert.normal.z ); } - if( vertFlags & 2 ){ + if( _vertFlags & 2 ){ Vec4 color=ReadVec4(); } - for( int i=0;imNumFaces=n_tris; mesh->mPrimitiveTypes=aiPrimitiveType_TRIANGLE; - aiVector3D *mv=mesh->mVertices=new aiVector3D[n_verts]; - aiVector3D *mn=mesh->mNormals=new aiVector3D[n_verts]; - aiVector3D *mc=mesh->mTextureCoords[0]=new aiVector3D[n_verts]; - + aiVector3D *mv,*mn=0,*mc=0; + + mv=mesh->mVertices=new aiVector3D[n_verts]; + if( _vertFlags & 1 ) mn=mesh->mNormals=new aiVector3D[n_verts]; + if( _tcSets ) mc=mesh->mTextureCoords[0]=new aiVector3D[n_verts]; + aiFace *face=mesh->mFaces=new aiFace[n_tris]; for( unsigned i=0;i _buf; std::vector _stack; - + + int _vertFlags,_tcSets,_tcSize; std::vector _textures; std::vector _materials; std::vector _vertices;