vrshoot

annotate libs/assimp/SMDLoader.cpp @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
rev   line source
nuclear@0 1 /*
nuclear@0 2 ---------------------------------------------------------------------------
nuclear@0 3 Open Asset Import Library (assimp)
nuclear@0 4 ---------------------------------------------------------------------------
nuclear@0 5
nuclear@0 6 Copyright (c) 2006-2012, assimp team
nuclear@0 7
nuclear@0 8 All rights reserved.
nuclear@0 9
nuclear@0 10 Redistribution and use of this software in source and binary forms,
nuclear@0 11 with or without modification, are permitted provided that the following
nuclear@0 12 conditions are met:
nuclear@0 13
nuclear@0 14 * Redistributions of source code must retain the above
nuclear@0 15 copyright notice, this list of conditions and the
nuclear@0 16 following disclaimer.
nuclear@0 17
nuclear@0 18 * Redistributions in binary form must reproduce the above
nuclear@0 19 copyright notice, this list of conditions and the
nuclear@0 20 following disclaimer in the documentation and/or other
nuclear@0 21 materials provided with the distribution.
nuclear@0 22
nuclear@0 23 * Neither the name of the assimp team, nor the names of its
nuclear@0 24 contributors may be used to endorse or promote products
nuclear@0 25 derived from this software without specific prior
nuclear@0 26 written permission of the assimp team.
nuclear@0 27
nuclear@0 28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
nuclear@0 29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
nuclear@0 30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
nuclear@0 31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
nuclear@0 32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
nuclear@0 33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
nuclear@0 34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
nuclear@0 35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
nuclear@0 36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
nuclear@0 37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nuclear@0 38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nuclear@0 39 ---------------------------------------------------------------------------
nuclear@0 40 */
nuclear@0 41
nuclear@0 42 /** @file SMDLoader.cpp
nuclear@0 43 * @brief Implementation of the SMD importer class
nuclear@0 44 */
nuclear@0 45
nuclear@0 46 #include "AssimpPCH.h"
nuclear@0 47 #ifndef ASSIMP_BUILD_NO_SMD_IMPORTER
nuclear@0 48
nuclear@0 49 // internal headers
nuclear@0 50 #include "SMDLoader.h"
nuclear@0 51 #include "fast_atof.h"
nuclear@0 52 #include "SkeletonMeshBuilder.h"
nuclear@0 53
nuclear@0 54 using namespace Assimp;
nuclear@0 55
nuclear@0 56 static const aiImporterDesc desc = {
nuclear@0 57 "Valve SMD Importer",
nuclear@0 58 "",
nuclear@0 59 "",
nuclear@0 60 "",
nuclear@0 61 aiImporterFlags_SupportTextFlavour,
nuclear@0 62 0,
nuclear@0 63 0,
nuclear@0 64 0,
nuclear@0 65 0,
nuclear@0 66 "smd vta"
nuclear@0 67 };
nuclear@0 68
nuclear@0 69 // ------------------------------------------------------------------------------------------------
nuclear@0 70 // Constructor to be privately used by Importer
nuclear@0 71 SMDImporter::SMDImporter()
nuclear@0 72 {}
nuclear@0 73
nuclear@0 74 // ------------------------------------------------------------------------------------------------
nuclear@0 75 // Destructor, private as well
nuclear@0 76 SMDImporter::~SMDImporter()
nuclear@0 77 {}
nuclear@0 78
nuclear@0 79 // ------------------------------------------------------------------------------------------------
nuclear@0 80 // Returns whether the class can handle the format of the given file.
nuclear@0 81 bool SMDImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool) const
nuclear@0 82 {
nuclear@0 83 // fixme: auto format detection
nuclear@0 84 return SimpleExtensionCheck(pFile,"smd","vta");
nuclear@0 85 }
nuclear@0 86
nuclear@0 87 // ------------------------------------------------------------------------------------------------
nuclear@0 88 // Get a list of all supported file extensions
nuclear@0 89 const aiImporterDesc* SMDImporter::GetInfo () const
nuclear@0 90 {
nuclear@0 91 return &desc;
nuclear@0 92 }
nuclear@0 93
nuclear@0 94 // ------------------------------------------------------------------------------------------------
nuclear@0 95 // Setup configuration properties
nuclear@0 96 void SMDImporter::SetupProperties(const Importer* pImp)
nuclear@0 97 {
nuclear@0 98 // The
nuclear@0 99 // AI_CONFIG_IMPORT_SMD_KEYFRAME option overrides the
nuclear@0 100 // AI_CONFIG_IMPORT_GLOBAL_KEYFRAME option.
nuclear@0 101 configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_SMD_KEYFRAME,-1);
nuclear@0 102 if(static_cast<unsigned int>(-1) == configFrameID) {
nuclear@0 103 configFrameID = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_GLOBAL_KEYFRAME,0);
nuclear@0 104 }
nuclear@0 105 }
nuclear@0 106
nuclear@0 107 // ------------------------------------------------------------------------------------------------
nuclear@0 108 // Imports the given file into the given scene structure.
nuclear@0 109 void SMDImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
nuclear@0 110 {
nuclear@0 111 boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
nuclear@0 112
nuclear@0 113 // Check whether we can read from the file
nuclear@0 114 if( file.get() == NULL) {
nuclear@0 115 throw DeadlyImportError( "Failed to open SMD/VTA file " + pFile + ".");
nuclear@0 116 }
nuclear@0 117
nuclear@0 118 iFileSize = (unsigned int)file->FileSize();
nuclear@0 119
nuclear@0 120 // Allocate storage and copy the contents of the file to a memory buffer
nuclear@0 121 this->pScene = pScene;
nuclear@0 122
nuclear@0 123 std::vector<char> buff(iFileSize+1);
nuclear@0 124 TextFileToBuffer(file.get(),buff);
nuclear@0 125 mBuffer = &buff[0];
nuclear@0 126
nuclear@0 127 iSmallestFrame = (1 << 31);
nuclear@0 128 bHasUVs = true;
nuclear@0 129 iLineNumber = 1;
nuclear@0 130
nuclear@0 131 // Reserve enough space for ... hm ... 10 textures
nuclear@0 132 aszTextures.reserve(10);
nuclear@0 133
nuclear@0 134 // Reserve enough space for ... hm ... 1000 triangles
nuclear@0 135 asTriangles.reserve(1000);
nuclear@0 136
nuclear@0 137 // Reserve enough space for ... hm ... 20 bones
nuclear@0 138 asBones.reserve(20);
nuclear@0 139
nuclear@0 140
nuclear@0 141 // parse the file ...
nuclear@0 142 ParseFile();
nuclear@0 143
nuclear@0 144 // If there are no triangles it seems to be an animation SMD,
nuclear@0 145 // containing only the animation skeleton.
nuclear@0 146 if (asTriangles.empty())
nuclear@0 147 {
nuclear@0 148 if (asBones.empty())
nuclear@0 149 {
nuclear@0 150 throw DeadlyImportError("SMD: No triangles and no bones have "
nuclear@0 151 "been found in the file. This file seems to be invalid.");
nuclear@0 152 }
nuclear@0 153
nuclear@0 154 // Set the flag in the scene structure which indicates
nuclear@0 155 // that there is nothing than an animation skeleton
nuclear@0 156 pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
nuclear@0 157 }
nuclear@0 158
nuclear@0 159 if (!asBones.empty())
nuclear@0 160 {
nuclear@0 161 // Check whether all bones have been initialized
nuclear@0 162 for (std::vector<SMD::Bone>::const_iterator
nuclear@0 163 i = asBones.begin();
nuclear@0 164 i != asBones.end();++i)
nuclear@0 165 {
nuclear@0 166 if (!(*i).mName.length())
nuclear@0 167 {
nuclear@0 168 DefaultLogger::get()->warn("SMD: Not all bones have been initialized");
nuclear@0 169 break;
nuclear@0 170 }
nuclear@0 171 }
nuclear@0 172
nuclear@0 173 // now fix invalid time values and make sure the animation starts at frame 0
nuclear@0 174 FixTimeValues();
nuclear@0 175
nuclear@0 176 // compute absolute bone transformation matrices
nuclear@0 177 // ComputeAbsoluteBoneTransformations();
nuclear@0 178 }
nuclear@0 179
nuclear@0 180 if (!(pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE))
nuclear@0 181 {
nuclear@0 182 // create output meshes
nuclear@0 183 CreateOutputMeshes();
nuclear@0 184
nuclear@0 185 // build an output material list
nuclear@0 186 CreateOutputMaterials();
nuclear@0 187 }
nuclear@0 188
nuclear@0 189 // build the output animation
nuclear@0 190 CreateOutputAnimations();
nuclear@0 191
nuclear@0 192 // build output nodes (bones are added as empty dummy nodes)
nuclear@0 193 CreateOutputNodes();
nuclear@0 194
nuclear@0 195 if (pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE)
nuclear@0 196 {
nuclear@0 197 SkeletonMeshBuilder skeleton(pScene);
nuclear@0 198 }
nuclear@0 199 }
nuclear@0 200 // ------------------------------------------------------------------------------------------------
nuclear@0 201 // Write an error message with line number to the log file
nuclear@0 202 void SMDImporter::LogErrorNoThrow(const char* msg)
nuclear@0 203 {
nuclear@0 204 char szTemp[1024];
nuclear@0 205 sprintf(szTemp,"Line %i: %s",iLineNumber,msg);
nuclear@0 206 DefaultLogger::get()->error(szTemp);
nuclear@0 207 }
nuclear@0 208
nuclear@0 209 // ------------------------------------------------------------------------------------------------
nuclear@0 210 // Write a warning with line number to the log file
nuclear@0 211 void SMDImporter::LogWarning(const char* msg)
nuclear@0 212 {
nuclear@0 213 char szTemp[1024];
nuclear@0 214 ai_assert(strlen(msg) < 1000);
nuclear@0 215 sprintf(szTemp,"Line %i: %s",iLineNumber,msg);
nuclear@0 216 DefaultLogger::get()->warn(szTemp);
nuclear@0 217 }
nuclear@0 218
nuclear@0 219 // ------------------------------------------------------------------------------------------------
nuclear@0 220 // Fix invalid time values in the file
nuclear@0 221 void SMDImporter::FixTimeValues()
nuclear@0 222 {
nuclear@0 223 double dDelta = (double)iSmallestFrame;
nuclear@0 224 double dMax = 0.0f;
nuclear@0 225 for (std::vector<SMD::Bone>::iterator
nuclear@0 226 iBone = asBones.begin();
nuclear@0 227 iBone != asBones.end();++iBone)
nuclear@0 228 {
nuclear@0 229 for (std::vector<SMD::Bone::Animation::MatrixKey>::iterator
nuclear@0 230 iKey = (*iBone).sAnim.asKeys.begin();
nuclear@0 231 iKey != (*iBone).sAnim.asKeys.end();++iKey)
nuclear@0 232 {
nuclear@0 233 (*iKey).dTime -= dDelta;
nuclear@0 234 dMax = std::max(dMax, (*iKey).dTime);
nuclear@0 235 }
nuclear@0 236 }
nuclear@0 237 dLengthOfAnim = dMax;
nuclear@0 238 }
nuclear@0 239
nuclear@0 240 // ------------------------------------------------------------------------------------------------
nuclear@0 241 // create output meshes
nuclear@0 242 void SMDImporter::CreateOutputMeshes()
nuclear@0 243 {
nuclear@0 244 if (aszTextures.empty())
nuclear@0 245 aszTextures.push_back(std::string());
nuclear@0 246
nuclear@0 247 // we need to sort all faces by their material index
nuclear@0 248 // in opposition to other loaders we can be sure that each
nuclear@0 249 // material is at least used once.
nuclear@0 250 pScene->mNumMeshes = (unsigned int) aszTextures.size();
nuclear@0 251 pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
nuclear@0 252
nuclear@0 253 typedef std::vector<unsigned int> FaceList;
nuclear@0 254 FaceList* aaiFaces = new FaceList[pScene->mNumMeshes];
nuclear@0 255
nuclear@0 256 // approximate the space that will be required
nuclear@0 257 unsigned int iNum = (unsigned int)asTriangles.size() / pScene->mNumMeshes;
nuclear@0 258 iNum += iNum >> 1;
nuclear@0 259 for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
nuclear@0 260 aaiFaces[i].reserve(iNum);
nuclear@0 261
nuclear@0 262
nuclear@0 263 // collect all faces
nuclear@0 264 iNum = 0;
nuclear@0 265 for (std::vector<SMD::Face>::const_iterator
nuclear@0 266 iFace = asTriangles.begin();
nuclear@0 267 iFace != asTriangles.end();++iFace,++iNum)
nuclear@0 268 {
nuclear@0 269 if (UINT_MAX == (*iFace).iTexture)aaiFaces[(*iFace).iTexture].push_back( 0 );
nuclear@0 270 else if ((*iFace).iTexture >= aszTextures.size())
nuclear@0 271 {
nuclear@0 272 DefaultLogger::get()->error("[SMD/VTA] Material index overflow in face");
nuclear@0 273 aaiFaces[(*iFace).iTexture].push_back((unsigned int)aszTextures.size()-1);
nuclear@0 274 }
nuclear@0 275 else aaiFaces[(*iFace).iTexture].push_back(iNum);
nuclear@0 276 }
nuclear@0 277
nuclear@0 278 // now create the output meshes
nuclear@0 279 for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
nuclear@0 280 {
nuclear@0 281 aiMesh*& pcMesh = pScene->mMeshes[i] = new aiMesh();
nuclear@0 282 ai_assert(!aaiFaces[i].empty()); // should not be empty ...
nuclear@0 283
nuclear@0 284 pcMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
nuclear@0 285 pcMesh->mNumVertices = (unsigned int)aaiFaces[i].size()*3;
nuclear@0 286 pcMesh->mNumFaces = (unsigned int)aaiFaces[i].size();
nuclear@0 287 pcMesh->mMaterialIndex = i;
nuclear@0 288
nuclear@0 289 // storage for bones
nuclear@0 290 typedef std::pair<unsigned int,float> TempWeightListEntry;
nuclear@0 291 typedef std::vector< TempWeightListEntry > TempBoneWeightList;
nuclear@0 292
nuclear@0 293 TempBoneWeightList* aaiBones = new TempBoneWeightList[asBones.size()]();
nuclear@0 294
nuclear@0 295 // try to reserve enough memory without wasting too much
nuclear@0 296 for (unsigned int iBone = 0; iBone < asBones.size();++iBone)
nuclear@0 297 {
nuclear@0 298 aaiBones[iBone].reserve(pcMesh->mNumVertices/asBones.size());
nuclear@0 299 }
nuclear@0 300
nuclear@0 301 // allocate storage
nuclear@0 302 pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
nuclear@0 303 aiVector3D* pcNormals = pcMesh->mNormals = new aiVector3D[pcMesh->mNumVertices];
nuclear@0 304 aiVector3D* pcVerts = pcMesh->mVertices = new aiVector3D[pcMesh->mNumVertices];
nuclear@0 305
nuclear@0 306 aiVector3D* pcUVs = NULL;
nuclear@0 307 if (bHasUVs)
nuclear@0 308 {
nuclear@0 309 pcUVs = pcMesh->mTextureCoords[0] = new aiVector3D[pcMesh->mNumVertices];
nuclear@0 310 pcMesh->mNumUVComponents[0] = 2;
nuclear@0 311 }
nuclear@0 312
nuclear@0 313 iNum = 0;
nuclear@0 314 for (unsigned int iFace = 0; iFace < pcMesh->mNumFaces;++iFace)
nuclear@0 315 {
nuclear@0 316 pcMesh->mFaces[iFace].mIndices = new unsigned int[3];
nuclear@0 317 pcMesh->mFaces[iFace].mNumIndices = 3;
nuclear@0 318
nuclear@0 319 // fill the vertices
nuclear@0 320 unsigned int iSrcFace = aaiFaces[i][iFace];
nuclear@0 321 SMD::Face& face = asTriangles[iSrcFace];
nuclear@0 322
nuclear@0 323 *pcVerts++ = face.avVertices[0].pos;
nuclear@0 324 *pcVerts++ = face.avVertices[1].pos;
nuclear@0 325 *pcVerts++ = face.avVertices[2].pos;
nuclear@0 326
nuclear@0 327 // fill the normals
nuclear@0 328 *pcNormals++ = face.avVertices[0].nor;
nuclear@0 329 *pcNormals++ = face.avVertices[1].nor;
nuclear@0 330 *pcNormals++ = face.avVertices[2].nor;
nuclear@0 331
nuclear@0 332 // fill the texture coordinates
nuclear@0 333 if (pcUVs)
nuclear@0 334 {
nuclear@0 335 *pcUVs++ = face.avVertices[0].uv;
nuclear@0 336 *pcUVs++ = face.avVertices[1].uv;
nuclear@0 337 *pcUVs++ = face.avVertices[2].uv;
nuclear@0 338 }
nuclear@0 339
nuclear@0 340 for (unsigned int iVert = 0; iVert < 3;++iVert)
nuclear@0 341 {
nuclear@0 342 float fSum = 0.0f;
nuclear@0 343 for (unsigned int iBone = 0;iBone < face.avVertices[iVert].aiBoneLinks.size();++iBone)
nuclear@0 344 {
nuclear@0 345 TempWeightListEntry& pairval = face.avVertices[iVert].aiBoneLinks[iBone];
nuclear@0 346
nuclear@0 347 // FIX: The second check is here just to make sure we won't
nuclear@0 348 // assign more than one weight to a single vertex index
nuclear@0 349 if (pairval.first >= asBones.size() ||
nuclear@0 350 pairval.first == face.avVertices[iVert].iParentNode)
nuclear@0 351 {
nuclear@0 352 DefaultLogger::get()->error("[SMD/VTA] Bone index overflow. "
nuclear@0 353 "The bone index will be ignored, the weight will be assigned "
nuclear@0 354 "to the vertex' parent node");
nuclear@0 355 continue;
nuclear@0 356 }
nuclear@0 357 aaiBones[pairval.first].push_back(TempWeightListEntry(iNum,pairval.second));
nuclear@0 358 fSum += pairval.second;
nuclear@0 359 }
nuclear@0 360 // ******************************************************************
nuclear@0 361 // If the sum of all vertex weights is not 1.0 we must assign
nuclear@0 362 // the rest to the vertex' parent node. Well, at least the doc says
nuclear@0 363 // we should ...
nuclear@0 364 // FIX: We use 0.975 as limit, floating-point inaccuracies seem to
nuclear@0 365 // be very strong in some SMD exporters. Furthermore it is possible
nuclear@0 366 // that the parent of a vertex is 0xffffffff (if the corresponding
nuclear@0 367 // entry in the file was unreadable)
nuclear@0 368 // ******************************************************************
nuclear@0 369 if (fSum < 0.975f && face.avVertices[iVert].iParentNode != UINT_MAX)
nuclear@0 370 {
nuclear@0 371 if (face.avVertices[iVert].iParentNode >= asBones.size())
nuclear@0 372 {
nuclear@0 373 DefaultLogger::get()->error("[SMD/VTA] Bone index overflow. "
nuclear@0 374 "The index of the vertex parent bone is invalid. "
nuclear@0 375 "The remaining weights will be normalized to 1.0");
nuclear@0 376
nuclear@0 377 if (fSum)
nuclear@0 378 {
nuclear@0 379 fSum = 1 / fSum;
nuclear@0 380 for (unsigned int iBone = 0;iBone < face.avVertices[iVert].aiBoneLinks.size();++iBone)
nuclear@0 381 {
nuclear@0 382 TempWeightListEntry& pairval = face.avVertices[iVert].aiBoneLinks[iBone];
nuclear@0 383 if (pairval.first >= asBones.size())continue;
nuclear@0 384 aaiBones[pairval.first].back().second *= fSum;
nuclear@0 385 }
nuclear@0 386 }
nuclear@0 387 }
nuclear@0 388 else
nuclear@0 389 {
nuclear@0 390 aaiBones[face.avVertices[iVert].iParentNode].push_back(
nuclear@0 391 TempWeightListEntry(iNum,1.0f-fSum));
nuclear@0 392 }
nuclear@0 393 }
nuclear@0 394 pcMesh->mFaces[iFace].mIndices[iVert] = iNum++;
nuclear@0 395 }
nuclear@0 396 }
nuclear@0 397
nuclear@0 398 // now build all bones of the mesh
nuclear@0 399 iNum = 0;
nuclear@0 400 for (unsigned int iBone = 0; iBone < asBones.size();++iBone)
nuclear@0 401 if (!aaiBones[iBone].empty())++iNum;
nuclear@0 402
nuclear@0 403 if (false && iNum)
nuclear@0 404 {
nuclear@0 405 pcMesh->mNumBones = iNum;
nuclear@0 406 pcMesh->mBones = new aiBone*[pcMesh->mNumBones];
nuclear@0 407 iNum = 0;
nuclear@0 408 for (unsigned int iBone = 0; iBone < asBones.size();++iBone)
nuclear@0 409 {
nuclear@0 410 if (aaiBones[iBone].empty())continue;
nuclear@0 411 aiBone*& bone = pcMesh->mBones[iNum] = new aiBone();
nuclear@0 412
nuclear@0 413 bone->mNumWeights = (unsigned int)aaiBones[iBone].size();
nuclear@0 414 bone->mWeights = new aiVertexWeight[bone->mNumWeights];
nuclear@0 415 bone->mOffsetMatrix = asBones[iBone].mOffsetMatrix;
nuclear@0 416 bone->mName.Set( asBones[iBone].mName );
nuclear@0 417
nuclear@0 418 asBones[iBone].bIsUsed = true;
nuclear@0 419
nuclear@0 420 for (unsigned int iWeight = 0; iWeight < bone->mNumWeights;++iWeight)
nuclear@0 421 {
nuclear@0 422 bone->mWeights[iWeight].mVertexId = aaiBones[iBone][iWeight].first;
nuclear@0 423 bone->mWeights[iWeight].mWeight = aaiBones[iBone][iWeight].second;
nuclear@0 424 }
nuclear@0 425 ++iNum;
nuclear@0 426 }
nuclear@0 427 }
nuclear@0 428 delete[] aaiBones;
nuclear@0 429 }
nuclear@0 430 delete[] aaiFaces;
nuclear@0 431 }
nuclear@0 432
nuclear@0 433 // ------------------------------------------------------------------------------------------------
nuclear@0 434 // add bone child nodes
nuclear@0 435 void SMDImporter::AddBoneChildren(aiNode* pcNode, uint32_t iParent)
nuclear@0 436 {
nuclear@0 437 ai_assert(NULL != pcNode && 0 == pcNode->mNumChildren && NULL == pcNode->mChildren);
nuclear@0 438
nuclear@0 439 // first count ...
nuclear@0 440 for (unsigned int i = 0; i < asBones.size();++i)
nuclear@0 441 {
nuclear@0 442 SMD::Bone& bone = asBones[i];
nuclear@0 443 if (bone.iParent == iParent)++pcNode->mNumChildren;
nuclear@0 444 }
nuclear@0 445
nuclear@0 446 // now allocate the output array
nuclear@0 447 pcNode->mChildren = new aiNode*[pcNode->mNumChildren];
nuclear@0 448
nuclear@0 449 // and fill all subnodes
nuclear@0 450 unsigned int qq = 0;
nuclear@0 451 for (unsigned int i = 0; i < asBones.size();++i)
nuclear@0 452 {
nuclear@0 453 SMD::Bone& bone = asBones[i];
nuclear@0 454 if (bone.iParent != iParent)continue;
nuclear@0 455
nuclear@0 456 aiNode* pc = pcNode->mChildren[qq++] = new aiNode();
nuclear@0 457 pc->mName.Set(bone.mName);
nuclear@0 458
nuclear@0 459 // store the local transformation matrix of the bind pose
nuclear@0 460 pc->mTransformation = bone.sAnim.asKeys[bone.sAnim.iFirstTimeKey].matrix;
nuclear@0 461 pc->mParent = pcNode;
nuclear@0 462
nuclear@0 463 // add children to this node, too
nuclear@0 464 AddBoneChildren(pc,i);
nuclear@0 465 }
nuclear@0 466 }
nuclear@0 467
nuclear@0 468 // ------------------------------------------------------------------------------------------------
nuclear@0 469 // create output nodes
nuclear@0 470 void SMDImporter::CreateOutputNodes()
nuclear@0 471 {
nuclear@0 472 pScene->mRootNode = new aiNode();
nuclear@0 473 if (!(pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE))
nuclear@0 474 {
nuclear@0 475 // create one root node that renders all meshes
nuclear@0 476 pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
nuclear@0 477 pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
nuclear@0 478 for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
nuclear@0 479 pScene->mRootNode->mMeshes[i] = i;
nuclear@0 480 }
nuclear@0 481
nuclear@0 482 // now add all bones as dummy sub nodes to the graph
nuclear@0 483 // AddBoneChildren(pScene->mRootNode,(uint32_t)-1);
nuclear@0 484
nuclear@0 485 // if we have only one bone we can even remove the root node
nuclear@0 486 if (pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE &&
nuclear@0 487 1 == pScene->mRootNode->mNumChildren)
nuclear@0 488 {
nuclear@0 489 aiNode* pcOldRoot = pScene->mRootNode;
nuclear@0 490 pScene->mRootNode = pcOldRoot->mChildren[0];
nuclear@0 491 pcOldRoot->mChildren[0] = NULL;
nuclear@0 492 delete pcOldRoot;
nuclear@0 493
nuclear@0 494 pScene->mRootNode->mParent = NULL;
nuclear@0 495 }
nuclear@0 496 else
nuclear@0 497 {
nuclear@0 498 ::strcpy(pScene->mRootNode->mName.data, "<SMD_root>");
nuclear@0 499 pScene->mRootNode->mName.length = 10;
nuclear@0 500 }
nuclear@0 501 }
nuclear@0 502
nuclear@0 503 // ------------------------------------------------------------------------------------------------
nuclear@0 504 // create output animations
nuclear@0 505 void SMDImporter::CreateOutputAnimations()
nuclear@0 506 {
nuclear@0 507 unsigned int iNumBones = 0;
nuclear@0 508 for (std::vector<SMD::Bone>::const_iterator
nuclear@0 509 i = asBones.begin();
nuclear@0 510 i != asBones.end();++i)
nuclear@0 511 {
nuclear@0 512 if ((*i).bIsUsed)++iNumBones;
nuclear@0 513 }
nuclear@0 514 if (!iNumBones)
nuclear@0 515 {
nuclear@0 516 // just make sure this case doesn't occur ... (it could occur
nuclear@0 517 // if the file was invalid)
nuclear@0 518 return;
nuclear@0 519 }
nuclear@0 520
nuclear@0 521 pScene->mNumAnimations = 1;
nuclear@0 522 pScene->mAnimations = new aiAnimation*[1];
nuclear@0 523 aiAnimation*& anim = pScene->mAnimations[0] = new aiAnimation();
nuclear@0 524
nuclear@0 525 anim->mDuration = dLengthOfAnim;
nuclear@0 526 anim->mNumChannels = iNumBones;
nuclear@0 527 anim->mTicksPerSecond = 25.0; // FIXME: is this correct?
nuclear@0 528
nuclear@0 529 aiNodeAnim** pp = anim->mChannels = new aiNodeAnim*[anim->mNumChannels];
nuclear@0 530
nuclear@0 531 // now build valid keys
nuclear@0 532 unsigned int a = 0;
nuclear@0 533 for (std::vector<SMD::Bone>::const_iterator
nuclear@0 534 i = asBones.begin();
nuclear@0 535 i != asBones.end();++i)
nuclear@0 536 {
nuclear@0 537 if (!(*i).bIsUsed)continue;
nuclear@0 538
nuclear@0 539 aiNodeAnim* p = pp[a] = new aiNodeAnim();
nuclear@0 540
nuclear@0 541 // copy the name of the bone
nuclear@0 542 p->mNodeName.Set( i->mName);
nuclear@0 543
nuclear@0 544 p->mNumRotationKeys = (unsigned int) (*i).sAnim.asKeys.size();
nuclear@0 545 if (p->mNumRotationKeys)
nuclear@0 546 {
nuclear@0 547 p->mNumPositionKeys = p->mNumRotationKeys;
nuclear@0 548 aiVectorKey* pVecKeys = p->mPositionKeys = new aiVectorKey[p->mNumRotationKeys];
nuclear@0 549 aiQuatKey* pRotKeys = p->mRotationKeys = new aiQuatKey[p->mNumRotationKeys];
nuclear@0 550
nuclear@0 551 for (std::vector<SMD::Bone::Animation::MatrixKey>::const_iterator
nuclear@0 552 qq = (*i).sAnim.asKeys.begin();
nuclear@0 553 qq != (*i).sAnim.asKeys.end(); ++qq)
nuclear@0 554 {
nuclear@0 555 pRotKeys->mTime = pVecKeys->mTime = (*qq).dTime;
nuclear@0 556
nuclear@0 557 // compute the rotation quaternion from the euler angles
nuclear@0 558 pRotKeys->mValue = aiQuaternion( (*qq).vRot.x, (*qq).vRot.y, (*qq).vRot.z );
nuclear@0 559 pVecKeys->mValue = (*qq).vPos;
nuclear@0 560
nuclear@0 561 ++pVecKeys; ++pRotKeys;
nuclear@0 562 }
nuclear@0 563 }
nuclear@0 564 ++a;
nuclear@0 565
nuclear@0 566 // there are no scaling keys ...
nuclear@0 567 }
nuclear@0 568 }
nuclear@0 569
nuclear@0 570 // ------------------------------------------------------------------------------------------------
nuclear@0 571 void SMDImporter::ComputeAbsoluteBoneTransformations()
nuclear@0 572 {
nuclear@0 573 // For each bone: determine the key with the lowest time value
nuclear@0 574 // theoretically the SMD format should have all keyframes
nuclear@0 575 // in order. However, I've seen a file where this wasn't true.
nuclear@0 576 for (unsigned int i = 0; i < asBones.size();++i)
nuclear@0 577 {
nuclear@0 578 SMD::Bone& bone = asBones[i];
nuclear@0 579
nuclear@0 580 uint32_t iIndex = 0;
nuclear@0 581 double dMin = 10e10;
nuclear@0 582 for (unsigned int i = 0; i < bone.sAnim.asKeys.size();++i)
nuclear@0 583 {
nuclear@0 584 double d = std::min(bone.sAnim.asKeys[i].dTime,dMin);
nuclear@0 585 if (d < dMin)
nuclear@0 586 {
nuclear@0 587 dMin = d;
nuclear@0 588 iIndex = i;
nuclear@0 589 }
nuclear@0 590 }
nuclear@0 591 bone.sAnim.iFirstTimeKey = iIndex;
nuclear@0 592 }
nuclear@0 593
nuclear@0 594 unsigned int iParent = 0;
nuclear@0 595 while (iParent < asBones.size())
nuclear@0 596 {
nuclear@0 597 for (unsigned int iBone = 0; iBone < asBones.size();++iBone)
nuclear@0 598 {
nuclear@0 599 SMD::Bone& bone = asBones[iBone];
nuclear@0 600
nuclear@0 601 if (iParent == bone.iParent)
nuclear@0 602 {
nuclear@0 603 SMD::Bone& parentBone = asBones[iParent];
nuclear@0 604
nuclear@0 605
nuclear@0 606 uint32_t iIndex = bone.sAnim.iFirstTimeKey;
nuclear@0 607 const aiMatrix4x4& mat = bone.sAnim.asKeys[iIndex].matrix;
nuclear@0 608 aiMatrix4x4& matOut = bone.sAnim.asKeys[iIndex].matrixAbsolute;
nuclear@0 609
nuclear@0 610 // The same for the parent bone ...
nuclear@0 611 iIndex = parentBone.sAnim.iFirstTimeKey;
nuclear@0 612 const aiMatrix4x4& mat2 = parentBone.sAnim.asKeys[iIndex].matrixAbsolute;
nuclear@0 613
nuclear@0 614 // Compute the absolute transformation matrix
nuclear@0 615 matOut = mat * mat2;
nuclear@0 616 }
nuclear@0 617 }
nuclear@0 618 ++iParent;
nuclear@0 619 }
nuclear@0 620
nuclear@0 621 // Store the inverse of the absolute transformation matrix
nuclear@0 622 // of the first key as bone offset matrix
nuclear@0 623 for (iParent = 0; iParent < asBones.size();++iParent)
nuclear@0 624 {
nuclear@0 625 SMD::Bone& bone = asBones[iParent];
nuclear@0 626 bone.mOffsetMatrix = bone.sAnim.asKeys[bone.sAnim.iFirstTimeKey].matrixAbsolute;
nuclear@0 627 bone.mOffsetMatrix.Inverse();
nuclear@0 628 }
nuclear@0 629 }
nuclear@0 630
nuclear@0 631 // ------------------------------------------------------------------------------------------------
nuclear@0 632 // create output materials
nuclear@0 633 void SMDImporter::CreateOutputMaterials()
nuclear@0 634 {
nuclear@0 635 pScene->mNumMaterials = (unsigned int)aszTextures.size();
nuclear@0 636 pScene->mMaterials = new aiMaterial*[std::max(1u, pScene->mNumMaterials)];
nuclear@0 637
nuclear@0 638 for (unsigned int iMat = 0; iMat < pScene->mNumMaterials;++iMat)
nuclear@0 639 {
nuclear@0 640 aiMaterial* pcMat = new aiMaterial();
nuclear@0 641 pScene->mMaterials[iMat] = pcMat;
nuclear@0 642
nuclear@0 643 aiString szName;
nuclear@0 644 szName.length = (size_t)::sprintf(szName.data,"Texture_%i",iMat);
nuclear@0 645 pcMat->AddProperty(&szName,AI_MATKEY_NAME);
nuclear@0 646
nuclear@0 647 if (aszTextures[iMat].length())
nuclear@0 648 {
nuclear@0 649 ::strcpy(szName.data, aszTextures[iMat].c_str() );
nuclear@0 650 szName.length = aszTextures[iMat].length();
nuclear@0 651 pcMat->AddProperty(&szName,AI_MATKEY_TEXTURE_DIFFUSE(0));
nuclear@0 652 }
nuclear@0 653 }
nuclear@0 654
nuclear@0 655 // create a default material if necessary
nuclear@0 656 if (0 == pScene->mNumMaterials)
nuclear@0 657 {
nuclear@0 658 pScene->mNumMaterials = 1;
nuclear@0 659
nuclear@0 660 aiMaterial* pcHelper = new aiMaterial();
nuclear@0 661 pScene->mMaterials[0] = pcHelper;
nuclear@0 662
nuclear@0 663 int iMode = (int)aiShadingMode_Gouraud;
nuclear@0 664 pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
nuclear@0 665
nuclear@0 666 aiColor3D clr;
nuclear@0 667 clr.b = clr.g = clr.r = 0.7f;
nuclear@0 668 pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
nuclear@0 669 pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
nuclear@0 670
nuclear@0 671 clr.b = clr.g = clr.r = 0.05f;
nuclear@0 672 pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
nuclear@0 673
nuclear@0 674 aiString szName;
nuclear@0 675 szName.Set(AI_DEFAULT_MATERIAL_NAME);
nuclear@0 676 pcHelper->AddProperty(&szName,AI_MATKEY_NAME);
nuclear@0 677 }
nuclear@0 678 }
nuclear@0 679
nuclear@0 680 // ------------------------------------------------------------------------------------------------
nuclear@0 681 // Parse the file
nuclear@0 682 void SMDImporter::ParseFile()
nuclear@0 683 {
nuclear@0 684 const char* szCurrent = mBuffer;
nuclear@0 685
nuclear@0 686 // read line per line ...
nuclear@0 687 for ( ;; )
nuclear@0 688 {
nuclear@0 689 if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) break;
nuclear@0 690
nuclear@0 691 // "version <n> \n", <n> should be 1 for hl and hl² SMD files
nuclear@0 692 if (TokenMatch(szCurrent,"version",7))
nuclear@0 693 {
nuclear@0 694 if(!SkipSpaces(szCurrent,&szCurrent)) break;
nuclear@0 695 if (1 != strtoul10(szCurrent,&szCurrent))
nuclear@0 696 {
nuclear@0 697 DefaultLogger::get()->warn("SMD.version is not 1. This "
nuclear@0 698 "file format is not known. Continuing happily ...");
nuclear@0 699 }
nuclear@0 700 continue;
nuclear@0 701 }
nuclear@0 702 // "nodes\n" - Starts the node section
nuclear@0 703 if (TokenMatch(szCurrent,"nodes",5))
nuclear@0 704 {
nuclear@0 705 ParseNodesSection(szCurrent,&szCurrent);
nuclear@0 706 continue;
nuclear@0 707 }
nuclear@0 708 // "triangles\n" - Starts the triangle section
nuclear@0 709 if (TokenMatch(szCurrent,"triangles",9))
nuclear@0 710 {
nuclear@0 711 ParseTrianglesSection(szCurrent,&szCurrent);
nuclear@0 712 continue;
nuclear@0 713 }
nuclear@0 714 // "vertexanimation\n" - Starts the vertex animation section
nuclear@0 715 if (TokenMatch(szCurrent,"vertexanimation",15))
nuclear@0 716 {
nuclear@0 717 bHasUVs = false;
nuclear@0 718 ParseVASection(szCurrent,&szCurrent);
nuclear@0 719 continue;
nuclear@0 720 }
nuclear@0 721 // "skeleton\n" - Starts the skeleton section
nuclear@0 722 if (TokenMatch(szCurrent,"skeleton",8))
nuclear@0 723 {
nuclear@0 724 ParseSkeletonSection(szCurrent,&szCurrent);
nuclear@0 725 continue;
nuclear@0 726 }
nuclear@0 727 SkipLine(szCurrent,&szCurrent);
nuclear@0 728 }
nuclear@0 729 return;
nuclear@0 730 }
nuclear@0 731
nuclear@0 732 // ------------------------------------------------------------------------------------------------
nuclear@0 733 unsigned int SMDImporter::GetTextureIndex(const std::string& filename)
nuclear@0 734 {
nuclear@0 735 unsigned int iIndex = 0;
nuclear@0 736 for (std::vector<std::string>::const_iterator
nuclear@0 737 i = aszTextures.begin();
nuclear@0 738 i != aszTextures.end();++i,++iIndex)
nuclear@0 739 {
nuclear@0 740 // case-insensitive ... it's a path
nuclear@0 741 if (0 == ASSIMP_stricmp ( filename.c_str(),(*i).c_str()))return iIndex;
nuclear@0 742 }
nuclear@0 743 iIndex = (unsigned int)aszTextures.size();
nuclear@0 744 aszTextures.push_back(filename);
nuclear@0 745 return iIndex;
nuclear@0 746 }
nuclear@0 747
nuclear@0 748 // ------------------------------------------------------------------------------------------------
nuclear@0 749 // Parse the nodes section of the file
nuclear@0 750 void SMDImporter::ParseNodesSection(const char* szCurrent,
nuclear@0 751 const char** szCurrentOut)
nuclear@0 752 {
nuclear@0 753 for ( ;; )
nuclear@0 754 {
nuclear@0 755 // "end\n" - Ends the nodes section
nuclear@0 756 if (0 == ASSIMP_strincmp(szCurrent,"end",3) &&
nuclear@0 757 IsSpaceOrNewLine(*(szCurrent+3)))
nuclear@0 758 {
nuclear@0 759 szCurrent += 4;
nuclear@0 760 break;
nuclear@0 761 }
nuclear@0 762 ParseNodeInfo(szCurrent,&szCurrent);
nuclear@0 763 }
nuclear@0 764 SkipSpacesAndLineEnd(szCurrent,&szCurrent);
nuclear@0 765 *szCurrentOut = szCurrent;
nuclear@0 766 }
nuclear@0 767
nuclear@0 768 // ------------------------------------------------------------------------------------------------
nuclear@0 769 // Parse the triangles section of the file
nuclear@0 770 void SMDImporter::ParseTrianglesSection(const char* szCurrent,
nuclear@0 771 const char** szCurrentOut)
nuclear@0 772 {
nuclear@0 773 // Parse a triangle, parse another triangle, parse the next triangle ...
nuclear@0 774 // and so on until we reach a token that looks quite similar to "end"
nuclear@0 775 for ( ;; )
nuclear@0 776 {
nuclear@0 777 if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) break;
nuclear@0 778
nuclear@0 779 // "end\n" - Ends the triangles section
nuclear@0 780 if (TokenMatch(szCurrent,"end",3))
nuclear@0 781 break;
nuclear@0 782 ParseTriangle(szCurrent,&szCurrent);
nuclear@0 783 }
nuclear@0 784 SkipSpacesAndLineEnd(szCurrent,&szCurrent);
nuclear@0 785 *szCurrentOut = szCurrent;
nuclear@0 786 }
nuclear@0 787 // ------------------------------------------------------------------------------------------------
nuclear@0 788 // Parse the vertex animation section of the file
nuclear@0 789 void SMDImporter::ParseVASection(const char* szCurrent,
nuclear@0 790 const char** szCurrentOut)
nuclear@0 791 {
nuclear@0 792 unsigned int iCurIndex = 0;
nuclear@0 793 for ( ;; )
nuclear@0 794 {
nuclear@0 795 if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) break;
nuclear@0 796
nuclear@0 797 // "end\n" - Ends the "vertexanimation" section
nuclear@0 798 if (TokenMatch(szCurrent,"end",3))
nuclear@0 799 break;
nuclear@0 800
nuclear@0 801 // "time <n>\n"
nuclear@0 802 if (TokenMatch(szCurrent,"time",4))
nuclear@0 803 {
nuclear@0 804 // NOTE: The doc says that time values COULD be negative ...
nuclear@0 805 // NOTE2: this is the shape key -> valve docs
nuclear@0 806 int iTime = 0;
nuclear@0 807 if(!ParseSignedInt(szCurrent,&szCurrent,iTime) || configFrameID != (unsigned int)iTime)break;
nuclear@0 808 SkipLine(szCurrent,&szCurrent);
nuclear@0 809 }
nuclear@0 810 else
nuclear@0 811 {
nuclear@0 812 if(0 == iCurIndex)
nuclear@0 813 {
nuclear@0 814 asTriangles.push_back(SMD::Face());
nuclear@0 815 }
nuclear@0 816 if (++iCurIndex == 3)iCurIndex = 0;
nuclear@0 817 ParseVertex(szCurrent,&szCurrent,asTriangles.back().avVertices[iCurIndex],true);
nuclear@0 818 }
nuclear@0 819 }
nuclear@0 820
nuclear@0 821 if (iCurIndex != 2 && !asTriangles.empty())
nuclear@0 822 {
nuclear@0 823 // we want to no degenerates, so throw this triangle away
nuclear@0 824 asTriangles.pop_back();
nuclear@0 825 }
nuclear@0 826
nuclear@0 827 SkipSpacesAndLineEnd(szCurrent,&szCurrent);
nuclear@0 828 *szCurrentOut = szCurrent;
nuclear@0 829 }
nuclear@0 830 // ------------------------------------------------------------------------------------------------
nuclear@0 831 // Parse the skeleton section of the file
nuclear@0 832 void SMDImporter::ParseSkeletonSection(const char* szCurrent,
nuclear@0 833 const char** szCurrentOut)
nuclear@0 834 {
nuclear@0 835 int iTime = 0;
nuclear@0 836 for ( ;; )
nuclear@0 837 {
nuclear@0 838 if(!SkipSpacesAndLineEnd(szCurrent,&szCurrent)) break;
nuclear@0 839
nuclear@0 840 // "end\n" - Ends the skeleton section
nuclear@0 841 if (TokenMatch(szCurrent,"end",3))
nuclear@0 842 break;
nuclear@0 843
nuclear@0 844 // "time <n>\n" - Specifies the current animation frame
nuclear@0 845 else if (TokenMatch(szCurrent,"time",4))
nuclear@0 846 {
nuclear@0 847 // NOTE: The doc says that time values COULD be negative ...
nuclear@0 848 if(!ParseSignedInt(szCurrent,&szCurrent,iTime))break;
nuclear@0 849
nuclear@0 850 iSmallestFrame = std::min(iSmallestFrame,iTime);
nuclear@0 851 SkipLine(szCurrent,&szCurrent);
nuclear@0 852 }
nuclear@0 853 else ParseSkeletonElement(szCurrent,&szCurrent,iTime);
nuclear@0 854 }
nuclear@0 855 *szCurrentOut = szCurrent;
nuclear@0 856 }
nuclear@0 857
nuclear@0 858 // ------------------------------------------------------------------------------------------------
nuclear@0 859 #define SMDI_PARSE_RETURN { \
nuclear@0 860 SkipLine(szCurrent,&szCurrent); \
nuclear@0 861 *szCurrentOut = szCurrent; \
nuclear@0 862 return; \
nuclear@0 863 }
nuclear@0 864 // ------------------------------------------------------------------------------------------------
nuclear@0 865 // Parse a node line
nuclear@0 866 void SMDImporter::ParseNodeInfo(const char* szCurrent,
nuclear@0 867 const char** szCurrentOut)
nuclear@0 868 {
nuclear@0 869 unsigned int iBone = 0;
nuclear@0 870 SkipSpacesAndLineEnd(szCurrent,&szCurrent);
nuclear@0 871 if(!ParseUnsignedInt(szCurrent,&szCurrent,iBone) || !SkipSpaces(szCurrent,&szCurrent))
nuclear@0 872 {
nuclear@0 873 LogErrorNoThrow("Unexpected EOF/EOL while parsing bone index");
nuclear@0 874 SMDI_PARSE_RETURN;
nuclear@0 875 }
nuclear@0 876 // add our bone to the list
nuclear@0 877 if (iBone >= asBones.size())asBones.resize(iBone+1);
nuclear@0 878 SMD::Bone& bone = asBones[iBone];
nuclear@0 879
nuclear@0 880 bool bQuota = true;
nuclear@0 881 if ('\"' != *szCurrent)
nuclear@0 882 {
nuclear@0 883 LogWarning("Bone name is expcted to be enclosed in "
nuclear@0 884 "double quotation marks. ");
nuclear@0 885 bQuota = false;
nuclear@0 886 }
nuclear@0 887 else ++szCurrent;
nuclear@0 888
nuclear@0 889 const char* szEnd = szCurrent;
nuclear@0 890 for ( ;; )
nuclear@0 891 {
nuclear@0 892 if (bQuota && '\"' == *szEnd)
nuclear@0 893 {
nuclear@0 894 iBone = (unsigned int)(szEnd - szCurrent);
nuclear@0 895 ++szEnd;
nuclear@0 896 break;
nuclear@0 897 }
nuclear@0 898 else if (IsSpaceOrNewLine(*szEnd))
nuclear@0 899 {
nuclear@0 900 iBone = (unsigned int)(szEnd - szCurrent);
nuclear@0 901 break;
nuclear@0 902 }
nuclear@0 903 else if (!(*szEnd))
nuclear@0 904 {
nuclear@0 905 LogErrorNoThrow("Unexpected EOF/EOL while parsing bone name");
nuclear@0 906 SMDI_PARSE_RETURN;
nuclear@0 907 }
nuclear@0 908 ++szEnd;
nuclear@0 909 }
nuclear@0 910 bone.mName = std::string(szCurrent,iBone);
nuclear@0 911 szCurrent = szEnd;
nuclear@0 912
nuclear@0 913 // the only negative bone parent index that could occur is -1 AFAIK
nuclear@0 914 if(!ParseSignedInt(szCurrent,&szCurrent,(int&)bone.iParent))
nuclear@0 915 {
nuclear@0 916 LogErrorNoThrow("Unexpected EOF/EOL while parsing bone parent index. Assuming -1");
nuclear@0 917 SMDI_PARSE_RETURN;
nuclear@0 918 }
nuclear@0 919
nuclear@0 920 // go to the beginning of the next line
nuclear@0 921 SMDI_PARSE_RETURN;
nuclear@0 922 }
nuclear@0 923
nuclear@0 924 // ------------------------------------------------------------------------------------------------
nuclear@0 925 // Parse a skeleton element
nuclear@0 926 void SMDImporter::ParseSkeletonElement(const char* szCurrent,
nuclear@0 927 const char** szCurrentOut,int iTime)
nuclear@0 928 {
nuclear@0 929 aiVector3D vPos;
nuclear@0 930 aiVector3D vRot;
nuclear@0 931
nuclear@0 932 unsigned int iBone = 0;
nuclear@0 933 if(!ParseUnsignedInt(szCurrent,&szCurrent,iBone))
nuclear@0 934 {
nuclear@0 935 DefaultLogger::get()->error("Unexpected EOF/EOL while parsing bone index");
nuclear@0 936 SMDI_PARSE_RETURN;
nuclear@0 937 }
nuclear@0 938 if (iBone >= asBones.size())
nuclear@0 939 {
nuclear@0 940 LogErrorNoThrow("Bone index in skeleton section is out of range");
nuclear@0 941 SMDI_PARSE_RETURN;
nuclear@0 942 }
nuclear@0 943 SMD::Bone& bone = asBones[iBone];
nuclear@0 944
nuclear@0 945 bone.sAnim.asKeys.push_back(SMD::Bone::Animation::MatrixKey());
nuclear@0 946 SMD::Bone::Animation::MatrixKey& key = bone.sAnim.asKeys.back();
nuclear@0 947
nuclear@0 948 key.dTime = (double)iTime;
nuclear@0 949 if(!ParseFloat(szCurrent,&szCurrent,(float&)vPos.x))
nuclear@0 950 {
nuclear@0 951 LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.pos.x");
nuclear@0 952 SMDI_PARSE_RETURN;
nuclear@0 953 }
nuclear@0 954 if(!ParseFloat(szCurrent,&szCurrent,(float&)vPos.y))
nuclear@0 955 {
nuclear@0 956 LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.pos.y");
nuclear@0 957 SMDI_PARSE_RETURN;
nuclear@0 958 }
nuclear@0 959 if(!ParseFloat(szCurrent,&szCurrent,(float&)vPos.z))
nuclear@0 960 {
nuclear@0 961 LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.pos.z");
nuclear@0 962 SMDI_PARSE_RETURN;
nuclear@0 963 }
nuclear@0 964 if(!ParseFloat(szCurrent,&szCurrent,(float&)vRot.x))
nuclear@0 965 {
nuclear@0 966 LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.rot.x");
nuclear@0 967 SMDI_PARSE_RETURN;
nuclear@0 968 }
nuclear@0 969 if(!ParseFloat(szCurrent,&szCurrent,(float&)vRot.y))
nuclear@0 970 {
nuclear@0 971 LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.rot.y");
nuclear@0 972 SMDI_PARSE_RETURN;
nuclear@0 973 }
nuclear@0 974 if(!ParseFloat(szCurrent,&szCurrent,(float&)vRot.z))
nuclear@0 975 {
nuclear@0 976 LogErrorNoThrow("Unexpected EOF/EOL while parsing bone.rot.z");
nuclear@0 977 SMDI_PARSE_RETURN;
nuclear@0 978 }
nuclear@0 979 // build the transformation matrix of the key
nuclear@0 980 key.matrix.FromEulerAnglesXYZ(vRot.x,vRot.y,vRot.z);
nuclear@0 981 {
nuclear@0 982 aiMatrix4x4 mTemp;
nuclear@0 983 mTemp.a4 = vPos.x;
nuclear@0 984 mTemp.b4 = vPos.y;
nuclear@0 985 mTemp.c4 = vPos.z;
nuclear@0 986 key.matrix = key.matrix * mTemp;
nuclear@0 987 }
nuclear@0 988
nuclear@0 989 // go to the beginning of the next line
nuclear@0 990 SMDI_PARSE_RETURN;
nuclear@0 991 }
nuclear@0 992
nuclear@0 993 // ------------------------------------------------------------------------------------------------
nuclear@0 994 // Parse a triangle
nuclear@0 995 void SMDImporter::ParseTriangle(const char* szCurrent,
nuclear@0 996 const char** szCurrentOut)
nuclear@0 997 {
nuclear@0 998 asTriangles.push_back(SMD::Face());
nuclear@0 999 SMD::Face& face = asTriangles.back();
nuclear@0 1000
nuclear@0 1001 if(!SkipSpaces(szCurrent,&szCurrent))
nuclear@0 1002 {
nuclear@0 1003 LogErrorNoThrow("Unexpected EOF/EOL while parsing a triangle");
nuclear@0 1004 return;
nuclear@0 1005 }
nuclear@0 1006
nuclear@0 1007 // read the texture file name
nuclear@0 1008 const char* szLast = szCurrent;
nuclear@0 1009 while (!IsSpaceOrNewLine(*szCurrent++));
nuclear@0 1010
nuclear@0 1011 // ... and get the index that belongs to this file name
nuclear@0 1012 face.iTexture = GetTextureIndex(std::string(szLast,(uintptr_t)szCurrent-(uintptr_t)szLast));
nuclear@0 1013
nuclear@0 1014 SkipSpacesAndLineEnd(szCurrent,&szCurrent);
nuclear@0 1015
nuclear@0 1016 // load three vertices
nuclear@0 1017 for (unsigned int iVert = 0; iVert < 3;++iVert)
nuclear@0 1018 {
nuclear@0 1019 ParseVertex(szCurrent,&szCurrent,
nuclear@0 1020 face.avVertices[iVert]);
nuclear@0 1021 }
nuclear@0 1022 *szCurrentOut = szCurrent;
nuclear@0 1023 }
nuclear@0 1024
nuclear@0 1025 // ------------------------------------------------------------------------------------------------
nuclear@0 1026 // Parse a float
nuclear@0 1027 bool SMDImporter::ParseFloat(const char* szCurrent,
nuclear@0 1028 const char** szCurrentOut, float& out)
nuclear@0 1029 {
nuclear@0 1030 if(!SkipSpaces(&szCurrent))
nuclear@0 1031 return false;
nuclear@0 1032
nuclear@0 1033 *szCurrentOut = fast_atoreal_move<float>(szCurrent,out);
nuclear@0 1034 return true;
nuclear@0 1035 }
nuclear@0 1036
nuclear@0 1037 // ------------------------------------------------------------------------------------------------
nuclear@0 1038 // Parse an unsigned int
nuclear@0 1039 bool SMDImporter::ParseUnsignedInt(const char* szCurrent,
nuclear@0 1040 const char** szCurrentOut, unsigned int& out)
nuclear@0 1041 {
nuclear@0 1042 if(!SkipSpaces(&szCurrent))
nuclear@0 1043 return false;
nuclear@0 1044
nuclear@0 1045 out = strtoul10(szCurrent,szCurrentOut);
nuclear@0 1046 return true;
nuclear@0 1047 }
nuclear@0 1048
nuclear@0 1049 // ------------------------------------------------------------------------------------------------
nuclear@0 1050 // Parse a signed int
nuclear@0 1051 bool SMDImporter::ParseSignedInt(const char* szCurrent,
nuclear@0 1052 const char** szCurrentOut, int& out)
nuclear@0 1053 {
nuclear@0 1054 if(!SkipSpaces(&szCurrent))
nuclear@0 1055 return false;
nuclear@0 1056
nuclear@0 1057 out = strtol10(szCurrent,szCurrentOut);
nuclear@0 1058 return true;
nuclear@0 1059 }
nuclear@0 1060
nuclear@0 1061 // ------------------------------------------------------------------------------------------------
nuclear@0 1062 // Parse a vertex
nuclear@0 1063 void SMDImporter::ParseVertex(const char* szCurrent,
nuclear@0 1064 const char** szCurrentOut, SMD::Vertex& vertex,
nuclear@0 1065 bool bVASection /*= false*/)
nuclear@0 1066 {
nuclear@0 1067 if (SkipSpaces(&szCurrent) && IsLineEnd(*szCurrent))
nuclear@0 1068 {
nuclear@0 1069 SkipSpacesAndLineEnd(szCurrent,&szCurrent);
nuclear@0 1070 return ParseVertex(szCurrent,szCurrentOut,vertex,bVASection);
nuclear@0 1071 }
nuclear@0 1072 if(!ParseSignedInt(szCurrent,&szCurrent,(int&)vertex.iParentNode))
nuclear@0 1073 {
nuclear@0 1074 LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.parent");
nuclear@0 1075 SMDI_PARSE_RETURN;
nuclear@0 1076 }
nuclear@0 1077 if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.pos.x))
nuclear@0 1078 {
nuclear@0 1079 LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.pos.x");
nuclear@0 1080 SMDI_PARSE_RETURN;
nuclear@0 1081 }
nuclear@0 1082 if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.pos.y))
nuclear@0 1083 {
nuclear@0 1084 LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.pos.y");
nuclear@0 1085 SMDI_PARSE_RETURN;
nuclear@0 1086 }
nuclear@0 1087 if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.pos.z))
nuclear@0 1088 {
nuclear@0 1089 LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.pos.z");
nuclear@0 1090 SMDI_PARSE_RETURN;
nuclear@0 1091 }
nuclear@0 1092 if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.nor.x))
nuclear@0 1093 {
nuclear@0 1094 LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.nor.x");
nuclear@0 1095 SMDI_PARSE_RETURN;
nuclear@0 1096 }
nuclear@0 1097 if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.nor.y))
nuclear@0 1098 {
nuclear@0 1099 LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.nor.y");
nuclear@0 1100 SMDI_PARSE_RETURN;
nuclear@0 1101 }
nuclear@0 1102 if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.nor.z))
nuclear@0 1103 {
nuclear@0 1104 LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.nor.z");
nuclear@0 1105 SMDI_PARSE_RETURN;
nuclear@0 1106 }
nuclear@0 1107
nuclear@0 1108 if (bVASection)SMDI_PARSE_RETURN;
nuclear@0 1109
nuclear@0 1110 if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.uv.x))
nuclear@0 1111 {
nuclear@0 1112 LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.uv.x");
nuclear@0 1113 SMDI_PARSE_RETURN;
nuclear@0 1114 }
nuclear@0 1115 if(!ParseFloat(szCurrent,&szCurrent,(float&)vertex.uv.y))
nuclear@0 1116 {
nuclear@0 1117 LogErrorNoThrow("Unexpected EOF/EOL while parsing vertex.uv.y");
nuclear@0 1118 SMDI_PARSE_RETURN;
nuclear@0 1119 }
nuclear@0 1120
nuclear@0 1121 // now read the number of bones affecting this vertex
nuclear@0 1122 // all elements from now are fully optional, we don't need them
nuclear@0 1123 unsigned int iSize = 0;
nuclear@0 1124 if(!ParseUnsignedInt(szCurrent,&szCurrent,iSize))SMDI_PARSE_RETURN;
nuclear@0 1125 vertex.aiBoneLinks.resize(iSize,std::pair<unsigned int, float>(0,0.0f));
nuclear@0 1126
nuclear@0 1127 for (std::vector<std::pair<unsigned int, float> >::iterator
nuclear@0 1128 i = vertex.aiBoneLinks.begin();
nuclear@0 1129 i != vertex.aiBoneLinks.end();++i)
nuclear@0 1130 {
nuclear@0 1131 if(!ParseUnsignedInt(szCurrent,&szCurrent,(*i).first))
nuclear@0 1132 SMDI_PARSE_RETURN;
nuclear@0 1133 if(!ParseFloat(szCurrent,&szCurrent,(*i).second))
nuclear@0 1134 SMDI_PARSE_RETURN;
nuclear@0 1135 }
nuclear@0 1136
nuclear@0 1137 // go to the beginning of the next line
nuclear@0 1138 SMDI_PARSE_RETURN;
nuclear@0 1139 }
nuclear@0 1140
nuclear@0 1141 #endif // !! ASSIMP_BUILD_NO_SMD_IMPORTER