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 "K3dFaceBuild.h" 00035 00039 K3dFaceBuild::K3dFaceBuild(K3dGameData *_pGameData) 00040 { 00041 m_pGameData = _pGameData; 00042 m_pVertexWork = m_pGameData->GetVertexWork(); 00043 } 00044 00045 K3dFaceBuild::~K3dFaceBuild(void) 00046 { 00047 DeleteFaces(); 00048 } 00049 00052 K3dFaceObj *K3dFaceBuild::CreateNewFaceObj() 00053 { 00054 // Create new face 00055 K3dFaceObj *pFace = new K3dFaceObj(m_pGameData); 00056 // Add face pointer to safe pointer array 00057 m_pGameData->GetFaceObjSP().Add(pFace); 00058 pFace->SetColor(m_pGameData->GetColor4SP().New()); 00059 pFace->SetNormal(m_pGameData->GetVector3SP().New()); 00060 pFace->SetCentre(m_pGameData->GetVector3SP().New()); 00061 pFace->GetId() = m_pGameData->GetFaceObjSP().GetNum()-1; 00062 return pFace; 00063 } 00064 00066 void K3dFaceBuild::LoadFaces() 00067 { 00068 cout << "void K3dFaceBuild::LoadFaces() " << endl; 00069 K3dString strFc = "void K3dFaceBuild::LoadFaces() -- Doesn`t load "; 00070 K3dString strOut; 00071 00072 // Get lua from global data 00073 K3dLua *pLua = m_pGameData->GetLua(); 00074 00075 // Get number of faces 00076 pLua->LuaGetGlobal(K_STR_NUM_FACES); 00077 if (!pLua->LuaIsNumber(-1)) 00078 { 00079 strOut = strFc + K_STR_NUM_FACES; 00080 strOut += " from map file "; 00081 pLua->LuaError(strOut.GetString().c_str()); 00082 //cerr << "Warning :: void K3dFaceBuild::LoadFaces() -- Doesn`t load " << K_STR_NUM_FACES << " from map file" << endl; 00083 return; 00084 } 00085 00086 int iNumFaces = (int) pLua->LuaToNumber(-1); 00087 for(int i=0; i<iNumFaces; i++) 00088 { 00089 K3dFaceObj *pFace = CreateNewFaceObj(); 00090 K3dString kString(K_STR_FACE); 00091 // Add number behind string 00092 kString.AddNumber(i); 00093 00094 pLua->LuaGetGlobal(kString.GetString().c_str()); 00095 if (!pLua->LuaIsTable(-1)) 00096 { 00097 strOut = strFc + kString; 00098 strOut += " is not a valid triangle table"; 00099 pLua->LuaError(strOut.GetString().c_str()); 00100 } 00101 pFace->GetName() = pLua->LuaGetFieldString(K_STR_NAME); 00102 pFace->GetId() = m_pGameData->GetFaceObjSP().GetNum()-1; 00103 int iNumVerts = (int)pLua->LuaGetFieldNumber(K_STR_NUM_VERTICES); 00104 const char* strName; 00105 //TIntArray *pVertexArray = pFace->GetVertexArray(); 00106 for(int iId=0; iId<iNumVerts; iId++) 00107 { 00108 kString.GetString() = K_STR_VERTEX; 00109 // Add number behind string 00110 kString.AddNumber(iId); 00111 strName = pLua->LuaGetFieldString(kString.GetString().c_str()); 00112 K3dVertexObj *pVertex = m_pVertexWork->FindVertex(strName); 00113 if(pVertex != NULL) 00114 { 00115 // Add current vertex index to the array 00116 pFace->GetVertexArray().push_back(pVertex); 00117 } 00118 else 00119 { 00120 cerr << "Error -- K3dFaceBuild::LoadFaces() - Vertex Object " << strName << " doesn`t exist !!!" << endl; 00121 } 00122 } 00123 // Load color 00124 pFace->GetColor()->GetR() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_R); 00125 pFace->GetColor()->GetG() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_G); 00126 pFace->GetColor()->GetB() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_B); 00127 pFace->GetColor()->GetA() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_A); 00128 00129 // Calculate face center and normal vector 00130 pFace->CalcCentre(); 00131 pFace->CalcNormal(); 00132 } 00133 } 00134 00136 void K3dFaceBuild::DeleteFaces() 00137 { 00138 std::cout << "void K3dFaceBuild::DeleteFaces()" << endl; 00139 while(m_pGameData->GetFaceObjSP().GetNum()) 00140 { 00141 K3dFaceObj *p = m_pGameData->GetFaceObjSP().Get(0); 00142 p = m_pGameData->GetFaceObjSP().Delete(p); 00143 } 00144 } 00145