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

BinaryAnimationLoader.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 "Animation/InputOutput/BinaryAnimationLoader.h"
00027 #include "Core/InputOutput/BinaryFileReader.h"
00028 #include "Core/InputOutput/FilePath.h"
00029 #include "Animation/VectorInterpolator/VectorConstantInterpolator.h"
00030 #include "Animation/VectorInterpolator/VectorArrayInterpolator.h"
00031 #include "Animation/VectorInterpolator/VectorLinearInterpolator.h"
00032 #include "Animation/RotationInterpolator/RotationConstantInterpolator.h"
00033 #include "Animation/RotationInterpolator/EulerArrayInterpolator.h"
00034 #include "Animation/RotationInterpolator/QuaternionArrayInterpolator.h"
00035 #include "Animation/RotationInterpolator/QuaternionLinearInterpolator.h"
00036 
00037 #include "Animation/System/AnimationManager.h"
00038 #include "Animation/System/AnimationSet.h"
00039 #include "Animation/Camera/CameraAnimation.h"
00040 #include "Animation/SceneNode/SceneNodeAnimation.h"
00041 #include "Animation/Model/CharacterModelAnimation.h"
00042 
00043 namespace Lamp{
00044 
00045 //------------------------------------------------------------------------------
00046 // コンストラクタ
00047 BinaryAnimationLoader::BinaryAnimationLoader(){
00048 }
00049 //------------------------------------------------------------------------------
00050 // デストラクタ
00051 BinaryAnimationLoader::~BinaryAnimationLoader(){
00052 }
00053 //------------------------------------------------------------------------------
00054 // ロード
00055 //------------------------------------------------------------------------------
00056 // ロード
00057 void BinaryAnimationLoader::load(
00058     const String& filePath, AnimationManager* manager){
00059     FilePath path(filePath);
00060     if(!path.existFile()){
00061         ErrorOut("BinaryAnimationLoader::load() File not Found");
00062     }
00063     BinaryFileReader* binaryFileReader = new BinaryFileReader(filePath);
00064     load(binaryFileReader, manager);
00065     delete binaryFileReader;
00066 }
00067 //------------------------------------------------------------------------------
00068 // ロード
00069 void BinaryAnimationLoader::load(
00070     BinaryReader* binaryReader, AnimationManager* manager){
00071     reader_ = binaryReader;
00072     manager_ = manager;
00073 
00074     // ヘッダの読み込み
00075     readHeader();
00076 
00077     // 各ブロックの読み込み
00078     while(true){
00079         if(reader_->isEnd()){ break; }
00080         // ブロックヘッダ読み込み
00081         String blockName = readString();
00082         int blockSize = reader_->readInt();
00083         int objectCount = reader_->readInt();
00084         align();
00085         // ブロック内容の読み込み
00086         int blockAddress = reader_->getPosition();
00087         if(blockName == "AnimationSet"){
00088             for(int i = 0; i < objectCount; i++){
00089                 readAnimationSet();
00090             }
00091         }else if(blockName == "Camera"){
00092             for(int i = 0; i < objectCount; i++){
00093                 readCameraAnimation();
00094             }
00095         }else if(blockName == "CameraData"){
00096             for(int i = 0; i < objectCount; i++){
00097                 readCameraAnimationData();
00098             }
00099         }else if(blockName == "SceneNode"){
00100             for(int i = 0; i < objectCount; i++){
00101                 readSceneNodeAnimation();
00102             }
00103         }else if(blockName == "SceneNodeData"){
00104             for(int i = 0; i < objectCount; i++){
00105                 readSceneNodeAnimationData();
00106             }
00107         }else if(blockName == "CharacterModel"){
00108             for(int i = 0; i < objectCount; i++){
00109                 readCharacterModelAnimation();
00110             }
00111         }else if(blockName == "CharacterModelData"){
00112             for(int i = 0; i < objectCount; i++){
00113                 readCharacterModelAnimationData();
00114             }
00115         }else if(blockName == "AnimationSetLink"){
00116             for(int i = 0; i < objectCount; i++){
00117                 readAnimationSetLink();
00118             }
00119         }else if(blockName == "CameraLink"){
00120             for(int i = 0; i < objectCount; i++){
00121                 readCameraAnimationLink();
00122             }
00123         }else if(blockName == "SceneNodeLink"){
00124             for(int i = 0; i < objectCount; i++){
00125                 readSceneNodeAnimationLink();
00126             }
00127         }else if(blockName == "CharacterModelLink"){
00128             for(int i = 0; i < objectCount; i++){
00129                 readCharacterModelAnimationLink();
00130             }
00131         }else{
00132             // 知らないブロックを読み飛ばす
00133             DebugOut("BinaryAnimationLoader::load() "
00134                 "Skip unknown block %s (%d)\n",
00135                 blockName.getBytes(), reader_->getPosition());
00136             reader_->skip(blockSize);
00137         }
00138         // 全てのブロックはアライメントをとる
00139         align();
00140         Assert((reader_->getPosition() - blockAddress) == blockSize);
00141     }
00142 }
00143 //------------------------------------------------------------------------------
00144 // ヘッダの読み込み
00145 void BinaryAnimationLoader::readHeader(){
00146     String header = readString();
00147     if(header != "LampBinaryAnimation"){
00148         ErrorOut("BinaryAnimationLoader::readHeader() Invalid header");
00149     }
00150     u_int version = reader_->readUInt();
00151     if(version != 0x00000900){
00152         ErrorOut("BinaryAnimationLoader::readHeader() Invalid version");
00153     }
00154     // アライメント
00155     align();
00156 }
00157 //------------------------------------------------------------------------------
00158 // アニメーションセット
00159 //------------------------------------------------------------------------------
00160 // アニメーションセットの読み込み
00161 void BinaryAnimationLoader::readAnimationSet(){
00162     // 名前
00163     String name = readString();
00164     AnimationSet* animation = manager_->createAnimationSet(name);
00165     // 有効、無効
00166     animation->setEnabled(reader_->readBool());
00167     // アライメント
00168     align();
00169 }
00170 //------------------------------------------------------------------------------
00171 // カメラ
00172 //------------------------------------------------------------------------------
00173 // カメラアニメーションの読み込み
00174 void BinaryAnimationLoader::readCameraAnimation(){
00175     // 名前
00176     String name = readString();
00177     CameraAnimation* animation = manager_->createCamera(name);
00178     // 有効、無効
00179     animation->setEnabled(reader_->readBool());
00180     // ターゲット名
00181     animation->setTargetName(readString());
00182     // アライメント
00183     align();
00184 }
00185 //------------------------------------------------------------------------------
00186 // カメラアニメーションデータの読み込み
00187 void BinaryAnimationLoader::readCameraAnimationData(){
00188     // 名前
00189     String name = readString();
00190     CameraAnimationData* data = manager_->createCameraData(name);
00191     // シーケンス数
00192     int sequenceCount = reader_->readInt();
00193     data->setSequenceCount(sequenceCount);
00194     // アニメーションデータ
00195     for(int i = 0; i < sequenceCount; i++){
00196         // 回転
00197         data->setRotation(i, readRotationInterpolator());
00198         // 移動
00199         data->setTranslation(i, readVectorInterpolator());
00200     }
00201     // ループフラグ
00202     for(int i = 0; i < sequenceCount; i++){
00203         data->setLooped(i, reader_->readBool());
00204     }
00205     // アライメント
00206     align();
00207 }
00208 //------------------------------------------------------------------------------
00209 // シーンノード
00210 //------------------------------------------------------------------------------
00211 // シーンノードアニメーションの読み込み
00212 void BinaryAnimationLoader::readSceneNodeAnimation(){
00213     // 名前
00214     String name = readString();
00215     SceneNodeAnimation* animation = manager_->createSceneNode(name);
00216     // 有効、無効
00217     animation->setEnabled(reader_->readBool());
00218     // ターゲット名
00219     animation->setTargetName(readString());
00220     // アライメント
00221     align();
00222 }
00223 //------------------------------------------------------------------------------
00224 // シーンノードアニメーションデータの読み込み
00225 void BinaryAnimationLoader::readSceneNodeAnimationData(){
00226     // 名前
00227     String name = readString();
00228     SceneNodeAnimationData* data = manager_->createSceneNodeData(name);
00229     // シーケンス数
00230     int sequenceCount = reader_->readInt();
00231     data->setSequenceCount(sequenceCount);
00232     // アニメーションデータ
00233     for(int i = 0; i < sequenceCount; i++){
00234         // スケール
00235         data->setScale(i, readVectorInterpolator());
00236         // 回転
00237         data->setRotation(i, readRotationInterpolator());
00238         // 移動
00239         data->setTranslation(i, readVectorInterpolator());
00240     }
00241     // ループフラグ
00242     for(int i = 0; i < sequenceCount; i++){
00243         data->setLooped(i, reader_->readBool());
00244     }
00245     // アライメント
00246     align();
00247 }
00248 //------------------------------------------------------------------------------
00249 // キャラクタモデル
00250 //------------------------------------------------------------------------------
00251 // キャラクタモデルアニメーションの読み込み
00252 void BinaryAnimationLoader::readCharacterModelAnimation(){
00253     // 名前
00254     String name = readString();
00255     CharacterModelAnimation* animation = manager_->createCharacterModel(name);
00256     // 有効、無効
00257     animation->setEnabled(reader_->readBool());
00258     // ターゲット名
00259     animation->setTargetName(readString());
00260     // ボーン数
00261     int boneCount = reader_->readInt();
00262     animation->setBoneCount(boneCount);
00263     // ボーン名
00264     for(int i = 0; i < boneCount; i++){
00265         animation->setBoneName(i, readString());
00266     }
00267     // アライメント
00268     align();
00269 }
00270 //------------------------------------------------------------------------------
00271 // キャラクタモデルアニメーションデータの読み込み
00272 void BinaryAnimationLoader::readCharacterModelAnimationData(){
00273     // 名前
00274     String name = readString();
00275     CharacterModelAnimationData* data =
00276         manager_->createCharacterModelData(name);
00277     // シーケンス数
00278     int sequenceCount = reader_->readInt();
00279     data->setSequenceCount(sequenceCount);
00280     // ボーン数
00281     int boneCount = reader_->readInt();
00282     data->setBoneCount(boneCount);
00283     // アニメーションデータ
00284     for(int i = 0; i < sequenceCount; i++){
00285         for(int j = 0; j < boneCount; j++){
00286             // スケール
00287             data->setScale(i, j, readVectorInterpolator());
00288             // 回転
00289             data->setRotation(i, j, readRotationInterpolator());
00290             // 移動
00291             data->setTranslation(i, j, readVectorInterpolator());
00292         }
00293     }
00294     // ループフラグ
00295     for(int i = 0; i < sequenceCount; i++){
00296         data->setLooped(i, reader_->readBool());
00297     }
00298     // アライメント
00299     align();
00300 }
00301 //------------------------------------------------------------------------------
00302 // リンク
00303 //------------------------------------------------------------------------------
00304 // アニメーションセットリンクの読み込み
00305 void BinaryAnimationLoader::readAnimationSetLink(){
00306     // アニメーション検索
00307     String animationName = readString();
00308     AnimationSet* animation = NULL;
00309     Animation* searchResult = manager_->search(animationName);
00310     if(searchResult != NULL){ animation = searchResult->castAnimationSet(); }
00311     if(animation == NULL){
00312         ErrorOut("BinaryAnimationLoader::readAnimationSetLink() "
00313             "AnimationSet not found %s (%d)",
00314             animationName.getBytes(), reader_->getPosition());
00315     }
00316     // 子供検索
00317     int childCount = reader_->readInt();
00318     for(int i = 0; i < childCount; i++){
00319         String childName = readString();
00320         Animation* child = manager_->search(childName);
00321         if(child == NULL){
00322             ErrorOut("BinaryAnimationLoader::readAnimationSetLink() "
00323                 "Child animation not found %s (%d)",
00324                 childName.getBytes(), reader_->getPosition());
00325         }
00326         animation->addAnimation(child);
00327     }
00328     // アライメント
00329     align();
00330 }
00331 //------------------------------------------------------------------------------
00332 // カメラアニメーションリンクの読み込み
00333 void BinaryAnimationLoader::readCameraAnimationLink(){
00334     // アニメーション検索
00335     String animationName = readString();
00336     CameraAnimation* animation = NULL;
00337     Animation* animationSearchResult = manager_->search(animationName);
00338     if(animationSearchResult != NULL){
00339         animation = animationSearchResult->castCameraAnimation();
00340     }
00341     if(animation == NULL){
00342         ErrorOut("BinaryAnimationLoader::readCameraAnimationLink() "
00343             "CameraAnimation not found %s (%d)",
00344             animationName.getBytes(), reader_->getPosition());
00345     }
00346     // データ検索
00347     CameraAnimationData* data = NULL;
00348     String dataName = readString();
00349     AnimationData* dataSearchResult = manager_->searchData(dataName);
00350     if(dataSearchResult != NULL){
00351         data = dataSearchResult->castCameraAnimationData();
00352     }
00353     if(data == NULL){
00354         ErrorOut("BinaryAnimationLoader::readCameraAnimationLink() "
00355             "CameraAnimationData not found %s (%d)",
00356             dataName.getBytes(), reader_->getPosition());
00357     }
00358     // リンク
00359     animation->setCameraAnimationData(data);
00360     // アライメント
00361     align();
00362 }
00363 //------------------------------------------------------------------------------
00364 // シーンノードアニメーションリンクの読み込み
00365 void BinaryAnimationLoader::readSceneNodeAnimationLink(){
00366     // アニメーション検索
00367     String animationName = readString();
00368     SceneNodeAnimation* animation = NULL;
00369     Animation* animationSearchResult = manager_->search(animationName);
00370     if(animationSearchResult != NULL){
00371         animation = animationSearchResult->castSceneNodeAnimation();
00372     }
00373     if(animation == NULL){
00374         ErrorOut("BinaryAnimationLoader::readSceneNodeAnimationLink() "
00375             "SceneNodeAnimation not found %s (%d)",
00376             animationName.getBytes(), reader_->getPosition());
00377     }
00378     // データ検索
00379     SceneNodeAnimationData* data = NULL;
00380     String dataName = readString();
00381     AnimationData* dataSearchResult = manager_->searchData(dataName);
00382     if(dataSearchResult != NULL){
00383         data = dataSearchResult->castSceneNodeAnimationData();
00384     }
00385     if(data == NULL){
00386         ErrorOut("BinaryAnimationLoader::readSceneNodeAnimationLink() "
00387             "SceneNodeAnimationData not found %s (%d)",
00388             dataName.getBytes(), reader_->getPosition());
00389     }
00390     // リンク
00391     animation->setSceneNodeAnimationData(data);
00392     // アライメント
00393     align();
00394 }
00395 //------------------------------------------------------------------------------
00396 // キャラクタモデルアニメーションリンクの読み込み
00397 void BinaryAnimationLoader::readCharacterModelAnimationLink(){
00398     // アニメーション検索
00399     String animationName = readString();
00400     CharacterModelAnimation* animation = NULL;
00401     Animation* animationSearchResult = manager_->search(animationName);
00402     if(animationSearchResult != NULL){
00403         animation = animationSearchResult->castCharacterModelAnimation();
00404     }
00405     if(animation == NULL){
00406         ErrorOut("BinaryAnimationLoader::readCharacterModelAnimationLink() "
00407             "CharacterModelAnimation not found %s (%d)",
00408             animationName.getBytes(), reader_->getPosition());
00409     }
00410     // データ検索
00411     CharacterModelAnimationData* data = NULL;
00412     String dataName = readString();
00413     AnimationData* dataSearchResult = manager_->searchData(dataName);
00414     if(dataSearchResult != NULL){
00415         data = dataSearchResult->castCharacterModelAnimationData();
00416     }
00417     if(data == NULL){
00418         ErrorOut("BinaryAnimationLoader::readCharacterModelAnimationLink() "
00419             "CharacterModelAnimationData not found %s (%d)",
00420             dataName.getBytes(), reader_->getPosition());
00421     }
00422     // リンク
00423     animation->setCharacterModelAnimationData(data);
00424     // アライメント
00425     align();
00426 }
00427 //------------------------------------------------------------------------------
00428 // 値の読み込み
00429 //------------------------------------------------------------------------------
00430 // 文字列の読み込み
00431 String BinaryAnimationLoader::readString(){
00432     String result;
00433     const int stringBufferSize = 256;
00434     char stringBuffer[stringBufferSize];
00435     // サイズの読み込み
00436     int size = reader_->readInt();
00437     if(size > stringBufferSize){
00438         // サイズが大きいのでアロケートして文字列読み込み
00439         char* allocateBuffer = new char[size];
00440         reader_->readBytes(allocateBuffer, size);
00441         result = allocateBuffer;
00442         delete allocateBuffer;
00443     }else{
00444         // 文字列読み込み
00445         reader_->readBytes(stringBuffer, size);
00446         result = stringBuffer;
00447     }
00448     align();
00449     return result;
00450 }
00451 //------------------------------------------------------------------------------
00452 // 三次元ベクトルの読み込み
00453 Vector3 BinaryAnimationLoader::readVector3(){
00454     Vector3 result;
00455     reader_->readBytes(result.array, sizeof(Vector3));
00456     return result;
00457 }
00458 //------------------------------------------------------------------------------
00459 // 四元数の読み込み
00460 Quaternion BinaryAnimationLoader::readQuaternion(){
00461     Quaternion result;
00462     reader_->readBytes(result.array, sizeof(Quaternion));
00463     return result;
00464 }
00465 //------------------------------------------------------------------------------
00466 // ベクトル補間の読み込み
00467 //------------------------------------------------------------------------------
00468 // ベクトル補間の読み込み
00469 VectorInterpolator* BinaryAnimationLoader::readVectorInterpolator(){
00470     VectorInterpolator* result = NULL;
00471     int type = reader_->readInt();
00472     if(type == 0){
00473         result = NULL;
00474     }else if(type == 1){
00475         result = readVectorConstantInterpolator();
00476     }else if(type == 2){
00477         result = readVectorArrayInterpolator();
00478     }else if(type == 3){
00479         result = readVectorLinearInterpolator();
00480     }else{
00481         ErrorOut("BinaryAnimationLoader::readVectorInterpolator() "
00482             "Unsupported interpolater (%d)", reader_->getPosition());
00483     }
00484     return result;
00485 }
00486 //------------------------------------------------------------------------------
00487 // ベクトル定数補間の読み込み
00488 VectorInterpolator* BinaryAnimationLoader::readVectorConstantInterpolator(){
00489     VectorConstantInterpolator* interpolator = new VectorConstantInterpolator();
00490     // 長さ
00491     interpolator->setLength(reader_->readFloat());
00492     // 値
00493     interpolator->setValue(readVector3());
00494     return interpolator;
00495 }
00496 //------------------------------------------------------------------------------
00497 // ベクトル配列補間の読み込み
00498 VectorInterpolator* BinaryAnimationLoader::readVectorArrayInterpolator(){
00499     VectorArrayInterpolator* interpolator = new VectorArrayInterpolator();
00500     // 値
00501     int size = reader_->readInt();
00502     interpolator->setSize(size);
00503     for(int i = 0; i < size; i++){ interpolator->setValue(i, readVector3()); }
00504     return interpolator;
00505 }
00506 //------------------------------------------------------------------------------
00507 // ベクトル線形補間の読み込み
00508 VectorInterpolator* BinaryAnimationLoader::readVectorLinearInterpolator(){
00509     VectorLinearInterpolator* interpolator = new VectorLinearInterpolator();
00510     // 値
00511     int keyCount = reader_->readInt();
00512     interpolator->setKeyCount(keyCount);
00513     for(int i = 0; i < keyCount; i++){
00514         float time = reader_->readFloat();
00515         interpolator->setKey(i, time, readVector3());
00516     }
00517     return interpolator;
00518 }
00519 //------------------------------------------------------------------------------
00520 // 回転補間の読み込み
00521 //------------------------------------------------------------------------------
00522 // 回転補間の読み込み
00523 RotationInterpolator* BinaryAnimationLoader::readRotationInterpolator(){
00524     RotationInterpolator* result = NULL;
00525     int type = reader_->readInt();
00526     if(type == 0){
00527         result = NULL;
00528     }else if(type == 1){
00529         result = readRotationConstantInterpolator();
00530     }else if(type == 2){
00531         result = readEulerArrayInterpolator();
00532     }else if(type == 3){
00533         result = readQuaternionArrayInterpolator();
00534     }else if(type == 4){
00535         result = readQuaternionLinearInterpolator();
00536     }else{
00537         ErrorOut("BinaryAnimationLoader::readRotationInterpolator() "
00538             "Unsupported interpolater (%d)", reader_->getPosition());
00539     }
00540     return result;
00541 }
00542 //------------------------------------------------------------------------------
00543 // 回転定数補間の読み込み
00544 RotationInterpolator* BinaryAnimationLoader::readRotationConstantInterpolator(){
00545     RotationConstantInterpolator* interpolator =
00546         new RotationConstantInterpolator();
00547     // 長さ
00548     interpolator->setLength(reader_->readFloat());
00549     // 値
00550     interpolator->setQuaternion(readQuaternion());
00551     return interpolator;
00552 }
00553 //------------------------------------------------------------------------------
00554 // オイラー回転配列補間の読み込み
00555 RotationInterpolator* BinaryAnimationLoader::readEulerArrayInterpolator(){
00556     EulerArrayInterpolator* interpolator = new EulerArrayInterpolator();
00557     // 値
00558     int size = reader_->readInt();
00559     interpolator->setSize(size);
00560     for(int i = 0; i < size; i++){ interpolator->setValue(i, readVector3()); }
00561     return interpolator;
00562 }
00563 //------------------------------------------------------------------------------
00564 // 四元数回転配列補間の読み込み
00565 RotationInterpolator* BinaryAnimationLoader::readQuaternionArrayInterpolator(){
00566     QuaternionArrayInterpolator* interpolator =
00567         new QuaternionArrayInterpolator();
00568     // 値
00569     int size = reader_->readInt();
00570     interpolator->setSize(size);
00571     for(int i = 0; i < size; i++){
00572         interpolator->setValue(i, readQuaternion());
00573     }
00574     return interpolator;
00575 }
00576 //------------------------------------------------------------------------------
00577 // 四元数回転線形補間の読み込み
00578 RotationInterpolator* BinaryAnimationLoader::readQuaternionLinearInterpolator(){
00579     QuaternionLinearInterpolator* interpolator =
00580         new QuaternionLinearInterpolator();
00581     // 値
00582     int keyCount = reader_->readInt();
00583     interpolator->setKeyCount(keyCount);
00584     for(int i = 0; i < keyCount; i++){
00585         float time = reader_->readFloat();
00586         interpolator->setKey(i, time, readQuaternion());
00587     }
00588     return interpolator;
00589 }
00590 //------------------------------------------------------------------------------
00591 // ユーティリティ
00592 //------------------------------------------------------------------------------
00593 // アライメントを取る
00594 void BinaryAnimationLoader::align(){
00595     reader_->align(16);
00596 }
00597 //------------------------------------------------------------------------------
00598 } // End of namespace Lamp
00599 //------------------------------------------------------------------------------

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