vrshoot

annotate libs/assimp/FixNormalsStep.cpp @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
rev   line source
nuclear@0 1 /*
nuclear@0 2 ---------------------------------------------------------------------------
nuclear@0 3 Open Asset Import Library (assimp)
nuclear@0 4 ---------------------------------------------------------------------------
nuclear@0 5
nuclear@0 6 Copyright (c) 2006-2012, assimp team
nuclear@0 7
nuclear@0 8 All rights reserved.
nuclear@0 9
nuclear@0 10 Redistribution and use of this software in source and binary forms,
nuclear@0 11 with or without modification, are permitted provided that the following
nuclear@0 12 conditions are met:
nuclear@0 13
nuclear@0 14 * Redistributions of source code must retain the above
nuclear@0 15 copyright notice, this list of conditions and the
nuclear@0 16 following disclaimer.
nuclear@0 17
nuclear@0 18 * Redistributions in binary form must reproduce the above
nuclear@0 19 copyright notice, this list of conditions and the
nuclear@0 20 following disclaimer in the documentation and/or other
nuclear@0 21 materials provided with the distribution.
nuclear@0 22
nuclear@0 23 * Neither the name of the assimp team, nor the names of its
nuclear@0 24 contributors may be used to endorse or promote products
nuclear@0 25 derived from this software without specific prior
nuclear@0 26 written permission of the assimp team.
nuclear@0 27
nuclear@0 28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
nuclear@0 29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
nuclear@0 30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
nuclear@0 31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
nuclear@0 32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
nuclear@0 33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
nuclear@0 34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
nuclear@0 35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
nuclear@0 36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
nuclear@0 37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
nuclear@0 38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nuclear@0 39 ---------------------------------------------------------------------------
nuclear@0 40 */
nuclear@0 41
nuclear@0 42 /** @file Implementation of the post processing step to invert
nuclear@0 43 * all normals in meshes with infacing normals.
nuclear@0 44 */
nuclear@0 45
nuclear@0 46 #include "AssimpPCH.h"
nuclear@0 47
nuclear@0 48 // internal headers
nuclear@0 49 #include "FixNormalsStep.h"
nuclear@0 50
nuclear@0 51 using namespace Assimp;
nuclear@0 52
nuclear@0 53
nuclear@0 54 // ------------------------------------------------------------------------------------------------
nuclear@0 55 // Constructor to be privately used by Importer
nuclear@0 56 FixInfacingNormalsProcess::FixInfacingNormalsProcess()
nuclear@0 57 {
nuclear@0 58 // nothing to do here
nuclear@0 59 }
nuclear@0 60
nuclear@0 61 // ------------------------------------------------------------------------------------------------
nuclear@0 62 // Destructor, private as well
nuclear@0 63 FixInfacingNormalsProcess::~FixInfacingNormalsProcess()
nuclear@0 64 {
nuclear@0 65 // nothing to do here
nuclear@0 66 }
nuclear@0 67
nuclear@0 68 // ------------------------------------------------------------------------------------------------
nuclear@0 69 // Returns whether the processing step is present in the given flag field.
nuclear@0 70 bool FixInfacingNormalsProcess::IsActive( unsigned int pFlags) const
nuclear@0 71 {
nuclear@0 72 return (pFlags & aiProcess_FixInfacingNormals) != 0;
nuclear@0 73 }
nuclear@0 74
nuclear@0 75 // ------------------------------------------------------------------------------------------------
nuclear@0 76 // Executes the post processing step on the given imported data.
nuclear@0 77 void FixInfacingNormalsProcess::Execute( aiScene* pScene)
nuclear@0 78 {
nuclear@0 79 DefaultLogger::get()->debug("FixInfacingNormalsProcess begin");
nuclear@0 80
nuclear@0 81 bool bHas = false;
nuclear@0 82 for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
nuclear@0 83 if(ProcessMesh( pScene->mMeshes[a],a))bHas = true;
nuclear@0 84
nuclear@0 85 if (bHas)
nuclear@0 86 DefaultLogger::get()->debug("FixInfacingNormalsProcess finished. Found issues.");
nuclear@0 87 else DefaultLogger::get()->debug("FixInfacingNormalsProcess finished. No changes to the scene.");
nuclear@0 88 }
nuclear@0 89
nuclear@0 90 // ------------------------------------------------------------------------------------------------
nuclear@0 91 // Apply the step to the mesh
nuclear@0 92 bool FixInfacingNormalsProcess::ProcessMesh( aiMesh* pcMesh, unsigned int index)
nuclear@0 93 {
nuclear@0 94 ai_assert(NULL != pcMesh);
nuclear@0 95
nuclear@0 96 // Nothing to do if there are no model normals
nuclear@0 97 if (!pcMesh->HasNormals())return false;
nuclear@0 98
nuclear@0 99 // Compute the bounding box of both the model vertices + normals and
nuclear@0 100 // the umodified model vertices. Then check whether the first BB
nuclear@0 101 // is smaller than the second. In this case we can assume that the
nuclear@0 102 // normals need to be flipped, although there are a few special cases ..
nuclear@0 103 // convex, concave, planar models ...
nuclear@0 104
nuclear@0 105 aiVector3D vMin0 (1e10f,1e10f,1e10f);
nuclear@0 106 aiVector3D vMin1 (1e10f,1e10f,1e10f);
nuclear@0 107 aiVector3D vMax0 (-1e10f,-1e10f,-1e10f);
nuclear@0 108 aiVector3D vMax1 (-1e10f,-1e10f,-1e10f);
nuclear@0 109
nuclear@0 110 for (unsigned int i = 0; i < pcMesh->mNumVertices;++i)
nuclear@0 111 {
nuclear@0 112 vMin1.x = std::min(vMin1.x,pcMesh->mVertices[i].x);
nuclear@0 113 vMin1.y = std::min(vMin1.y,pcMesh->mVertices[i].y);
nuclear@0 114 vMin1.z = std::min(vMin1.z,pcMesh->mVertices[i].z);
nuclear@0 115
nuclear@0 116 vMax1.x = std::max(vMax1.x,pcMesh->mVertices[i].x);
nuclear@0 117 vMax1.y = std::max(vMax1.y,pcMesh->mVertices[i].y);
nuclear@0 118 vMax1.z = std::max(vMax1.z,pcMesh->mVertices[i].z);
nuclear@0 119
nuclear@0 120 const aiVector3D vWithNormal = pcMesh->mVertices[i] + pcMesh->mNormals[i];
nuclear@0 121
nuclear@0 122 vMin0.x = std::min(vMin0.x,vWithNormal.x);
nuclear@0 123 vMin0.y = std::min(vMin0.y,vWithNormal.y);
nuclear@0 124 vMin0.z = std::min(vMin0.z,vWithNormal.z);
nuclear@0 125
nuclear@0 126 vMax0.x = std::max(vMax0.x,vWithNormal.x);
nuclear@0 127 vMax0.y = std::max(vMax0.y,vWithNormal.y);
nuclear@0 128 vMax0.z = std::max(vMax0.z,vWithNormal.z);
nuclear@0 129 }
nuclear@0 130
nuclear@0 131 const float fDelta0_x = (vMax0.x - vMin0.x);
nuclear@0 132 const float fDelta0_y = (vMax0.y - vMin0.y);
nuclear@0 133 const float fDelta0_z = (vMax0.z - vMin0.z);
nuclear@0 134
nuclear@0 135 const float fDelta1_x = (vMax1.x - vMin1.x);
nuclear@0 136 const float fDelta1_y = (vMax1.y - vMin1.y);
nuclear@0 137 const float fDelta1_z = (vMax1.z - vMin1.z);
nuclear@0 138
nuclear@0 139 // Check whether the boxes are overlapping
nuclear@0 140 if ((fDelta0_x > 0.0f) != (fDelta1_x > 0.0f))return false;
nuclear@0 141 if ((fDelta0_y > 0.0f) != (fDelta1_y > 0.0f))return false;
nuclear@0 142 if ((fDelta0_z > 0.0f) != (fDelta1_z > 0.0f))return false;
nuclear@0 143
nuclear@0 144 // Check whether this is a planar surface
nuclear@0 145 const float fDelta1_yz = fDelta1_y * fDelta1_z;
nuclear@0 146
nuclear@0 147 if (fDelta1_x < 0.05f * sqrtf( fDelta1_yz ))return false;
nuclear@0 148 if (fDelta1_y < 0.05f * sqrtf( fDelta1_z * fDelta1_x ))return false;
nuclear@0 149 if (fDelta1_z < 0.05f * sqrtf( fDelta1_y * fDelta1_x ))return false;
nuclear@0 150
nuclear@0 151 // now compare the volumes of the bounding boxes
nuclear@0 152 if (::fabsf(fDelta0_x * fDelta1_yz) <
nuclear@0 153 ::fabsf(fDelta1_x * fDelta1_y * fDelta1_z))
nuclear@0 154 {
nuclear@0 155 if (!DefaultLogger::isNullLogger())
nuclear@0 156 {
nuclear@0 157 char buffer[128]; // should be sufficiently large
nuclear@0 158 ::sprintf(buffer,"Mesh %i: Normals are facing inwards (or the mesh is planar)",index);
nuclear@0 159 DefaultLogger::get()->info(buffer);
nuclear@0 160 }
nuclear@0 161
nuclear@0 162 // Invert normals
nuclear@0 163 for (unsigned int i = 0; i < pcMesh->mNumVertices;++i)
nuclear@0 164 pcMesh->mNormals[i] *= -1.0f;
nuclear@0 165
nuclear@0 166 // ... and flip faces
nuclear@0 167 for (unsigned int i = 0; i < pcMesh->mNumFaces;++i)
nuclear@0 168 {
nuclear@0 169 aiFace& face = pcMesh->mFaces[i];
nuclear@0 170 for( unsigned int b = 0; b < face.mNumIndices / 2; b++)
nuclear@0 171 std::swap( face.mIndices[b], face.mIndices[ face.mNumIndices - 1 - b]);
nuclear@0 172 }
nuclear@0 173 return true;
nuclear@0 174 }
nuclear@0 175 return false;
nuclear@0 176 }