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