vrshoot

view libs/assimp/GenFaceNormalsProcess.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +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 generate face
43 * normals for all imported faces.
44 */
46 #include "AssimpPCH.h"
47 #include "GenFaceNormalsProcess.h"
50 using namespace Assimp;
52 // ------------------------------------------------------------------------------------------------
53 // Constructor to be privately used by Importer
54 GenFaceNormalsProcess::GenFaceNormalsProcess()
55 {
56 // nothing to do here
57 }
59 // ------------------------------------------------------------------------------------------------
60 // Destructor, private as well
61 GenFaceNormalsProcess::~GenFaceNormalsProcess()
62 {
63 // nothing to do here
64 }
66 // ------------------------------------------------------------------------------------------------
67 // Returns whether the processing step is present in the given flag field.
68 bool GenFaceNormalsProcess::IsActive( unsigned int pFlags) const
69 {
70 return (pFlags & aiProcess_GenNormals) != 0;
71 }
73 // ------------------------------------------------------------------------------------------------
74 // Executes the post processing step on the given imported data.
75 void GenFaceNormalsProcess::Execute( aiScene* pScene)
76 {
77 DefaultLogger::get()->debug("GenFaceNormalsProcess begin");
79 if (pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
80 throw DeadlyImportError("Post-processing order mismatch: expecting pseudo-indexed (\"verbose\") vertices here");
81 }
83 bool bHas = false;
84 for( unsigned int a = 0; a < pScene->mNumMeshes; a++) {
85 if(this->GenMeshFaceNormals( pScene->mMeshes[a])) {
86 bHas = true;
87 }
88 }
89 if (bHas) {
90 DefaultLogger::get()->info("GenFaceNormalsProcess finished. "
91 "Face normals have been calculated");
92 }
93 else DefaultLogger::get()->debug("GenFaceNormalsProcess finished. "
94 "Normals are already there");
95 }
97 // ------------------------------------------------------------------------------------------------
98 // Executes the post processing step on the given imported data.
99 bool GenFaceNormalsProcess::GenMeshFaceNormals (aiMesh* pMesh)
100 {
101 if (NULL != pMesh->mNormals) {
102 return false;
103 }
105 // If the mesh consists of lines and/or points but not of
106 // triangles or higher-order polygons the normal vectors
107 // are undefined.
108 if (!(pMesh->mPrimitiveTypes & (aiPrimitiveType_TRIANGLE | aiPrimitiveType_POLYGON))) {
109 DefaultLogger::get()->info("Normal vectors are undefined for line and point meshes");
110 return false;
111 }
113 // allocate an array to hold the output normals
114 pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
115 const float qnan = get_qnan();
117 // iterate through all faces and compute per-face normals but store them per-vertex.
118 for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
119 const aiFace& face = pMesh->mFaces[a];
120 if (face.mNumIndices < 3) {
121 // either a point or a line -> no well-defined normal vector
122 for (unsigned int i = 0;i < face.mNumIndices;++i) {
123 pMesh->mNormals[face.mIndices[i]] = aiVector3D(qnan);
124 }
125 continue;
126 }
128 const aiVector3D* pV1 = &pMesh->mVertices[face.mIndices[0]];
129 const aiVector3D* pV2 = &pMesh->mVertices[face.mIndices[1]];
130 const aiVector3D* pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices-1]];
131 const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).Normalize();
133 for (unsigned int i = 0;i < face.mNumIndices;++i) {
134 pMesh->mNormals[face.mIndices[i]] = vNor;
135 }
136 }
137 return true;
138 }