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