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

TextCollisionSaver.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/InputOutput/TextCollisionSaver.h"
00027 #include "Core/InputOutput/TextFileWriter.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 TextCollisionSaver::TextCollisionSaver(){
00038 }
00039 //------------------------------------------------------------------------------
00040 // デストラクタ
00041 TextCollisionSaver::~TextCollisionSaver(){
00042 }
00043 //------------------------------------------------------------------------------
00044 // セーブ
00045 void TextCollisionSaver::save(const String& filePath, CollisionScene* scene){
00046     TextFileWriter* textFileWriter = new TextFileWriter(filePath);
00047     save(textFileWriter, scene);
00048     delete textFileWriter;
00049 }
00050 //------------------------------------------------------------------------------
00051 // セーブ
00052 void TextCollisionSaver::save(TextWriter* textWriter, CollisionScene* scene){
00053     writer_ = textWriter;
00054     scene_ = scene;
00055 
00056     // ヘッダの書き出し
00057     writeHeader();
00058 
00059     // ノードリストの書き出し
00060     int nodeCount = scene_->getNodeCount();
00061     // ルートノードは書き出さない
00062     if(nodeCount > 1){
00063         writeBlockComment("CollisionNode");
00064         writer_->writeText("CollisionNode {\n");
00065         for(int i = 1; i < nodeCount; i++){
00066             CollisionNode* node = scene_->getNode(i);
00067             writeCollisionNode(node);
00068         }
00069         writer_->writeText("}\n\n");
00070     }
00071 
00072     // リーフリストの書き出し
00073     int leafCount = scene_->getLeafCount();
00074     if(leafCount > 0){
00075         writeBlockComment("CollisionLeaf");
00076         writer_->writeText("CollisionLeaf {\n");
00077         for(int i = 0; i < leafCount; i++){
00078             CollisionLeaf* leaf = scene_->getLeaf(i);
00079             if(leaf->isStaticSphereCollision()){
00080                 writeStaticSphereCollision(leaf->castStaticSphereCollision());
00081             }else if(leaf->isStaticDeformedMeshCollision()){
00082                 writeStaticDeformedMeshCollision(
00083                     leaf->castStaticDeformedMeshCollision());
00084             }else{
00085                 ErrorOut("TextCollisionSaver::save() "
00086                     "対応していないコリジョンリーフです %s",
00087                     leaf->getName().getBytes());
00088             }
00089         }
00090         writer_->writeText("}\n\n");
00091     }
00092 
00093     // リンクの書き出し
00094     if((nodeCount + leafCount) > 1){
00095         writeBlockComment("CollisionNodeLink");
00096         writer_->writeText("CollisionNodeLink {\n");
00097         for(int i = 0; i < nodeCount; i++){
00098             CollisionNode* node = scene_->getNode(i);
00099             writeCollisionNodeLink(node);
00100         }
00101         writer_->writeText("}\n\n");
00102     }
00103     writeLineComment();
00104 }
00105 //------------------------------------------------------------------------------
00106 // ヘッダの書き出し
00107 void TextCollisionSaver::writeHeader(){
00108     writeBlockComment("Header");
00109     writer_->writeText("Header {\n");
00110     writer_->writeText("\ttype LampTextCollisionFormat\n");
00111     writer_->writeText("\tversion 0_1_0\n");
00112     writer_->writeText("}\n\n");
00113 }
00114 //------------------------------------------------------------------------------
00115 // コリジョンノードの書き出し
00116 void TextCollisionSaver::writeCollisionNode(CollisionNode* node){
00117     // 名前
00118     writer_->writeFormat("\t%s {\n", node->getName().getBytes());
00119 
00120     // スケール
00121     const Vector3& scale = node->getScale();
00122     writer_->writeFormat("\t\tscale { %.8f %.8f %.8f }\n",
00123         scale.x, scale.y, scale.z);
00124 
00125     // 回転
00126     const Vector3& rotation = node->getRotationXYZ();
00127     writer_->writeFormat("\t\trotation { %.8f %.8f %.8f }\n",
00128         rotation.x, rotation.y, rotation.z);
00129 
00130     // 移動
00131     const Vector3& translation = node->getTranslation();
00132     writer_->writeFormat("\t\ttranslation { %.8f %.8f %.8f }\n",
00133         translation.x, translation.y, translation.z);
00134 
00135     // 有効、無効
00136     writer_->writeText("\t\tenabled ");
00137     writeBool(node->isEnabled());
00138     writer_->writeText("\n\t}\n\n");
00139 }
00140 //------------------------------------------------------------------------------
00141 // リーフ
00142 //------------------------------------------------------------------------------
00143 // コリジョンリーフの書き出し
00144 void TextCollisionSaver::writeCollisionLeaf(
00145     CollisionLeaf* leaf, const String& type){
00146     // 名前
00147     writer_->writeFormat("\t%s {\n", leaf->getName().getBytes());
00148 
00149     // タイプ
00150     writer_->writeText("\t\ttype ");
00151     writer_->writeText(type);
00152 
00153     // コリジョンマスク
00154     writer_->writeFormat("\n\t\tcollisionMask %u\n", leaf->getCollisionMask());
00155 
00156     // 有効、無効
00157     writer_->writeText("\t\tenabled ");
00158     writeBool(leaf->isEnabled());
00159     writer_->writeText("\n");
00160 }
00161 //------------------------------------------------------------------------------
00162 // 静的球コリジョンの書き出し
00163 void TextCollisionSaver::writeStaticSphereCollision(
00164     StaticSphereCollision* sphere){
00165     // コリジョンリーフの書き出し
00166     writeCollisionLeaf(sphere, "StaticSphere");
00167     writer_->writeFormat("\t\tsphere { %.8f %.8f %.8f %.8f }\n",
00168         sphere->getCenter().x, sphere->getCenter().y, sphere->getCenter().z,
00169         sphere->getRadius());
00170     writer_->writeText("\t}\n\n");
00171 }
00172 //------------------------------------------------------------------------------
00173 // 静的変形メッシュコリジョンの書き出し
00174 void TextCollisionSaver::writeStaticDeformedMeshCollision(
00175     StaticDeformedMeshCollision* mesh){
00176     // コリジョンリーフの書き出し
00177     writeCollisionLeaf(mesh, "StaticDeformedMesh");
00178 
00179     // バウンディングボックス
00180     const AxisAlignedBox& box = mesh->getBoundingBox();
00181     writer_->writeFormat(
00182         "\t\tboundingBox { %.8f %.8f %.8f } { %.8f %.8f %.8f }\n",
00183         box.getMinimum().x, box.getMinimum().y, box.getMinimum().z,
00184         box.getMaximum().x, box.getMaximum().y, box.getMaximum().z);
00185 
00186     // バウンディングスフィア
00187     const Sphere& sphere = mesh->getBoundingSphere();
00188     writer_->writeFormat(
00189         "\t\tboundingSphere { %.8f %.8f %.8f %.8f }\n",
00190         sphere.getCenter().x, sphere.getCenter().y, sphere.getCenter().z,
00191         sphere.getRadius());
00192 
00193     // トライアングルリスト
00194     int triangleCount = mesh->getTriangleCount();
00195     writer_->writeFormat("\t\ttriangleCount %d {\n", triangleCount);
00196     for(int i = 0; i < triangleCount; i++){
00197         const Triangle& triangle = mesh->getTriangle(i);
00198         const Vector3& vertex0 = triangle.getVertex(0);
00199         const Vector3& vertex1 = triangle.getVertex(1);
00200         const Vector3& vertex2 = triangle.getVertex(2);
00201         writer_->writeFormat(
00202             "\t\t\t{ %.8f %.8f %.8f } { %.8f %.8f %.8f } { %.8f %.8f %.8f }\n",
00203             vertex0.x, vertex0.y, vertex0.z, vertex1.x, vertex1.y, vertex1.z,
00204             vertex2.x, vertex2.y, vertex2.z);
00205     }
00206     writer_->writeText("\t\t}\n");
00207     writer_->writeText("\t}\n\n");
00208 }
00209 //------------------------------------------------------------------------------
00210 // リンク
00211 //------------------------------------------------------------------------------
00212 // コリジョンノードリンクの書き出し
00213 void TextCollisionSaver::writeCollisionNodeLink(CollisionNode* node){
00214     int childCount = node->getChildCount();
00215     if(childCount == 0){ return; }
00216     writer_->writeFormat("\t%s {\n", node->getName().getBytes());
00217     for(int i = 0; i < childCount; i++){
00218         CollisionObject* object = node->getChild(i);
00219         if(object->isCollisionNode()){
00220             writer_->writeFormat("\t\tnode %s\n", object->getName().getBytes());
00221         }else{
00222             writer_->writeFormat("\t\tleaf %s\n", object->getName().getBytes());
00223         }
00224     }
00225     writer_->writeText("\t}\n\n");
00226 }
00227 //------------------------------------------------------------------------------
00228 // ユーティリティ
00229 //------------------------------------------------------------------------------
00230 // boolの書き出し
00231 void TextCollisionSaver::writeBool(bool flag){
00232     if(flag){
00233         writer_->writeText("true");
00234     }else{
00235         writer_->writeText("false");
00236     }
00237 }
00238 //------------------------------------------------------------------------------
00239 // 線コメントの書き出し
00240 void TextCollisionSaver::writeLineComment(){
00241     writer_->writeText("//----------------------------"
00242         "--------------------------------------------------\n");
00243 }
00244 //------------------------------------------------------------------------------
00245 // ブロックコメントの書き出し
00246 void TextCollisionSaver::writeBlockComment(const String& blockName){
00247     writeLineComment();
00248     writer_->writeText("// ");
00249     writer_->writeText(blockName);
00250     writer_->writeText(" Block\n");
00251     writeLineComment();
00252 }
00253 //------------------------------------------------------------------------------
00254 } // End of namespace Lamp
00255 //------------------------------------------------------------------------------

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