vrshoot

diff libs/assimp/MakeVerboseFormat.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/MakeVerboseFormat.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,215 @@
     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 +/** @file Implementation of the post processing step "MakeVerboseFormat"
    1.45 +*/
    1.46 +
    1.47 +#include "AssimpPCH.h"
    1.48 +#include "MakeVerboseFormat.h"
    1.49 +
    1.50 +using namespace Assimp;
    1.51 +
    1.52 +// ------------------------------------------------------------------------------------------------
    1.53 +MakeVerboseFormatProcess::MakeVerboseFormatProcess()
    1.54 +{
    1.55 +	// nothing to do here
    1.56 +}
    1.57 +// ------------------------------------------------------------------------------------------------
    1.58 +MakeVerboseFormatProcess::~MakeVerboseFormatProcess()
    1.59 +{
    1.60 +	// nothing to do here
    1.61 +}
    1.62 +// ------------------------------------------------------------------------------------------------
    1.63 +// Executes the post processing step on the given imported data.
    1.64 +void MakeVerboseFormatProcess::Execute( aiScene* pScene)
    1.65 +{
    1.66 +	ai_assert(NULL != pScene);
    1.67 +	DefaultLogger::get()->debug("MakeVerboseFormatProcess begin");
    1.68 +
    1.69 +	bool bHas = false;
    1.70 +	for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
    1.71 +	{
    1.72 +		if(	MakeVerboseFormat( pScene->mMeshes[a]))
    1.73 +			bHas = true;
    1.74 +	}
    1.75 +	if (bHas) DefaultLogger::get()->info("MakeVerboseFormatProcess finished. There was much work to do ...");
    1.76 +	else DefaultLogger::get()->debug("MakeVerboseFormatProcess. There was nothing to do.");
    1.77 +
    1.78 +	pScene->mFlags &= ~AI_SCENE_FLAGS_NON_VERBOSE_FORMAT;
    1.79 +
    1.80 +}
    1.81 +// ------------------------------------------------------------------------------------------------
    1.82 +// Executes the post processing step on the given imported data.
    1.83 +bool MakeVerboseFormatProcess::MakeVerboseFormat(aiMesh* pcMesh)
    1.84 +{
    1.85 +	ai_assert(NULL != pcMesh);
    1.86 +
    1.87 +	unsigned int iOldNumVertices = pcMesh->mNumVertices;
    1.88 +	const unsigned int iNumVerts = pcMesh->mNumFaces*3;
    1.89 +
    1.90 +	aiVector3D* pvPositions = new aiVector3D[ iNumVerts ];
    1.91 +
    1.92 +	aiVector3D* pvNormals = NULL;
    1.93 +	if (pcMesh->HasNormals()) 
    1.94 +	{
    1.95 +		pvNormals = new aiVector3D[iNumVerts];
    1.96 +	}
    1.97 +	aiVector3D* pvTangents = NULL, *pvBitangents = NULL;
    1.98 +	if (pcMesh->HasTangentsAndBitangents()) 
    1.99 +	{
   1.100 +		pvTangents = new aiVector3D[iNumVerts];
   1.101 +		pvBitangents = new aiVector3D[iNumVerts];
   1.102 +	}
   1.103 +
   1.104 +	aiVector3D* apvTextureCoords[AI_MAX_NUMBER_OF_TEXTURECOORDS] = {0};
   1.105 +	aiColor4D* apvColorSets[AI_MAX_NUMBER_OF_COLOR_SETS] = {0};
   1.106 +
   1.107 +	unsigned int p = 0;
   1.108 +	while (pcMesh->HasTextureCoords(p))
   1.109 +		apvTextureCoords[p++] = new aiVector3D[iNumVerts];
   1.110 +
   1.111 +	p = 0;
   1.112 +	while (pcMesh->HasVertexColors(p))
   1.113 +		apvColorSets[p++] = new aiColor4D[iNumVerts];
   1.114 +
   1.115 +	// allocate enough memory to hold output bones and vertex weights ...
   1.116 +	std::vector<aiVertexWeight>* newWeights = new std::vector<aiVertexWeight>[pcMesh->mNumBones];
   1.117 +	for (unsigned int i = 0;i < pcMesh->mNumBones;++i) {
   1.118 +		newWeights[i].reserve(pcMesh->mBones[i]->mNumWeights*3);
   1.119 +	}
   1.120 +
   1.121 +	// iterate through all faces and build a clean list
   1.122 +	unsigned int iIndex = 0;
   1.123 +	for (unsigned int a = 0; a< pcMesh->mNumFaces;++a)
   1.124 +	{
   1.125 +		aiFace* pcFace = &pcMesh->mFaces[a];
   1.126 +		for (unsigned int q = 0; q < pcFace->mNumIndices;++q,++iIndex)
   1.127 +		{
   1.128 +			// need to build a clean list of bones, too
   1.129 +			for (unsigned int i = 0;i < pcMesh->mNumBones;++i) 
   1.130 +			{
   1.131 +				for (unsigned int a = 0;  a < pcMesh->mBones[i]->mNumWeights;a++)
   1.132 +				{
   1.133 +					const aiVertexWeight& w = pcMesh->mBones[i]->mWeights[a];
   1.134 +					if(pcFace->mIndices[q] == w.mVertexId)
   1.135 +					{
   1.136 +						aiVertexWeight wNew;
   1.137 +						wNew.mVertexId = iIndex;
   1.138 +						wNew.mWeight = w.mWeight;
   1.139 +						newWeights[i].push_back(wNew);
   1.140 +					}
   1.141 +				}
   1.142 +			}
   1.143 +
   1.144 +			pvPositions[iIndex] = pcMesh->mVertices[pcFace->mIndices[q]];
   1.145 +
   1.146 +			if (pcMesh->HasNormals()) 
   1.147 +			{
   1.148 +				pvNormals[iIndex] = pcMesh->mNormals[pcFace->mIndices[q]];
   1.149 +			}
   1.150 +			if (pcMesh->HasTangentsAndBitangents()) 
   1.151 +			{
   1.152 +				pvTangents[iIndex] = pcMesh->mTangents[pcFace->mIndices[q]];
   1.153 +				pvBitangents[iIndex] = pcMesh->mBitangents[pcFace->mIndices[q]];
   1.154 +			}
   1.155 +
   1.156 +			unsigned int p = 0;
   1.157 +			while (pcMesh->HasTextureCoords(p))
   1.158 +			{
   1.159 +				apvTextureCoords[p][iIndex] = pcMesh->mTextureCoords[p][pcFace->mIndices[q]];
   1.160 +				++p;
   1.161 +			}
   1.162 +			p = 0;
   1.163 +			while (pcMesh->HasVertexColors(p))
   1.164 +			{
   1.165 +				apvColorSets[p][iIndex] = pcMesh->mColors[p][pcFace->mIndices[q]];
   1.166 +				++p;
   1.167 +			}
   1.168 +			pcFace->mIndices[q] = iIndex;
   1.169 +		}
   1.170 +	}
   1.171 +
   1.172 +	// build output vertex weights
   1.173 +	for (unsigned int i = 0;i < pcMesh->mNumBones;++i) 
   1.174 +	{
   1.175 +		delete pcMesh->mBones[i]->mWeights;
   1.176 +		if (!newWeights[i].empty())
   1.177 +		{
   1.178 +			pcMesh->mBones[i]->mWeights = new aiVertexWeight[newWeights[i].size()];
   1.179 +			memcpy(pcMesh->mBones[i]->mWeights,&newWeights[i][0],
   1.180 +				sizeof(aiVertexWeight) * newWeights[i].size());
   1.181 +		}
   1.182 +		else pcMesh->mBones[i]->mWeights = NULL;
   1.183 +	}
   1.184 +
   1.185 +	// delete the old members
   1.186 +	delete[] pcMesh->mVertices;
   1.187 +	pcMesh->mVertices = pvPositions;
   1.188 +
   1.189 +	p = 0;
   1.190 +	while (pcMesh->HasTextureCoords(p))
   1.191 +	{
   1.192 +		delete pcMesh->mTextureCoords[p];
   1.193 +		pcMesh->mTextureCoords[p] = apvTextureCoords[p];
   1.194 +		++p;
   1.195 +	}
   1.196 +	p = 0;
   1.197 +	while (pcMesh->HasVertexColors(p))
   1.198 +	{
   1.199 +		delete pcMesh->mColors[p];
   1.200 +		pcMesh->mColors[p] = apvColorSets[p];
   1.201 +		++p;
   1.202 +	}
   1.203 +	pcMesh->mNumVertices = iNumVerts;
   1.204 +
   1.205 +	if (pcMesh->HasNormals()) 
   1.206 +	{
   1.207 +		delete[] pcMesh->mNormals;
   1.208 +		pcMesh->mNormals = pvNormals;
   1.209 +	}
   1.210 +	if (pcMesh->HasTangentsAndBitangents()) 
   1.211 +	{
   1.212 +		delete[] pcMesh->mTangents;
   1.213 +		pcMesh->mTangents = pvTangents;
   1.214 +		delete[] pcMesh->mBitangents;
   1.215 +		pcMesh->mBitangents = pvBitangents;
   1.216 +	}
   1.217 +	return (pcMesh->mNumVertices != iOldNumVertices);
   1.218 +}