Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

CollisionRenderer.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * コリジョンレンダラ実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Collision/Renderer/CollisionRenderer.h"
00027 #include "Graphics/PrimitiveRenderer/PrimitiveRenderer.h"
00028 #include "Collision/System/CollisionScene.h"
00029 #include "Collision/System/CollisionNode.h"
00030 #include "Collision/Leaf/StaticSphereCollision.h"
00031 #include "Collision/Leaf/StaticDeformedMeshCollision.h"
00032 
00033 namespace Lamp{
00034 
00035 //------------------------------------------------------------------------------
00036 // コンストラクタ
00037 CollisionRenderer::CollisionRenderer() : scene_(NULL), camera_(NULL),
00038     isDrawnNode_(true), isDrawnLeaf_(true){
00039     renderer_ = new PrimitiveRenderer;
00040 }
00041 //------------------------------------------------------------------------------
00042 // デストラクタ
00043 CollisionRenderer::~CollisionRenderer(){
00044     SafeDelete(renderer_);
00045 }
00046 //------------------------------------------------------------------------------
00047 // レンダリング準備
00048 //------------------------------------------------------------------------------
00049 // レンダリング準備を行う
00050 void CollisionRenderer::renderingSetup(CollisionScene* scene, Camera* camera){
00051     scene_ = scene;
00052     camera_ = camera;
00053     Assert((scene_ != NULL) && (camera_ != NULL));
00054     // ノードの描画
00055     if(isDrawnNode()){
00056         int nodeCount = scene_->getNodeCount();
00057         for(int i = 0; i < nodeCount; i++){
00058             CollisionNode* node = scene_->getNode(i);
00059             if(!node->isGlobalEnabled()){ continue; }
00060             renderer_->requestAxis(node->getWorldMatrix(),
00061                 Color4c::white, false);
00062         }
00063     }
00064     // リーフの描画
00065     if(isDrawnLeaf()){
00066         int leafCount = scene_->getLeafCount();
00067         for(int i = 0; i < leafCount; i++){
00068             CollisionLeaf* leaf = scene_->getLeaf(i);
00069             if(!leaf->isGlobalEnabled()){ continue; }
00070             if(leaf->isStaticSphereCollision()){
00071                 setupStaticSphereCollision(leaf->castStaticSphereCollision());
00072             }else if(leaf->isStaticDeformedMeshCollision()){
00073                 setupStaticDeformedMeshCollision(
00074                     leaf->castStaticDeformedMeshCollision());
00075             }
00076         }
00077     }
00078 }
00079 //------------------------------------------------------------------------------
00080 // 静的球コリジョンのセットアップ
00081 void CollisionRenderer::setupStaticSphereCollision(
00082     StaticSphereCollision* sphereCollision){
00083     const Sphere& sphere = sphereCollision->getWorldSphere();
00084     float radius = sphere.getRadius();
00085     Vector3 scale(radius, radius, radius);
00086     renderer_->requestSphere(scale, Vector3::zero, sphere.getCenter(),
00087         Color4c(255, 255, 128, 128), true);
00088  }
00089 //------------------------------------------------------------------------------
00090 // 静的変形メッシュコリジョンのセットアップ
00091 void CollisionRenderer::setupStaticDeformedMeshCollision(
00092     StaticDeformedMeshCollision* meshCollision){
00093     const DeformedMeshGeometry& geometry = meshCollision->getWorldMesh();
00094     // バウンディングスフィア
00095     const Sphere& boundingSphere = geometry.getBoundingSphere();
00096     float radius = boundingSphere.getRadius();
00097     renderer_->requestSphere(Vector3(radius, radius, radius),
00098         Vector3::zero, boundingSphere.getCenter(),
00099         Color4c(128, 255, 128, 64), true);
00100 
00101     // バウンディングボックス
00102     const AxisAlignedBox& boundingBox = geometry.getBoundingBox();
00103     renderer_->requestBox(boundingBox.getSize(), Vector3::zero,
00104         boundingBox.getCenter(), Color4c(128, 255, 128, 64), true);
00105 
00106     // メッシュ
00107     int triangleCount = geometry.getTriangleCount();
00108     int vertexCount = triangleCount * 6;
00109     Vector3* positions = new Vector3[vertexCount];
00110     for(int i = 0; i < triangleCount; i++){
00111         const Triangle& triangle = geometry.getTriangle(i);
00112         int offset = i * 6;
00113         positions[offset + 0] = triangle.getVertex(0);
00114         positions[offset + 1] = triangle.getVertex(1);
00115         positions[offset + 2] = triangle.getVertex(1);
00116         positions[offset + 3] = triangle.getVertex(2);
00117         positions[offset + 4] = triangle.getVertex(2);
00118         positions[offset + 5] = triangle.getVertex(0);
00119     }
00120     renderer_->requestLine(vertexCount, positions, Matrix34::unit,
00121         Color4c(255, 255, 128, 64), true);
00122     SafeArrayDelete(positions);
00123 }
00124 //------------------------------------------------------------------------------
00125 // レンダリング
00126 //------------------------------------------------------------------------------
00127 // レンダリングを行う
00128 void CollisionRenderer::rendering(){
00129     Assert((scene_ != NULL) && (camera_ != NULL));
00130     renderer_->render(camera_);
00131 }
00132 //------------------------------------------------------------------------------
00133 } // End of namespace Lamp
00134 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:29 2005 for Lamp by doxygen 1.3.2