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 "K3dVertexBuild.h" 00035 00039 K3dVertexBuild::K3dVertexBuild(K3dGameData *_pGameData): 00040 K3dVertexWork(_pGameData) 00041 { 00042 m_pGameData = _pGameData; 00043 // Set this pointer to the game data 00044 m_pGameData->SetVertexBuild(this); 00045 } 00046 00047 K3dVertexBuild::~K3dVertexBuild(void) 00048 { 00049 DeleteVertices(); 00050 } 00051 00054 K3dVertexObj *K3dVertexBuild::CreateNewVertexObj() 00055 { 00056 // Create new vertex 00057 K3dVertexObj *pVertex = m_pGameData->GetVertexObjSP().New(); 00058 pVertex->SetPosition(m_pGameData->GetVector3Build()->CreateNewVector3()); 00059 pVertex->SetTexCoord(m_pGameData->GetVector2Build()->CreateNewVector2()); 00060 pVertex->SetNormal(m_pGameData->GetVector3Build()->CreateNewVector3()); 00061 pVertex->SetColor(m_pGameData->GetColor4Build()->CreateNewColor4()); 00062 // Set vertex index 00063 pVertex->GetId() = m_pGameData->GetVertexObjSP().GetNum()-1; 00064 return pVertex; 00065 } 00066 00068 void K3dVertexBuild::LoadVertices() 00069 { 00070 cout << "void K3dVertexBuild::LoadVertices()" << endl; 00071 K3dString strFc = "void K3dVertexBuild::LoadVertices() -- "; 00072 K3dString strOut; 00073 00074 // Get lua from global data 00075 K3dLua *pLua = m_pGameData->GetLua(); 00076 00077 // Get number of vertices 00078 pLua->LuaGetGlobal(K_STR_NUM_VERTICES); 00079 if (!pLua->LuaIsNumber(-1)) 00080 { 00081 //strOut = strFc + K_STR_NUM_VERTICES; 00082 //strOut += " is not a valid number"; 00083 //pLua->LuaError(strOut.GetString().c_str()); 00084 cerr << "Warning :: void K3dVertexBuild::LoadVertices() -- Doesn`t load " << K_STR_NUM_VERTICES << " from map file" << endl; 00085 return; 00086 } 00087 00088 int iNumVerts = (int) pLua->LuaToNumber(-1); 00089 for(int i=0; i<iNumVerts; i++) 00090 { 00091 00092 K3dVertexObj *pVertex = CreateNewVertexObj(); 00093 00094 K3dString kString(K_STR_VERTEX); 00095 // Add number behind string 00096 kString.AddNumber(i); 00097 00098 pLua->LuaGetGlobal(kString.GetString().c_str()); 00099 if (!pLua->LuaIsTable(-1)) 00100 { 00101 strOut = strFc + kString; 00102 strOut += " is not a valid vertex table"; 00103 pLua->LuaError(strOut.GetString().c_str()); 00104 } 00105 pVertex->GetName() = pLua->LuaGetFieldString(K_STR_NAME); 00106 // Load vertex positions 00107 pVertex->GetPosition()->GetX() = (float)pLua->LuaGetFieldNumber(K_STR_POS_X); 00108 pVertex->GetPosition()->GetY() = (float)pLua->LuaGetFieldNumber(K_STR_POS_Y); 00109 pVertex->GetPosition()->GetZ() = (float)pLua->LuaGetFieldNumber(K_STR_POS_Z); 00110 00111 // Load Texutre coordinations positions 00112 pVertex->GetTexCoord()->GetX() = (float)pLua->LuaGetFieldNumber(K_STR_COORD_X); 00113 pVertex->GetTexCoord()->GetY() = (float)pLua->LuaGetFieldNumber(K_STR_COORD_Y); 00114 00115 // Load normal vector 00116 pVertex->GetNormal()->GetX() = (float)pLua->LuaGetFieldNumber(K_STR_NORMAL_X); 00117 pVertex->GetNormal()->GetY() = (float)pLua->LuaGetFieldNumber(K_STR_NORMAL_Y); 00118 pVertex->GetNormal()->GetZ() = (float)pLua->LuaGetFieldNumber(K_STR_NORMAL_Z); 00119 00120 // Load RGBA color vector 00121 pVertex->GetColor()->GetR() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_R); 00122 pVertex->GetColor()->GetG() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_G); 00123 pVertex->GetColor()->GetB() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_B); 00124 pVertex->GetColor()->GetA() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_A); 00125 } 00126 } 00127 00129 void K3dVertexBuild::DeleteVertices() 00130 { 00131 std::cout << "void K3dVertexBuild::DeleteVertices()" << endl; 00132 while(m_pGameData->GetVertexObjSP().GetNum()) 00133 { 00134 K3dVertexObj *p = m_pGameData->GetVertexObjSP().Get(0); 00135 p = m_pGameData->GetVertexObjSP().Delete(p); 00136 } 00137 } 00138