K3dTriangleBuild.cpp

Go to the documentation of this file.
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 "K3dTriangleBuild.h"
00035 
00039 K3dTriangleBuild::K3dTriangleBuild(K3dGameData *_pGameData):
00040         K3dTriangleWork(_pGameData)
00041 {
00042         m_pGameData = _pGameData;
00043         // Set this pointer to the game data
00044         m_pGameData->SetTriangleBuild(this);
00045         m_pVector3Build = m_pGameData->GetVector3Build();
00046         m_pTriangleWork = m_pGameData->GetTriangleWork();
00047 }
00048 
00049 K3dTriangleBuild::~K3dTriangleBuild(void)
00050 {
00051         DeleteTriangles();
00052 }
00053 
00055 void K3dTriangleBuild::LoadTriangles()
00056 {
00057         /*
00058         K3dString strFc = "void K3dTriangleBuild::LoadTriangles() -- ";
00059         K3dString strOut;
00060 
00061         // Get Lua from global data
00062         K3dLua *pLua = m_pGameData->GetLua();
00063 
00064         // Get number of triangles
00065         pLua->LuaGetGlobal(K_STR_NUM_TRIANGLES);
00066     if (!pLua->LuaIsNumber(-1))
00067         {
00068         strOut = strFc + K_STR_NUM_TRIANGLES;
00069                 strOut += " is not a valid number";
00070                 pLua->LuaError(strOut.GetString().c_str());
00071     }
00072         
00073         int iNumTriangles = (int) pLua->LuaToNumber(-1);
00074         for(int i=0; i<iNumTriangles; i++)
00075         {
00076                 
00077                 K3dTriangle *pTriangle = new K3dTriangle();
00078                 
00079                 K3dString kString(K_STR_TRIANGLE);
00080                 // Add number behind string
00081                 kString.AddNumber(i);
00082                 
00083                 pLua->LuaGetGlobal(kString.GetString().c_str());
00084                 if (!pLua->LuaIsTable(-1))
00085                 {
00086                         strOut = strFc + kString;
00087                         strOut += " is not a valid triangle table";
00088                         pLua->LuaError(strOut.GetString().c_str());
00089                 }
00090                 // Load blend
00091                 pTriangle->IsBlend() = pLua->LuaGetFieldBool(K_STR_BLEND);
00092                 // Load vertex indexes
00093                 int aiV[3];
00094                 aiV[0] = (int)pLua->LuaGetFieldNumber(K_STR_VERTEX_A);
00095                 aiV[1] = (int)pLua->LuaGetFieldNumber(K_STR_VERTEX_B);
00096                 aiV[2] = (int)pLua->LuaGetFieldNumber(K_STR_VERTEX_C);
00097                 pTriangle->SetVerts(aiV);
00098                 
00099                 // Load color
00100                 pTriangle->Color()[0] = (float)pLua->LuaGetFieldNumber(K_STR_R);
00101                 pTriangle->Color()[1] = (float)pLua->LuaGetFieldNumber(K_STR_G);
00102                 pTriangle->Color()[2] = (float)pLua->LuaGetFieldNumber(K_STR_B);
00103                 
00104                 // Add triangle to global game data
00105                 m_pGameData->AddTriangle(pTriangle);
00106                 // Set triangle index
00107                 m_iTriangleId = m_pGameData->GetNumTriangles() -1;
00108                 // Calculate triangle center and normal vector
00109                 CalcTriangleCentre();
00110                 CalcNormal();
00111         }
00112         */
00113 }
00114 
00116 void K3dTriangleBuild::DeleteTriangles()
00117 {
00118         std::cout << "void K3dTriangleBuild::DeleteTriangles()" << endl;
00119         while(m_pGameData->GetTriangleObjSP().GetNum())
00120         {
00121                 K3dTriangleObj *p = m_pGameData->GetTriangleObjSP().Get(0);
00122                 p = m_pGameData->GetTriangleObjSP().Delete(p);;
00123         }
00124 }
00125 
00126 K3dTriangleObj *K3dTriangleBuild::CreateNewTriangleObj()
00127 {       
00128         K3dTriangleObj *pTriangleObj = m_pGameData->GetTriangleObjSP().New();
00129         for(int i=0; i<3; i++)
00130         {
00131                 pTriangleObj->SetVertex(i, m_pVector3Build->CreateNewVector3());
00132         }
00133         pTriangleObj->SetColor(m_pVector3Build->CreateNewVector3());
00134         pTriangleObj->SetNormal(m_pVector3Build->CreateNewVector3());
00135         pTriangleObj->SetCentre(m_pVector3Build->CreateNewVector3());
00136         pTriangleObj->GetId() = m_pGameData->GetTriangleObjSP().GetNum()-1;
00137         return pTriangleObj;
00138 }
00139 
00140 K3dTriangleObj *K3dTriangleBuild::CreateNewTriangleObj(K3dVector3Obj *_pV0, K3dVector3Obj *_pV1, K3dVector3Obj *_pV2)
00141 {       
00142         K3dTriangleObj *pTriangleObj = m_pGameData->GetTriangleObjSP().New();
00143         pTriangleObj->SetVertex(0, _pV0);
00144         pTriangleObj->SetVertex(1, _pV1);
00145         pTriangleObj->SetVertex(2, _pV2);
00146         pTriangleObj->SetColor(m_pVector3Build->CreateNewVector3());
00147         pTriangleObj->SetNormal(m_pVector3Build->CreateNewVector3());
00148         pTriangleObj->SetCentre(m_pVector3Build->CreateNewVector3());
00149         pTriangleObj->GetId() = m_pGameData->GetTriangleObjSP().GetNum()-1;
00150         // Calculate triangle center and normal vector
00151         m_pTriangleWork->CalcTriangleCentre(pTriangleObj->GetId());
00152         m_pTriangleWork->CalcNormal(pTriangleObj->GetId());
00153         return pTriangleObj;
00154 }
00155 

Generated on Thu Aug 16 23:53:28 2007 for K3dEngine by  doxygen 1.5.0