K3dBoxBuild.cpp

Go to the documentation of this file.
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 "K3dBoxBuild.h"
00034 
00035 K3dBoxBuild::K3dBoxBuild(K3dGameData *_pGameData):
00036         K3dBoxWork(_pGameData)
00037 {
00038         m_pGameData = _pGameData;
00039         // Set this pointer to the game data
00040         m_pGameData->SetBoxBuild(this);
00041         m_pIntersection = m_pGameData->GetIntersection();
00042         m_pPolyPlaneBuild = m_pGameData->GetPolyPlaneBuild();
00043         m_pLineBuild = m_pGameData->GetLineBuild();
00044         m_pVertexBuild = m_pGameData->GetVertexBuild();
00045 }
00046 
00047 K3dBoxBuild::~K3dBoxBuild()
00048 {
00049         DeleteBoxes();
00050 }
00051 
00055 K3dLineObj *K3dBoxBuild::CreateEdge(K3dVector3Obj *_pOrigin,K3dVector3Obj *_pDirection)
00056 {
00057         K3dLineObj *pEdge = m_pLineBuild->CreateNewLineObj();   
00058         pEdge->SetOrigin(_pOrigin);
00059         pEdge->SetDirection(_pDirection);
00060         pEdge->GetIsVisible() = m_bIsVisible;
00061         pEdge->Init();
00062         return pEdge;
00063 }
00064 
00066 K3dPolyPlane *K3dBoxBuild::CreatePlane(const K3dVector3Obj *_pV0,const K3dVector3Obj *_pV1,const K3dVector3Obj *_pV2)
00067 {
00068         K3dPolyPlane *pPlane = m_pPolyPlaneBuild->CreateNewPolyPlaneObj(*_pV0,*_pV1,*_pV2);
00069         pPlane->GetWidth() = K3D_PLANE_SIZE;
00070         pPlane->GetHeight() = K3D_PLANE_SIZE;
00071         pPlane->GetIsVisible() = m_bIsVisible;
00072         // Calculate vertices lies on the plane
00073         m_pPolyPlaneBuild->CalcVertsOnPlane(pPlane);
00074         return pPlane;
00075 }
00076 
00079 K3dPolyObj *K3dBoxBuild::CreateSceneBox()
00080 {
00081         m_pPoly = m_pGameData->GetPolyObjSP().New();
00082         // Get max scene axis
00083         float fMaxAxis = (float) K_MAX_SCENE_LENGTH * (float)0.5;
00084         // Create box vertices
00085         K3dVertexObj *pVertex0 = m_pVertexBuild->CreateNewVertexObj();
00086         pVertex0->GetPosition()->Set(fMaxAxis, fMaxAxis, fMaxAxis);
00087         m_pPoly->AddVertex(pVertex0);
00088 
00089         K3dVertexObj *pVertex1 = m_pVertexBuild->CreateNewVertexObj();
00090         pVertex1->GetPosition()->Set(-fMaxAxis, fMaxAxis, fMaxAxis);
00091         m_pPoly->AddVertex(pVertex1);
00092 
00093         K3dVertexObj *pVertex2 = m_pVertexBuild->CreateNewVertexObj();
00094         pVertex2->GetPosition()->Set(-fMaxAxis, -fMaxAxis, fMaxAxis);
00095         m_pPoly->AddVertex(pVertex2);
00096 
00097         K3dVertexObj *pVertex3 = m_pVertexBuild->CreateNewVertexObj();
00098         pVertex3->GetPosition()->Set(fMaxAxis, -fMaxAxis, fMaxAxis);
00099         m_pPoly->AddVertex(pVertex3);
00100 
00101         K3dVertexObj *pVertex4 = m_pVertexBuild->CreateNewVertexObj();
00102         pVertex4->GetPosition()->Set(fMaxAxis, -fMaxAxis, -fMaxAxis);
00103         m_pPoly->AddVertex(pVertex4);
00104 
00105         K3dVertexObj *pVertex5 = m_pVertexBuild->CreateNewVertexObj();
00106         pVertex5->GetPosition()->Set(fMaxAxis, fMaxAxis, -fMaxAxis);
00107         m_pPoly->AddVertex(pVertex5);
00108 
00109         K3dVertexObj *pVertex6 = m_pVertexBuild->CreateNewVertexObj();
00110         pVertex6->GetPosition()->Set(-fMaxAxis, fMaxAxis, -fMaxAxis);
00111         m_pPoly->AddVertex(pVertex6);
00112 
00113         K3dVertexObj *pVertex7 = m_pVertexBuild->CreateNewVertexObj();
00114         pVertex7->GetPosition()->Set(-fMaxAxis, -fMaxAxis, -fMaxAxis);
00115         m_pPoly->AddVertex(pVertex7);
00116 
00117         // Create box edges
00118         K3dLineObj *pEdge;
00119         pEdge = CreateEdge(pVertex0->GetPosition(), pVertex1->GetPosition());
00120         m_pPoly->AddEdge(pEdge);
00121         pEdge = CreateEdge(pVertex1->GetPosition(), pVertex2->GetPosition());
00122         m_pPoly->AddEdge(pEdge);
00123         pEdge = CreateEdge(pVertex2->GetPosition(), pVertex3->GetPosition());
00124         m_pPoly->AddEdge(pEdge);
00125         pEdge = CreateEdge(pVertex3->GetPosition(), pVertex0->GetPosition());
00126         m_pPoly->AddEdge(pEdge);        
00127         pEdge = CreateEdge(pVertex4->GetPosition(), pVertex5->GetPosition());
00128         m_pPoly->AddEdge(pEdge);
00129         pEdge = CreateEdge(pVertex5->GetPosition(), pVertex6->GetPosition());
00130         m_pPoly->AddEdge(pEdge);
00131         pEdge = CreateEdge(pVertex6->GetPosition(), pVertex7->GetPosition());
00132         m_pPoly->AddEdge(pEdge);
00133         pEdge = CreateEdge(pVertex7->GetPosition(), pVertex4->GetPosition());
00134         m_pPoly->AddEdge(pEdge);
00135         pEdge = CreateEdge(pVertex0->GetPosition(), pVertex5->GetPosition());
00136         m_pPoly->AddEdge(pEdge);
00137         pEdge = CreateEdge(pVertex1->GetPosition(), pVertex6->GetPosition());
00138         m_pPoly->AddEdge(pEdge);
00139         pEdge = CreateEdge(pVertex2->GetPosition(), pVertex7->GetPosition());
00140         m_pPoly->AddEdge(pEdge);
00141         pEdge = CreateEdge(pVertex3->GetPosition(), pVertex4->GetPosition());
00142         m_pPoly->AddEdge(pEdge);
00143 
00144         // Create box planes
00145         K3dPolyPlane *pPolyPlane = NULL;
00146         pPolyPlane = CreatePlane(pVertex0->GetPosition(), pVertex1->GetPosition(), pVertex2->GetPosition());
00147         m_pPoly->AddPlane(pPolyPlane);
00148         pPolyPlane = CreatePlane(pVertex5->GetPosition(), pVertex0->GetPosition(), pVertex3->GetPosition());
00149         m_pPoly->AddPlane(pPolyPlane);
00150         pPolyPlane = CreatePlane(pVertex6->GetPosition(), pVertex5->GetPosition(), pVertex4->GetPosition());
00151         m_pPoly->AddPlane(pPolyPlane);
00152         pPolyPlane = CreatePlane(pVertex1->GetPosition(), pVertex6->GetPosition(), pVertex7->GetPosition());
00153         m_pPoly->AddPlane(pPolyPlane);
00154         pPolyPlane = CreatePlane(pVertex5->GetPosition(), pVertex6->GetPosition(), pVertex1->GetPosition());
00155         m_pPoly->AddPlane(pPolyPlane);
00156         pPolyPlane = CreatePlane(pVertex7->GetPosition(), pVertex4->GetPosition(), pVertex3->GetPosition());
00157         m_pPoly->AddPlane(pPolyPlane);
00158 
00159         // Set box variables
00160         m_pPoly->GetIsVisible() = m_bIsVisible;
00161         m_pPoly->GetId() = m_pGameData->GetPolyObjSP().GetNum();
00162         return m_pPoly;
00163 }
00164 
00166 void K3dBoxBuild::DeleteBoxes()
00167 {
00168         // Delete polyhedron array
00169         while(m_pGameData->GetPolyObjSP().GetNum())
00170         {
00171                 K3dPolyObj *p = m_pGameData->GetPolyObjSP().Get(0);
00172                 p = m_pGameData->GetPolyObjSP().Delete(p);
00173         }
00174 }
00175 

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