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

# TextSceneSaver.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 <direct.h>
    00027 #include "Graphics/InputOutput/TextSceneSaver.h"
    00028 #include "Core/InputOutput/TextFileWriter.h"
    00029 #include "Core/InputOutput/FilePath.h"
    00030 #include "Core/Codec/Tga/TargaSaver.h"
    00031
    00032 #include "Graphics/Scene/Scene.h"
    00033 #include "Graphics/Fog/Fog.h"
    00034 #include "Graphics/Light/LightManager.h"
    00035 #include "Graphics/SceneNode/SceneNodeManager.h"
    00036 #include "Graphics/Model/ModelManager.h"
    00037 #include "Graphics/Mesh/MeshManager.h"
    00038 #include "Graphics/MeshData/MeshDataManager.h"
    00039 #include "Graphics/Material/MaterialManager.h"
    00040 #include "Graphics/Texture/TextureManager.h"
    00041 #include "Graphics/Picture/PictureManager.h"
    00042
    00043 namespace Lamp{
    00044
    00045 //------------------------------------------------------------------------------
    00046 // コンストラクタ
    00047 TextSceneSaver::TextSceneSaver(){
    00048 }
    00049 //------------------------------------------------------------------------------
    00050 // デストラクタ
    00051 TextSceneSaver::~TextSceneSaver(){
    00052 }
    00053 //------------------------------------------------------------------------------
    00054 // セーブ
    00055 void TextSceneSaver::save(const String& filePath, Scene* scene){
    00056     FilePath path(filePath);
    00057     TextFileWriter* textFileWriter = new TextFileWriter(filePath);
    00058     save(textFileWriter, scene, path.getFolderPath());
    00059     delete textFileWriter;
    00060 }
    00061 //------------------------------------------------------------------------------
    00062 // セーブ
    00063 void TextSceneSaver::save(TextWriter* textWriter, Scene* scene,
    00064     const String& basePath){
    00065     writer_ = textWriter;
    00066     basePath_ = basePath;
    00067     scene_ = scene;
    00068     sceneNodeManager_ = scene->getSceneNodeManager();
    00069     lightManager_ = scene->getLightManager();
    00070     modelManager_ = scene->getModelManager();
    00071     meshManager_ = scene->getMeshManager();
    00072     meshDataManager_ = scene->getMeshDataManager();
    00073     materialManager_ = scene->getMaterialManager();
    00074     textureManager_ = scene->getTextureManager();
    00075     pictureManager_ = scene->getPictureManager();
    00076
    00077     // ヘッダの書き出し
    00078     writeHeader();
    00079
    00080     // オブジェクトリストの書き出し
    00081     int objectCount = writeObjectList();
    00082
    00083     // リンクの書き出し
    00084     writeLink(objectCount);
    00085 }
    00086 //------------------------------------------------------------------------------
    00087 // ヘッダの書き出し
    00088 void TextSceneSaver::writeHeader(){
    00089     writeBlockComment("Header");
    00090     writer_->writeText("Header {\n");
    00091     writer_->writeText("\ttype LampTextSceneFormat\n");
    00092     writer_->writeText("\tversion 0_9_0\n");
    00093     writer_->writeText("}\n\n");
    00094 }
    00095 //------------------------------------------------------------------------------
    00096 // オブジェクトリストの書き出し
    00097 int TextSceneSaver::writeObjectList(){
    00098     int objectCount = 0;
    00099
    00100     // フォグの書き出し
    00101     writeFog(scene_->getFog());
    00102
    00103     // シーンノードリストの書き出し
    00104     int sceneNodeCount = sceneNodeManager_->getCount();
    00105     if(sceneNodeCount > 1){// ルートノードは出力しないので2以上
    00106         writeBlockComment("SceneNode");
    00107         writer_->writeText("SceneNode {\n");
    00108         writeSceneNodeList();
    00109         writer_->writeText("}\n\n");
    00110     }
    00111     objectCount += sceneNodeCount;
    00112
    00113     // ライトリストの書き出し
    00114     int lightCount = lightManager_->getCount();
    00115     if(lightCount > 0){
    00116         writeBlockComment("Light");
    00117         writer_->writeText("Light {\n");
    00118         writeLightList();
    00119         writer_->writeText("}\n\n");
    00120     }
    00121     // 現在オブジェクトカウントはリンクのために使用されているので
    00122     // ライトはオブジェクト数にカウントしない
    00123
    00124     // モデルリストの書き出し
    00125     int modelCount = modelManager_->getCount();
    00126     if(modelCount > 0){
    00127         writeBlockComment("Model");
    00128         writer_->writeText("Model {\n");
    00129         writeModelList();
    00130         writer_->writeText("}\n\n");
    00131     }
    00132     objectCount += modelCount;
    00133
    00134     // メッシュリストの書き出し
    00135     int meshCount = meshManager_->getCount();
    00136     if(meshCount > 0){
    00137         writeBlockComment("Mesh");
    00138         writer_->writeText("Mesh {\n");
    00139         writeMeshList();
    00140         writer_->writeText("}\n\n");
    00141     }
    00142     objectCount += meshCount;
    00143
    00144     // メッシュデータリストの書き出し
    00145     int meshDataCount = meshDataManager_->getCount();
    00146     if(meshDataCount > 0){
    00147         writeBlockComment("MeshData");
    00148         writer_->writeText("MeshData {\n");
    00149         writeMeshDataList();
    00150         writer_->writeText("}\n\n");
    00151     }
    00152     objectCount += meshDataCount;
    00153
    00154     // マテリアルリストの書き出し
    00155     int materialCount = materialManager_->getCount();
    00156     if(materialCount > 0){
    00157         writeBlockComment("Material");
    00158         writer_->writeText("Material {\n");
    00159         writeMaterialList();
    00160         writer_->writeText("}\n\n");
    00161     }
    00162     objectCount += materialCount;
    00163
    00164     // テクスチャリストの書き出し
    00165     int textureCount = textureManager_->getCount();
    00166     if(textureCount > 0){
    00167         writeBlockComment("Texture");
    00168         writer_->writeText("Texture {\n");
    00169         writeTextureList();
    00170         writer_->writeText("}\n\n");
    00171     }
    00172     objectCount += textureCount;
    00173
    00174     // ピクチャリストの書き出し
    00175     int pictureCount = pictureManager_->getCount();
    00176     if(pictureCount > 0){
    00177         writeBlockComment("Picture");
    00178         writer_->writeText("Picture {\n");
    00179         for(int i = 0; i < pictureCount; i++){
    00180             writePicture(pictureManager_->get(i));
    00181         }
    00182         writer_->writeText("}\n\n");
    00183     }
    00184     objectCount += pictureCount;
    00185
    00186     return objectCount;
    00187 }
    00188 //------------------------------------------------------------------------------
    00189 // リンクの書き出し
    00190 void TextSceneSaver::writeLink(int objectCount){
    00191     // オブジェクトが2個以上ならリンクが存在する
    00192     if(objectCount > 1){ writeBlockComment("Link"); }
    00193
    00194     // シーンノードリンクの書き出し
    00195     int sceneNodeCount = sceneNodeManager_->getCount();
    00196     if(sceneNodeCount > 1){
    00197         writer_->writeText("SceneNodeLink {\n");
    00198         for(int i = 0; i < sceneNodeCount; i++){
    00199             writeSceneNodeLink(sceneNodeManager_->get(i));
    00200         }
    00201         writer_->writeText("}\n\n");
    00202         writeLineComment();
    00203     }
    00204
    00205     // モデルリンクの書き出し
    00206     int modelCount = modelManager_->getCount();
    00207     if(modelCount > 0){
    00208         writer_->writeText("ModelLink {\n");
    00209         for(int i = 0; i < modelCount; i++){
    00210             writeModelLink(modelManager_->get(i));
    00211         }
    00212         writer_->writeText("}\n\n");
    00213         writeLineComment();
    00214     }
    00215
    00216     // メッシュリンクの書き出し
    00217     int meshCount = meshManager_->getCount();
    00218     if(meshCount > 0){
    00219         writer_->writeText("MeshLink {\n");
    00220         for(int i = 0; i < meshCount; i++){
    00221             writeMeshLink(meshManager_->get(i));
    00222         }
    00223         writer_->writeText("}\n\n");
    00224         writeLineComment();
    00225     }
    00226
    00227     // マテリアルテクスチャリンクの書き出し
    00228     // テクスチャが存在すればマテリアルからのリンクがある
    00229     int textureCount = textureManager_->getCount();
    00230     if(textureCount > 0){
    00231         writeMaterialLinkList();
    00232         writeLineComment();
    00233     }
    00234
    00235     // テクスチャリンクの書き出し
    00236     if(textureCount > 0){
    00237         writer_->writeText("TextureLink {\n");
    00238         for(int i = 0; i < textureCount; i++){
    00239             writeTextureLink(textureManager_->get(i));
    00240         }
    00241         writer_->writeText("}\n\n");
    00242         writeLineComment();
    00243     }
    00244 }
    00245 //------------------------------------------------------------------------------
    00246 // フォグ
    00247 //------------------------------------------------------------------------------
    00248 // フォグの書き出し
    00249 void TextSceneSaver::writeFog(Fog* fog){
    00250     writeBlockComment("Fog");
    00251     writer_->writeText("Fog {\n");
    00252     // カラー
    00253     Color4c color = fog->getColor();
    00254     writer_->writeFormat("\tcolor { %d %d %d %d }\n",
    00255         color.r, color.g, color.b, color.a);
    00256     // モード
    00257     writer_->writeFormat("\tmode %s\n", fog->getModeString());
    00258     // 濃度
    00259     writer_->writeFormat("\tdensity %.8f\n", fog->getDensity());
    00260     // ニア
    00261     writer_->writeFormat("\tnear %.8f\n", fog->getNear());
    00262     // ファー
    00263     writer_->writeFormat("\tfar %.8f\n", fog->getFar());
    00264     // 有効、無効
    00265     writer_->writeText("\tenabled ");
    00266     writeBool(fog->isEnabled());
    00267     writer_->writeText("\n}\n\n");
    00268 }
    00269 //------------------------------------------------------------------------------
    00270 // シーンノード
    00271 //------------------------------------------------------------------------------
    00272 // シーンノードリストの書き出し
    00273 void TextSceneSaver::writeSceneNodeList(){
    00274     int sceneNodeCount = sceneNodeManager_->getCount();
    00275     // ルートノードは出力しない
    00276     for(int i = 1; i < sceneNodeCount; i++){
    00277         SceneNode* sceneNode = sceneNodeManager_->get(i);
    00278         if(sceneNode->isLODSceneNode()){
    00279             writeLODSceneNode(sceneNode->castLODSceneNode());
    00280         }else{
    00281             writeSceneNode(sceneNode);
    00282         }
    00283     }
    00284 }
    00285 //------------------------------------------------------------------------------
    00286 // シーンノードの書き出し
    00287 void TextSceneSaver::writeSceneNode(SceneNode* sceneNode){
    00288     writer_->writeText("\t");
    00289     writer_->writeText(sceneNode->getName());
    00290     writer_->writeText(" {\n");
    00291     writer_->writeText("\t\ttype Standard\n");
    00292     // スケール
    00293     Vector3 scale = sceneNode->getScale();
    00294     writer_->writeFormat("\t\tscale { %.8f %.8f %.8f }\n",
    00295         scale.x, scale.y, scale.z);
    00296     // 回転
    00297     Vector3 rotation = sceneNode->getRotationXYZ();
    00298     writer_->writeFormat("\t\trotation { %.8f %.8f %.8f }\n",
    00299         rotation.x, rotation.y, rotation.z);
    00300     // 移動
    00301     Vector3 translation = sceneNode->getTranslation();
    00302     writer_->writeFormat("\t\ttranslation { %.8f %.8f %.8f }\n",
    00303         translation.x, translation.y, translation.z);
    00304     // 有効、無効
    00305     writer_->writeText("\t\tenabled ");
    00306     writeBool(sceneNode->isEnabled());
    00307     writer_->writeText("\n\t}\n\n");
    00308 }
    00309 //------------------------------------------------------------------------------
    00310 // レベルオブディティールシーンノードの書き出し
    00311 void TextSceneSaver::writeLODSceneNode(LODSceneNode* sceneNode){
    00312     writer_->writeText("\t");
    00313     writer_->writeText(sceneNode->getName());
    00314     writer_->writeText(" {\n");
    00315     writer_->writeText("\t\ttype LOD\n");
    00316     // スケール
    00317     Vector3 scale = sceneNode->getScale();
    00318     writer_->writeFormat("\t\tscale { %.8f %.8f %.8f }\n",
    00319         scale.x, scale.y, scale.z);
    00320     // 回転
    00321     Vector3 rotation = sceneNode->getRotationXYZ();
    00322     writer_->writeFormat("\t\trotation { %.8f %.8f %.8f }\n",
    00323         rotation.x, rotation.y, rotation.z);
    00324     // 移動
    00325     Vector3 translation = sceneNode->getTranslation();
    00326     writer_->writeFormat("\t\ttranslation { %.8f %.8f %.8f }\n",
    00327         translation.x, translation.y, translation.z);
    00328     // 有効、無効
    00329     writer_->writeText("\t\tenabled ");
    00330     writeBool(sceneNode->isEnabled());
    00331     writer_->writeText("\n");
    00332     // LOD分割数
    00333     int lodThresholdCount = sceneNode->getLODThresholdCount();
    00334     writer_->writeFormat("\t\tlodThresholdCount %d {\n", lodThresholdCount);
    00335     // LOD分割値
    00336     for(int i = 0; i < lodThresholdCount; i++){
    00337         writer_->writeFormat("\t\t\t%.8f\n", sceneNode->getLODThreshold(i));
    00338     }
    00339     writer_->writeText("\t\t}\n");
    00340     writer_->writeText("\t}\n\n");
    00341 }
    00342 //------------------------------------------------------------------------------
    00343 // ライト
    00344 //------------------------------------------------------------------------------
    00345 // ライトリストの書き出し
    00346 void TextSceneSaver::writeLightList(){
    00347     int lightCount = lightManager_->getCount();
    00348     for(int i = 0; i < lightCount; i++){
    00349         Light* light = lightManager_->get(i);
    00350         if(light->isAmbientLight()){
    00351             writeAmbientLight(light->castAmbientLight());
    00352         }else if(light->isDirectionalLight()){
    00353             writeDirectionalLight(light->castDirectionalLight());
    00354         }else if(light->isPointLight()){
    00355             writePointLight(light->castPointLight());
    00356         }else{
    00357             ErrorOut("TextSceneSaver::writeLightList() unsupported light %s",
    00358                 light->getName().getBytes());
    00359         }
    00360     }
    00361 }
    00362 //------------------------------------------------------------------------------
    00363 // ライトの書き出し
    00364 void TextSceneSaver::writeLight(Light* light){
    00365     writer_->writeText("\t\tLight {\n");
    00366     // ライトマスク
    00367     writer_->writeFormat("\t\t\tlightMask %u\n", light->getLightMask());
    00368     // 有効、無効
    00369     writer_->writeText("\t\t\tenabled ");
    00370     writeBool(light->isEnabled());
    00371     writer_->writeText("\n\t\t}\n");
    00372 }
    00373 //------------------------------------------------------------------------------
    00374 // アンビエントライトの書き出し
    00375 void TextSceneSaver::writeAmbientLight(AmbientLight* light){
    00376     writer_->writeText("\t");
    00377     writer_->writeText(light->getName());
    00378     writer_->writeText(" {\n");
    00379     writer_->writeText("\t\ttype Ambient\n");
    00380     // カラー
    00381     Color3f color = light->getColor();
    00382     writer_->writeFormat("\t\tcolor { %.8f %.8f %.8f }\n",
    00383         color.r, color.g, color.b);
    00384     // ライトの書き出し
    00385     writeLight(light);
    00386     writer_->writeText("\t}\n\n");
    00387 }
    00388 //------------------------------------------------------------------------------
    00389 // ディレクショナルライトの書き出し
    00390 void TextSceneSaver::writeDirectionalLight(DirectionalLight* light){
    00391     writer_->writeText("\t");
    00392     writer_->writeText(light->getName());
    00393     writer_->writeText(" {\n");
    00394     writer_->writeText("\t\ttype Directional\n");
    00395     // ディフューズカラー
    00396     Color3f diffuse = light->getDiffuseColor();
    00397     writer_->writeFormat("\t\tdiffuse { %.8f %.8f %.8f }\n",
    00398         diffuse.r, diffuse.g, diffuse.b);
    00399     // スペキュラカラー
    00400     Color3f specular = light->getSpecularColor();
    00401     writer_->writeFormat("\t\tspecular { %.8f %.8f %.8f }\n",
    00402         specular.r, specular.g, specular.b);
    00403     // 方向
    00404     Vector3 direction = light->getDirection();
    00405     writer_->writeFormat("\t\tdirection { %.8f %.8f %.8f }\n",
    00406         direction.x, direction.y, direction.z);
    00407     // ライトの書き出し
    00408     writeLight(light);
    00409     writer_->writeText("\t}\n\n");
    00410 }
    00411 //------------------------------------------------------------------------------
    00412 // ポイントライトの書き出し
    00413 void TextSceneSaver::writePointLight(PointLight* light){
    00414     writer_->writeText("\t");
    00415     writer_->writeText(light->getName());
    00416     writer_->writeText(" {\n");
    00417     writer_->writeText("\t\ttype Point\n");
    00418     // ディフューズカラー
    00419     Color3f diffuse = light->getDiffuseColor();
    00420     writer_->writeFormat("\t\tdiffuse { %.8f %.8f %.8f }\n",
    00421         diffuse.r, diffuse.g, diffuse.b);
    00422     // スペキュラカラー
    00423     Color3f specular = light->getSpecularColor();
    00424     writer_->writeFormat("\t\tspecular { %.8f %.8f %.8f }\n",
    00425         specular.r, specular.g, specular.b);
    00426     // 位置
    00427     Vector3 position = light->getPosition();
    00428     writer_->writeFormat("\t\tposition { %.8f %.8f %.8f }\n",
    00429         position.x, position.y, position.z);
    00430     // レンジ
    00431     writer_->writeFormat("\t\trange %.8f\n", light->getRange());
    00432     // 減衰係数
    00433     writer_->writeFormat("\t\tattenuation { %.8f %.8f %.8f }\n",
    00434         light->getAttenuation0(), light->getAttenuation1(),
    00435         light->getAttenuation2());
    00436     // ライトの書き出し
    00437     writeLight(light);
    00438     writer_->writeText("\t}\n\n");
    00439 }
    00440 //------------------------------------------------------------------------------
    00441 // モデル
    00442 //------------------------------------------------------------------------------
    00443 // モデルリストの書き出し
    00444 void TextSceneSaver::writeModelList(){
    00445     int modelCount = modelManager_->getCount();
    00446     for(int i = 0; i < modelCount; i++){
    00447         Model* model = modelManager_->get(i);
    00448         if(model->isStandardModel()){
    00449             writeStandardModel(model->castStandardModel());
    00450         }else if(model->isCharacterModel()){
    00451             writeCharacterModel(model->castCharacterModel());
    00452         }else{
    00453             ErrorOut("TextSceneSaver::writeModelList() unsupported model %s",
    00454                 model->getName().getBytes());
    00455         }
    00456     }
    00457 }
    00458 //------------------------------------------------------------------------------
    00459 // 標準モデルの書き出し
    00460 void TextSceneSaver::writeStandardModel(StandardModel* model){
    00461     writer_->writeText("\t");
    00462     writer_->writeText(model->getName());
    00463     writer_->writeText(" {\n");
    00464     writer_->writeText("\t\ttype Standard\n");
    00465     // 有効、無効
    00466     writer_->writeText("\t\tenabled ");
    00467     writeBool(model->isEnabled());
    00468     writer_->writeText("\n\t}\n\n");
    00469 }
    00470 //------------------------------------------------------------------------------
    00471 // キャラクタモデルの書き出し
    00472 void TextSceneSaver::writeCharacterModel(CharacterModel* model){
    00473     writer_->writeText("\t");
    00474     writer_->writeText(model->getName());
    00475     writer_->writeText(" {\n");
    00476     writer_->writeText("\t\ttype Character\n");
    00477     // 有効、無効
    00478     writer_->writeText("\t\tenabled ");
    00479     writeBool(model->isEnabled());
    00480     // ボーンの書き出し
    00481     writer_->writeText("\n\t\tbone {\n");
    00482     int boneCount = model->getBoneCount();
    00483     for(int i = 0; i < boneCount; i++){
    00484         writeBone(model->getBone(i));
    00485     }
    00486     writer_->writeText("\t\t}\n");
    00487     // ボーンリンクの書き出し
    00488     writer_->writeText("\t\tboneLink {\n");
    00489     for(int i = 0; i < boneCount; i++){
    00490         writeBoneLink(model->getBone(i));
    00491     }
    00492     writer_->writeText("\t\t}\n");
    00493     writer_->writeText("\t}\n\n");
    00494 }
    00495 //------------------------------------------------------------------------------
    00496 // ボーンの書き出し
    00497 void TextSceneSaver::writeBone(Bone* bone){
    00498     writer_->writeText("\t\t\t");
    00499     writer_->writeText(bone->getName());
    00500     writer_->writeText(" {\n");
    00501     // ポーズ逆行列
    00502     Matrix34 invPose = bone->getInversePoseMatrix();
    00503     writer_->writeFormat("\t\t\t\tinversePoseMatrix Matrix34 {\n"
    00504         "\t\t\t\t\t %.8f %.8f %.8f %.8f\n\t\t\t\t\t %.8f %.8f %.8f %.8f\n"
    00505         "\t\t\t\t\t %.8f %.8f %.8f %.8f\n\t\t\t\t}\n",
    00506         invPose.m00, invPose.m01, invPose.m02, invPose.m03,
    00507         invPose.m10, invPose.m11, invPose.m12, invPose.m13,
    00508         invPose.m20, invPose.m21, invPose.m22, invPose.m23);
    00509     // スケール
    00510     Vector3 scale = bone->getScale();
    00511     writer_->writeFormat("\t\t\t\tscale { %.8f %.8f %.8f }\n",
    00512         scale.x, scale.y, scale.z);
    00513     // 回転
    00514     Vector3 rotation = bone->getRotationXYZ();
    00515     writer_->writeFormat("\t\t\t\trotation { %.8f %.8f %.8f }\n",
    00516         rotation.x, rotation.y, rotation.z);
    00517     // 移動
    00518     Vector3 translation = bone->getTranslation();
    00519     writer_->writeFormat("\t\t\t\ttranslation { %.8f %.8f %.8f }\n",
    00520         translation.x, translation.y, translation.z);
    00521
    00522     writer_->writeText("\t\t\t}\n");
    00523 }
    00524 //------------------------------------------------------------------------------
    00525 // ボーンリンクの書き出し
    00526 void TextSceneSaver::writeBoneLink(Bone* bone){
    00527     int boneCount = bone->getBoneCount();
    00528     if(boneCount == 0){ return; }
    00529     writer_->writeText("\t\t\t");
    00530     writer_->writeText(bone->getName());
    00531     writer_->writeText(" {\n");
    00532     for(int i = 0; i < boneCount; i++){
    00533         Bone* child = bone->getBone(i);
    00534         writer_->writeText("\t\t\t\t");
    00535         writer_->writeText(child->getName());
    00536         writer_->writeText("\n");
    00537     }
    00538     writer_->writeText("\t\t\t}\n");
    00539 }
    00540 //------------------------------------------------------------------------------
    00541 // メッシュ
    00542 //------------------------------------------------------------------------------
    00543 // メッシュリストの書き出し
    00544 void TextSceneSaver::writeMeshList(){
    00545     int meshCount = meshManager_->getCount();
    00546     for(int i = 0; i < meshCount; i++){
    00547         Mesh* mesh = meshManager_->get(i);
    00548         if(mesh->isRigidMesh()){
    00549             writeRigidMesh(mesh->castRigidMesh());
    00550         }else if(mesh->isCharacterMesh()){
    00551             writeCharacterMesh(mesh->castCharacterMesh());
    00552         }else{
    00553             ErrorOut("TextSceneSaver::writeMeshList() unsupported mesh %s",
    00554                 mesh->getName().getBytes());
    00555         }
    00556     }
    00557 }
    00558 //------------------------------------------------------------------------------
    00559 // 剛体メッシュの書き出し
    00560 void TextSceneSaver::writeRigidMesh(RigidMesh* mesh){
    00561     writer_->writeText("\t");
    00562     writer_->writeText(mesh->getName());
    00563     writer_->writeText(" {\n");
    00564     writer_->writeText("\t\ttype Rigid\n");
    00565     // 有効、無効
    00566     writer_->writeText("\t\tenabled ");
    00567     writeBool(mesh->isEnabled());
    00568     writer_->writeText("\n\t}\n\n");
    00569 }
    00570 //------------------------------------------------------------------------------
    00571 // キャラクタメッシュの書き出し
    00572 void TextSceneSaver::writeCharacterMesh(CharacterMesh* mesh){
    00573     writer_->writeText("\t");
    00574     writer_->writeText(mesh->getName());
    00575     writer_->writeText(" {\n");
    00576     writer_->writeText("\t\ttype Character\n");
    00577     // 有効、無効
    00578     writer_->writeText("\t\tenabled ");
    00579     writeBool(mesh->isEnabled());
    00580     writer_->writeText("\n\t}\n\n");
    00581 }
    00582 //------------------------------------------------------------------------------
    00583 // メッシュデータ
    00584 //------------------------------------------------------------------------------
    00585 // メッシュデータリストの書き出し
    00586 void TextSceneSaver::writeMeshDataList(){
    00587     int meshDataCount = meshDataManager_->getCount();
    00588     for(int i = 0; i < meshDataCount; i++){
    00589         writeMeshData(meshDataManager_->get(i));
    00590     }
    00591 }
    00592 //------------------------------------------------------------------------------
    00593 // メッシュデータの書き出し
    00594 void TextSceneSaver::writeMeshData(MeshData* meshData){
    00595     writer_->writeText("\t");
    00596     writer_->writeText(meshData->getName());
    00597     writer_->writeText(" {\n");
    00598     // バウンディングボックス
    00599     const AxisAlignedBox& box = meshData->getBoundingBox();
    00600     writer_->writeFormat(
    00601         "\t\tboundingBox { %.8f %.8f %.8f } { %.8f %.8f %.8f }\n",
    00602         box.getMinimum().x, box.getMinimum().y, box.getMinimum().z,
    00603         box.getMaximum().x, box.getMaximum().y, box.getMaximum().z);
    00604     // バウンディングスフィア
    00605     const Sphere& sphere = meshData->getBoundingSphere();
    00606     writer_->writeFormat(
    00607         "\t\tboundingSphere { %.8f %.8f %.8f %.8f }\n",
    00608         sphere.getCenter().x, sphere.getCenter().y, sphere.getCenter().z,
    00609         sphere.getRadius());
    00610     // プリミティブタイプ
    00611     writer_->writeText("\t\tprimitiveType ");
    00612     writer_->writeText(Mesh::primitiveTypeToString(
    00613         meshData->getPrimitiveType()));
    00614     writer_->writeText("\n");
    00615     // トライアングルリストのインデックス出力
    00616     if(meshData->getPrimitiveType() == Mesh::indexedTriangleList){
    00617         int vertexIndexCount = meshData->getVertexIndexCount();
    00618         writer_->writeFormat("\t\tindices %d {\n", vertexIndexCount);
    00619         vertexIndexCount /= 3;
    00620         for(int i = 0; i < vertexIndexCount; i++){
    00621             int index = i * 3;
    00622             writer_->writeFormat("\t\t\t%d %d %d\n",
    00623                 meshData->getVertexIndex(index),
    00624                 meshData->getVertexIndex(index + 1),
    00625                 meshData->getVertexIndex(index + 2));
    00626         }
    00627         writer_->writeFormat("\t\t}\n");
    00628     }
    00629     // 法線を持っているか
    00630     bool hasNormal = meshData->hasNormal();
    00631     writer_->writeText("\t\thasNormal ");
    00632     writeBool(hasNormal);
    00633     // 頂点カラーを持っているか
    00634     bool hasColor = meshData->hasColor();
    00635     writer_->writeText("\n\t\thasColor ");
    00636     writeBool(hasColor);
    00637     // テクスチャ座標セット数
    00638     int texCoordSetCount = meshData->getTexCoordSetCount();
    00639     writer_->writeFormat("\n\t\ttexCoordSetCount %d\n", texCoordSetCount);
    00640     // テクスチャ座標タイプ
    00641     writer_->writeText("\t\ttexCoordType");
    00642     int texCoordType[TexCoord::maxSetCount];
    00643     for(int i = 0; i < texCoordSetCount; i++){
    00644         texCoordType[i] = meshData->getTexCoordType(i);
    00645         writer_->writeFormat(" %d", texCoordType[i]);
    00646     }
    00647     writer_->writeText("\n");
    00648     // 頂点あたりボーン数
    00649     int bonesPerVertex = meshData->getBonesPerVertex();
    00650     writer_->writeFormat("\t\tbonesPerVertex %d\n", bonesPerVertex);
    00651     // 頂点あたりウェイト数
    00652     int weightsPerVertex = meshData->getWeightsPerVertex();
    00653     // 頂点リスト
    00654     int vertexCount = meshData->getVertexCount();
    00655     writer_->writeFormat("\t\tvertices %d {\n", vertexCount);
    00656     for(int i = 0; i < vertexCount; i++){
    00657         // 位置
    00658         Vector3 pos = meshData->getPosition(i);
    00659         writer_->writeFormat("\t\t\t%d { %.8f %.8f %.8f }",
    00660             i, pos.x, pos.y, pos.z);
    00661         // 法線
    00662         if(hasNormal){
    00663             Vector3 nom = meshData->getNormal(i);
    00664             writer_->writeFormat(" { %.8f %.8f %.8f }",
    00665                 nom.x, nom.y, nom.z);
    00666         }
    00667         // 頂点カラー
    00668         if(hasColor){
    00669             Color4c color = meshData->getColor(i);
    00670             writer_->writeFormat(" { %d %d %d %d }",
    00671                 color.r, color.g, color.b, color.a);
    00672         }
    00673         // テクスチャ座標
    00674         for(int j = 0; j < texCoordSetCount; j++){
    00675             writer_->writeText(" { ");
    00676             for(int k = 0; k < texCoordType[j]; k++){
    00677                 writer_->writeFormat("%.8f ",
    00678                     *(meshData->getTexCoordArray(j) + (i * texCoordType[j]) + k));
    00679             }
    00680             writer_->writeText("}");
    00681         }
    00682         // ボーンインデックス
    00683         if(bonesPerVertex != 0){
    00684             writer_->writeText(" { ");
    00685             for(int j = 0; j < bonesPerVertex; j++){
    00686                 writer_->writeFormat("%d ",
    00687                     (int)meshData->getBoneIndex(i, j));
    00688             }
    00689             writer_->writeText("}");
    00690         }
    00691         // ウェイト
    00692         if(weightsPerVertex != 0){
    00693             writer_->writeText(" { ");
    00694             for(int j = 0; j < weightsPerVertex; j++){
    00695                 writer_->writeFormat("%.8f ", meshData->getWeight(i, j));
    00696             }
    00697             writer_->writeText("}");
    00698         }
    00699         writer_->writeText("\n");
    00700     }
    00701     writer_->writeText("\t\t}\n");
    00702     writer_->writeText("\t}\n\n");
    00703 }
    00704 //------------------------------------------------------------------------------
    00705 // マテリアル
    00706 //------------------------------------------------------------------------------
    00707 // マテリアルリストの書き出し
    00708 void TextSceneSaver::writeMaterialList(){
    00709     int materialCount = materialManager_->getCount();
    00710     for(int i = 0; i < materialCount; i++){
    00711         Material* material = materialManager_->get(i);
    00712         if(material->isBasicMaterial()){
    00713             writeBasicMaterial(material->castBasicMaterial());
    00714         }else{
    00715             ErrorOut("TextSceneSaver::writeMaterialList() "
    00716                 "unsupported material %s", material->getName().getBytes());
    00717         }
    00718     }
    00719 }
    00720 //------------------------------------------------------------------------------
    00721 // マテリアルの書き出し
    00722 void TextSceneSaver::writeMaterial(const Material* material){
    00723     writer_->writeText("\t\tMaterial {\n");
    00724     writer_->writeFormat("\t\t\tpriority %d\n", material->getPriority());
    00725     writer_->writeText("\t\t\tblendMode ");
    00726     writer_->writeText(Material::blendModeToString(material->getBlendMode()));
    00727     writer_->writeFormat("\n\t\t\talpha %.8f\n", material->getAlpha());
    00728     writer_->writeText("\t\t\tblendSource ");
    00729     writer_->writeText(
    00730         Material::blendStateToString(material->getBlendSource()));
    00731     writer_->writeText("\n\t\t\tblendDestination ");
    00732     writer_->writeText(
    00733         Material::blendStateToString(material->getBlendDestination()));
    00734     writer_->writeText("\n\t\t\tzWrite ");
    00735     writeBool(material->useZWrite());
    00736     writer_->writeText("\n\t\t\tzTest ");
    00737     writeBool(material->useZTest());
    00738     writer_->writeText("\n\t\t\tfogOption ");
    00739     writer_->writeText(Material::fogOptionToString(material->getFogOption()));
    00740     writer_->writeFormat("\n\t\t\tlightMask %u\n", material->getLightMask());
    00741     writer_->writeText("\t\t}\n");
    00742 }
    00743 //------------------------------------------------------------------------------
    00744 // 基本マテリアルの書き出し
    00745 void TextSceneSaver::writeBasicMaterial(const BasicMaterial* material){
    00746     writer_->writeText("\t");
    00747     writer_->writeText(material->getName());
    00748     writer_->writeText(" {\n");
    00749     writer_->writeText("\t\ttype Basic\n");
    00750     writer_->writeFormat("\t\tbaseUVIndex %d\n", material->getBaseUVIndex());
    00751     writer_->writeFormat("\t\tglossUVIndex %d\n", material->getGlossUVIndex());
    00752     writer_->writeFormat("\t\tlightUVIndex %d\n", material->getLightUVIndex());
    00753     writer_->writeFormat("\t\tstainUVIndex %d\n", material->getStainUVIndex());
    00754     Color3f diffuse = material->getDiffuseColor();
    00755     writer_->writeFormat("\t\tdiffuseColor { %.8f %.8f %.8f }\n",
    00756         diffuse.r, diffuse.g, diffuse.b);
    00757     Color3f specular = material->getSpecularColor();
    00758     writer_->writeFormat("\t\tspecularColor { %.8f %.8f %.8f }\n",
    00759         specular.r, specular.g, specular.b);
    00760     writer_->writeFormat("\t\tspecularPower %.8f\n",
    00761         material->getSpecularPower());
    00762     Color3f ambient = material->getAmbientColor();
    00763     writer_->writeFormat("\t\tambientColor { %.8f %.8f %.8f }\n",
    00764         ambient.r, ambient.g, ambient.b);
    00765     Color3f emissive = material->getEmissiveColor();
    00766     writer_->writeFormat("\t\temissiveColor { %.8f %.8f %.8f }\n",
    00767         emissive.r, emissive.g, emissive.b);
    00768     // マテリアルの書き出し
    00769     writeMaterial(material);
    00770     writer_->writeText("\t}\n\n");
    00771 }
    00772 //------------------------------------------------------------------------------
    00773 // テクスチャ
    00774 //------------------------------------------------------------------------------
    00775 // テクスチャリストの書き出し
    00776 void TextSceneSaver::writeTextureList(){
    00777     int textureCount = textureManager_->getCount();
    00778     for(int i = 0; i < textureCount; i++){
    00779         Texture* texture = textureManager_->get(i);
    00780         if(texture->isSurfaceTexture()){
    00781             writeSurfaceTexture(texture->castSurfaceTexture());
    00782         }else{
    00783             ErrorOut("TextSceneSaver::writeTextureList() unsupported texture %s",
    00784                 texture->getName().getBytes());
    00785         }
    00786     }
    00787 }
    00788 //------------------------------------------------------------------------------
    00789 // サーフェーステクスチャの書き出し
    00790 void TextSceneSaver::writeSurfaceTexture(const SurfaceTexture* texture){
    00791     writer_->writeText("\t");
    00792     writer_->writeText(texture->getName());
    00793     writer_->writeText(" {\n");
    00794     writer_->writeText("\t\ttype Surface\n");
    00795     writer_->writeText("\t\taddressModeU ");
    00796     writer_->writeText(Texture::addressModeToString(
    00797         texture->getAddressModeU()));
    00798     writer_->writeText("\n\t\taddressModeV ");
    00799     writer_->writeText(Texture::addressModeToString(
    00800         texture->getAddressModeV()));
    00801     TexCoord2 repeat = texture->getRepeatUV();
    00802     writer_->writeFormat("\n\t\trepeatUV { %.8f %.8f }", repeat.u, repeat.v);
    00803     TexCoord2 offset = texture->getOffsetUV();
    00804     writer_->writeFormat("\n\t\toffsetUV { %.8f %.8f }", offset.u, offset.v);
    00805     writer_->writeText("\n\t}\n\n");
    00806 }
    00807 //------------------------------------------------------------------------------
    00808 // ピクチャ
    00809 //------------------------------------------------------------------------------
    00810 // ピクチャの書き出し
    00811 void TextSceneSaver::writePicture(const Picture* picture){
    00812     writer_->writeText("\t");
    00813     writer_->writeText(picture->getName());
    00814     writer_->writeText(" {\n");
    00815     writer_->writeText("\t\tpath ");
    00816     String path = picture->getPath();
    00817     writer_->writeText(path);
    00818     writer_->writeText("\n\t}\n\n");
    00819     // ピクチャ出力ディレクトリの作成
    00820     FilePath filePath(path);
    00821     path = filePath.getFolderPath();
    00822     if(path != ""){
    00823         path = basePath_ + path;
    00824         mkdir(path.getBytes());
    00825     }
    00826     // ピクチャファイルの保存
    00827     TargaSaver saver;
    00828     if(picture->isPictureRGB8()){
    00829         // RGB8は24bitのTargaで保存
    00830         PictureRGB8* rgb8 = picture->castPictureRGB8();
    00831         saver.save(basePath_ + rgb8->getPath(),
    00832             rgb8->getSize(), rgb8->getImage());
    00833     }else if(picture->isPictureRGBA8()){
    00834         // RGBA8は32bitのTargaで保存
    00835         PictureRGBA8* rgba8 = picture->castPictureRGBA8();
    00836         saver.save(basePath_ + rgba8->getPath(),
    00837             rgba8->getSize(), rgba8->getImage());
    00838     }else{
    00839         ErrorOut("TextSceneSaver::writePictureList() "
    00840             "unsupported picture %s", picture->getName().getBytes());
    00841     }
    00842 }
    00843 //------------------------------------------------------------------------------
    00844 // リンク
    00845 //------------------------------------------------------------------------------
    00846 // シーンノードリンクの書き出し
    00847 void TextSceneSaver::writeSceneNodeLink(SceneNode* sceneNode){
    00848     // リンクが0かどうかチェック
    00849     int sceneNodeCount = sceneNode->getSceneNodeCount();
    00850     int sceneLeafCount = sceneNode->getSceneLeafCount();
    00851     if(sceneNodeCount + sceneLeafCount == 0){ return; }
    00852     writer_->writeText("\t");
    00853     writer_->writeText(sceneNode->getName());
    00854     writer_->writeText(" {\n");
    00855     if(sceneNodeCount > 0){
    00856         writer_->writeText("\t\tSceneNode {\n");
    00857         for(int i = 0; i < sceneNodeCount; i++){
    00858             SceneNode* linkNode = sceneNode->getSceneNode(i);
    00859             writer_->writeText("\t\t\t");
    00860             writer_->writeText(linkNode->getName());
    00861             writer_->writeText("\n");
    00862         }
    00863         writer_->writeText("\t\t}\n");
    00864     }
    00865     if(sceneLeafCount > 0){
    00866         writer_->writeText("\t\tSceneLeaf {\n");
    00867         for(int i = 0; i < sceneLeafCount; i++){
    00868             SceneLeaf* sceneLeaf = sceneNode->getSceneLeaf(i);
    00869             if(sceneLeaf->isModel()){
    00870                 writer_->writeText("\t\t\tModel ");
    00871                 writer_->writeText(sceneLeaf->getName());
    00872                 writer_->writeText("\n");
    00873             }else if(sceneLeaf->isLight()){
    00874                 writer_->writeText("\t\t\tLight ");
    00875                 writer_->writeText(sceneLeaf->getName());
    00876                 writer_->writeText("\n");
    00877             }else{
    00878                 Assert(false);
    00879             }
    00880         }
    00881         writer_->writeText("\t\t}\n");
    00882     }
    00883     writer_->writeText("\t}\n\n");
    00884 }
    00885 //------------------------------------------------------------------------------
    00886 // モデルリンクの書き出し
    00887 void TextSceneSaver::writeModelLink(const Model* model){
    00888     int meshCount = model->getMeshCount();
    00889     if(meshCount == 0){ return; }
    00890     writer_->writeText("\t");
    00891     writer_->writeText(model->getName());
    00892     writer_->writeText(" {\n");
    00893     for(int i = 0; i < meshCount; i++){
    00894         Mesh* mesh = model->getMesh(i);
    00895         writer_->writeText("\t\t");
    00896         writer_->writeText(mesh->getName());
    00897         writer_->writeText("\n");
    00898     }
    00899     writer_->writeText("\t}\n\n");
    00900 }
    00901 //------------------------------------------------------------------------------
    00902 // メッシュリンクの書き出し
    00903 void TextSceneSaver::writeMeshLink(const Mesh* mesh){
    00904     if((mesh->getMeshData() == NULL) &&
    00905         (mesh->getMaterial() == NULL)){ return; }
    00906     writer_->writeText("\t");
    00907     writer_->writeText(mesh->getName());
    00908     writer_->writeText(" {\n");
    00909     // メッシュデータ
    00910     if(mesh->getMeshData() != NULL){
    00911         writer_->writeText("\t\tMeshData ");
    00912         writer_->writeText(mesh->getMeshData()->getName());
    00913         writer_->writeText("\n");
    00914     }
    00915     // マテリアル
    00916     if(mesh->getMaterial() != NULL){
    00917         writer_->writeText("\t\tMaterial ");
    00918         writer_->writeText(mesh->getMaterial()->getName());
    00919         writer_->writeText("\n");
    00920     }
    00921     writer_->writeText("\t}\n\n");
    00922 }
    00923 //------------------------------------------------------------------------------
    00924 // マテリアルリンクリストの書き出し
    00925 void TextSceneSaver::writeMaterialLinkList(){
    00926     writer_->writeText("MaterialLink {\n");
    00927     int materialCount = materialManager_->getCount();
    00928     for(int i = 0; i < materialCount; i++){
    00929         Material* material = materialManager_->get(i);
    00930         if(material->isBasicMaterial()){
    00931             writeBasicMaterialLink(material->castBasicMaterial());
    00932         }else{
    00933             ErrorOut("TextSceneSaver::writeMaterialLinkList() "
    00934                 "unsupported material %s", material->getName().getBytes());
    00935         }
    00936     }
    00937     writer_->writeText("}\n\n");
    00938 }
    00939 //------------------------------------------------------------------------------
    00940 // 基本マテリアルリンクの書き出し
    00941 void TextSceneSaver::writeBasicMaterialLink(const BasicMaterial* material){
    00942     Texture* baseTexture = material->getBaseTexture();
    00943     Texture* glossTexture = material->getGlossTexture();
    00944     Texture* lightTexture = material->getLightTexture();
    00945     Texture* stainTexture = material->getStainTexture();
    00946     // テクスチャの有無をチェック
    00947     if((baseTexture == NULL) && (glossTexture == NULL) &&
    00948         (lightTexture == NULL) && (stainTexture == NULL)){ return; }
    00949     writer_->writeText("\t");
    00950     writer_->writeText(material->getName());
    00951     writer_->writeText(" {\n");
    00952     writer_->writeText("\t\ttype Basic\n");
    00953     if(baseTexture != NULL){
    00954         writer_->writeText("\t\tbaseTexture ");
    00955         writer_->writeText(baseTexture->getName());
    00956         writer_->writeText("\n");
    00957     }
    00958     if(glossTexture != NULL){
    00959         writer_->writeText("\t\tglossTexture ");
    00960         writer_->writeText(glossTexture->getName());
    00961         writer_->writeText("\n");
    00962     }
    00963     if(lightTexture != NULL){
    00964         writer_->writeText("\t\tlightTexture ");
    00965         writer_->writeText(lightTexture->getName());
    00966         writer_->writeText("\n");
    00967     }
    00968     if(stainTexture != NULL){
    00969         writer_->writeText("\t\tstainTexture ");
    00970         writer_->writeText(stainTexture->getName());
    00971         writer_->writeText("\n");
    00972     }
    00973     writer_->writeText("\t}\n\n");
    00974 }
    00975 //------------------------------------------------------------------------------
    00976 // テクスチャリンクの書き出し
    00977 void TextSceneSaver::writeTextureLink(const Texture* texture){
    00978     int pictureCount = texture->getPictureCount();
    00979     if(pictureCount == 0){ return; }
    00980     writer_->writeText("\t");
    00981     writer_->writeText(texture->getName());
    00982     writer_->writeText(" {\n");
    00983     for(int i = 0; i < pictureCount; i++){
    00984         Picture* picture = texture->getPicture(i);
    00985         writer_->writeText("\t\t");
    00986         writer_->writeText(picture->getName());
    00987         writer_->writeText("\n");
    00988     }
    00989     writer_->writeText("\t}\n\n");
    00990 }
    00991 //------------------------------------------------------------------------------
    00992 // ユーティリティ
    00993 //------------------------------------------------------------------------------
    00994 // boolの書き出し
    00995 void TextSceneSaver::writeBool(bool flag){
    00996     if(flag){
    00997         writer_->writeText("true");
    00998     }else{
    00999         writer_->writeText("false");
    01000     }
    01001 }
    01002 //------------------------------------------------------------------------------
    01003 // 線コメントの書き出し
    01004 void TextSceneSaver::writeLineComment(){
    01005     writer_->writeText("//----------------------------"
    01006         "--------------------------------------------------\n");
    01007 }
    01008 //------------------------------------------------------------------------------
    01009 // ブロックコメントの書き出し
    01010 void TextSceneSaver::writeBlockComment(const String& blockName){
    01011     writeLineComment();
    01012     writer_->writeText("// ");
    01013     writer_->writeText(blockName);
    01014     writer_->writeText(" Block\n");
    01015     writeLineComment();
    01016 }
    01017 //------------------------------------------------------------------------------
    01018 } // End of namespace Lamp
    01019 //------------------------------------------------------------------------------