1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
|
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2012, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp 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.
---------------------------------------------------------------------------
*/
/** @file XFileImporter.cpp
* @brief Implementation of the XFile importer class
*/
#include "AssimpPCH.h"
#ifndef ASSIMP_BUILD_NO_X_IMPORTER
#include "XFileImporter.h"
#include "XFileParser.h"
#include "ConvertToLHProcess.h"
using namespace Assimp;
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
XFileImporter::XFileImporter()
{}
// ------------------------------------------------------------------------------------------------
// Destructor, private as well
XFileImporter::~XFileImporter()
{}
// ------------------------------------------------------------------------------------------------
// Returns whether the class can handle the format of the given file.
bool XFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
{
std::string extension = GetExtension(pFile);
if(extension == "x") {
return true;
}
if (!extension.length() || checkSig) {
uint32_t token[1];
token[0] = AI_MAKE_MAGIC("xof ");
return CheckMagicToken(pIOHandler,pFile,token,1,0);
}
return false;
}
// ------------------------------------------------------------------------------------------------
void XFileImporter::GetExtensionList(std::set<std::string>& extensions)
{
extensions.insert("x");
}
// ------------------------------------------------------------------------------------------------
// Imports the given file into the given scene structure.
void XFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
{
// read file into memory
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
if( file.get() == NULL)
throw DeadlyImportError( "Failed to open file " + pFile + ".");
size_t fileSize = file->FileSize();
if( fileSize < 16)
throw DeadlyImportError( "XFile is too small.");
// need to clear members - this method might be called multiple
// times on a single XFileImporter instance.
mImportedMats.clear();
// in the hope that binary files will never start with a BOM ...
mBuffer.resize( fileSize + 1);
file->Read( &mBuffer.front(), 1, fileSize);
ConvertToUTF8(mBuffer);
// parse the file into a temporary representation
XFileParser parser( mBuffer);
// and create the proper return structures out of it
CreateDataRepresentationFromImport( pScene, parser.GetImportedData());
// if nothing came from it, report it as error
if( !pScene->mRootNode)
throw DeadlyImportError( "XFile is ill-formatted - no content imported.");
}
// ------------------------------------------------------------------------------------------------
// Constructs the return data structure out of the imported data.
void XFileImporter::CreateDataRepresentationFromImport( aiScene* pScene, const XFile::Scene* pData)
{
// Read the global materials first so that meshes referring to them can find them later
ConvertMaterials( pScene, pData->mGlobalMaterials);
// copy nodes, extracting meshes and materials on the way
pScene->mRootNode = CreateNodes( pScene, NULL, pData->mRootNode);
// extract animations
CreateAnimations( pScene, pData);
// read the global meshes that were stored outside of any node
if( pData->mGlobalMeshes.size() > 0)
{
// create a root node to hold them if there isn't any, yet
if( pScene->mRootNode == NULL)
{
pScene->mRootNode = new aiNode;
pScene->mRootNode->mName.Set( "$dummy_node");
}
// convert all global meshes and store them in the root node.
// If there was one before, the global meshes now suddenly have its transformation matrix...
// Don't know what to do there, I don't want to insert another node under the present root node
// just to avoid this.
CreateMeshes( pScene, pScene->mRootNode, pData->mGlobalMeshes);
}
// Convert everything to OpenGL space... it's the same operation as the conversion back, so we can reuse the step directly
MakeLeftHandedProcess convertProcess;
convertProcess.Execute( pScene);
FlipWindingOrderProcess flipper;
flipper.Execute(pScene);
// finally: create a dummy material if not material was imported
if( pScene->mNumMaterials == 0)
{
pScene->mNumMaterials = 1;
// create the Material
aiMaterial* mat = new aiMaterial;
int shadeMode = (int) aiShadingMode_Gouraud;
mat->AddProperty<int>( &shadeMode, 1, AI_MATKEY_SHADING_MODEL);
// material colours
int specExp = 1;
aiColor3D clr = aiColor3D( 0, 0, 0);
mat->AddProperty( &clr, 1, AI_MATKEY_COLOR_EMISSIVE);
mat->AddProperty( &clr, 1, AI_MATKEY_COLOR_SPECULAR);
clr = aiColor3D( 0.5f, 0.5f, 0.5f);
mat->AddProperty( &clr, 1, AI_MATKEY_COLOR_DIFFUSE);
mat->AddProperty( &specExp, 1, AI_MATKEY_SHININESS);
pScene->mMaterials = new aiMaterial*[1];
pScene->mMaterials[0] = mat;
}
}
// ------------------------------------------------------------------------------------------------
// Recursively creates scene nodes from the imported hierarchy.
aiNode* XFileImporter::CreateNodes( aiScene* pScene, aiNode* pParent, const XFile::Node* pNode)
{
if( !pNode)
return NULL;
// create node
aiNode* node = new aiNode;
node->mName.length = pNode->mName.length();
node->mParent = pParent;
memcpy( node->mName.data, pNode->mName.c_str(), pNode->mName.length());
node->mName.data[node->mName.length] = 0;
node->mTransformation = pNode->mTrafoMatrix;
// convert meshes from the source node
CreateMeshes( pScene, node, pNode->mMeshes);
// handle childs
if( pNode->mChildren.size() > 0)
{
node->mNumChildren = (unsigned int)pNode->mChildren.size();
node->mChildren = new aiNode* [node->mNumChildren];
for( unsigned int a = 0; a < pNode->mChildren.size(); a++)
node->mChildren[a] = CreateNodes( pScene, node, pNode->mChildren[a]);
}
return node;
}
// ------------------------------------------------------------------------------------------------
// Creates the meshes for the given node.
void XFileImporter::CreateMeshes( aiScene* pScene, aiNode* pNode, const std::vector<XFile::Mesh*>& pMeshes)
{
if( pMeshes.size() == 0)
return;
// create a mesh for each mesh-material combination in the source node
std::vector<aiMesh*> meshes;
for( unsigned int a = 0; a < pMeshes.size(); a++)
{
const XFile::Mesh* sourceMesh = pMeshes[a];
// first convert its materials so that we can find them when searching by name afterwards
ConvertMaterials( pScene, sourceMesh->mMaterials);
unsigned int numMaterials = std::max( (unsigned int)sourceMesh->mMaterials.size(), 1u);
for( unsigned int b = 0; b < numMaterials; b++)
{
// collect the faces belonging to this material
std::vector<unsigned int> faces;
unsigned int numVertices = 0;
if( sourceMesh->mFaceMaterials.size() > 0)
{
// if there is a per-face material defined, select the faces with the corresponding material
for( unsigned int c = 0; c < sourceMesh->mFaceMaterials.size(); c++)
{
if( sourceMesh->mFaceMaterials[c] == b)
{
faces.push_back( c);
numVertices += (unsigned int)sourceMesh->mPosFaces[c].mIndices.size();
}
}
} else
{
// if there is no per-face material, place everything into one mesh
for( unsigned int c = 0; c < sourceMesh->mPosFaces.size(); c++)
{
faces.push_back( c);
numVertices += (unsigned int)sourceMesh->mPosFaces[c].mIndices.size();
}
}
// no faces/vertices using this material? strange...
if( numVertices == 0)
continue;
// create a submesh using this material
aiMesh* mesh = new aiMesh;
meshes.push_back( mesh);
// find the material by name in the scene's material list. Either own material
// or referenced material, it should already be found there
if( sourceMesh->mFaceMaterials.size() > 0)
{
std::map<std::string, unsigned int>::const_iterator matIt = mImportedMats.find( sourceMesh->mMaterials[b].mName);
if( matIt == mImportedMats.end())
mesh->mMaterialIndex = 0;
else
mesh->mMaterialIndex = matIt->second;
} else
{
mesh->mMaterialIndex = 0;
}
// Create properly sized data arrays in the mesh. We store unique vertices per face,
// as specified
mesh->mNumVertices = numVertices;
mesh->mVertices = new aiVector3D[numVertices];
mesh->mNumFaces = (unsigned int)faces.size();
mesh->mFaces = new aiFace[mesh->mNumFaces];
// normals?
if( sourceMesh->mNormals.size() > 0)
mesh->mNormals = new aiVector3D[numVertices];
// texture coords
for( unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS; c++)
{
if( sourceMesh->mTexCoords[c].size() > 0)
mesh->mTextureCoords[c] = new aiVector3D[numVertices];
}
// vertex colors
for( unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS; c++)
{
if( sourceMesh->mColors[c].size() > 0)
mesh->mColors[c] = new aiColor4D[numVertices];
}
// now collect the vertex data of all data streams present in the imported mesh
unsigned int newIndex = 0;
std::vector<unsigned int> orgPoints; // from which original point each new vertex stems
orgPoints.resize( numVertices, 0);
for( unsigned int c = 0; c < faces.size(); c++)
{
unsigned int f = faces[c]; // index of the source face
const XFile::Face& pf = sourceMesh->mPosFaces[f]; // position source face
// create face. either triangle or triangle fan depending on the index count
aiFace& df = mesh->mFaces[c]; // destination face
df.mNumIndices = (unsigned int)pf.mIndices.size();
df.mIndices = new unsigned int[ df.mNumIndices];
// collect vertex data for indices of this face
for( unsigned int d = 0; d < df.mNumIndices; d++)
{
df.mIndices[d] = newIndex;
orgPoints[newIndex] = pf.mIndices[d];
// Position
mesh->mVertices[newIndex] = sourceMesh->mPositions[pf.mIndices[d]];
// Normal, if present
if( mesh->HasNormals())
mesh->mNormals[newIndex] = sourceMesh->mNormals[sourceMesh->mNormFaces[f].mIndices[d]];
// texture coord sets
for( unsigned int e = 0; e < AI_MAX_NUMBER_OF_TEXTURECOORDS; e++)
{
if( mesh->HasTextureCoords( e))
{
aiVector2D tex = sourceMesh->mTexCoords[e][pf.mIndices[d]];
mesh->mTextureCoords[e][newIndex] = aiVector3D( tex.x, 1.0f - tex.y, 0.0f);
}
}
// vertex color sets
for( unsigned int e = 0; e < AI_MAX_NUMBER_OF_COLOR_SETS; e++)
if( mesh->HasVertexColors( e))
mesh->mColors[e][newIndex] = sourceMesh->mColors[e][pf.mIndices[d]];
newIndex++;
}
}
// there should be as much new vertices as we calculated before
assert( newIndex == numVertices);
// convert all bones of the source mesh which influence vertices in this newly created mesh
const std::vector<XFile::Bone>& bones = sourceMesh->mBones;
std::vector<aiBone*> newBones;
for( unsigned int c = 0; c < bones.size(); c++)
{
const XFile::Bone& obone = bones[c];
// set up a vertex-linear array of the weights for quick searching if a bone influences a vertex
std::vector<float> oldWeights( sourceMesh->mPositions.size(), 0.0f);
for( unsigned int d = 0; d < obone.mWeights.size(); d++)
oldWeights[obone.mWeights[d].mVertex] = obone.mWeights[d].mWeight;
// collect all vertex weights that influence a vertex in the new mesh
std::vector<aiVertexWeight> newWeights;
newWeights.reserve( numVertices);
for( unsigned int d = 0; d < orgPoints.size(); d++)
{
// does the new vertex stem from an old vertex which was influenced by this bone?
float w = oldWeights[orgPoints[d]];
if( w > 0.0f)
newWeights.push_back( aiVertexWeight( d, w));
}
// if the bone has no weights in the newly created mesh, ignore it
if( newWeights.size() == 0)
continue;
// create
aiBone* nbone = new aiBone;
newBones.push_back( nbone);
// copy name and matrix
nbone->mName.Set( obone.mName);
nbone->mOffsetMatrix = obone.mOffsetMatrix;
nbone->mNumWeights = (unsigned int)newWeights.size();
nbone->mWeights = new aiVertexWeight[nbone->mNumWeights];
for( unsigned int d = 0; d < newWeights.size(); d++)
nbone->mWeights[d] = newWeights[d];
}
// store the bones in the mesh
mesh->mNumBones = (unsigned int)newBones.size();
if( newBones.size() > 0)
{
mesh->mBones = new aiBone*[mesh->mNumBones];
std::copy( newBones.begin(), newBones.end(), mesh->mBones);
}
}
}
// reallocate scene mesh array to be large enough
aiMesh** prevArray = pScene->mMeshes;
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes + meshes.size()];
if( prevArray)
{
memcpy( pScene->mMeshes, prevArray, pScene->mNumMeshes * sizeof( aiMesh*));
delete [] prevArray;
}
// allocate mesh index array in the node
pNode->mNumMeshes = (unsigned int)meshes.size();
pNode->mMeshes = new unsigned int[pNode->mNumMeshes];
// store all meshes in the mesh library of the scene and store their indices in the node
for( unsigned int a = 0; a < meshes.size(); a++)
{
pScene->mMeshes[pScene->mNumMeshes] = meshes[a];
pNode->mMeshes[a] = pScene->mNumMeshes;
pScene->mNumMeshes++;
}
}
// ------------------------------------------------------------------------------------------------
// Converts the animations from the given imported data and creates them in the scene.
void XFileImporter::CreateAnimations( aiScene* pScene, const XFile::Scene* pData)
{
std::vector<aiAnimation*> newAnims;
for( unsigned int a = 0; a < pData->mAnims.size(); a++)
{
const XFile::Animation* anim = pData->mAnims[a];
// some exporters mock me with empty animation tags.
if( anim->mAnims.size() == 0)
continue;
// create a new animation to hold the data
aiAnimation* nanim = new aiAnimation;
newAnims.push_back( nanim);
nanim->mName.Set( anim->mName);
// duration will be determined by the maximum length
nanim->mDuration = 0;
nanim->mTicksPerSecond = pData->mAnimTicksPerSecond;
nanim->mNumChannels = (unsigned int)anim->mAnims.size();
nanim->mChannels = new aiNodeAnim*[nanim->mNumChannels];
for( unsigned int b = 0; b < anim->mAnims.size(); b++)
{
const XFile::AnimBone* bone = anim->mAnims[b];
aiNodeAnim* nbone = new aiNodeAnim;
nbone->mNodeName.Set( bone->mBoneName);
nanim->mChannels[b] = nbone;
// keyframes are given as combined transformation matrix keys
if( bone->mTrafoKeys.size() > 0)
{
nbone->mNumPositionKeys = (unsigned int)bone->mTrafoKeys.size();
nbone->mPositionKeys = new aiVectorKey[nbone->mNumPositionKeys];
nbone->mNumRotationKeys = (unsigned int)bone->mTrafoKeys.size();
nbone->mRotationKeys = new aiQuatKey[nbone->mNumRotationKeys];
nbone->mNumScalingKeys = (unsigned int)bone->mTrafoKeys.size();
nbone->mScalingKeys = new aiVectorKey[nbone->mNumScalingKeys];
for( unsigned int c = 0; c < bone->mTrafoKeys.size(); c++)
{
// deconstruct each matrix into separate position, rotation and scaling
double time = bone->mTrafoKeys[c].mTime;
aiMatrix4x4 trafo = bone->mTrafoKeys[c].mMatrix;
// extract position
aiVector3D pos( trafo.a4, trafo.b4, trafo.c4);
nbone->mPositionKeys[c].mTime = time;
nbone->mPositionKeys[c].mValue = pos;
// extract scaling
aiVector3D scale;
scale.x = aiVector3D( trafo.a1, trafo.b1, trafo.c1).Length();
scale.y = aiVector3D( trafo.a2, trafo.b2, trafo.c2).Length();
scale.z = aiVector3D( trafo.a3, trafo.b3, trafo.c3).Length();
nbone->mScalingKeys[c].mTime = time;
nbone->mScalingKeys[c].mValue = scale;
// reconstruct rotation matrix without scaling
aiMatrix3x3 rotmat(
trafo.a1 / scale.x, trafo.a2 / scale.y, trafo.a3 / scale.z,
trafo.b1 / scale.x, trafo.b2 / scale.y, trafo.b3 / scale.z,
trafo.c1 / scale.x, trafo.c2 / scale.y, trafo.c3 / scale.z);
// and convert it into a quaternion
nbone->mRotationKeys[c].mTime = time;
nbone->mRotationKeys[c].mValue = aiQuaternion( rotmat);
}
// longest lasting key sequence determines duration
nanim->mDuration = std::max( nanim->mDuration, bone->mTrafoKeys.back().mTime);
} else
{
// separate key sequences for position, rotation, scaling
nbone->mNumPositionKeys = (unsigned int)bone->mPosKeys.size();
nbone->mPositionKeys = new aiVectorKey[nbone->mNumPositionKeys];
for( unsigned int c = 0; c < nbone->mNumPositionKeys; c++)
{
aiVector3D pos = bone->mPosKeys[c].mValue;
nbone->mPositionKeys[c].mTime = bone->mPosKeys[c].mTime;
nbone->mPositionKeys[c].mValue = pos;
}
// rotation
nbone->mNumRotationKeys = (unsigned int)bone->mRotKeys.size();
nbone->mRotationKeys = new aiQuatKey[nbone->mNumRotationKeys];
for( unsigned int c = 0; c < nbone->mNumRotationKeys; c++)
{
aiMatrix3x3 rotmat = bone->mRotKeys[c].mValue.GetMatrix();
nbone->mRotationKeys[c].mTime = bone->mRotKeys[c].mTime;
nbone->mRotationKeys[c].mValue = aiQuaternion( rotmat);
nbone->mRotationKeys[c].mValue.w *= -1.0f; // needs quat inversion
}
// scaling
nbone->mNumScalingKeys = (unsigned int)bone->mScaleKeys.size();
nbone->mScalingKeys = new aiVectorKey[nbone->mNumScalingKeys];
for( unsigned int c = 0; c < nbone->mNumScalingKeys; c++)
nbone->mScalingKeys[c] = bone->mScaleKeys[c];
// longest lasting key sequence determines duration
if( bone->mPosKeys.size() > 0)
nanim->mDuration = std::max( nanim->mDuration, bone->mPosKeys.back().mTime);
if( bone->mRotKeys.size() > 0)
nanim->mDuration = std::max( nanim->mDuration, bone->mRotKeys.back().mTime);
if( bone->mScaleKeys.size() > 0)
nanim->mDuration = std::max( nanim->mDuration, bone->mScaleKeys.back().mTime);
}
}
}
// store all converted animations in the scene
if( newAnims.size() > 0)
{
pScene->mNumAnimations = (unsigned int)newAnims.size();
pScene->mAnimations = new aiAnimation* [pScene->mNumAnimations];
for( unsigned int a = 0; a < newAnims.size(); a++)
pScene->mAnimations[a] = newAnims[a];
}
}
// ------------------------------------------------------------------------------------------------
// Converts all materials in the given array and stores them in the scene's material list.
void XFileImporter::ConvertMaterials( aiScene* pScene, const std::vector<XFile::Material>& pMaterials)
{
// count the non-referrer materials in the array
unsigned int numMaterials = 0;
for( unsigned int a = 0; a < pMaterials.size(); a++)
if( !pMaterials[a].mIsReference)
numMaterials++;
if( numMaterials == 0)
return;
// resize the scene's material list to offer enough space for the new materials
aiMaterial** prevMats = pScene->mMaterials;
pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials + numMaterials];
if( prevMats)
{
memcpy( pScene->mMaterials, prevMats, pScene->mNumMaterials * sizeof( aiMaterial*));
delete [] prevMats;
}
// convert all the materials given in the array
for( unsigned int a = 0; a < pMaterials.size(); a++)
{
const XFile::Material& oldMat = pMaterials[a];
if( oldMat.mIsReference)
continue;
aiMaterial* mat = new aiMaterial;
aiString name;
name.Set( oldMat.mName);
mat->AddProperty( &name, AI_MATKEY_NAME);
// Shading model: hardcoded to PHONG, there is no such information in an XFile
// FIX (aramis): If the specular exponent is 0, use gouraud shading. This is a bugfix
// for some models in the SDK (e.g. good old tiny.x)
int shadeMode = (int)oldMat.mSpecularExponent == 0.0f
? aiShadingMode_Gouraud : aiShadingMode_Phong;
mat->AddProperty<int>( &shadeMode, 1, AI_MATKEY_SHADING_MODEL);
// material colours
// FIX: Setup this as ambient not as emissive color
mat->AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_AMBIENT);
mat->AddProperty( &oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
mat->AddProperty( &oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
mat->AddProperty( &oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
// texture, if there is one
if (1 == oldMat.mTextures.size())
{
const XFile::TexEntry& otex = oldMat.mTextures.back();
if (otex.mName.length())
{
// if there is only one texture assume it contains the diffuse color
aiString tex( otex.mName);
if( otex.mIsNormalMap)
mat->AddProperty( &tex, AI_MATKEY_TEXTURE_NORMALS(0));
else
mat->AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
}
}
else
{
// Otherwise ... try to search for typical strings in the
// texture's file name like 'bump' or 'diffuse'
unsigned int iHM = 0,iNM = 0,iDM = 0,iSM = 0,iAM = 0,iEM = 0;
for( unsigned int b = 0; b < oldMat.mTextures.size(); b++)
{
const XFile::TexEntry& otex = oldMat.mTextures[b];
std::string sz = otex.mName;
if (!sz.length())continue;
// find the file name
//const size_t iLen = sz.length();
std::string::size_type s = sz.find_last_of("\\/");
if (std::string::npos == s)
s = 0;
// cut off the file extension
std::string::size_type sExt = sz.find_last_of('.');
if (std::string::npos != sExt){
sz[sExt] = '\0';
}
// convert to lower case for easier comparision
for( unsigned int c = 0; c < sz.length(); c++)
if( isalpha( sz[c]))
sz[c] = tolower( sz[c]);
// Place texture filename property under the corresponding name
aiString tex( oldMat.mTextures[b].mName);
// bump map
if (std::string::npos != sz.find("bump", s) || std::string::npos != sz.find("height", s))
{
mat->AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(iHM++));
} else
if (otex.mIsNormalMap || std::string::npos != sz.find( "normal", s) || std::string::npos != sz.find("nm", s))
{
mat->AddProperty( &tex, AI_MATKEY_TEXTURE_NORMALS(iNM++));
} else
if (std::string::npos != sz.find( "spec", s) || std::string::npos != sz.find( "glanz", s))
{
mat->AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(iSM++));
} else
if (std::string::npos != sz.find( "ambi", s) || std::string::npos != sz.find( "env", s))
{
mat->AddProperty( &tex, AI_MATKEY_TEXTURE_AMBIENT(iAM++));
} else
if (std::string::npos != sz.find( "emissive", s) || std::string::npos != sz.find( "self", s))
{
mat->AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(iEM++));
} else
{
// Assume it is a diffuse texture
mat->AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(iDM++));
}
}
}
pScene->mMaterials[pScene->mNumMaterials] = mat;
mImportedMats[oldMat.mName] = pScene->mNumMaterials;
pScene->mNumMaterials++;
}
}
#endif // !! ASSIMP_BUILD_NO_X_IMPORTER
|