00001 00012 /*************************************************************************** 00013 * Copyright (C) 2007 by Jan Koci * 00014 * honza.koci@email.cz * 00015 * http://kengine.sourceforge.net/tutorial/ 00016 * * 00017 * This program is free software; you can redistribute it and/or modify * 00018 * it under the terms of the GNU General Public License as published by * 00019 * the Free Software Foundation; either version 2 of the License, or * 00020 * (at your option) any later version. * 00021 * * 00022 * This program is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00025 * GNU General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU General Public License * 00028 * along with this program; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00031 ***************************************************************************/ 00032 00033 #include "K3dLineBuild.h" 00034 00038 K3dLineBuild::K3dLineBuild(K3dGameData *_pGameData) 00039 { 00040 m_pGameData = _pGameData; 00041 // Set this pointer to the game data 00042 m_pGameData->SetLineBuild(this); 00043 m_pVector3Build = m_pGameData->GetVector3Build(); 00044 m_pColor3Build = m_pGameData->GetColor3Build(); 00045 m_pMatrixBuild = m_pGameData->GetMatrixBuild(); 00046 } 00047 00048 K3dLineBuild::~K3dLineBuild(void) 00049 { 00050 DeleteLines(); 00051 } 00052 00055 K3dLineObj *K3dLineBuild::CreateNewLineObj() 00056 { 00057 K3dLineObj *pLineObj = m_pGameData->GetLineObjSP().New(); 00058 pLineObj->SetOrigin(m_pVector3Build->CreateNewVector3()); 00059 pLineObj->SetDirection(m_pVector3Build->CreateNewVector3()); 00060 pLineObj->SetColor(m_pColor3Build->CreateNewColor3()); 00061 pLineObj->SetPosition(m_pVector3Build->CreateNewVector3()); 00062 pLineObj->SetRotation(m_pVector3Build->CreateNewVector3()); 00063 pLineObj->SetRelOrigin(m_pVector3Build->CreateNewVector3()); 00064 pLineObj->SetRelDirection(m_pVector3Build->CreateNewVector3()); 00065 pLineObj->SetMatrix(m_pMatrixBuild->CreateNewMatrix()); 00066 pLineObj->SetRotMatrix(m_pMatrixBuild->CreateNewMatrix()); 00067 pLineObj->GetId() = m_pGameData->GetLineObjSP().GetNum()-1; 00068 return pLineObj; 00069 } 00070 00075 K3dLineObj *K3dLineBuild::CreateNewLineObj(const K3dVector3Obj &_rV0, const K3dVector3Obj &_rV1) 00076 { 00077 K3dLineObj *pLineObj = CreateNewLineObj(); 00078 *pLineObj->GetOrigin() = _rV0; 00079 *pLineObj->GetDirection() = _rV1; 00080 return pLineObj; 00081 } 00082 00084 void K3dLineBuild::LoadLines() 00085 { 00086 cout << "void K3dLineBuild::LoadLines()" << endl; 00087 K3dString strFc = "void K3dLineBuild::LoadLines() -- "; 00088 K3dString strOut; 00089 // Get virtual machine from global data 00090 K3dLua *pLua = m_pGameData->GetLua(); 00091 // Get number of lines 00092 pLua->LuaGetGlobal(K_STR_NUM_LINES); 00093 if (!pLua->LuaIsNumber(-1)) 00094 { 00095 cerr << "Warning :: void K3dLineBuild::LoadLines() -- Doesn`t load " << K_STR_NUM_LINES << " from map file" << endl; 00096 return; 00097 } 00098 int iNumLines = (int) pLua->LuaToNumber(-1); 00099 for(int i=0; i<iNumLines; i++) 00100 { 00101 K3dLineObj *pLine = CreateNewLineObj(); 00102 K3dString kString(K_STR_LINE); 00103 // Add number behind string 00104 kString.AddNumber(i); 00105 pLua->LuaGetGlobal(kString.GetString().c_str()); 00106 if (!pLua->LuaIsTable(-1)) 00107 { 00108 strOut = strFc + kString; 00109 strOut += " is not a valid line table"; 00110 pLua->LuaError(strOut.GetString().c_str()); 00111 return; 00112 } 00113 pLine->GetName() = pLua->LuaGetFieldString(K_STR_NAME); 00114 pLine->GetRelOrigin()->GetX() = (float)pLua->LuaGetFieldNumber(K_STR_ORIG_X); 00115 pLine->GetRelOrigin()->GetY() = (float)pLua->LuaGetFieldNumber(K_STR_ORIG_Y); 00116 pLine->GetRelOrigin()->GetZ() = (float)pLua->LuaGetFieldNumber(K_STR_ORIG_Z); 00117 pLine->GetRelDirection()->GetX() = (float)pLua->LuaGetFieldNumber(K_STR_DIRECT_X); 00118 pLine->GetRelDirection()->GetY() = (float)pLua->LuaGetFieldNumber(K_STR_DIRECT_Y); 00119 pLine->GetRelDirection()->GetZ() = (float)pLua->LuaGetFieldNumber(K_STR_DIRECT_Z); 00120 pLine->GetColor()->GetR() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_R); 00121 pLine->GetColor()->GetG() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_G); 00122 pLine->GetColor()->GetB() = (GLubyte)pLua->LuaGetFieldNumber(K_STR_B); 00123 pLine->GetWidth() = (float)pLua->LuaGetFieldNumber(K_STR_WIDTH); 00124 } 00125 } 00126 00128 void K3dLineBuild::BuildLines() 00129 { 00130 /* 00131 // Get number of block objects 00132 int iNumBlocks = m_pGameData->GetNumBlocks(); 00133 for(int i=0; i<iNumBlocks; i++) 00134 { 00135 // Get block 00136 K3dBlock *pBlock = m_pGameData->GetBlock( i); 00137 // Get number of face ids 00138 int iNumFaceIds = pBlock->GetNumFaceIds(); 00139 for(int j=0; j<iNumFaceIds; j++) 00140 { 00141 // Get face 00142 K3dFace *pFace = m_pGameData->GetFace( j); 00143 // Get num triangles 00144 int iNumTriangleIds = pFace->GetNumTriangleIds(); 00145 00146 for(int k=0; k<iNumTriangleIds; k++) 00147 { 00148 // Get triangle 00149 K3dTriangle *pTriangle = m_pGameData->GetTriangle( k); 00150 // Create new edge and edd to the temporary array 00151 K3dEdge kEdge; 00152 K3dVector3 kVertex[3]; 00153 kVertex[0] = m_pGameData->GetVertex(pTriangle->VertexId(0))->Position(); 00154 kVertex[1] = m_pGameData->GetVertex(pTriangle->VertexId(1))->Position(); 00155 kVertex[2] = m_pGameData->GetVertex(pTriangle->VertexId(2))->Position(); 00156 kEdge.SetVertices(kVertex[0], kVertex[1]); 00157 m_tEdgeArray.push_back(kEdge); 00158 kEdge.SetVertices(kVertex[1], kVertex[2]); 00159 m_tEdgeArray.push_back(kEdge); 00160 } 00161 } 00162 } 00163 */ 00164 } 00165 00166 void K3dLineBuild::DeleteLines() 00167 { 00168 std::cout << "void K3dBlockBuild::DeleteLines()" << endl; 00169 while(m_pGameData->GetLineObjSP().GetNum()) 00170 { 00171 K3dLineObj *pLineObj = m_pGameData->GetLineObjSP().Get(0); 00172 pLineObj = m_pGameData->GetLineObjSP().Delete(pLineObj); 00173 } 00174 } 00175