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

# CollisionConverter.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/Converter/CollisionConverter.h"
    00027
    00028 #include "Graphics/Scene/Scene.h"
    00029 #include "Graphics/SceneNode/SceneNode.h"
    00030 #include "Graphics/Model/Model.h"
    00031 #include "Graphics/Mesh/Mesh.h"
    00032 #include "Graphics/Material/Material.h"
    00033
    00034 #include "Collision/System/CollisionScene.h"
    00035 #include "Collision/System/CollisionNode.h"
    00036 #include "Collision/Leaf/StaticDeformedMeshCollision.h"
    00037
    00038 namespace Lamp{
    00039
    00040 //------------------------------------------------------------------------------
    00041 // コンストラクタ
    00042 CollisionConverter::CollisionConverter(){
    00043 }
    00044 //------------------------------------------------------------------------------
    00045 // デストラクタ
    00046 CollisionConverter::~CollisionConverter(){
    00047 }
    00048 //------------------------------------------------------------------------------
    00049 // コンバート
    00050 bool CollisionConverter::convert(Scene* scene, CollisionScene* collisionScene){
    00051     Assert((scene != NULL) && (collisionScene != NULL));
    00052     collisionScene_ = collisionScene;
    00053     SceneNode* sceneNode = scene->getRootNode();
    00054     CollisionNode* collisionNode = collisionScene_->getRootNode();
    00055     // 子ノードループ
    00056     int nodeCount = sceneNode->getSceneNodeCount();
    00057     for(int i = 0; i < nodeCount; i++){
    00058         if(!convertNode(collisionNode, sceneNode->getSceneNode(i))){
    00059             return false;
    00060         }
    00061     }
    00062     // 子リーフループ
    00063     int leafCount = sceneNode->getSceneLeafCount();
    00064     for(int i = 0; i < leafCount; i++){
    00065         if(!convertLeaf(collisionNode, sceneNode->getSceneLeaf(i))){
    00066             return false;
    00067         }
    00068     }
    00069     return true;
    00070 }
    00071 //------------------------------------------------------------------------------
    00072 // ノードのコンバート
    00073 bool CollisionConverter::convertNode(
    00074     CollisionNode* parentCollisionNode, SceneNode* sceneNode){
    00075     // ノードのコンバート
    00076     CollisionNode* collisionNode =
    00077         collisionScene_->createCollisionNode(sceneNode->getName());
    00078     parentCollisionNode->addChild(collisionNode);
    00079     collisionNode->setEnabled(sceneNode->isEnabled());
    00080     collisionNode->setScale(sceneNode->getScale());
    00081     collisionNode->setRotationXYZ(sceneNode->getRotationXYZ());
    00082     collisionNode->setTranslation(sceneNode->getTranslation());
    00083
    00084     // 子ノードループ
    00085     int nodeCount = sceneNode->getSceneNodeCount();
    00086     for(int i = 0; i < nodeCount; i++){
    00087         if(!convertNode(collisionNode, sceneNode->getSceneNode(i))){
    00088             return false;
    00089         }
    00090     }
    00091     // 子リーフループ
    00092     int leafCount = sceneNode->getSceneLeafCount();
    00093     for(int i = 0; i < leafCount; i++){
    00094         if(!convertLeaf(collisionNode, sceneNode->getSceneLeaf(i))){
    00095             return false;
    00096         }
    00097     }
    00098     return true;
    00099 }
    00100 //------------------------------------------------------------------------------
    00101 // リーフのコンバート
    00102 bool CollisionConverter::convertLeaf(
    00103     CollisionNode* parentCollisionNode, SceneLeaf* sceneLeaf){
    00104     // モデルでなかったら無視する
    00105     if(!sceneLeaf->isModel()){ return true; }
    00106     Model* model = sceneLeaf->castModel();
    00107     int meshCount = model->getMeshCount();
    00108     for(int i = 0; i < meshCount; i++){
    00109         Mesh* mesh = model->getMesh(i);
    00110         // コンバート対象の振り分けを行う
    00111         convertStaticDeformedMesh(parentCollisionNode, mesh);
    00112     }
    00113     return true;
    00114 }
    00115 //------------------------------------------------------------------------------
    00116 // 静的変形メッシュのコンバート
    00117 bool CollisionConverter::convertStaticDeformedMesh(
    00118     CollisionNode* parentCollisionNode, Mesh* mesh){
    00119     // メッシュのコンバート
    00120     StaticDeformedMeshCollision* collisionMesh =
    00121         collisionScene_->createStaticDeformedMeshCollision(mesh->getName());
    00122     if(!setCollisionLeafData(parentCollisionNode, mesh, collisionMesh)){
    00123         return false;
    00124     }
    00125
    00126     // 静的変形メッシュのコンバート
    00127     Mesh::PrimitiveType type = mesh->getPrimitiveType();
    00128     if((type != Mesh::triangleList) && (type != Mesh::indexedTriangleList)){
    00129         ErrorOut("CollisionConverter::convertStaticDeformedMesh() "
    00130             "サポートされていないプリミティブタイプです %s",
    00131             mesh->getName().getBytes());
    00132         return false;
    00133     }
    00134
    00135     // バウンディング
    00136     collisionMesh->setBoundingBox(mesh->getBoundingBox());
    00137     collisionMesh->setBoundingSphere(mesh->getBoundingSphere());
    00138
    00139     // 三角の取得
    00140     int triangleCount = mesh->getPrimitiveCount();
    00141     collisionMesh->setTriangleCount(triangleCount);
    00142     for(int i = 0; i < triangleCount; i++){
    00143         collisionMesh->setTriangle(i, mesh->getTriangle(i));
    00144     }
    00145     return true;
    00146 }
    00147 //------------------------------------------------------------------------------
    00148 // コリジョンリーフデータ設定
    00149 bool CollisionConverter::setCollisionLeafData(
    00150     CollisionNode* parentCollisionNode, Mesh* mesh,
    00151     CollisionLeaf* collisionLeaf){
    00152     parentCollisionNode->addChild(collisionLeaf);
    00153     // 有効、無効はメッシュから取得
    00154     collisionLeaf->setEnabled(mesh->isEnabled());
    00155     // コリジョンマスクはライトマスクから取得
    00156     Material* material = mesh->getMaterial();
    00157     if(material == NULL){
    00158         ErrorOut("CollisionConverter::setCollisionLeafData() "
    00159             "マテリアルが設定されていません %s", mesh->getName().getBytes());
    00160         return false;
    00161     }
    00162     collisionLeaf->setCollisionMask(material->getLightMask());
    00163     return true;
    00164 }
    00165 //------------------------------------------------------------------------------
    00166 } // End of namespace Lamp
    00167 //------------------------------------------------------------------------------