vrshoot

diff libs/assimp/3DSLoader.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/3DSLoader.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,1392 @@
     1.4 +/*
     1.5 +---------------------------------------------------------------------------
     1.6 +Open Asset Import Library (assimp)
     1.7 +---------------------------------------------------------------------------
     1.8 +
     1.9 +Copyright (c) 2006-2012, assimp team
    1.10 +
    1.11 +All rights reserved.
    1.12 +
    1.13 +Redistribution and use of this software in source and binary forms, 
    1.14 +with or without modification, are permitted provided that the following 
    1.15 +conditions are met:
    1.16 +
    1.17 +* Redistributions of source code must retain the above
    1.18 +  copyright notice, this list of conditions and the
    1.19 +  following disclaimer.
    1.20 +
    1.21 +* Redistributions in binary form must reproduce the above
    1.22 +  copyright notice, this list of conditions and the
    1.23 +  following disclaimer in the documentation and/or other
    1.24 +  materials provided with the distribution.
    1.25 +
    1.26 +* Neither the name of the assimp team, nor the names of its
    1.27 +  contributors may be used to endorse or promote products
    1.28 +  derived from this software without specific prior
    1.29 +  written permission of the assimp team.
    1.30 +
    1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.42 +---------------------------------------------------------------------------
    1.43 +*/
    1.44 +
    1.45 +/** @file  3DSLoader.cpp
    1.46 + *  @brief Implementation of the 3ds importer class
    1.47 + *
    1.48 + *  http://www.the-labs.com/Blender/3DS-details.html
    1.49 + */
    1.50 +
    1.51 +#include "AssimpPCH.h"
    1.52 +#ifndef ASSIMP_BUILD_NO_3DS_IMPORTER
    1.53 +
    1.54 +// internal headers
    1.55 +#include "3DSLoader.h"
    1.56 +
    1.57 +using namespace Assimp;
    1.58 +
    1.59 +static const aiImporterDesc desc = {
    1.60 +	"Discreet 3DS Importer",
    1.61 +	"",
    1.62 +	"",
    1.63 +	"Limited animation support",
    1.64 +	aiImporterFlags_SupportBinaryFlavour,
    1.65 +	0,
    1.66 +	0,
    1.67 +	0,
    1.68 +	0,
    1.69 +	"3ds prj" 
    1.70 +};
    1.71 +
    1.72 +		
    1.73 +// ------------------------------------------------------------------------------------------------
    1.74 +// Begins a new parsing block
    1.75 +// - Reads the current chunk and validates it
    1.76 +// - computes its length
    1.77 +#define ASSIMP_3DS_BEGIN_CHUNK()                                         \
    1.78 +	while (true) {                                                       \
    1.79 +	if (stream->GetRemainingSizeToLimit() < sizeof(Discreet3DS::Chunk)){ \
    1.80 +		return;                                                          \
    1.81 +	}                                                                    \
    1.82 +	Discreet3DS::Chunk chunk;                                            \
    1.83 +	ReadChunk(&chunk);                                                   \
    1.84 +	int chunkSize = chunk.Size-sizeof(Discreet3DS::Chunk);	             \
    1.85 +	const int oldReadLimit = stream->GetReadLimit();                     \
    1.86 +	stream->SetReadLimit(stream->GetCurrentPos() + chunkSize);           \
    1.87 +	
    1.88 +
    1.89 +// ------------------------------------------------------------------------------------------------
    1.90 +// End a parsing block
    1.91 +// Must follow at the end of each parsing block, reset chunk end marker to previous value
    1.92 +#define ASSIMP_3DS_END_CHUNK()                  \
    1.93 +	stream->SkipToReadLimit();                  \
    1.94 +	stream->SetReadLimit(oldReadLimit);         \
    1.95 +	if (stream->GetRemainingSizeToLimit() == 0) \
    1.96 +		return;                                 \
    1.97 +	}
    1.98 +
    1.99 +// ------------------------------------------------------------------------------------------------
   1.100 +// Constructor to be privately used by Importer
   1.101 +Discreet3DSImporter::Discreet3DSImporter()
   1.102 +{}
   1.103 +
   1.104 +// ------------------------------------------------------------------------------------------------
   1.105 +// Destructor, private as well 
   1.106 +Discreet3DSImporter::~Discreet3DSImporter()
   1.107 +{}
   1.108 +
   1.109 +// ------------------------------------------------------------------------------------------------
   1.110 +// Returns whether the class can handle the format of the given file. 
   1.111 +bool Discreet3DSImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
   1.112 +{
   1.113 +	std::string extension = GetExtension(pFile);
   1.114 +	if(extension == "3ds" || extension == "prj" ) {
   1.115 +		return true;
   1.116 +	}
   1.117 +	if (!extension.length() || checkSig) {
   1.118 +		uint16_t token[3];
   1.119 +		token[0] = 0x4d4d;
   1.120 +		token[1] = 0x3dc2;
   1.121 +		//token[2] = 0x3daa;
   1.122 +		return CheckMagicToken(pIOHandler,pFile,token,2,0,2);
   1.123 +	}
   1.124 +	return false;
   1.125 +}
   1.126 +
   1.127 +// ------------------------------------------------------------------------------------------------
   1.128 +// Loader registry entry
   1.129 +const aiImporterDesc* Discreet3DSImporter::GetInfo () const
   1.130 +{
   1.131 +	return &desc;
   1.132 +}
   1.133 +
   1.134 +// ------------------------------------------------------------------------------------------------
   1.135 +// Setup configuration properties
   1.136 +void Discreet3DSImporter::SetupProperties(const Importer* /*pImp*/)
   1.137 +{
   1.138 +	// nothing to be done for the moment
   1.139 +}
   1.140 +
   1.141 +// ------------------------------------------------------------------------------------------------
   1.142 +// Imports the given file into the given scene structure. 
   1.143 +void Discreet3DSImporter::InternReadFile( const std::string& pFile, 
   1.144 +	aiScene* pScene, IOSystem* pIOHandler)
   1.145 +{
   1.146 +	StreamReaderLE stream(pIOHandler->Open(pFile,"rb"));
   1.147 +	this->stream = &stream;
   1.148 +
   1.149 +	// We should have at least one chunk
   1.150 +	if (stream.GetRemainingSize() < 16) {
   1.151 +		throw DeadlyImportError("3DS file is either empty or corrupt: " + pFile);
   1.152 +	}
   1.153 +
   1.154 +	// Allocate our temporary 3DS representation
   1.155 +	mScene = new D3DS::Scene();
   1.156 +
   1.157 +	// Initialize members
   1.158 +	mLastNodeIndex             = -1;
   1.159 +	mCurrentNode               = new D3DS::Node();
   1.160 +	mRootNode                  = mCurrentNode;
   1.161 +	mRootNode->mHierarchyPos   = -1;
   1.162 +	mRootNode->mHierarchyIndex = -1;
   1.163 +	mRootNode->mParent         = NULL;
   1.164 +	mMasterScale               = 1.0f;
   1.165 +	mBackgroundImage           = "";
   1.166 +	bHasBG                     = false;
   1.167 +	bIsPrj                     = false;
   1.168 +
   1.169 +	// Parse the file
   1.170 +	ParseMainChunk();
   1.171 +
   1.172 +	// Process all meshes in the file. First check whether all
   1.173 +	// face indices haev valid values. The generate our 
   1.174 +	// internal verbose representation. Finally compute normal
   1.175 +	// vectors from the smoothing groups we read from the
   1.176 +	// file.
   1.177 +	for (std::vector<D3DS::Mesh>::iterator i = mScene->mMeshes.begin(),
   1.178 +		 end = mScene->mMeshes.end(); i != end;++i)	{
   1.179 +		CheckIndices(*i);
   1.180 +		MakeUnique  (*i);
   1.181 +		ComputeNormalsWithSmoothingsGroups<D3DS::Face>(*i);
   1.182 +	}
   1.183 +
   1.184 +	// Replace all occurences of the default material with a
   1.185 +	// valid material. Generate it if no material containing
   1.186 +	// DEFAULT in its name has been found in the file
   1.187 +	ReplaceDefaultMaterial();
   1.188 +
   1.189 +	// Convert the scene from our internal representation to an
   1.190 +	// aiScene object. This involves copying all meshes, lights
   1.191 +	// and cameras to the scene
   1.192 +	ConvertScene(pScene);
   1.193 +
   1.194 +	// Generate the node graph for the scene. This is a little bit
   1.195 +	// tricky since we'll need to split some meshes into submeshes
   1.196 +	GenerateNodeGraph(pScene);
   1.197 +
   1.198 +	// Now apply the master scaling factor to the scene
   1.199 +	ApplyMasterScale(pScene);
   1.200 +
   1.201 +	// Delete our internal scene representation and the root
   1.202 +	// node, so the whole hierarchy will follow
   1.203 +	delete mRootNode;
   1.204 +	delete mScene;
   1.205 +
   1.206 +	AI_DEBUG_INVALIDATE_PTR(mRootNode);
   1.207 +	AI_DEBUG_INVALIDATE_PTR(mScene);
   1.208 +	AI_DEBUG_INVALIDATE_PTR(this->stream);
   1.209 +}
   1.210 +
   1.211 +// ------------------------------------------------------------------------------------------------
   1.212 +// Applies a master-scaling factor to the imported scene
   1.213 +void Discreet3DSImporter::ApplyMasterScale(aiScene* pScene)
   1.214 +{
   1.215 +	// There are some 3DS files with a zero scaling factor
   1.216 +	if (!mMasterScale)mMasterScale = 1.0f;
   1.217 +	else mMasterScale = 1.0f / mMasterScale;
   1.218 +
   1.219 +	// Construct an uniform scaling matrix and multiply with it
   1.220 +	pScene->mRootNode->mTransformation *= aiMatrix4x4( 
   1.221 +		mMasterScale,0.0f, 0.0f, 0.0f,
   1.222 +		0.0f, mMasterScale,0.0f, 0.0f,
   1.223 +		0.0f, 0.0f, mMasterScale,0.0f,
   1.224 +		0.0f, 0.0f, 0.0f, 1.0f);
   1.225 +
   1.226 +	// Check whether a scaling track is assigned to the root node.
   1.227 +}
   1.228 +
   1.229 +// ------------------------------------------------------------------------------------------------
   1.230 +// Reads a new chunk from the file
   1.231 +void Discreet3DSImporter::ReadChunk(Discreet3DS::Chunk* pcOut)
   1.232 +{
   1.233 +	ai_assert(pcOut != NULL);
   1.234 +
   1.235 +	pcOut->Flag = stream->GetI2();
   1.236 +	pcOut->Size = stream->GetI4();
   1.237 +
   1.238 +	if (pcOut->Size - sizeof(Discreet3DS::Chunk) > stream->GetRemainingSize())
   1.239 +		throw DeadlyImportError("Chunk is too large");
   1.240 +	
   1.241 +	if (pcOut->Size - sizeof(Discreet3DS::Chunk) > stream->GetRemainingSizeToLimit())
   1.242 +		DefaultLogger::get()->error("3DS: Chunk overflow");
   1.243 +}
   1.244 +
   1.245 +// ------------------------------------------------------------------------------------------------
   1.246 +// Skip a chunk
   1.247 +void Discreet3DSImporter::SkipChunk()
   1.248 +{
   1.249 +	Discreet3DS::Chunk psChunk;
   1.250 +	ReadChunk(&psChunk);
   1.251 +	
   1.252 +	stream->IncPtr(psChunk.Size-sizeof(Discreet3DS::Chunk));
   1.253 +	return;
   1.254 +}
   1.255 +
   1.256 +// ------------------------------------------------------------------------------------------------
   1.257 +// Process the primary chunk of the file
   1.258 +void Discreet3DSImporter::ParseMainChunk()
   1.259 +{
   1.260 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.261 +
   1.262 +	// get chunk type
   1.263 +	switch (chunk.Flag)
   1.264 +	{
   1.265 +	
   1.266 +	case Discreet3DS::CHUNK_PRJ:
   1.267 +		bIsPrj = true;
   1.268 +	case Discreet3DS::CHUNK_MAIN:
   1.269 +		ParseEditorChunk();
   1.270 +		break;
   1.271 +	};
   1.272 +
   1.273 +	ASSIMP_3DS_END_CHUNK();
   1.274 +	// recursively continue processing this hierarchy level
   1.275 +	return ParseMainChunk();
   1.276 +}
   1.277 +
   1.278 +// ------------------------------------------------------------------------------------------------
   1.279 +void Discreet3DSImporter::ParseEditorChunk()
   1.280 +{
   1.281 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.282 +
   1.283 +	// get chunk type
   1.284 +	switch (chunk.Flag)
   1.285 +	{
   1.286 +	case Discreet3DS::CHUNK_OBJMESH:
   1.287 +
   1.288 +		ParseObjectChunk();
   1.289 +		break;
   1.290 +
   1.291 +	// NOTE: In several documentations in the internet this
   1.292 +	// chunk appears at different locations
   1.293 +	case Discreet3DS::CHUNK_KEYFRAMER:
   1.294 +
   1.295 +		ParseKeyframeChunk();
   1.296 +		break;
   1.297 +
   1.298 +	case Discreet3DS::CHUNK_VERSION:
   1.299 +		{
   1.300 +		// print the version number
   1.301 +		char buff[10];
   1.302 +		ASSIMP_itoa10(buff,stream->GetI2());
   1.303 +		DefaultLogger::get()->info(std::string("3DS file format version: ") + buff);
   1.304 +		}
   1.305 +		break;
   1.306 +	};
   1.307 +	ASSIMP_3DS_END_CHUNK();
   1.308 +}
   1.309 +
   1.310 +// ------------------------------------------------------------------------------------------------
   1.311 +void Discreet3DSImporter::ParseObjectChunk()
   1.312 +{
   1.313 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.314 +
   1.315 +	// get chunk type
   1.316 +	switch (chunk.Flag)
   1.317 +	{
   1.318 +	case Discreet3DS::CHUNK_OBJBLOCK:
   1.319 +		{
   1.320 +		unsigned int cnt = 0;
   1.321 +		const char* sz = (const char*)stream->GetPtr();
   1.322 +
   1.323 +		// Get the name of the geometry object
   1.324 +		while (stream->GetI1())++cnt;
   1.325 +		ParseChunk(sz,cnt);
   1.326 +		}
   1.327 +		break;
   1.328 +
   1.329 +	case Discreet3DS::CHUNK_MAT_MATERIAL:
   1.330 +
   1.331 +		// Add a new material to the list
   1.332 +		mScene->mMaterials.push_back(D3DS::Material());
   1.333 +		ParseMaterialChunk();
   1.334 +		break;
   1.335 +
   1.336 +	case Discreet3DS::CHUNK_AMBCOLOR:
   1.337 +
   1.338 +		// This is the ambient base color of the scene.
   1.339 +		// We add it to the ambient color of all materials
   1.340 +		ParseColorChunk(&mClrAmbient,true);
   1.341 +		if (is_qnan(mClrAmbient.r))
   1.342 +		{
   1.343 +			// We failed to read the ambient base color.
   1.344 +			DefaultLogger::get()->error("3DS: Failed to read ambient base color");
   1.345 +			mClrAmbient.r = mClrAmbient.g = mClrAmbient.b = 0.0f;
   1.346 +		}
   1.347 +		break;
   1.348 +
   1.349 +	case Discreet3DS::CHUNK_BIT_MAP:
   1.350 +		{
   1.351 +		// Specifies the background image. The string should already be 
   1.352 +		// properly 0 terminated but we need to be sure
   1.353 +		unsigned int cnt = 0;
   1.354 +		const char* sz = (const char*)stream->GetPtr();
   1.355 +		while (stream->GetI1())++cnt;
   1.356 +		mBackgroundImage = std::string(sz,cnt);
   1.357 +		}
   1.358 +		break;
   1.359 +
   1.360 +	case Discreet3DS::CHUNK_BIT_MAP_EXISTS:
   1.361 +		bHasBG = true;
   1.362 +		break;
   1.363 +
   1.364 +	case Discreet3DS::CHUNK_MASTER_SCALE:
   1.365 +		// Scene master scaling factor
   1.366 +		mMasterScale = stream->GetF4();
   1.367 +		break;
   1.368 +	};
   1.369 +	ASSIMP_3DS_END_CHUNK();
   1.370 +}
   1.371 +
   1.372 +// ------------------------------------------------------------------------------------------------
   1.373 +void Discreet3DSImporter::ParseChunk(const char* name, unsigned int num)
   1.374 +{
   1.375 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.376 +
   1.377 +	// IMPLEMENTATION NOTE;
   1.378 +	// Cameras or lights define their transformation in their parent node and in the
   1.379 +	// corresponding light or camera chunks. However, we read and process the latter
   1.380 +	// to to be able to return valid cameras/lights even if no scenegraph is given.
   1.381 +
   1.382 +	// get chunk type
   1.383 +	switch (chunk.Flag)
   1.384 +	{
   1.385 +	case Discreet3DS::CHUNK_TRIMESH:
   1.386 +		{
   1.387 +		// this starts a new triangle mesh
   1.388 +		mScene->mMeshes.push_back(D3DS::Mesh());
   1.389 +		D3DS::Mesh& m = mScene->mMeshes.back();
   1.390 +
   1.391 +		// Setup the name of the mesh
   1.392 +		m.mName = std::string(name, num);
   1.393 +
   1.394 +		// Read mesh chunks
   1.395 +		ParseMeshChunk();
   1.396 +		}
   1.397 +		break;
   1.398 +
   1.399 +	case Discreet3DS::CHUNK_LIGHT:	
   1.400 +		{
   1.401 +		// This starts a new light
   1.402 +		aiLight* light = new aiLight();
   1.403 +		mScene->mLights.push_back(light);
   1.404 +
   1.405 +		light->mName.Set(std::string(name, num));
   1.406 +
   1.407 +		// First read the position of the light
   1.408 +		light->mPosition.x = stream->GetF4();
   1.409 +		light->mPosition.y = stream->GetF4();
   1.410 +		light->mPosition.z = stream->GetF4();
   1.411 +
   1.412 +		light->mColorDiffuse = aiColor3D(1.f,1.f,1.f);
   1.413 +
   1.414 +		// Now check for further subchunks
   1.415 +		if (!bIsPrj) /* fixme */
   1.416 +			ParseLightChunk();
   1.417 +
   1.418 +		// The specular light color is identical the the diffuse light color. The ambient light color
   1.419 +		// is equal to the ambient base color of the whole scene.
   1.420 +		light->mColorSpecular = light->mColorDiffuse;
   1.421 +		light->mColorAmbient  = mClrAmbient;
   1.422 +
   1.423 +		if (light->mType == aiLightSource_UNDEFINED)
   1.424 +		{
   1.425 +			// It must be a point light
   1.426 +			light->mType = aiLightSource_POINT;
   1.427 +		}}
   1.428 +		break;
   1.429 +
   1.430 +	case Discreet3DS::CHUNK_CAMERA:
   1.431 +		{
   1.432 +		// This starts a new camera
   1.433 +		aiCamera* camera = new aiCamera();
   1.434 +		mScene->mCameras.push_back(camera);
   1.435 +		camera->mName.Set(std::string(name, num));
   1.436 +
   1.437 +		// First read the position of the camera
   1.438 +		camera->mPosition.x = stream->GetF4();
   1.439 +		camera->mPosition.y = stream->GetF4();
   1.440 +		camera->mPosition.z = stream->GetF4();
   1.441 +
   1.442 +		// Then the camera target
   1.443 +		camera->mLookAt.x = stream->GetF4() - camera->mPosition.x;
   1.444 +		camera->mLookAt.y = stream->GetF4() - camera->mPosition.y;
   1.445 +		camera->mLookAt.z = stream->GetF4() - camera->mPosition.z;
   1.446 +		float len = camera->mLookAt.Length();
   1.447 +		if (len < 1e-5f) {
   1.448 +			
   1.449 +			// There are some files with lookat == position. Don't know why or whether it's ok or not.
   1.450 +			DefaultLogger::get()->error("3DS: Unable to read proper camera look-at vector");
   1.451 +			camera->mLookAt = aiVector3D(0.f,1.f,0.f);
   1.452 +
   1.453 +		}
   1.454 +		else camera->mLookAt /= len;
   1.455 +
   1.456 +		// And finally - the camera rotation angle, in counter clockwise direction 
   1.457 +		const float angle =  AI_DEG_TO_RAD( stream->GetF4() );
   1.458 +		aiQuaternion quat(camera->mLookAt,angle);
   1.459 +		camera->mUp = quat.GetMatrix() * aiVector3D(0.f,1.f,0.f);
   1.460 +
   1.461 +		// Read the lense angle
   1.462 +		camera->mHorizontalFOV = AI_DEG_TO_RAD ( stream->GetF4() );
   1.463 +		if (camera->mHorizontalFOV < 0.001f)  {
   1.464 +			camera->mHorizontalFOV = AI_DEG_TO_RAD(45.f);
   1.465 +		}
   1.466 +
   1.467 +		// Now check for further subchunks 
   1.468 +		if (!bIsPrj) /* fixme */ {
   1.469 +			ParseCameraChunk();
   1.470 +		}}
   1.471 +		break;
   1.472 +	};
   1.473 +	ASSIMP_3DS_END_CHUNK();
   1.474 +}
   1.475 +
   1.476 +// ------------------------------------------------------------------------------------------------
   1.477 +void Discreet3DSImporter::ParseLightChunk()
   1.478 +{
   1.479 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.480 +	aiLight* light = mScene->mLights.back();
   1.481 +
   1.482 +	// get chunk type
   1.483 +	switch (chunk.Flag)
   1.484 +	{
   1.485 +	case Discreet3DS::CHUNK_DL_SPOTLIGHT:
   1.486 +		// Now we can be sure that the light is a spot light
   1.487 +		light->mType = aiLightSource_SPOT;
   1.488 +
   1.489 +		// We wouldn't need to normalize here, but we do it
   1.490 +		light->mDirection.x = stream->GetF4() - light->mPosition.x;
   1.491 +		light->mDirection.y = stream->GetF4() - light->mPosition.y;
   1.492 +		light->mDirection.z = stream->GetF4() - light->mPosition.z;
   1.493 +		light->mDirection.Normalize();
   1.494 +
   1.495 +		// Now the hotspot and falloff angles - in degrees
   1.496 +		light->mAngleInnerCone = AI_DEG_TO_RAD( stream->GetF4() );
   1.497 +
   1.498 +		// FIX: the falloff angle is just an offset
   1.499 +		light->mAngleOuterCone = light->mAngleInnerCone+AI_DEG_TO_RAD( stream->GetF4() );
   1.500 +		break; 
   1.501 +
   1.502 +		// intensity multiplier
   1.503 +	case Discreet3DS::CHUNK_DL_MULTIPLIER:
   1.504 +		light->mColorDiffuse = light->mColorDiffuse * stream->GetF4();
   1.505 +		break;
   1.506 +
   1.507 +		// light color
   1.508 +	case Discreet3DS::CHUNK_RGBF:
   1.509 +	case Discreet3DS::CHUNK_LINRGBF:
   1.510 +		light->mColorDiffuse.r *= stream->GetF4();
   1.511 +		light->mColorDiffuse.g *= stream->GetF4();
   1.512 +		light->mColorDiffuse.b *= stream->GetF4();
   1.513 +		break;
   1.514 +
   1.515 +		// light attenuation
   1.516 +	case Discreet3DS::CHUNK_DL_ATTENUATE: 
   1.517 +		light->mAttenuationLinear = stream->GetF4();
   1.518 +		break;
   1.519 +	};
   1.520 +
   1.521 +	ASSIMP_3DS_END_CHUNK();
   1.522 +}
   1.523 +
   1.524 +// ------------------------------------------------------------------------------------------------
   1.525 +void Discreet3DSImporter::ParseCameraChunk()
   1.526 +{
   1.527 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.528 +	aiCamera* camera = mScene->mCameras.back();
   1.529 +
   1.530 +	// get chunk type
   1.531 +	switch (chunk.Flag)
   1.532 +	{
   1.533 +		// near and far clip plane
   1.534 +	case Discreet3DS::CHUNK_CAM_RANGES:
   1.535 +		camera->mClipPlaneNear = stream->GetF4();
   1.536 +		camera->mClipPlaneFar  = stream->GetF4();
   1.537 +		break;
   1.538 +	}
   1.539 +
   1.540 +	ASSIMP_3DS_END_CHUNK();
   1.541 +}
   1.542 +
   1.543 +// ------------------------------------------------------------------------------------------------
   1.544 +void Discreet3DSImporter::ParseKeyframeChunk()
   1.545 +{
   1.546 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.547 +
   1.548 +	// get chunk type
   1.549 +	switch (chunk.Flag)
   1.550 +	{
   1.551 +	case Discreet3DS::CHUNK_TRACKCAMTGT:
   1.552 +	case Discreet3DS::CHUNK_TRACKSPOTL:
   1.553 +	case Discreet3DS::CHUNK_TRACKCAMERA:
   1.554 +	case Discreet3DS::CHUNK_TRACKINFO:
   1.555 +	case Discreet3DS::CHUNK_TRACKLIGHT:
   1.556 +	case Discreet3DS::CHUNK_TRACKLIGTGT:
   1.557 +
   1.558 +		// this starts a new mesh hierarchy chunk
   1.559 +		ParseHierarchyChunk(chunk.Flag);
   1.560 +		break;
   1.561 +	};
   1.562 +
   1.563 +	ASSIMP_3DS_END_CHUNK();
   1.564 +}
   1.565 +
   1.566 +// ------------------------------------------------------------------------------------------------
   1.567 +// Little helper function for ParseHierarchyChunk
   1.568 +void Discreet3DSImporter::InverseNodeSearch(D3DS::Node* pcNode,D3DS::Node* pcCurrent)
   1.569 +{
   1.570 +	if (!pcCurrent)	{
   1.571 +		mRootNode->push_back(pcNode);
   1.572 +		return;
   1.573 +	}
   1.574 +
   1.575 +	if (pcCurrent->mHierarchyPos == pcNode->mHierarchyPos)	{
   1.576 +		if(pcCurrent->mParent) {
   1.577 +			pcCurrent->mParent->push_back(pcNode);
   1.578 +		}
   1.579 +		else pcCurrent->push_back(pcNode);
   1.580 +		return;
   1.581 +	}
   1.582 +	return InverseNodeSearch(pcNode,pcCurrent->mParent);
   1.583 +}
   1.584 +
   1.585 +// ------------------------------------------------------------------------------------------------
   1.586 +// Find a node with a specific name in the import hierarchy
   1.587 +D3DS::Node* FindNode(D3DS::Node* root, const std::string& name)
   1.588 +{
   1.589 +	if (root->mName == name)
   1.590 +		return root;
   1.591 +	for (std::vector<D3DS::Node*>::iterator it = root->mChildren.begin();it != root->mChildren.end(); ++it)	{
   1.592 +		D3DS::Node* nd;
   1.593 +		if (( nd = FindNode(*it,name)))
   1.594 +			return nd;
   1.595 +	}
   1.596 +	return NULL;
   1.597 +}
   1.598 +
   1.599 +// ------------------------------------------------------------------------------------------------
   1.600 +// Binary predicate for std::unique()
   1.601 +template <class T>
   1.602 +bool KeyUniqueCompare(const T& first, const T& second)
   1.603 +{
   1.604 +	return first.mTime == second.mTime;
   1.605 +}
   1.606 +
   1.607 +// ------------------------------------------------------------------------------------------------
   1.608 +// Skip some additional import data.
   1.609 +void Discreet3DSImporter::SkipTCBInfo()
   1.610 +{
   1.611 +	unsigned int flags = stream->GetI2();
   1.612 +
   1.613 +	if (!flags)	{
   1.614 +		// Currently we can't do anything with these values. They occur
   1.615 +		// quite rare, so it wouldn't be worth the effort implementing
   1.616 +		// them. 3DS ist not really suitable for complex animations,
   1.617 +		// so full support is not required.
   1.618 +		DefaultLogger::get()->warn("3DS: Skipping TCB animation info");
   1.619 +	}
   1.620 +
   1.621 +	if (flags & Discreet3DS::KEY_USE_TENS) {
   1.622 +		stream->IncPtr(4);
   1.623 +	}
   1.624 +	if (flags & Discreet3DS::KEY_USE_BIAS) {
   1.625 +		stream->IncPtr(4);
   1.626 +	}
   1.627 +	if (flags & Discreet3DS::KEY_USE_CONT) {
   1.628 +		stream->IncPtr(4);
   1.629 +	}
   1.630 +	if (flags & Discreet3DS::KEY_USE_EASE_FROM) {
   1.631 +		stream->IncPtr(4);
   1.632 +	}
   1.633 +	if (flags & Discreet3DS::KEY_USE_EASE_TO) {
   1.634 +		stream->IncPtr(4);
   1.635 +	}
   1.636 +}
   1.637 +
   1.638 +// ------------------------------------------------------------------------------------------------
   1.639 +// Read hierarchy and keyframe info
   1.640 +void Discreet3DSImporter::ParseHierarchyChunk(uint16_t parent)
   1.641 +{
   1.642 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.643 +
   1.644 +	// get chunk type
   1.645 +	switch (chunk.Flag)
   1.646 +	{
   1.647 +	case Discreet3DS::CHUNK_TRACKOBJNAME:
   1.648 +
   1.649 +		// This is the name of the object to which the track applies. The chunk also
   1.650 +		// defines the position of this object in the hierarchy.
   1.651 +		{
   1.652 +
   1.653 +		// First of all: get the name of the object
   1.654 +		unsigned int cnt = 0;
   1.655 +		const char* sz = (const char*)stream->GetPtr();
   1.656 +
   1.657 +		while (stream->GetI1())++cnt;
   1.658 +		std::string name = std::string(sz,cnt);
   1.659 +
   1.660 +		// Now find out whether we have this node already (target animation channels 
   1.661 +		// are stored with a separate object ID)
   1.662 +		D3DS::Node* pcNode = FindNode(mRootNode,name);
   1.663 +		if (pcNode)
   1.664 +		{
   1.665 +			// Make this node the current node
   1.666 +			mCurrentNode = pcNode;
   1.667 +			break;	
   1.668 +		}
   1.669 +		pcNode = new D3DS::Node();
   1.670 +		pcNode->mName = name;
   1.671 +
   1.672 +		// There are two unknown values which we can safely ignore
   1.673 +		stream->IncPtr(4);
   1.674 +
   1.675 +		// Now read the hierarchy position of the object
   1.676 +		uint16_t hierarchy = stream->GetI2() + 1;
   1.677 +		pcNode->mHierarchyPos   = hierarchy;
   1.678 +		pcNode->mHierarchyIndex = mLastNodeIndex;
   1.679 +
   1.680 +		// And find a proper position in the graph for it
   1.681 +		if (mCurrentNode && mCurrentNode->mHierarchyPos == hierarchy)	{
   1.682 +
   1.683 +			// add to the parent of the last touched node
   1.684 +			mCurrentNode->mParent->push_back(pcNode);
   1.685 +			mLastNodeIndex++;	
   1.686 +		}
   1.687 +		else if(hierarchy >= mLastNodeIndex)	{
   1.688 +
   1.689 +			// place it at the current position in the hierarchy
   1.690 +			mCurrentNode->push_back(pcNode);
   1.691 +			mLastNodeIndex = hierarchy;
   1.692 +		}
   1.693 +		else	{
   1.694 +			// need to go back to the specified position in the hierarchy.
   1.695 +			InverseNodeSearch(pcNode,mCurrentNode);
   1.696 +			mLastNodeIndex++;	
   1.697 +		}
   1.698 +		// Make this node the current node
   1.699 +		mCurrentNode = pcNode;
   1.700 +		}
   1.701 +		break;
   1.702 +
   1.703 +	case Discreet3DS::CHUNK_TRACKDUMMYOBJNAME:
   1.704 +
   1.705 +		// This is the "real" name of a $$$DUMMY object
   1.706 +		{
   1.707 +			const char* sz = (const char*) stream->GetPtr();
   1.708 +			while (stream->GetI1());
   1.709 +
   1.710 +			// If object name is DUMMY, take this one instead
   1.711 +			if (mCurrentNode->mName == "$$$DUMMY")	{
   1.712 +				//DefaultLogger::get()->warn("3DS: Skipping dummy object name for non-dummy object");
   1.713 +				mCurrentNode->mName = std::string(sz);
   1.714 +				break;
   1.715 +			}
   1.716 +		}
   1.717 +		break;
   1.718 +
   1.719 +	case Discreet3DS::CHUNK_TRACKPIVOT:
   1.720 +
   1.721 +		if ( Discreet3DS::CHUNK_TRACKINFO != parent) 
   1.722 +		{
   1.723 +			DefaultLogger::get()->warn("3DS: Skipping pivot subchunk for non usual object");
   1.724 +			break;
   1.725 +		}
   1.726 +
   1.727 +		// Pivot = origin of rotation and scaling
   1.728 +		mCurrentNode->vPivot.x = stream->GetF4();
   1.729 +		mCurrentNode->vPivot.y = stream->GetF4();
   1.730 +		mCurrentNode->vPivot.z = stream->GetF4();
   1.731 +		break;
   1.732 +
   1.733 +
   1.734 +		// ////////////////////////////////////////////////////////////////////
   1.735 +		// POSITION KEYFRAME
   1.736 +	case Discreet3DS::CHUNK_TRACKPOS:
   1.737 +		{
   1.738 +		stream->IncPtr(10);
   1.739 +		const unsigned int numFrames = stream->GetI4();
   1.740 +		bool sortKeys = false;
   1.741 +
   1.742 +		// This could also be meant as the target position for
   1.743 +		// (targeted) lights and cameras
   1.744 +		std::vector<aiVectorKey>* l;
   1.745 +		if ( Discreet3DS::CHUNK_TRACKCAMTGT == parent || Discreet3DS::CHUNK_TRACKLIGTGT == parent)	{
   1.746 +			l = & mCurrentNode->aTargetPositionKeys;
   1.747 +		}
   1.748 +		else l = & mCurrentNode->aPositionKeys;
   1.749 +
   1.750 +		l->reserve(numFrames);
   1.751 +		for (unsigned int i = 0; i < numFrames;++i)	{
   1.752 +			const unsigned int fidx = stream->GetI4();
   1.753 +
   1.754 +			// Setup a new position key
   1.755 +			aiVectorKey v;
   1.756 +			v.mTime = (double)fidx;
   1.757 +
   1.758 +			SkipTCBInfo();
   1.759 +			v.mValue.x = stream->GetF4();
   1.760 +			v.mValue.y = stream->GetF4();
   1.761 +			v.mValue.z = stream->GetF4();
   1.762 +
   1.763 +			// check whether we'll need to sort the keys
   1.764 +			if (!l->empty() && v.mTime <= l->back().mTime)
   1.765 +				sortKeys = true;
   1.766 +
   1.767 +			// Add the new keyframe to the list
   1.768 +			l->push_back(v);
   1.769 +		}
   1.770 +
   1.771 +		// Sort all keys with ascending time values and remove duplicates?
   1.772 +		if (sortKeys)	{
   1.773 +			std::stable_sort(l->begin(),l->end());
   1.774 +			l->erase ( std::unique (l->begin(),l->end(),&KeyUniqueCompare<aiVectorKey>), l->end() );
   1.775 +		}}
   1.776 +
   1.777 +		break;
   1.778 +
   1.779 +		// ////////////////////////////////////////////////////////////////////
   1.780 +		// CAMERA ROLL KEYFRAME
   1.781 +	case Discreet3DS::CHUNK_TRACKROLL:
   1.782 +		{
   1.783 +		// roll keys are accepted for cameras only
   1.784 +		if (parent != Discreet3DS::CHUNK_TRACKCAMERA)	{
   1.785 +			DefaultLogger::get()->warn("3DS: Ignoring roll track for non-camera object");
   1.786 +			break;
   1.787 +		}
   1.788 +		bool sortKeys = false;
   1.789 +		std::vector<aiFloatKey>* l = &mCurrentNode->aCameraRollKeys;
   1.790 +
   1.791 +		stream->IncPtr(10);
   1.792 +		const unsigned int numFrames = stream->GetI4();
   1.793 +		l->reserve(numFrames);
   1.794 +		for (unsigned int i = 0; i < numFrames;++i)	{
   1.795 +			const unsigned int fidx = stream->GetI4();
   1.796 +
   1.797 +			// Setup a new position key
   1.798 +			aiFloatKey v;
   1.799 +			v.mTime = (double)fidx;
   1.800 +
   1.801 +			// This is just a single float 
   1.802 +			SkipTCBInfo();
   1.803 +			v.mValue = stream->GetF4();
   1.804 +
   1.805 +			// Check whether we'll need to sort the keys
   1.806 +			if (!l->empty() && v.mTime <= l->back().mTime)
   1.807 +				sortKeys = true;
   1.808 +
   1.809 +			// Add the new keyframe to the list
   1.810 +			l->push_back(v);
   1.811 +		}
   1.812 +
   1.813 +		// Sort all keys with ascending time values and remove duplicates?
   1.814 +		if (sortKeys)	{
   1.815 +			std::stable_sort(l->begin(),l->end());
   1.816 +			l->erase ( std::unique (l->begin(),l->end(),&KeyUniqueCompare<aiFloatKey>), l->end() );
   1.817 +		}}
   1.818 +		break;
   1.819 +
   1.820 +
   1.821 +		// ////////////////////////////////////////////////////////////////////
   1.822 +		// CAMERA FOV KEYFRAME
   1.823 +	case Discreet3DS::CHUNK_TRACKFOV:
   1.824 +		{
   1.825 +			DefaultLogger::get()->error("3DS: Skipping FOV animation track. "
   1.826 +				"This is not supported");
   1.827 +		}
   1.828 +		break;
   1.829 +
   1.830 +
   1.831 +		// ////////////////////////////////////////////////////////////////////
   1.832 +		// ROTATION KEYFRAME
   1.833 +	case Discreet3DS::CHUNK_TRACKROTATE:
   1.834 +		{
   1.835 +		stream->IncPtr(10);
   1.836 +		const unsigned int numFrames = stream->GetI4();
   1.837 +
   1.838 +		bool sortKeys = false;
   1.839 +		std::vector<aiQuatKey>* l = &mCurrentNode->aRotationKeys;
   1.840 +		l->reserve(numFrames);
   1.841 +
   1.842 +		for (unsigned int i = 0; i < numFrames;++i)	{
   1.843 +			const unsigned int fidx = stream->GetI4();
   1.844 +			SkipTCBInfo();
   1.845 +
   1.846 +			aiQuatKey v;
   1.847 +			v.mTime = (double)fidx;
   1.848 +
   1.849 +			// The rotation keyframe is given as an axis-angle pair
   1.850 +			const float rad = stream->GetF4();
   1.851 +			aiVector3D axis;
   1.852 +			axis.x = stream->GetF4();
   1.853 +			axis.y = stream->GetF4();
   1.854 +			axis.z = stream->GetF4();
   1.855 +
   1.856 +			if (!axis.x && !axis.y && !axis.z)
   1.857 +				axis.y = 1.f;
   1.858 +
   1.859 +			// Construct a rotation quaternion from the axis-angle pair
   1.860 +			v.mValue = aiQuaternion(axis,rad);
   1.861 +
   1.862 +			// Check whether we'll need to sort the keys
   1.863 +			if (!l->empty() && v.mTime <= l->back().mTime)
   1.864 +				sortKeys = true;
   1.865 +
   1.866 +			// add the new keyframe to the list
   1.867 +			l->push_back(v);
   1.868 +		}
   1.869 +		// Sort all keys with ascending time values and remove duplicates?
   1.870 +		if (sortKeys)	{
   1.871 +			std::stable_sort(l->begin(),l->end());
   1.872 +			l->erase ( std::unique (l->begin(),l->end(),&KeyUniqueCompare<aiQuatKey>), l->end() );
   1.873 +		}}
   1.874 +		break;
   1.875 +
   1.876 +		// ////////////////////////////////////////////////////////////////////
   1.877 +		// SCALING KEYFRAME
   1.878 +	case Discreet3DS::CHUNK_TRACKSCALE:
   1.879 +		{
   1.880 +		stream->IncPtr(10);
   1.881 +		const unsigned int numFrames = stream->GetI2();
   1.882 +		stream->IncPtr(2);
   1.883 +
   1.884 +		bool sortKeys = false;
   1.885 +		std::vector<aiVectorKey>* l = &mCurrentNode->aScalingKeys;
   1.886 +		l->reserve(numFrames);
   1.887 +
   1.888 +		for (unsigned int i = 0; i < numFrames;++i)	{
   1.889 +			const unsigned int fidx = stream->GetI4();
   1.890 +			SkipTCBInfo();
   1.891 +
   1.892 +			// Setup a new key
   1.893 +			aiVectorKey v;
   1.894 +			v.mTime = (double)fidx;
   1.895 +
   1.896 +			// ... and read its value
   1.897 +			v.mValue.x = stream->GetF4();
   1.898 +			v.mValue.y = stream->GetF4();
   1.899 +			v.mValue.z = stream->GetF4();
   1.900 +
   1.901 +			// check whether we'll need to sort the keys
   1.902 +			if (!l->empty() && v.mTime <= l->back().mTime)
   1.903 +				sortKeys = true;
   1.904 +			
   1.905 +			// Remove zero-scalings on singular axes - they've been reported to be there erroneously in some strange files
   1.906 +			if (!v.mValue.x) v.mValue.x = 1.f;
   1.907 +			if (!v.mValue.y) v.mValue.y = 1.f;
   1.908 +			if (!v.mValue.z) v.mValue.z = 1.f;
   1.909 +
   1.910 +			l->push_back(v);
   1.911 +		}
   1.912 +		// Sort all keys with ascending time values and remove duplicates?
   1.913 +		if (sortKeys)	{
   1.914 +			std::stable_sort(l->begin(),l->end());
   1.915 +			l->erase ( std::unique (l->begin(),l->end(),&KeyUniqueCompare<aiVectorKey>), l->end() );
   1.916 +		}}
   1.917 +		break;
   1.918 +	};
   1.919 +
   1.920 +	ASSIMP_3DS_END_CHUNK();
   1.921 +}
   1.922 +
   1.923 +// ------------------------------------------------------------------------------------------------
   1.924 +// Read a face chunk - it contains smoothing groups and material assignments
   1.925 +void Discreet3DSImporter::ParseFaceChunk()
   1.926 +{
   1.927 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.928 +
   1.929 +	// Get the mesh we're currently working on
   1.930 +	D3DS::Mesh& mMesh = mScene->mMeshes.back();
   1.931 +
   1.932 +	// Get chunk type
   1.933 +	switch (chunk.Flag)
   1.934 +	{
   1.935 +	case Discreet3DS::CHUNK_SMOOLIST:
   1.936 +		{
   1.937 +		// This is the list of smoothing groups - a bitfield for every face. 
   1.938 +		// Up to 32 smoothing groups assigned to a single face.
   1.939 +		unsigned int num = chunkSize/4, m = 0;
   1.940 +		for (std::vector<D3DS::Face>::iterator i =  mMesh.mFaces.begin(); m != num;++i, ++m)	{
   1.941 +			// nth bit is set for nth smoothing group
   1.942 +			(*i).iSmoothGroup = stream->GetI4();
   1.943 +		}}
   1.944 +		break;
   1.945 +
   1.946 +	case Discreet3DS::CHUNK_FACEMAT:
   1.947 +		{
   1.948 +		// at fist an asciiz with the material name
   1.949 +		const char* sz = (const char*)stream->GetPtr();
   1.950 +		while (stream->GetI1());
   1.951 +
   1.952 +		// find the index of the material
   1.953 +		unsigned int idx = 0xcdcdcdcd, cnt = 0;
   1.954 +		for (std::vector<D3DS::Material>::const_iterator i =  mScene->mMaterials.begin();i != mScene->mMaterials.end();++i,++cnt)	{
   1.955 +			// use case independent comparisons. hopefully it will work.
   1.956 +			if ((*i).mName.length() && !ASSIMP_stricmp(sz, (*i).mName.c_str()))	{
   1.957 +				idx = cnt;
   1.958 +				break;
   1.959 +			}
   1.960 +		}
   1.961 +		if (0xcdcdcdcd == idx)	{
   1.962 +			DefaultLogger::get()->error(std::string("3DS: Unknown material: ") + sz);
   1.963 +		}
   1.964 +
   1.965 +		// Now continue and read all material indices
   1.966 +		cnt = (uint16_t)stream->GetI2();
   1.967 +		for (unsigned int i = 0; i < cnt;++i)	{
   1.968 +			unsigned int fidx = (uint16_t)stream->GetI2();
   1.969 +
   1.970 +			// check range
   1.971 +			if (fidx >= mMesh.mFaceMaterials.size())	{
   1.972 +				DefaultLogger::get()->error("3DS: Invalid face index in face material list");
   1.973 +			}
   1.974 +			else mMesh.mFaceMaterials[fidx] = idx;
   1.975 +		}}
   1.976 +		break;
   1.977 +	};
   1.978 +	ASSIMP_3DS_END_CHUNK();
   1.979 +}
   1.980 +
   1.981 +// ------------------------------------------------------------------------------------------------
   1.982 +// Read a mesh chunk. Here's the actual mesh data
   1.983 +void Discreet3DSImporter::ParseMeshChunk()
   1.984 +{
   1.985 +	ASSIMP_3DS_BEGIN_CHUNK();
   1.986 +
   1.987 +	// Get the mesh we're currently working on
   1.988 +	D3DS::Mesh& mMesh = mScene->mMeshes.back();
   1.989 +
   1.990 +	// get chunk type
   1.991 +	switch (chunk.Flag)
   1.992 +	{
   1.993 +	case Discreet3DS::CHUNK_VERTLIST:
   1.994 +		{
   1.995 +		// This is the list of all vertices in the current mesh
   1.996 +		int num = (int)(uint16_t)stream->GetI2();
   1.997 +		mMesh.mPositions.reserve(num);
   1.998 +		while (num-- > 0)	{
   1.999 +			aiVector3D v;
  1.1000 +			v.x = stream->GetF4();
  1.1001 +			v.y = stream->GetF4();
  1.1002 +			v.z = stream->GetF4();
  1.1003 +			mMesh.mPositions.push_back(v);
  1.1004 +		}}
  1.1005 +		break;
  1.1006 +	case Discreet3DS::CHUNK_TRMATRIX:
  1.1007 +		{
  1.1008 +		// This is the RLEATIVE transformation matrix of the current mesh. Vertices are
  1.1009 +		// pretransformed by this matrix wonder.
  1.1010 +		mMesh.mMat.a1 = stream->GetF4();
  1.1011 +		mMesh.mMat.b1 = stream->GetF4();
  1.1012 +		mMesh.mMat.c1 = stream->GetF4();
  1.1013 +		mMesh.mMat.a2 = stream->GetF4();
  1.1014 +		mMesh.mMat.b2 = stream->GetF4();
  1.1015 +		mMesh.mMat.c2 = stream->GetF4();
  1.1016 +		mMesh.mMat.a3 = stream->GetF4();
  1.1017 +		mMesh.mMat.b3 = stream->GetF4();
  1.1018 +		mMesh.mMat.c3 = stream->GetF4();
  1.1019 +		mMesh.mMat.a4 = stream->GetF4();
  1.1020 +		mMesh.mMat.b4 = stream->GetF4();
  1.1021 +		mMesh.mMat.c4 = stream->GetF4();
  1.1022 +		}
  1.1023 +		break;
  1.1024 +
  1.1025 +	case Discreet3DS::CHUNK_MAPLIST:
  1.1026 +		{
  1.1027 +		// This is the list of all UV coords in the current mesh
  1.1028 +		int num = (int)(uint16_t)stream->GetI2();
  1.1029 +		mMesh.mTexCoords.reserve(num);
  1.1030 +		while (num-- > 0)	{
  1.1031 +			aiVector3D v;
  1.1032 +			v.x = stream->GetF4();
  1.1033 +			v.y = stream->GetF4();
  1.1034 +			mMesh.mTexCoords.push_back(v);
  1.1035 +		}}
  1.1036 +		break;
  1.1037 +
  1.1038 +	case Discreet3DS::CHUNK_FACELIST:
  1.1039 +		{
  1.1040 +		// This is the list of all faces in the current mesh
  1.1041 +		int num = (int)(uint16_t)stream->GetI2();
  1.1042 +		mMesh.mFaces.reserve(num);
  1.1043 +		while (num-- > 0)	{
  1.1044 +			// 3DS faces are ALWAYS triangles
  1.1045 +			mMesh.mFaces.push_back(D3DS::Face());
  1.1046 +			D3DS::Face& sFace = mMesh.mFaces.back();
  1.1047 +
  1.1048 +			sFace.mIndices[0] = (uint16_t)stream->GetI2();
  1.1049 +			sFace.mIndices[1] = (uint16_t)stream->GetI2();
  1.1050 +			sFace.mIndices[2] = (uint16_t)stream->GetI2();
  1.1051 +
  1.1052 +			stream->IncPtr(2); // skip edge visibility flag
  1.1053 +		}
  1.1054 +
  1.1055 +		// Resize the material array (0xcdcdcdcd marks the default material; so if a face is 
  1.1056 +		// not referenced by a material, $$DEFAULT will be assigned to it)
  1.1057 +		mMesh.mFaceMaterials.resize(mMesh.mFaces.size(),0xcdcdcdcd);
  1.1058 +
  1.1059 +		// Larger 3DS files could have multiple FACE chunks here
  1.1060 +		chunkSize = stream->GetRemainingSizeToLimit();
  1.1061 +		if ( chunkSize > (int) sizeof(Discreet3DS::Chunk ) )
  1.1062 +			ParseFaceChunk();
  1.1063 +		}
  1.1064 +		break;
  1.1065 +	};
  1.1066 +	ASSIMP_3DS_END_CHUNK();
  1.1067 +}
  1.1068 +
  1.1069 +// ------------------------------------------------------------------------------------------------
  1.1070 +// Read a 3DS material chunk
  1.1071 +void Discreet3DSImporter::ParseMaterialChunk()
  1.1072 +{
  1.1073 +	ASSIMP_3DS_BEGIN_CHUNK();
  1.1074 +	switch (chunk.Flag)
  1.1075 +	{
  1.1076 +	case Discreet3DS::CHUNK_MAT_MATNAME:
  1.1077 +
  1.1078 +		{
  1.1079 +		// The material name string is already zero-terminated, but we need to be sure ...
  1.1080 +		const char* sz = (const char*)stream->GetPtr();
  1.1081 +		unsigned int cnt = 0;
  1.1082 +		while (stream->GetI1())
  1.1083 +			++cnt;
  1.1084 +
  1.1085 +		if (!cnt)	{
  1.1086 +			// This may not be, we use the default name instead
  1.1087 +			DefaultLogger::get()->error("3DS: Empty material name");
  1.1088 +		}
  1.1089 +		else mScene->mMaterials.back().mName = std::string(sz,cnt);
  1.1090 +		}
  1.1091 +		break;
  1.1092 +
  1.1093 +	case Discreet3DS::CHUNK_MAT_DIFFUSE:
  1.1094 +		{
  1.1095 +		// This is the diffuse material color
  1.1096 +		aiColor3D* pc = &mScene->mMaterials.back().mDiffuse;
  1.1097 +		ParseColorChunk(pc);
  1.1098 +		if (is_qnan(pc->r))	{
  1.1099 +			// color chunk is invalid. Simply ignore it
  1.1100 +			DefaultLogger::get()->error("3DS: Unable to read DIFFUSE chunk");
  1.1101 +			pc->r = pc->g = pc->b = 1.0f;
  1.1102 +		}}
  1.1103 +		break;
  1.1104 +
  1.1105 +	case Discreet3DS::CHUNK_MAT_SPECULAR:
  1.1106 +		{
  1.1107 +		// This is the specular material color
  1.1108 +		aiColor3D* pc = &mScene->mMaterials.back().mSpecular;
  1.1109 +		ParseColorChunk(pc);
  1.1110 +		if (is_qnan(pc->r))	{
  1.1111 +			// color chunk is invalid. Simply ignore it
  1.1112 +			DefaultLogger::get()->error("3DS: Unable to read SPECULAR chunk");
  1.1113 +			pc->r = pc->g = pc->b = 1.0f;
  1.1114 +		}}
  1.1115 +		break;
  1.1116 +
  1.1117 +	case Discreet3DS::CHUNK_MAT_AMBIENT:
  1.1118 +		{
  1.1119 +		// This is the ambient material color
  1.1120 +		aiColor3D* pc = &mScene->mMaterials.back().mAmbient;
  1.1121 +		ParseColorChunk(pc);
  1.1122 +		if (is_qnan(pc->r))	{
  1.1123 +			// color chunk is invalid. Simply ignore it
  1.1124 +			DefaultLogger::get()->error("3DS: Unable to read AMBIENT chunk");
  1.1125 +			pc->r = pc->g = pc->b = 0.0f;
  1.1126 +		}}
  1.1127 +		break;
  1.1128 +
  1.1129 +	case Discreet3DS::CHUNK_MAT_SELF_ILLUM:
  1.1130 +		{
  1.1131 +		// This is the emissive material color
  1.1132 +		aiColor3D* pc = &mScene->mMaterials.back().mEmissive;
  1.1133 +		ParseColorChunk(pc);
  1.1134 +		if (is_qnan(pc->r))	{
  1.1135 +			// color chunk is invalid. Simply ignore it
  1.1136 +			DefaultLogger::get()->error("3DS: Unable to read EMISSIVE chunk");
  1.1137 +			pc->r = pc->g = pc->b = 0.0f;
  1.1138 +		}}
  1.1139 +		break;
  1.1140 +
  1.1141 +	case Discreet3DS::CHUNK_MAT_TRANSPARENCY:
  1.1142 +		{
  1.1143 +		// This is the material's transparency
  1.1144 +		float* pcf = &mScene->mMaterials.back().mTransparency;
  1.1145 +		*pcf = ParsePercentageChunk();
  1.1146 +
  1.1147 +		// NOTE: transparency, not opacity
  1.1148 +		if (is_qnan(*pcf))
  1.1149 +			*pcf = 1.0f;
  1.1150 +		else *pcf = 1.0f - *pcf * (float)0xFFFF / 100.0f;
  1.1151 +		}
  1.1152 +		break;
  1.1153 +
  1.1154 +	case Discreet3DS::CHUNK_MAT_SHADING:
  1.1155 +		// This is the material shading mode
  1.1156 +		mScene->mMaterials.back().mShading = (D3DS::Discreet3DS::shadetype3ds)stream->GetI2();
  1.1157 +		break;
  1.1158 +
  1.1159 +	case Discreet3DS::CHUNK_MAT_TWO_SIDE:
  1.1160 +		// This is the two-sided flag
  1.1161 +		mScene->mMaterials.back().mTwoSided = true;
  1.1162 +		break;
  1.1163 +
  1.1164 +	case Discreet3DS::CHUNK_MAT_SHININESS:
  1.1165 +		{ // This is the shininess of the material
  1.1166 +		float* pcf = &mScene->mMaterials.back().mSpecularExponent;
  1.1167 +		*pcf = ParsePercentageChunk();
  1.1168 +		if (is_qnan(*pcf))
  1.1169 +			*pcf = 0.0f;
  1.1170 +		else *pcf *= (float)0xFFFF;
  1.1171 +		}
  1.1172 +		break;
  1.1173 +
  1.1174 +	case Discreet3DS::CHUNK_MAT_SHININESS_PERCENT:
  1.1175 +		{ // This is the shininess strength of the material
  1.1176 +		float* pcf = &mScene->mMaterials.back().mShininessStrength;
  1.1177 +		*pcf = ParsePercentageChunk();
  1.1178 +		if (is_qnan(*pcf))
  1.1179 +			*pcf = 0.0f;
  1.1180 +		else *pcf *= (float)0xffff / 100.0f;
  1.1181 +		}
  1.1182 +		break;
  1.1183 +
  1.1184 +	case Discreet3DS::CHUNK_MAT_SELF_ILPCT:
  1.1185 +		{ // This is the self illumination strength of the material
  1.1186 +		float f = ParsePercentageChunk();
  1.1187 +		if (is_qnan(f))
  1.1188 +			f = 0.0f;
  1.1189 +		else f *= (float)0xFFFF / 100.0f;
  1.1190 +		mScene->mMaterials.back().mEmissive = aiColor3D(f,f,f);
  1.1191 +		}
  1.1192 +		break;
  1.1193 +
  1.1194 +	// Parse texture chunks
  1.1195 +	case Discreet3DS::CHUNK_MAT_TEXTURE:
  1.1196 +		// Diffuse texture
  1.1197 +		ParseTextureChunk(&mScene->mMaterials.back().sTexDiffuse);
  1.1198 +		break;
  1.1199 +	case Discreet3DS::CHUNK_MAT_BUMPMAP:
  1.1200 +		// Height map
  1.1201 +		ParseTextureChunk(&mScene->mMaterials.back().sTexBump);
  1.1202 +		break;
  1.1203 +	case Discreet3DS::CHUNK_MAT_OPACMAP:
  1.1204 +		// Opacity texture
  1.1205 +		ParseTextureChunk(&mScene->mMaterials.back().sTexOpacity);
  1.1206 +		break;
  1.1207 +	case Discreet3DS::CHUNK_MAT_MAT_SHINMAP:
  1.1208 +		// Shininess map
  1.1209 +		ParseTextureChunk(&mScene->mMaterials.back().sTexShininess);
  1.1210 +		break;
  1.1211 +	case Discreet3DS::CHUNK_MAT_SPECMAP:
  1.1212 +		// Specular map
  1.1213 +		ParseTextureChunk(&mScene->mMaterials.back().sTexSpecular);
  1.1214 +		break;
  1.1215 +	case Discreet3DS::CHUNK_MAT_SELFIMAP:
  1.1216 +		// Self-illumination (emissive) map
  1.1217 +		ParseTextureChunk(&mScene->mMaterials.back().sTexEmissive);
  1.1218 +		break;
  1.1219 +	case Discreet3DS::CHUNK_MAT_REFLMAP:
  1.1220 +		// Reflection map
  1.1221 +		ParseTextureChunk(&mScene->mMaterials.back().sTexReflective);
  1.1222 +		break;
  1.1223 +	};
  1.1224 +	ASSIMP_3DS_END_CHUNK();
  1.1225 +}
  1.1226 +
  1.1227 +// ------------------------------------------------------------------------------------------------
  1.1228 +void Discreet3DSImporter::ParseTextureChunk(D3DS::Texture* pcOut)
  1.1229 +{
  1.1230 +	ASSIMP_3DS_BEGIN_CHUNK();
  1.1231 +
  1.1232 +	// get chunk type
  1.1233 +	switch (chunk.Flag)
  1.1234 +	{
  1.1235 +	case Discreet3DS::CHUNK_MAPFILE:
  1.1236 +		{
  1.1237 +		// The material name string is already zero-terminated, but we need to be sure ...
  1.1238 +		const char* sz = (const char*)stream->GetPtr();
  1.1239 +		unsigned int cnt = 0;
  1.1240 +		while (stream->GetI1())
  1.1241 +			++cnt;
  1.1242 +		pcOut->mMapName = std::string(sz,cnt);
  1.1243 +		}
  1.1244 +		break;
  1.1245 +
  1.1246 +
  1.1247 +	case Discreet3DS::CHUNK_PERCENTF:
  1.1248 +		// Manually parse the blend factor
  1.1249 +		pcOut->mTextureBlend = stream->GetF4();
  1.1250 +		break;
  1.1251 +
  1.1252 +	case Discreet3DS::CHUNK_PERCENTW:
  1.1253 +		// Manually parse the blend factor
  1.1254 +		pcOut->mTextureBlend = (float)((uint16_t)stream->GetI2()) / 100.0f;
  1.1255 +		break;
  1.1256 +
  1.1257 +	case Discreet3DS::CHUNK_MAT_MAP_USCALE:
  1.1258 +		// Texture coordinate scaling in the U direction
  1.1259 +		pcOut->mScaleU = stream->GetF4();
  1.1260 +		if (0.0f == pcOut->mScaleU)
  1.1261 +		{
  1.1262 +			DefaultLogger::get()->warn("Texture coordinate scaling in the x direction is zero. Assuming 1.");
  1.1263 +			pcOut->mScaleU = 1.0f;
  1.1264 +		}
  1.1265 +		break;
  1.1266 +	case Discreet3DS::CHUNK_MAT_MAP_VSCALE:
  1.1267 +		// Texture coordinate scaling in the V direction
  1.1268 +		pcOut->mScaleV = stream->GetF4();
  1.1269 +		if (0.0f == pcOut->mScaleV)
  1.1270 +		{
  1.1271 +			DefaultLogger::get()->warn("Texture coordinate scaling in the y direction is zero. Assuming 1.");
  1.1272 +			pcOut->mScaleV = 1.0f;
  1.1273 +		}
  1.1274 +		break;
  1.1275 +
  1.1276 +	case Discreet3DS::CHUNK_MAT_MAP_UOFFSET:
  1.1277 +		// Texture coordinate offset in the U direction
  1.1278 +		pcOut->mOffsetU = -stream->GetF4();
  1.1279 +		break;
  1.1280 +
  1.1281 +	case Discreet3DS::CHUNK_MAT_MAP_VOFFSET:
  1.1282 +		// Texture coordinate offset in the V direction
  1.1283 +		pcOut->mOffsetV = stream->GetF4();
  1.1284 +		break;
  1.1285 +
  1.1286 +	case Discreet3DS::CHUNK_MAT_MAP_ANG:
  1.1287 +		// Texture coordinate rotation, CCW in DEGREES
  1.1288 +		pcOut->mRotation = -AI_DEG_TO_RAD( stream->GetF4() );
  1.1289 +		break;
  1.1290 +
  1.1291 +	case Discreet3DS::CHUNK_MAT_MAP_TILING:
  1.1292 +		{
  1.1293 +		const uint16_t iFlags = stream->GetI2();
  1.1294 +
  1.1295 +		// Get the mapping mode (for both axes)
  1.1296 +		if (iFlags & 0x2u)
  1.1297 +			pcOut->mMapMode = aiTextureMapMode_Mirror;
  1.1298 +		
  1.1299 +		else if (iFlags & 0x10u)
  1.1300 +			pcOut->mMapMode = aiTextureMapMode_Decal;
  1.1301 +		
  1.1302 +		// wrapping in all remaining cases
  1.1303 +		else pcOut->mMapMode = aiTextureMapMode_Wrap;
  1.1304 +		}
  1.1305 +		break;
  1.1306 +	};
  1.1307 +
  1.1308 +	ASSIMP_3DS_END_CHUNK();
  1.1309 +}
  1.1310 +
  1.1311 +// ------------------------------------------------------------------------------------------------
  1.1312 +// Read a percentage chunk
  1.1313 +float Discreet3DSImporter::ParsePercentageChunk()
  1.1314 +{
  1.1315 +	Discreet3DS::Chunk chunk;
  1.1316 +	ReadChunk(&chunk);
  1.1317 +
  1.1318 +	if (Discreet3DS::CHUNK_PERCENTF == chunk.Flag)
  1.1319 +		return stream->GetF4();
  1.1320 +	else if (Discreet3DS::CHUNK_PERCENTW == chunk.Flag)
  1.1321 +		return (float)((uint16_t)stream->GetI2()) / (float)0xFFFF;
  1.1322 +	return get_qnan();
  1.1323 +}
  1.1324 +
  1.1325 +// ------------------------------------------------------------------------------------------------
  1.1326 +// Read a color chunk. If a percentage chunk is found instead it is read as a grayscale color
  1.1327 +void Discreet3DSImporter::ParseColorChunk(aiColor3D* out,
  1.1328 +	bool acceptPercent)
  1.1329 +{
  1.1330 +	ai_assert(out != NULL);
  1.1331 +
  1.1332 +	// error return value
  1.1333 +	const float qnan = get_qnan();
  1.1334 +	static const aiColor3D clrError = aiColor3D(qnan,qnan,qnan);
  1.1335 +
  1.1336 +	Discreet3DS::Chunk chunk;
  1.1337 +	ReadChunk(&chunk);
  1.1338 +	const unsigned int diff = chunk.Size - sizeof(Discreet3DS::Chunk);
  1.1339 +
  1.1340 +	bool bGamma = false;
  1.1341 +
  1.1342 +	// Get the type of the chunk
  1.1343 +	switch(chunk.Flag)
  1.1344 +	{
  1.1345 +	case Discreet3DS::CHUNK_LINRGBF:
  1.1346 +		bGamma = true;
  1.1347 +
  1.1348 +	case Discreet3DS::CHUNK_RGBF:
  1.1349 +		if (sizeof(float) * 3 > diff)	{
  1.1350 +			*out = clrError;
  1.1351 +			return;
  1.1352 +		}
  1.1353 +		out->r = stream->GetF4();
  1.1354 +		out->g = stream->GetF4();
  1.1355 +		out->b = stream->GetF4();
  1.1356 +		break;
  1.1357 +
  1.1358 +	case Discreet3DS::CHUNK_LINRGBB:
  1.1359 +		bGamma = true;
  1.1360 +	case Discreet3DS::CHUNK_RGBB:
  1.1361 +		if (sizeof(char) * 3 > diff)	{
  1.1362 +			*out = clrError;
  1.1363 +			return;
  1.1364 +		}
  1.1365 +		out->r = (float)(uint8_t)stream->GetI1() / 255.0f;
  1.1366 +		out->g = (float)(uint8_t)stream->GetI1() / 255.0f;
  1.1367 +		out->b = (float)(uint8_t)stream->GetI1() / 255.0f;
  1.1368 +		break;
  1.1369 +
  1.1370 +	// Percentage chunks are accepted, too.
  1.1371 +	case Discreet3DS::CHUNK_PERCENTF:
  1.1372 +		if (acceptPercent && 4 <= diff)	{
  1.1373 +			out->g = out->b = out->r = stream->GetF4();
  1.1374 +			break;
  1.1375 +		}
  1.1376 +		*out = clrError;
  1.1377 +		return;
  1.1378 +
  1.1379 +	case Discreet3DS::CHUNK_PERCENTW:
  1.1380 +		if (acceptPercent && 1 <= diff)	{
  1.1381 +			out->g = out->b = out->r = (float)(uint8_t)stream->GetI1() / 255.0f;
  1.1382 +			break;
  1.1383 +		}
  1.1384 +		*out = clrError;
  1.1385 +		return;
  1.1386 +
  1.1387 +	default:
  1.1388 +		stream->IncPtr(diff);
  1.1389 +		// Skip unknown chunks, hope this won't cause any problems.
  1.1390 +		return ParseColorChunk(out,acceptPercent);
  1.1391 +	};
  1.1392 +	(void)bGamma;
  1.1393 +}
  1.1394 +
  1.1395 +#endif // !! ASSIMP_BUILD_NO_3DS_IMPORTER