00001 00013 /*************************************************************************** 00014 * Copyright (C) 2007 by Jan Koci * 00015 * honza.koci@email.cz * 00016 * http://kengine.sourceforge.net/tutorial/ 00017 * * 00018 * This program is free software; you can redistribute it and/or modify * 00019 * it under the terms of the GNU General Public License as published by * 00020 * the Free Software Foundation; either version 2 of the License, or * 00021 * (at your option) any later version. * 00022 * * 00023 * This program is distributed in the hope that it will be useful, * 00024 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00025 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00026 * GNU General Public License for more details. * 00027 * * 00028 * You should have received a copy of the GNU General Public License * 00029 * along with this program; if not, write to the * 00030 * Free Software Foundation, Inc., * 00031 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00032 ***************************************************************************/ 00033 00034 #include "K3dFaceWork.h" 00035 00036 K3dFaceWork::K3dFaceWork(K3dGameData *_pGameData) 00037 { 00038 m_pGameData = _pGameData; 00039 } 00040 00041 K3dFaceWork::~K3dFaceWork() 00042 { 00043 } 00044 00046 void K3dFaceWork::CalcCentre() 00047 { 00048 int iNumVerts = (int) GetVertexArray().size(); 00049 for(int i=0; i<iNumVerts; i++) 00050 { 00051 K3dVertex *pVertex = GetVertexArray()[i]; 00052 // Add x scalar of cetre position 00053 GetCentre()->GetX() += pVertex->GetPosition()->GetX(); 00054 // Add y scalar of cetre position 00055 GetCentre()->GetY() += pVertex->GetPosition()->GetY(); 00056 // Add z scalar of cetre position 00057 GetCentre()->GetZ() += pVertex->GetPosition()->GetZ(); 00058 } 00059 00060 // Calculate result cetre of face 00061 if(iNumVerts) 00062 { 00063 GetCentre()->GetX() = GetCentre()->GetX() / iNumVerts; 00064 GetCentre()->GetY() = GetCentre()->GetY() / iNumVerts; 00065 GetCentre()->GetZ() = GetCentre()->GetZ() / iNumVerts; 00066 } 00067 } 00068 00070 void K3dFaceWork::CalcNormal() 00071 { 00072 int iNumVerts = (int) GetVertexArray().size(); 00073 // Get only first 3 vertices 00074 K3dVector3Obj akVertex[3]; 00075 if(iNumVerts >= 3) 00076 { 00077 for(int i=0; i<3; i++) 00078 { 00079 K3dVertex *pVertex = GetVertexArray()[i]; 00080 // Add x scalar of cetre position 00081 akVertex[i] = *pVertex->GetPosition(); 00082 } 00083 } 00084 else 00085 { 00086 cout << "K3dVector3 &K3dFaceBuild::CalcNormal() -- Number of vertices is: " << iNumVerts << " The minimum requierement are 3 vertices" << endl; 00087 } 00088 00089 // Calculate normal vector 00090 *GetNormal() = m_pGameData->GetVector3Work()->Normal( akVertex[0], akVertex[1], akVertex[2]); 00091 } 00092