vrshoot

diff libs/assimp/PlyLoader.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/PlyLoader.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,1062 @@
     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  PlyLoader.cpp
    1.46 + *  @brief Implementation of the PLY importer class
    1.47 + */
    1.48 +
    1.49 +#include "AssimpPCH.h"
    1.50 +#ifndef ASSIMP_BUILD_NO_PLY_IMPORTER
    1.51 +
    1.52 +// internal headers
    1.53 +#include "PlyLoader.h"
    1.54 +
    1.55 +using namespace Assimp;
    1.56 +
    1.57 +static const aiImporterDesc desc = {
    1.58 +	"Stanford Polygon Library (PLY) Importer",
    1.59 +	"",
    1.60 +	"",
    1.61 +	"",
    1.62 +	aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportTextFlavour,
    1.63 +	0,
    1.64 +	0,
    1.65 +	0,
    1.66 +	0,
    1.67 +	"ply" 
    1.68 +};
    1.69 +
    1.70 +// ------------------------------------------------------------------------------------------------
    1.71 +// Constructor to be privately used by Importer
    1.72 +PLYImporter::PLYImporter()
    1.73 +{}
    1.74 +
    1.75 +// ------------------------------------------------------------------------------------------------
    1.76 +// Destructor, private as well 
    1.77 +PLYImporter::~PLYImporter()
    1.78 +{}
    1.79 +
    1.80 +// ------------------------------------------------------------------------------------------------
    1.81 +// Returns whether the class can handle the format of the given file. 
    1.82 +bool PLYImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
    1.83 +{
    1.84 +	const std::string extension = GetExtension(pFile);
    1.85 +
    1.86 +	if (extension == "ply")
    1.87 +		return true;
    1.88 +	else if (!extension.length() || checkSig)
    1.89 +	{
    1.90 +		if (!pIOHandler)return true;
    1.91 +		const char* tokens[] = {"ply"};
    1.92 +		return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
    1.93 +	}
    1.94 +	return false;
    1.95 +}
    1.96 +
    1.97 +// ------------------------------------------------------------------------------------------------
    1.98 +const aiImporterDesc* PLYImporter::GetInfo () const
    1.99 +{
   1.100 +	return &desc;
   1.101 +}
   1.102 +
   1.103 +// ------------------------------------------------------------------------------------------------
   1.104 +// Imports the given file into the given scene structure. 
   1.105 +void PLYImporter::InternReadFile( const std::string& pFile, 
   1.106 +	aiScene* pScene, IOSystem* pIOHandler)
   1.107 +{
   1.108 +	boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
   1.109 +
   1.110 +	// Check whether we can read from the file
   1.111 +	if( file.get() == NULL) {
   1.112 +		throw DeadlyImportError( "Failed to open PLY file " + pFile + ".");
   1.113 +	}
   1.114 +
   1.115 +	// allocate storage and copy the contents of the file to a memory buffer
   1.116 +	std::vector<char> mBuffer2;
   1.117 +	TextFileToBuffer(file.get(),mBuffer2);
   1.118 +	mBuffer = (unsigned char*)&mBuffer2[0];
   1.119 +
   1.120 +	// the beginning of the file must be PLY - magic, magic
   1.121 +	if ((mBuffer[0] != 'P' && mBuffer[0] != 'p') ||
   1.122 +		(mBuffer[1] != 'L' && mBuffer[1] != 'l') ||
   1.123 +		(mBuffer[2] != 'Y' && mBuffer[2] != 'y'))	{
   1.124 +		throw DeadlyImportError( "Invalid .ply file: Magic number \'ply\' is no there");
   1.125 +	}
   1.126 +
   1.127 +	char* szMe = (char*)&this->mBuffer[3];
   1.128 +	SkipSpacesAndLineEnd(szMe,(const char**)&szMe);
   1.129 +	
   1.130 +	// determine the format of the file data
   1.131 +	PLY::DOM sPlyDom;
   1.132 +	if (TokenMatch(szMe,"format",6))
   1.133 +	{
   1.134 +		if (TokenMatch(szMe,"ascii",5))
   1.135 +		{
   1.136 +			SkipLine(szMe,(const char**)&szMe);
   1.137 +			if(!PLY::DOM::ParseInstance(szMe,&sPlyDom))
   1.138 +				throw DeadlyImportError( "Invalid .ply file: Unable to build DOM (#1)");
   1.139 +		}
   1.140 +		else if (!::strncmp(szMe,"binary_",7))
   1.141 +		{
   1.142 +			bool bIsBE = false;
   1.143 +			szMe+=7;
   1.144 +
   1.145 +			// binary_little_endian
   1.146 +			// binary_big_endian
   1.147 +#if (defined AI_BUILD_BIG_ENDIAN)
   1.148 +			if ('l' == *szMe || 'L' == *szMe)bIsBE = true;
   1.149 +#else
   1.150 +			if ('b' == *szMe || 'B' == *szMe)bIsBE = true;
   1.151 +#endif // ! AI_BUILD_BIG_ENDIAN
   1.152 +
   1.153 +			// skip the line, parse the rest of the header and build the DOM
   1.154 +			SkipLine(szMe,(const char**)&szMe);
   1.155 +			if(!PLY::DOM::ParseInstanceBinary(szMe,&sPlyDom,bIsBE))
   1.156 +				throw DeadlyImportError( "Invalid .ply file: Unable to build DOM (#2)");
   1.157 +		}
   1.158 +		else throw DeadlyImportError( "Invalid .ply file: Unknown file format");
   1.159 +	}
   1.160 +	else
   1.161 +	{
   1.162 +		delete[] this->mBuffer;
   1.163 +		AI_DEBUG_INVALIDATE_PTR(this->mBuffer);
   1.164 +		throw DeadlyImportError( "Invalid .ply file: Missing format specification");
   1.165 +	}
   1.166 +	this->pcDOM = &sPlyDom;
   1.167 +
   1.168 +	// now load a list of vertices. This must be sucessfull in order to procede
   1.169 +	std::vector<aiVector3D> avPositions;
   1.170 +	this->LoadVertices(&avPositions,false);
   1.171 +
   1.172 +	if (avPositions.empty())
   1.173 +		throw DeadlyImportError( "Invalid .ply file: No vertices found. "
   1.174 +			"Unable to parse the data format of the PLY file.");
   1.175 +
   1.176 +	// now load a list of normals. 
   1.177 +	std::vector<aiVector3D> avNormals;
   1.178 +	LoadVertices(&avNormals,true);
   1.179 +
   1.180 +	// load the face list
   1.181 +	std::vector<PLY::Face> avFaces;
   1.182 +	LoadFaces(&avFaces);
   1.183 +
   1.184 +	// if no face list is existing we assume that the vertex
   1.185 +	// list is containing a list of triangles
   1.186 +	if (avFaces.empty())
   1.187 +	{
   1.188 +		if (avPositions.size() < 3)
   1.189 +		{
   1.190 +			throw DeadlyImportError( "Invalid .ply file: Not enough "
   1.191 +				"vertices to build a proper face list. ");
   1.192 +		}
   1.193 +
   1.194 +		const unsigned int iNum = (unsigned int)avPositions.size() / 3;
   1.195 +		for (unsigned int i = 0; i< iNum;++i)
   1.196 +		{
   1.197 +			PLY::Face sFace;
   1.198 +			sFace.mIndices.push_back((iNum*3));
   1.199 +			sFace.mIndices.push_back((iNum*3)+1);
   1.200 +			sFace.mIndices.push_back((iNum*3)+2);
   1.201 +			avFaces.push_back(sFace);
   1.202 +		}
   1.203 +	}
   1.204 +
   1.205 +	// now load a list of all materials
   1.206 +	std::vector<aiMaterial*> avMaterials;
   1.207 +	LoadMaterial(&avMaterials);
   1.208 +
   1.209 +	// now load a list of all vertex color channels
   1.210 +	std::vector<aiColor4D> avColors;
   1.211 +	avColors.reserve(avPositions.size());
   1.212 +	LoadVertexColor(&avColors);
   1.213 +
   1.214 +	// now try to load texture coordinates
   1.215 +	std::vector<aiVector2D> avTexCoords;
   1.216 +	avTexCoords.reserve(avPositions.size());
   1.217 +	LoadTextureCoordinates(&avTexCoords);
   1.218 +
   1.219 +	// now replace the default material in all faces and validate all material indices
   1.220 +	ReplaceDefaultMaterial(&avFaces,&avMaterials);
   1.221 +
   1.222 +	// now convert this to a list of aiMesh instances
   1.223 +	std::vector<aiMesh*> avMeshes;
   1.224 +	avMeshes.reserve(avMaterials.size()+1);
   1.225 +	ConvertMeshes(&avFaces,&avPositions,&avNormals,
   1.226 +		&avColors,&avTexCoords,&avMaterials,&avMeshes);
   1.227 +
   1.228 +	if (avMeshes.empty())
   1.229 +		throw DeadlyImportError( "Invalid .ply file: Unable to extract mesh data ");
   1.230 +
   1.231 +	// now generate the output scene object. Fill the material list
   1.232 +	pScene->mNumMaterials = (unsigned int)avMaterials.size();
   1.233 +	pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
   1.234 +	for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
   1.235 +		pScene->mMaterials[i] = avMaterials[i];
   1.236 +
   1.237 +	// fill the mesh list
   1.238 +	pScene->mNumMeshes = (unsigned int)avMeshes.size();
   1.239 +	pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
   1.240 +	for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
   1.241 +		pScene->mMeshes[i] = avMeshes[i];
   1.242 +
   1.243 +	// generate a simple node structure
   1.244 +	pScene->mRootNode = new aiNode();
   1.245 +	pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
   1.246 +	pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
   1.247 +
   1.248 +	for (unsigned int i = 0; i < pScene->mRootNode->mNumMeshes;++i)
   1.249 +		pScene->mRootNode->mMeshes[i] = i;
   1.250 +}
   1.251 +
   1.252 +// ------------------------------------------------------------------------------------------------
   1.253 +// Split meshes by material IDs
   1.254 +void PLYImporter::ConvertMeshes(std::vector<PLY::Face>* avFaces,
   1.255 +	const std::vector<aiVector3D>*			avPositions,
   1.256 +	const std::vector<aiVector3D>*			avNormals,
   1.257 +	const std::vector<aiColor4D>*			avColors,
   1.258 +	const std::vector<aiVector2D>*			avTexCoords,
   1.259 +	const std::vector<aiMaterial*>*		avMaterials,
   1.260 +	std::vector<aiMesh*>* avOut)
   1.261 +{
   1.262 +	ai_assert(NULL != avFaces);
   1.263 +	ai_assert(NULL != avPositions);
   1.264 +	ai_assert(NULL != avMaterials);
   1.265 +
   1.266 +	// split by materials
   1.267 +	std::vector<unsigned int>* aiSplit = new std::vector<unsigned int>[avMaterials->size()];
   1.268 +
   1.269 +	unsigned int iNum = 0;
   1.270 +	for (std::vector<PLY::Face>::const_iterator i = avFaces->begin();i != avFaces->end();++i,++iNum)
   1.271 +		aiSplit[(*i).iMaterialIndex].push_back(iNum);
   1.272 +	
   1.273 +	// now generate submeshes
   1.274 +	for (unsigned int p = 0; p < avMaterials->size();++p)
   1.275 +	{
   1.276 +		if (aiSplit[p].size() != 0)
   1.277 +		{
   1.278 +			// allocate the mesh object
   1.279 +			aiMesh* p_pcOut = new aiMesh();
   1.280 +			p_pcOut->mMaterialIndex = p;
   1.281 +
   1.282 +			p_pcOut->mNumFaces = (unsigned int)aiSplit[p].size();
   1.283 +			p_pcOut->mFaces = new aiFace[aiSplit[p].size()];
   1.284 +
   1.285 +			// at first we need to determine the size of the output vector array
   1.286 +			unsigned int iNum = 0;
   1.287 +			for (unsigned int i = 0; i < aiSplit[p].size();++i)
   1.288 +			{
   1.289 +				iNum += (unsigned int)(*avFaces)[aiSplit[p][i]].mIndices.size();
   1.290 +			}
   1.291 +			p_pcOut->mNumVertices = iNum;
   1.292 +			p_pcOut->mVertices = new aiVector3D[iNum];
   1.293 +
   1.294 +			if (!avColors->empty())
   1.295 +				p_pcOut->mColors[0] = new aiColor4D[iNum];
   1.296 +			if (!avTexCoords->empty())
   1.297 +			{
   1.298 +				p_pcOut->mNumUVComponents[0] = 2;
   1.299 +				p_pcOut->mTextureCoords[0] = new aiVector3D[iNum];
   1.300 +			}
   1.301 +			if (!avNormals->empty())
   1.302 +				p_pcOut->mNormals = new aiVector3D[iNum];
   1.303 +
   1.304 +			// add all faces
   1.305 +			iNum = 0;
   1.306 +			unsigned int iVertex = 0;
   1.307 +			for (std::vector<unsigned int>::const_iterator i =  aiSplit[p].begin();
   1.308 +				i != aiSplit[p].end();++i,++iNum)
   1.309 +			{
   1.310 +				p_pcOut->mFaces[iNum].mNumIndices = (unsigned int)(*avFaces)[*i].mIndices.size(); 
   1.311 +				p_pcOut->mFaces[iNum].mIndices = new unsigned int[p_pcOut->mFaces[iNum].mNumIndices];
   1.312 +
   1.313 +				// build an unique set of vertices/colors for this face
   1.314 +				for (unsigned int q = 0; q <  p_pcOut->mFaces[iNum].mNumIndices;++q)
   1.315 +				{
   1.316 +					p_pcOut->mFaces[iNum].mIndices[q] = iVertex;
   1.317 +					p_pcOut->mVertices[iVertex] = (*avPositions)[(*avFaces)[*i].mIndices[q]];
   1.318 +
   1.319 +					if (!avColors->empty())
   1.320 +						p_pcOut->mColors[0][iVertex] = (*avColors)[(*avFaces)[*i].mIndices[q]];
   1.321 +
   1.322 +					if (!avTexCoords->empty())
   1.323 +					{
   1.324 +						const aiVector2D& vec = (*avTexCoords)[(*avFaces)[*i].mIndices[q]];
   1.325 +						p_pcOut->mTextureCoords[0][iVertex].x = vec.x;
   1.326 +						p_pcOut->mTextureCoords[0][iVertex].y = vec.y;
   1.327 +					}
   1.328 +
   1.329 +					if (!avNormals->empty())
   1.330 +						p_pcOut->mNormals[iVertex] = (*avNormals)[(*avFaces)[*i].mIndices[q]];
   1.331 +					iVertex++;
   1.332 +				}
   1.333 +
   1.334 +			}
   1.335 +			// add the mesh to the output list
   1.336 +			avOut->push_back(p_pcOut);
   1.337 +		}
   1.338 +	}
   1.339 +	delete[] aiSplit; // cleanup
   1.340 +}
   1.341 +
   1.342 +// ------------------------------------------------------------------------------------------------
   1.343 +// Generate a default material if none was specified and apply it to all vanilla faces
   1.344 +void PLYImporter::ReplaceDefaultMaterial(std::vector<PLY::Face>* avFaces,
   1.345 +	std::vector<aiMaterial*>* avMaterials)
   1.346 +{
   1.347 +	bool bNeedDefaultMat = false;
   1.348 +
   1.349 +	for (std::vector<PLY::Face>::iterator i =  avFaces->begin();i != avFaces->end();++i)	{
   1.350 +		if (0xFFFFFFFF == (*i).iMaterialIndex)	{
   1.351 +			bNeedDefaultMat = true;
   1.352 +			(*i).iMaterialIndex = (unsigned int)avMaterials->size();
   1.353 +		}
   1.354 +		else if ((*i).iMaterialIndex >= avMaterials->size() )	{
   1.355 +			// clamp the index
   1.356 +			(*i).iMaterialIndex = (unsigned int)avMaterials->size()-1;
   1.357 +		}
   1.358 +	}
   1.359 +
   1.360 +	if (bNeedDefaultMat)	{
   1.361 +		// generate a default material
   1.362 +		aiMaterial* pcHelper = new aiMaterial();
   1.363 +
   1.364 +		// fill in a default material
   1.365 +		int iMode = (int)aiShadingMode_Gouraud;
   1.366 +		pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
   1.367 +
   1.368 +		aiColor3D clr;
   1.369 +		clr.b = clr.g = clr.r = 0.6f;
   1.370 +		pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_DIFFUSE);
   1.371 +		pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_SPECULAR);
   1.372 +
   1.373 +		clr.b = clr.g = clr.r = 0.05f;
   1.374 +		pcHelper->AddProperty<aiColor3D>(&clr, 1,AI_MATKEY_COLOR_AMBIENT);
   1.375 +
   1.376 +		// The face order is absolutely undefined for PLY, so we have to
   1.377 +		// use two-sided rendering to be sure it's ok.
   1.378 +		const int two_sided = 1;
   1.379 +		pcHelper->AddProperty(&two_sided,1,AI_MATKEY_TWOSIDED);
   1.380 +
   1.381 +		avMaterials->push_back(pcHelper);
   1.382 +	}
   1.383 +}
   1.384 +
   1.385 +// ------------------------------------------------------------------------------------------------
   1.386 +void PLYImporter::LoadTextureCoordinates(std::vector<aiVector2D>* pvOut)
   1.387 +{
   1.388 +	ai_assert(NULL != pvOut);
   1.389 +
   1.390 +	unsigned int aiPositions[2] = {0xFFFFFFFF,0xFFFFFFFF};
   1.391 +	PLY::EDataType aiTypes[2] = {EDT_Char,EDT_Char};
   1.392 +	PLY::ElementInstanceList* pcList = NULL;
   1.393 +	unsigned int cnt = 0;
   1.394 +
   1.395 +	// serach in the DOM for a vertex entry
   1.396 +	unsigned int _i = 0;
   1.397 +	for (std::vector<PLY::Element>::const_iterator i = pcDOM->alElements.begin();
   1.398 +		i != pcDOM->alElements.end();++i,++_i)
   1.399 +	{
   1.400 +		if (PLY::EEST_Vertex == (*i).eSemantic)
   1.401 +		{
   1.402 +			pcList = &this->pcDOM->alElementData[_i];
   1.403 +
   1.404 +			// now check whether which normal components are available
   1.405 +			unsigned int _a = 0;
   1.406 +			for (std::vector<PLY::Property>::const_iterator a =  (*i).alProperties.begin();
   1.407 +				a != (*i).alProperties.end();++a,++_a)
   1.408 +			{
   1.409 +				if ((*a).bIsList)continue;
   1.410 +				if (PLY::EST_UTextureCoord == (*a).Semantic)
   1.411 +				{
   1.412 +					cnt++;
   1.413 +					aiPositions[0] = _a;
   1.414 +					aiTypes[0] = (*a).eType;
   1.415 +				}
   1.416 +				else if (PLY::EST_VTextureCoord == (*a).Semantic)
   1.417 +				{
   1.418 +					cnt++;
   1.419 +					aiPositions[1] = _a;
   1.420 +					aiTypes[1] = (*a).eType;
   1.421 +				}
   1.422 +			}
   1.423 +		}
   1.424 +	}
   1.425 +	// check whether we have a valid source for the texture coordinates data
   1.426 +	if (NULL != pcList && 0 != cnt)
   1.427 +	{
   1.428 +		pvOut->reserve(pcList->alInstances.size());
   1.429 +		for (std::vector<ElementInstance>::const_iterator i = pcList->alInstances.begin();
   1.430 +			i != pcList->alInstances.end();++i)
   1.431 +		{
   1.432 +			// convert the vertices to sp floats
   1.433 +			aiVector2D vOut;
   1.434 +
   1.435 +			if (0xFFFFFFFF != aiPositions[0])
   1.436 +			{
   1.437 +				vOut.x = PLY::PropertyInstance::ConvertTo<float>(
   1.438 +					(*i).alProperties[aiPositions[0]].avList.front(),aiTypes[0]);
   1.439 +			}
   1.440 +
   1.441 +			if (0xFFFFFFFF != aiPositions[1])
   1.442 +			{
   1.443 +				vOut.y = PLY::PropertyInstance::ConvertTo<float>(
   1.444 +					(*i).alProperties[aiPositions[1]].avList.front(),aiTypes[1]);
   1.445 +			}
   1.446 +			// and add them to our nice list
   1.447 +			pvOut->push_back(vOut);
   1.448 +		}
   1.449 +	}
   1.450 +}
   1.451 +
   1.452 +// ------------------------------------------------------------------------------------------------
   1.453 +// Try to extract vertices from the PLY DOM
   1.454 +void PLYImporter::LoadVertices(std::vector<aiVector3D>* pvOut, bool p_bNormals)
   1.455 +{
   1.456 +	ai_assert(NULL != pvOut);
   1.457 +
   1.458 +	unsigned int aiPositions[3] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
   1.459 +	PLY::EDataType aiTypes[3] = {EDT_Char,EDT_Char,EDT_Char};
   1.460 +	PLY::ElementInstanceList* pcList = NULL;
   1.461 +	unsigned int cnt = 0;
   1.462 +
   1.463 +	// serach in the DOM for a vertex entry
   1.464 +	unsigned int _i = 0;
   1.465 +	for (std::vector<PLY::Element>::const_iterator i =  pcDOM->alElements.begin();
   1.466 +		i != pcDOM->alElements.end();++i,++_i)
   1.467 +	{
   1.468 +		if (PLY::EEST_Vertex == (*i).eSemantic)
   1.469 +		{
   1.470 +			pcList = &pcDOM->alElementData[_i];
   1.471 +
   1.472 +			// load normal vectors?
   1.473 +			if (p_bNormals)
   1.474 +			{
   1.475 +				// now check whether which normal components are available
   1.476 +				unsigned int _a = 0;
   1.477 +				for (std::vector<PLY::Property>::const_iterator a = (*i).alProperties.begin();
   1.478 +					a != (*i).alProperties.end();++a,++_a)
   1.479 +				{
   1.480 +					if ((*a).bIsList)continue;
   1.481 +					if (PLY::EST_XNormal == (*a).Semantic)
   1.482 +					{
   1.483 +						cnt++;
   1.484 +						aiPositions[0] = _a;
   1.485 +						aiTypes[0] = (*a).eType;
   1.486 +					}
   1.487 +					else if (PLY::EST_YNormal == (*a).Semantic)
   1.488 +					{
   1.489 +						cnt++;
   1.490 +						aiPositions[1] = _a;
   1.491 +						aiTypes[1] = (*a).eType;
   1.492 +					}
   1.493 +					else if (PLY::EST_ZNormal == (*a).Semantic)
   1.494 +					{
   1.495 +						cnt++;
   1.496 +						aiPositions[2] = _a;
   1.497 +						aiTypes[2] = (*a).eType;
   1.498 +					}
   1.499 +				}
   1.500 +			}
   1.501 +			// load vertex coordinates
   1.502 +			else
   1.503 +			{
   1.504 +				// now check whether which coordinate sets are available
   1.505 +				unsigned int _a = 0;
   1.506 +				for (std::vector<PLY::Property>::const_iterator a = (*i).alProperties.begin();
   1.507 +					a != (*i).alProperties.end();++a,++_a)
   1.508 +				{
   1.509 +					if ((*a).bIsList)continue;
   1.510 +					if (PLY::EST_XCoord == (*a).Semantic)
   1.511 +					{
   1.512 +						cnt++;
   1.513 +						aiPositions[0] = _a;
   1.514 +						aiTypes[0] = (*a).eType;
   1.515 +					}
   1.516 +					else if (PLY::EST_YCoord == (*a).Semantic)
   1.517 +					{
   1.518 +						cnt++;
   1.519 +						aiPositions[1] = _a;
   1.520 +						aiTypes[1] = (*a).eType;
   1.521 +					}
   1.522 +					else if (PLY::EST_ZCoord == (*a).Semantic)
   1.523 +					{
   1.524 +						cnt++;
   1.525 +						aiPositions[2] = _a;
   1.526 +						aiTypes[2] = (*a).eType;
   1.527 +					}
   1.528 +					if (3 == cnt)break; 
   1.529 +				}
   1.530 +			}
   1.531 +			break;
   1.532 +		}
   1.533 +	}
   1.534 +	// check whether we have a valid source for the vertex data
   1.535 +	if (NULL != pcList && 0 != cnt)
   1.536 +	{
   1.537 +		pvOut->reserve(pcList->alInstances.size());
   1.538 +		for (std::vector<ElementInstance>::const_iterator
   1.539 +			i =  pcList->alInstances.begin();
   1.540 +			i != pcList->alInstances.end();++i)
   1.541 +		{
   1.542 +			// convert the vertices to sp floats
   1.543 +			aiVector3D vOut;
   1.544 +
   1.545 +			if (0xFFFFFFFF != aiPositions[0])
   1.546 +			{
   1.547 +				vOut.x = PLY::PropertyInstance::ConvertTo<float>(
   1.548 +					(*i).alProperties[aiPositions[0]].avList.front(),aiTypes[0]);
   1.549 +			}
   1.550 +
   1.551 +			if (0xFFFFFFFF != aiPositions[1])
   1.552 +			{
   1.553 +				vOut.y = PLY::PropertyInstance::ConvertTo<float>(
   1.554 +					(*i).alProperties[aiPositions[1]].avList.front(),aiTypes[1]);
   1.555 +			}
   1.556 +
   1.557 +			if (0xFFFFFFFF != aiPositions[2])
   1.558 +			{
   1.559 +				vOut.z = PLY::PropertyInstance::ConvertTo<float>(
   1.560 +					(*i).alProperties[aiPositions[2]].avList.front(),aiTypes[2]);
   1.561 +			}
   1.562 +
   1.563 +			// and add them to our nice list
   1.564 +			pvOut->push_back(vOut);
   1.565 +		}
   1.566 +	}
   1.567 +}
   1.568 +
   1.569 +// ------------------------------------------------------------------------------------------------
   1.570 +// Convert a color component to [0...1]
   1.571 +float PLYImporter::NormalizeColorValue (PLY::PropertyInstance::ValueUnion val,
   1.572 +	PLY::EDataType eType)
   1.573 +{
   1.574 +	switch (eType)
   1.575 +	{
   1.576 +	case EDT_Float:
   1.577 +		return val.fFloat;
   1.578 +	case EDT_Double:
   1.579 +		return (float)val.fDouble;
   1.580 +
   1.581 +	case EDT_UChar:
   1.582 +		return (float)val.iUInt / (float)0xFF;
   1.583 +	case EDT_Char:
   1.584 +		return (float)(val.iInt+(0xFF/2)) / (float)0xFF;
   1.585 +	case EDT_UShort:
   1.586 +		return (float)val.iUInt / (float)0xFFFF;
   1.587 +	case EDT_Short:
   1.588 +		return (float)(val.iInt+(0xFFFF/2)) / (float)0xFFFF;
   1.589 +	case EDT_UInt:
   1.590 +		return (float)val.iUInt / (float)0xFFFF;
   1.591 +	case EDT_Int:
   1.592 +		return ((float)val.iInt / (float)0xFF) + 0.5f;
   1.593 +	default: ;
   1.594 +	};
   1.595 +	return 0.0f;
   1.596 +}
   1.597 +
   1.598 +// ------------------------------------------------------------------------------------------------
   1.599 +// Try to extract proper vertex colors from the PLY DOM
   1.600 +void PLYImporter::LoadVertexColor(std::vector<aiColor4D>* pvOut)
   1.601 +{
   1.602 +	ai_assert(NULL != pvOut);
   1.603 +
   1.604 +	unsigned int aiPositions[4] = {0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF};
   1.605 +	PLY::EDataType aiTypes[4] = {EDT_Char, EDT_Char, EDT_Char, EDT_Char}; // silencing gcc
   1.606 +	unsigned int cnt = 0;
   1.607 +	PLY::ElementInstanceList* pcList = NULL;
   1.608 +
   1.609 +	// serach in the DOM for a vertex entry
   1.610 +	unsigned int _i = 0;
   1.611 +	for (std::vector<PLY::Element>::const_iterator i = pcDOM->alElements.begin();
   1.612 +		i != pcDOM->alElements.end();++i,++_i)
   1.613 +	{
   1.614 +		if (PLY::EEST_Vertex == (*i).eSemantic)
   1.615 +		{
   1.616 +			pcList = &this->pcDOM->alElementData[_i];
   1.617 +
   1.618 +			// now check whether which coordinate sets are available
   1.619 +			unsigned int _a = 0;
   1.620 +			for (std::vector<PLY::Property>::const_iterator
   1.621 +				a =  (*i).alProperties.begin();
   1.622 +				a != (*i).alProperties.end();++a,++_a)
   1.623 +			{
   1.624 +				if ((*a).bIsList)continue;
   1.625 +				if (PLY::EST_Red == (*a).Semantic)
   1.626 +				{
   1.627 +					cnt++;
   1.628 +					aiPositions[0] = _a;
   1.629 +					aiTypes[0] = (*a).eType;
   1.630 +				}
   1.631 +				else if (PLY::EST_Green == (*a).Semantic)
   1.632 +				{
   1.633 +					cnt++;
   1.634 +					aiPositions[1] = _a;
   1.635 +					aiTypes[1] = (*a).eType;
   1.636 +				}
   1.637 +				else if (PLY::EST_Blue == (*a).Semantic)
   1.638 +				{
   1.639 +					cnt++;
   1.640 +					aiPositions[2] = _a;
   1.641 +					aiTypes[2] = (*a).eType;
   1.642 +				}
   1.643 +				else if (PLY::EST_Alpha == (*a).Semantic)
   1.644 +				{
   1.645 +					cnt++;
   1.646 +					aiPositions[3] = _a;
   1.647 +					aiTypes[3] = (*a).eType;
   1.648 +				}
   1.649 +				if (4 == cnt)break; 
   1.650 +			}
   1.651 +			break;
   1.652 +		}
   1.653 +	}
   1.654 +	// check whether we have a valid source for the vertex data
   1.655 +	if (NULL != pcList && 0 != cnt)
   1.656 +	{
   1.657 +		pvOut->reserve(pcList->alInstances.size());
   1.658 +		for (std::vector<ElementInstance>::const_iterator i = pcList->alInstances.begin();
   1.659 +			i != pcList->alInstances.end();++i)
   1.660 +		{
   1.661 +			// convert the vertices to sp floats
   1.662 +			aiColor4D vOut;
   1.663 +			
   1.664 +			if (0xFFFFFFFF != aiPositions[0])
   1.665 +			{
   1.666 +				vOut.r = NormalizeColorValue((*i).alProperties[
   1.667 +					aiPositions[0]].avList.front(),aiTypes[0]);
   1.668 +			}
   1.669 +
   1.670 +			if (0xFFFFFFFF != aiPositions[1])
   1.671 +			{
   1.672 +				vOut.g = NormalizeColorValue((*i).alProperties[
   1.673 +					aiPositions[1]].avList.front(),aiTypes[1]);
   1.674 +			}
   1.675 +
   1.676 +			if (0xFFFFFFFF != aiPositions[2])
   1.677 +			{
   1.678 +				vOut.b = NormalizeColorValue((*i).alProperties[
   1.679 +					aiPositions[2]].avList.front(),aiTypes[2]);
   1.680 +			}
   1.681 +
   1.682 +			// assume 1.0 for the alpha channel ifit is not set
   1.683 +			if (0xFFFFFFFF == aiPositions[3])vOut.a = 1.0f;
   1.684 +			else
   1.685 +			{
   1.686 +				vOut.a = NormalizeColorValue((*i).alProperties[
   1.687 +					aiPositions[3]].avList.front(),aiTypes[3]);
   1.688 +			}
   1.689 +
   1.690 +			// and add them to our nice list
   1.691 +			pvOut->push_back(vOut);
   1.692 +		}
   1.693 +	}
   1.694 +}
   1.695 +
   1.696 +// ------------------------------------------------------------------------------------------------
   1.697 +// Try to extract proper faces from the PLY DOM
   1.698 +void PLYImporter::LoadFaces(std::vector<PLY::Face>* pvOut)
   1.699 +{
   1.700 +	ai_assert(NULL != pvOut);
   1.701 +
   1.702 +	PLY::ElementInstanceList* pcList = NULL;
   1.703 +	bool bOne = false;
   1.704 +
   1.705 +	// index of the vertex index list
   1.706 +	unsigned int iProperty = 0xFFFFFFFF;
   1.707 +	PLY::EDataType eType = EDT_Char;
   1.708 +	bool bIsTristrip = false;
   1.709 +
   1.710 +	// index of the material index property
   1.711 +	unsigned int iMaterialIndex = 0xFFFFFFFF;
   1.712 +	PLY::EDataType eType2 = EDT_Char;
   1.713 +
   1.714 +	// serach in the DOM for a face entry
   1.715 +	unsigned int _i = 0;
   1.716 +	for (std::vector<PLY::Element>::const_iterator i =  pcDOM->alElements.begin();
   1.717 +		i != pcDOM->alElements.end();++i,++_i)
   1.718 +	{
   1.719 +		// face = unique number of vertex indices
   1.720 +		if (PLY::EEST_Face == (*i).eSemantic)
   1.721 +		{
   1.722 +			pcList = &pcDOM->alElementData[_i];
   1.723 +			unsigned int _a = 0;
   1.724 +			for (std::vector<PLY::Property>::const_iterator a =  (*i).alProperties.begin();
   1.725 +				a != (*i).alProperties.end();++a,++_a)
   1.726 +			{
   1.727 +				if (PLY::EST_VertexIndex == (*a).Semantic)
   1.728 +				{
   1.729 +					// must be a dynamic list!
   1.730 +					if (!(*a).bIsList)continue;
   1.731 +					iProperty	= _a;
   1.732 +					bOne		= true;
   1.733 +					eType		= (*a).eType;		
   1.734 +				}
   1.735 +				else if (PLY::EST_MaterialIndex == (*a).Semantic)
   1.736 +				{
   1.737 +					if ((*a).bIsList)continue;
   1.738 +					iMaterialIndex	= _a;
   1.739 +					bOne			= true;
   1.740 +					eType2		= (*a).eType;		
   1.741 +				}
   1.742 +			}
   1.743 +			break;
   1.744 +		}
   1.745 +		// triangle strip
   1.746 +		// TODO: triangle strip and material index support???
   1.747 +		else if (PLY::EEST_TriStrip == (*i).eSemantic)
   1.748 +		{
   1.749 +			// find a list property in this ...
   1.750 +			pcList = &this->pcDOM->alElementData[_i];
   1.751 +			unsigned int _a = 0;
   1.752 +			for (std::vector<PLY::Property>::const_iterator a =  (*i).alProperties.begin();
   1.753 +				a != (*i).alProperties.end();++a,++_a)
   1.754 +			{
   1.755 +				// must be a dynamic list!
   1.756 +				if (!(*a).bIsList)continue;
   1.757 +				iProperty	= _a;
   1.758 +				bOne		= true;
   1.759 +				bIsTristrip	= true;
   1.760 +				eType		= (*a).eType;	
   1.761 +				break;
   1.762 +			}
   1.763 +			break;
   1.764 +		}
   1.765 +	}
   1.766 +	// check whether we have at least one per-face information set
   1.767 +	if (pcList && bOne)
   1.768 +	{
   1.769 +		if (!bIsTristrip)
   1.770 +		{
   1.771 +			pvOut->reserve(pcList->alInstances.size());
   1.772 +			for (std::vector<ElementInstance>::const_iterator i =  pcList->alInstances.begin();
   1.773 +				i != pcList->alInstances.end();++i)
   1.774 +			{
   1.775 +				PLY::Face sFace;
   1.776 +
   1.777 +				// parse the list of vertex indices
   1.778 +				if (0xFFFFFFFF != iProperty)
   1.779 +				{
   1.780 +					const unsigned int iNum = (unsigned int)(*i).alProperties[iProperty].avList.size();
   1.781 +					sFace.mIndices.resize(iNum);
   1.782 +
   1.783 +					std::vector<PLY::PropertyInstance::ValueUnion>::const_iterator p = 
   1.784 +						(*i).alProperties[iProperty].avList.begin();
   1.785 +
   1.786 +					for (unsigned int a = 0; a < iNum;++a,++p)
   1.787 +					{
   1.788 +						sFace.mIndices[a] = PLY::PropertyInstance::ConvertTo<unsigned int>(*p,eType);
   1.789 +					}
   1.790 +				}
   1.791 +
   1.792 +				// parse the material index
   1.793 +				if (0xFFFFFFFF != iMaterialIndex)
   1.794 +				{
   1.795 +					sFace.iMaterialIndex = PLY::PropertyInstance::ConvertTo<unsigned int>(
   1.796 +						(*i).alProperties[iMaterialIndex].avList.front(),eType2);
   1.797 +				}
   1.798 +				pvOut->push_back(sFace);
   1.799 +			}
   1.800 +		}
   1.801 +		else // triangle strips
   1.802 +		{
   1.803 +			// normally we have only one triangle strip instance where
   1.804 +			// a value of -1 indicates a restart of the strip
   1.805 +			bool flip = false;
   1.806 +			for (std::vector<ElementInstance>::const_iterator i = pcList->alInstances.begin();i != pcList->alInstances.end();++i) {
   1.807 +				const std::vector<PLY::PropertyInstance::ValueUnion>& quak = (*i).alProperties[iProperty].avList;
   1.808 +				pvOut->reserve(pvOut->size() + quak.size() + (quak.size()>>2u));
   1.809 +
   1.810 +				int aiTable[2] = {-1,-1};
   1.811 +				for (std::vector<PLY::PropertyInstance::ValueUnion>::const_iterator a =  quak.begin();a != quak.end();++a)	{
   1.812 +					const int p = PLY::PropertyInstance::ConvertTo<int>(*a,eType);
   1.813 +
   1.814 +					if (-1 == p)	{
   1.815 +						// restart the strip ...
   1.816 +						aiTable[0] = aiTable[1] = -1;
   1.817 +						flip = false;
   1.818 +						continue;
   1.819 +					}
   1.820 +					if (-1 == aiTable[0]) {
   1.821 +						aiTable[0] = p;
   1.822 +						continue;
   1.823 +					}
   1.824 +					if (-1 == aiTable[1]) {
   1.825 +						aiTable[1] = p;
   1.826 +						continue;
   1.827 +					}
   1.828 +				
   1.829 +					pvOut->push_back(PLY::Face());
   1.830 +					PLY::Face& sFace = pvOut->back();
   1.831 +					sFace.mIndices[0] = aiTable[0];
   1.832 +					sFace.mIndices[1] = aiTable[1];
   1.833 +					sFace.mIndices[2] = p;
   1.834 +					if ((flip = !flip)) {
   1.835 +						std::swap(sFace.mIndices[0],sFace.mIndices[1]);
   1.836 +					}
   1.837 +					
   1.838 +					aiTable[0] = aiTable[1];
   1.839 +					aiTable[1] = p;
   1.840 +				}
   1.841 +			}
   1.842 +		}
   1.843 +	}
   1.844 +}
   1.845 +
   1.846 +// ------------------------------------------------------------------------------------------------
   1.847 +// Get a RGBA color in [0...1] range
   1.848 +void PLYImporter::GetMaterialColor(const std::vector<PLY::PropertyInstance>& avList,
   1.849 +	unsigned int aiPositions[4], 
   1.850 +	PLY::EDataType aiTypes[4],
   1.851 +	 aiColor4D* clrOut)
   1.852 +{
   1.853 +	ai_assert(NULL != clrOut);
   1.854 +
   1.855 +	if (0xFFFFFFFF == aiPositions[0])clrOut->r = 0.0f;
   1.856 +	else
   1.857 +	{
   1.858 +		clrOut->r = NormalizeColorValue(avList[
   1.859 +			aiPositions[0]].avList.front(),aiTypes[0]);
   1.860 +	}
   1.861 +
   1.862 +	if (0xFFFFFFFF == aiPositions[1])clrOut->g = 0.0f;
   1.863 +	else
   1.864 +	{
   1.865 +		clrOut->g = NormalizeColorValue(avList[
   1.866 +			aiPositions[1]].avList.front(),aiTypes[1]);
   1.867 +	}
   1.868 +
   1.869 +	if (0xFFFFFFFF == aiPositions[2])clrOut->b = 0.0f;
   1.870 +	else
   1.871 +	{
   1.872 +		clrOut->b = NormalizeColorValue(avList[
   1.873 +			aiPositions[2]].avList.front(),aiTypes[2]);
   1.874 +	}
   1.875 +
   1.876 +	// assume 1.0 for the alpha channel ifit is not set
   1.877 +	if (0xFFFFFFFF == aiPositions[3])clrOut->a = 1.0f;
   1.878 +	else
   1.879 +	{
   1.880 +		clrOut->a = NormalizeColorValue(avList[
   1.881 +			aiPositions[3]].avList.front(),aiTypes[3]);
   1.882 +	}
   1.883 +}
   1.884 +
   1.885 +// ------------------------------------------------------------------------------------------------
   1.886 +// Extract a material from the PLY DOM
   1.887 +void PLYImporter::LoadMaterial(std::vector<aiMaterial*>* pvOut)
   1.888 +{
   1.889 +	ai_assert(NULL != pvOut);
   1.890 +
   1.891 +	// diffuse[4], specular[4], ambient[4]
   1.892 +	// rgba order
   1.893 +	unsigned int aaiPositions[3][4] = {
   1.894 +
   1.895 +		{0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
   1.896 +		{0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
   1.897 +		{0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF},
   1.898 +	};
   1.899 +
   1.900 +	PLY::EDataType aaiTypes[3][4] = {
   1.901 +		{EDT_Char,EDT_Char,EDT_Char,EDT_Char},
   1.902 +		{EDT_Char,EDT_Char,EDT_Char,EDT_Char},
   1.903 +		{EDT_Char,EDT_Char,EDT_Char,EDT_Char}
   1.904 +	};
   1.905 +	PLY::ElementInstanceList* pcList = NULL;
   1.906 +
   1.907 +	unsigned int iPhong = 0xFFFFFFFF;
   1.908 +	PLY::EDataType ePhong = EDT_Char;
   1.909 +
   1.910 +	unsigned int iOpacity = 0xFFFFFFFF;
   1.911 +	PLY::EDataType eOpacity = EDT_Char;
   1.912 +
   1.913 +	// serach in the DOM for a vertex entry
   1.914 +	unsigned int _i = 0;
   1.915 +	for (std::vector<PLY::Element>::const_iterator i =  this->pcDOM->alElements.begin();
   1.916 +		i != this->pcDOM->alElements.end();++i,++_i)
   1.917 +	{
   1.918 +		if (PLY::EEST_Material == (*i).eSemantic)
   1.919 +		{
   1.920 +			pcList = &this->pcDOM->alElementData[_i];
   1.921 +
   1.922 +			// now check whether which coordinate sets are available
   1.923 +			unsigned int _a = 0;
   1.924 +			for (std::vector<PLY::Property>::const_iterator
   1.925 +				a =  (*i).alProperties.begin();
   1.926 +				a != (*i).alProperties.end();++a,++_a)
   1.927 +			{
   1.928 +				if ((*a).bIsList)continue;
   1.929 +
   1.930 +				// pohng specularity      -----------------------------------
   1.931 +				if (PLY::EST_PhongPower == (*a).Semantic)
   1.932 +				{
   1.933 +					iPhong		= _a;
   1.934 +					ePhong		= (*a).eType;
   1.935 +				}
   1.936 +
   1.937 +				// general opacity        -----------------------------------
   1.938 +				if (PLY::EST_Opacity == (*a).Semantic)
   1.939 +				{
   1.940 +					iOpacity		= _a;
   1.941 +					eOpacity		= (*a).eType;
   1.942 +				}
   1.943 +
   1.944 +				// diffuse color channels -----------------------------------
   1.945 +				if (PLY::EST_DiffuseRed == (*a).Semantic)
   1.946 +				{
   1.947 +					aaiPositions[0][0]	= _a;
   1.948 +					aaiTypes[0][0]		= (*a).eType;
   1.949 +				}
   1.950 +				else if (PLY::EST_DiffuseGreen == (*a).Semantic)
   1.951 +				{
   1.952 +					aaiPositions[0][1]	= _a;
   1.953 +					aaiTypes[0][1]		= (*a).eType;
   1.954 +				}
   1.955 +				else if (PLY::EST_DiffuseBlue == (*a).Semantic)
   1.956 +				{
   1.957 +					aaiPositions[0][2]	= _a;
   1.958 +					aaiTypes[0][2]		= (*a).eType;
   1.959 +				}
   1.960 +				else if (PLY::EST_DiffuseAlpha == (*a).Semantic)
   1.961 +				{
   1.962 +					aaiPositions[0][3]	= _a;
   1.963 +					aaiTypes[0][3]		= (*a).eType;
   1.964 +				}
   1.965 +				// specular color channels -----------------------------------
   1.966 +				else if (PLY::EST_SpecularRed == (*a).Semantic)
   1.967 +				{
   1.968 +					aaiPositions[1][0]	= _a;
   1.969 +					aaiTypes[1][0]		= (*a).eType;
   1.970 +				}
   1.971 +				else if (PLY::EST_SpecularGreen == (*a).Semantic)
   1.972 +				{
   1.973 +					aaiPositions[1][1]	= _a;
   1.974 +					aaiTypes[1][1]		= (*a).eType;
   1.975 +				}
   1.976 +				else if (PLY::EST_SpecularBlue == (*a).Semantic)
   1.977 +				{
   1.978 +					aaiPositions[1][2]	= _a;
   1.979 +					aaiTypes[1][2]		= (*a).eType;
   1.980 +				}
   1.981 +				else if (PLY::EST_SpecularAlpha == (*a).Semantic)
   1.982 +				{
   1.983 +					aaiPositions[1][3]	= _a;
   1.984 +					aaiTypes[1][3]		= (*a).eType;
   1.985 +				}
   1.986 +				// ambient color channels -----------------------------------
   1.987 +				else if (PLY::EST_AmbientRed == (*a).Semantic)
   1.988 +				{
   1.989 +					aaiPositions[2][0]	= _a;
   1.990 +					aaiTypes[2][0]		= (*a).eType;
   1.991 +				}
   1.992 +				else if (PLY::EST_AmbientGreen == (*a).Semantic)
   1.993 +				{
   1.994 +					aaiPositions[2][1]	= _a;
   1.995 +					aaiTypes[2][1]		= (*a).eType;
   1.996 +				}
   1.997 +				else if (PLY::EST_AmbientBlue == (*a).Semantic)
   1.998 +				{
   1.999 +					aaiPositions[2][2]	= _a;
  1.1000 +					aaiTypes[2][2]		= (*a).eType;
  1.1001 +				}
  1.1002 +				else if (PLY::EST_AmbientAlpha == (*a).Semantic)
  1.1003 +				{
  1.1004 +					aaiPositions[2][3]	= _a;
  1.1005 +					aaiTypes[2][3]		= (*a).eType;
  1.1006 +				}
  1.1007 +			}
  1.1008 +			break;
  1.1009 +		}
  1.1010 +	}
  1.1011 +	// check whether we have a valid source for the material data
  1.1012 +	if (NULL != pcList)	{
  1.1013 +		for (std::vector<ElementInstance>::const_iterator i =  pcList->alInstances.begin();i != pcList->alInstances.end();++i)	{
  1.1014 +			aiColor4D clrOut;
  1.1015 +			aiMaterial* pcHelper = new aiMaterial();
  1.1016 +	
  1.1017 +			// build the diffuse material color
  1.1018 +			GetMaterialColor((*i).alProperties,aaiPositions[0],aaiTypes[0],&clrOut);
  1.1019 +			pcHelper->AddProperty<aiColor4D>(&clrOut,1,AI_MATKEY_COLOR_DIFFUSE);
  1.1020 +
  1.1021 +			// build the specular material color
  1.1022 +			GetMaterialColor((*i).alProperties,aaiPositions[1],aaiTypes[1],&clrOut);
  1.1023 +			pcHelper->AddProperty<aiColor4D>(&clrOut,1,AI_MATKEY_COLOR_SPECULAR);
  1.1024 +
  1.1025 +			// build the ambient material color
  1.1026 +			GetMaterialColor((*i).alProperties,aaiPositions[2],aaiTypes[2],&clrOut);
  1.1027 +			pcHelper->AddProperty<aiColor4D>(&clrOut,1,AI_MATKEY_COLOR_AMBIENT);
  1.1028 +
  1.1029 +			// handle phong power and shading mode
  1.1030 +			int iMode;
  1.1031 +			if (0xFFFFFFFF != iPhong)	{
  1.1032 +				float fSpec = PLY::PropertyInstance::ConvertTo<float>((*i).alProperties[iPhong].avList.front(),ePhong);
  1.1033 +
  1.1034 +				// if shininess is 0 (and the pow() calculation would therefore always
  1.1035 +				// become 1, not depending on the angle), use gouraud lighting
  1.1036 +				if (fSpec)	{
  1.1037 +					// scale this with 15 ... hopefully this is correct
  1.1038 +					fSpec *= 15;
  1.1039 +					pcHelper->AddProperty<float>(&fSpec, 1, AI_MATKEY_SHININESS);
  1.1040 +
  1.1041 +					iMode = (int)aiShadingMode_Phong;
  1.1042 +				}
  1.1043 +				else iMode = (int)aiShadingMode_Gouraud;
  1.1044 +			}
  1.1045 +			else iMode = (int)aiShadingMode_Gouraud;
  1.1046 +			pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
  1.1047 +
  1.1048 +			// handle opacity
  1.1049 +			if (0xFFFFFFFF != iOpacity)	{
  1.1050 +				float fOpacity = PLY::PropertyInstance::ConvertTo<float>((*i).alProperties[iPhong].avList.front(),eOpacity);
  1.1051 +				pcHelper->AddProperty<float>(&fOpacity, 1, AI_MATKEY_OPACITY);
  1.1052 +			}
  1.1053 +
  1.1054 +			// The face order is absolutely undefined for PLY, so we have to
  1.1055 +			// use two-sided rendering to be sure it's ok.
  1.1056 +			const int two_sided = 1;
  1.1057 +			pcHelper->AddProperty(&two_sided,1,AI_MATKEY_TWOSIDED);
  1.1058 +
  1.1059 +			// add the newly created material instance to the list
  1.1060 +			pvOut->push_back(pcHelper);
  1.1061 +		}
  1.1062 +	}
  1.1063 +}
  1.1064 +
  1.1065 +#endif // !! ASSIMP_BUILD_NO_PLY_IMPORTER