nuclear@0: /* nuclear@0: --------------------------------------------------------------------------- nuclear@0: Open Asset Import Library (assimp) nuclear@0: --------------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the following nuclear@0: conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: --------------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: /** @file Implementation of the post processing step to calculate nuclear@0: * tangents and bitangents for all imported meshes nuclear@0: */ nuclear@0: nuclear@0: #include "AssimpPCH.h" nuclear@0: nuclear@0: // internal headers nuclear@0: #include "CalcTangentsProcess.h" nuclear@0: #include "ProcessHelper.h" nuclear@0: #include "TinyFormatter.h" nuclear@0: nuclear@0: using namespace Assimp; nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Constructor to be privately used by Importer nuclear@0: CalcTangentsProcess::CalcTangentsProcess() nuclear@0: { nuclear@0: this->configMaxAngle = AI_DEG_TO_RAD(45.f); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Destructor, private as well nuclear@0: CalcTangentsProcess::~CalcTangentsProcess() nuclear@0: { nuclear@0: // nothing to do here nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Returns whether the processing step is present in the given flag field. nuclear@0: bool CalcTangentsProcess::IsActive( unsigned int pFlags) const nuclear@0: { nuclear@0: return (pFlags & aiProcess_CalcTangentSpace) != 0; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Executes the post processing step on the given imported data. nuclear@0: void CalcTangentsProcess::SetupProperties(const Importer* pImp) nuclear@0: { nuclear@0: // get the current value of the property nuclear@0: configMaxAngle = pImp->GetPropertyFloat(AI_CONFIG_PP_CT_MAX_SMOOTHING_ANGLE,45.f); nuclear@0: configMaxAngle = std::max(std::min(configMaxAngle,45.0f),0.0f); nuclear@0: configMaxAngle = AI_DEG_TO_RAD(configMaxAngle); nuclear@0: nuclear@0: configSourceUV = pImp->GetPropertyInteger(AI_CONFIG_PP_CT_TEXTURE_CHANNEL_INDEX,0); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Executes the post processing step on the given imported data. nuclear@0: void CalcTangentsProcess::Execute( aiScene* pScene) nuclear@0: { nuclear@0: DefaultLogger::get()->debug("CalcTangentsProcess begin"); nuclear@0: nuclear@0: bool bHas = false; nuclear@0: for( unsigned int a = 0; a < pScene->mNumMeshes; a++) nuclear@0: if(ProcessMesh( pScene->mMeshes[a],a))bHas = true; nuclear@0: nuclear@0: if (bHas)DefaultLogger::get()->info("CalcTangentsProcess finished. Tangents have been calculated"); nuclear@0: else DefaultLogger::get()->debug("CalcTangentsProcess finished"); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------------------------------------ nuclear@0: // Calculates tangents and bitangents for the given mesh nuclear@0: bool CalcTangentsProcess::ProcessMesh( aiMesh* pMesh, unsigned int meshIndex) nuclear@0: { nuclear@0: // we assume that the mesh is still in the verbose vertex format where each face has its own set nuclear@0: // of vertices and no vertices are shared between faces. Sadly I don't know any quick test to nuclear@0: // assert() it here. nuclear@0: //assert( must be verbose, dammit); nuclear@0: nuclear@0: if (pMesh->mTangents) // thisimplies that mBitangents is also there nuclear@0: return false; nuclear@0: nuclear@0: // If the mesh consists of lines and/or points but not of nuclear@0: // triangles or higher-order polygons the normal vectors nuclear@0: // are undefined. nuclear@0: if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON))) nuclear@0: { nuclear@0: DefaultLogger::get()->info("Tangents are undefined for line and point meshes"); nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: // what we can check, though, is if the mesh has normals and texture coordinates. That's a requirement nuclear@0: if( pMesh->mNormals == NULL) nuclear@0: { nuclear@0: DefaultLogger::get()->error("Failed to compute tangents; need normals"); nuclear@0: return false; nuclear@0: } nuclear@0: if( configSourceUV >= AI_MAX_NUMBER_OF_TEXTURECOORDS || !pMesh->mTextureCoords[configSourceUV] ) nuclear@0: { nuclear@0: DefaultLogger::get()->error((Formatter::format("Failed to compute tangents; need UV data in channel"),configSourceUV)); nuclear@0: return false; nuclear@0: } nuclear@0: nuclear@0: const float angleEpsilon = 0.9999f; nuclear@0: nuclear@0: std::vector vertexDone( pMesh->mNumVertices, false); nuclear@0: const float qnan = get_qnan(); nuclear@0: nuclear@0: // create space for the tangents and bitangents nuclear@0: pMesh->mTangents = new aiVector3D[pMesh->mNumVertices]; nuclear@0: pMesh->mBitangents = new aiVector3D[pMesh->mNumVertices]; nuclear@0: nuclear@0: const aiVector3D* meshPos = pMesh->mVertices; nuclear@0: const aiVector3D* meshNorm = pMesh->mNormals; nuclear@0: const aiVector3D* meshTex = pMesh->mTextureCoords[configSourceUV]; nuclear@0: aiVector3D* meshTang = pMesh->mTangents; nuclear@0: aiVector3D* meshBitang = pMesh->mBitangents; nuclear@0: nuclear@0: // calculate the tangent and bitangent for every face nuclear@0: for( unsigned int a = 0; a < pMesh->mNumFaces; a++) nuclear@0: { nuclear@0: const aiFace& face = pMesh->mFaces[a]; nuclear@0: if (face.mNumIndices < 3) nuclear@0: { nuclear@0: // There are less than three indices, thus the tangent vector nuclear@0: // is not defined. We are finished with these vertices now, nuclear@0: // their tangent vectors are set to qnan. nuclear@0: for (unsigned int i = 0; i < face.mNumIndices;++i) nuclear@0: { nuclear@0: register unsigned int idx = face.mIndices[i]; nuclear@0: vertexDone [idx] = true; nuclear@0: meshTang [idx] = aiVector3D(qnan); nuclear@0: meshBitang [idx] = aiVector3D(qnan); nuclear@0: } nuclear@0: nuclear@0: continue; nuclear@0: } nuclear@0: nuclear@0: // triangle or polygon... we always use only the first three indices. A polygon nuclear@0: // is supposed to be planar anyways.... nuclear@0: // FIXME: (thom) create correct calculation for multi-vertex polygons maybe? nuclear@0: const unsigned int p0 = face.mIndices[0], p1 = face.mIndices[1], p2 = face.mIndices[2]; nuclear@0: nuclear@0: // position differences p1->p2 and p1->p3 nuclear@0: aiVector3D v = meshPos[p1] - meshPos[p0], w = meshPos[p2] - meshPos[p0]; nuclear@0: nuclear@0: // texture offset p1->p2 and p1->p3 nuclear@0: float sx = meshTex[p1].x - meshTex[p0].x, sy = meshTex[p1].y - meshTex[p0].y; nuclear@0: float tx = meshTex[p2].x - meshTex[p0].x, ty = meshTex[p2].y - meshTex[p0].y; nuclear@0: float dirCorrection = (tx * sy - ty * sx) < 0.0f ? -1.0f : 1.0f; nuclear@0: nuclear@0: // tangent points in the direction where to positive X axis of the texture coords would point in model space nuclear@0: // bitangents points along the positive Y axis of the texture coords, respectively nuclear@0: aiVector3D tangent, bitangent; nuclear@0: tangent.x = (w.x * sy - v.x * ty) * dirCorrection; nuclear@0: tangent.y = (w.y * sy - v.y * ty) * dirCorrection; nuclear@0: tangent.z = (w.z * sy - v.z * ty) * dirCorrection; nuclear@0: bitangent.x = (w.x * sx - v.x * tx) * dirCorrection; nuclear@0: bitangent.y = (w.y * sx - v.y * tx) * dirCorrection; nuclear@0: bitangent.z = (w.z * sx - v.z * tx) * dirCorrection; nuclear@0: nuclear@0: // store for every vertex of that face nuclear@0: for( unsigned int b = 0; b < face.mNumIndices; b++) nuclear@0: { nuclear@0: unsigned int p = face.mIndices[b]; nuclear@0: nuclear@0: // project tangent and bitangent into the plane formed by the vertex' normal nuclear@0: aiVector3D localTangent = tangent - meshNorm[p] * (tangent * meshNorm[p]); nuclear@0: aiVector3D localBitangent = bitangent - meshNorm[p] * (bitangent * meshNorm[p]); nuclear@0: localTangent.Normalize(); localBitangent.Normalize(); nuclear@0: nuclear@0: // and write it into the mesh. nuclear@0: meshTang[p] = localTangent; nuclear@0: meshBitang[p] = localBitangent; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: nuclear@0: // create a helper to quickly find locally close vertices among the vertex array nuclear@0: // FIX: check whether we can reuse the SpatialSort of a previous step nuclear@0: SpatialSort* vertexFinder = NULL; nuclear@0: SpatialSort _vertexFinder; nuclear@0: float posEpsilon; nuclear@0: if (shared) nuclear@0: { nuclear@0: std::vector >* avf; nuclear@0: shared->GetProperty(AI_SPP_SPATIAL_SORT,avf); nuclear@0: if (avf) nuclear@0: { nuclear@0: std::pair& blubb = avf->operator [] (meshIndex); nuclear@0: vertexFinder = &blubb.first; nuclear@0: posEpsilon = blubb.second;; nuclear@0: } nuclear@0: } nuclear@0: if (!vertexFinder) nuclear@0: { nuclear@0: _vertexFinder.Fill(pMesh->mVertices, pMesh->mNumVertices, sizeof( aiVector3D)); nuclear@0: vertexFinder = &_vertexFinder; nuclear@0: posEpsilon = ComputePositionEpsilon(pMesh); nuclear@0: } nuclear@0: std::vector verticesFound; nuclear@0: nuclear@0: const float fLimit = cosf(configMaxAngle); nuclear@0: std::vector closeVertices; nuclear@0: nuclear@0: // in the second pass we now smooth out all tangents and bitangents at the same local position nuclear@0: // if they are not too far off. nuclear@0: for( unsigned int a = 0; a < pMesh->mNumVertices; a++) nuclear@0: { nuclear@0: if( vertexDone[a]) nuclear@0: continue; nuclear@0: nuclear@0: const aiVector3D& origPos = pMesh->mVertices[a]; nuclear@0: const aiVector3D& origNorm = pMesh->mNormals[a]; nuclear@0: const aiVector3D& origTang = pMesh->mTangents[a]; nuclear@0: const aiVector3D& origBitang = pMesh->mBitangents[a]; nuclear@0: closeVertices.clear(); nuclear@0: nuclear@0: // find all vertices close to that position nuclear@0: vertexFinder->FindPositions( origPos, posEpsilon, verticesFound); nuclear@0: nuclear@0: closeVertices.reserve (verticesFound.size()+5); nuclear@0: closeVertices.push_back( a); nuclear@0: nuclear@0: // look among them for other vertices sharing the same normal and a close-enough tangent/bitangent nuclear@0: for( unsigned int b = 0; b < verticesFound.size(); b++) nuclear@0: { nuclear@0: unsigned int idx = verticesFound[b]; nuclear@0: if( vertexDone[idx]) nuclear@0: continue; nuclear@0: if( meshNorm[idx] * origNorm < angleEpsilon) nuclear@0: continue; nuclear@0: if( meshTang[idx] * origTang < fLimit) nuclear@0: continue; nuclear@0: if( meshBitang[idx] * origBitang < fLimit) nuclear@0: continue; nuclear@0: nuclear@0: // it's similar enough -> add it to the smoothing group nuclear@0: closeVertices.push_back( idx); nuclear@0: vertexDone[idx] = true; nuclear@0: } nuclear@0: nuclear@0: // smooth the tangents and bitangents of all vertices that were found to be close enough nuclear@0: aiVector3D smoothTangent( 0, 0, 0), smoothBitangent( 0, 0, 0); nuclear@0: for( unsigned int b = 0; b < closeVertices.size(); ++b) nuclear@0: { nuclear@0: smoothTangent += meshTang[ closeVertices[b] ]; nuclear@0: smoothBitangent += meshBitang[ closeVertices[b] ]; nuclear@0: } nuclear@0: smoothTangent.Normalize(); nuclear@0: smoothBitangent.Normalize(); nuclear@0: nuclear@0: // and write it back into all affected tangents nuclear@0: for( unsigned int b = 0; b < closeVertices.size(); ++b) nuclear@0: { nuclear@0: meshTang[ closeVertices[b] ] = smoothTangent; nuclear@0: meshBitang[ closeVertices[b] ] = smoothBitangent; nuclear@0: } nuclear@0: } nuclear@0: return true; nuclear@0: }