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

# TranslationCamera.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 "System/stdafx.h"
    00026 #include "Translator/Camera/TranslationCamera.h"
    00027 #include "Translator/Camera/TranslationCameraManager.h"
    00028 #include "Translator/Model/TranslationModelManager.h"
    00029 #include "Translator/Animation/TranslationAnimationUtility.h"
    00030 #include "Graphics/Scene/Scene.h"
    00031 #include "Graphics/Camera/CameraManager.h"
    00032 #include "Graphics/Model/ModelManager.h"
    00033 #include "Animation/VectorInterpolator/VectorArrayInterpolator.h"
    00034 #include "Animation/RotationInterpolator/EulerArrayInterpolator.h"
    00035 #include "Animation/System/AnimationManager.h"
    00036 #include "Animation/System/AnimationSet.h"
    00037 #include "Animation/Camera/CameraAnimation.h"
    00038
    00039 namespace LampForMaya{
    00040
    00041 //------------------------------------------------------------------------------
    00042 // コンストラクタ
    00043 TranslationCamera::TranslationCamera(
    00044     const MDagPath& initializePath, const String& initializeName) :
    00045     dagPath_(initializePath), name_(initializeName),
    00046     rotationAnimation_(NULL), translationAnimation_(NULL),
    00047     hasAnimation_(false){
    00048     MStatus result;
    00049     // オブジェクトの取得
    00050     object_ = dagPath_.node(&result);
    00051     MayaStatusCheck(result);
    00052 }
    00053 //------------------------------------------------------------------------------
    00054 // デストラクタ
    00055 TranslationCamera::~TranslationCamera(){
    00056     SafeDelete(translationAnimation_);
    00057     SafeDelete(rotationAnimation_);
    00058 }
    00059 //------------------------------------------------------------------------------
    00060 // 分析
    00061 bool TranslationCamera::analyze(){
    00062     MStatus result;
    00063     String errorString;
    00064     MFnDagNode dagNode(dagPath_, &result);
    00065     MayaStatusCheck(result);
    00066
    00067     // トランスフォーム初期化
    00068     MFnTransform transform(dagPath_.transform(), &result);
    00069     MayaStatusCheck(result);
    00070
    00071     // スケールピボット移動値が0であるかチェック
    00072     MPoint scalePivotTrans =
    00073         transform.scalePivotTranslation(MSpace::kTransform, &result);
    00074     MayaStatusCheck(result);
    00075     if(!zeroCheck(scalePivotTrans)){
    00076         errorString.format("TranslationCamera::analyze() "
    00077             "scalePivotTranslationが0でない ( %8f, %8f, %8f ) %s",
    00078             scalePivotTrans.x, scalePivotTrans.y, scalePivotTrans.z,
    00079             name_.getBytes());
    00080         MayaErrorOut(errorString);
    00081         return false;
    00082     }
    00083
    00084     // 回転ピボット移動値が0であるかチェック
    00085     MPoint rotationPivotTrans =
    00086         transform.rotatePivotTranslation(MSpace::kTransform, &result);
    00087     MayaStatusCheck(result);
    00088     if(!zeroCheck(rotationPivotTrans)){
    00089         errorString.format("TranslationCamera::analyze() "
    00090             "rotationPivotTransが0でない ( %8f, %8f, %8f ) %s",
    00091             rotationPivotTrans.x, rotationPivotTrans.y,
    00092             rotationPivotTrans.z, name_.getBytes());
    00093         MayaErrorOut(errorString);
    00094         return false;
    00095     }
    00096
    00097     // シアーが0であるかチェック
    00098     double shearArray[3];
    00099     result = transform.getShear(shearArray);
    00100     MPoint shear(shearArray);
    00101     if(!zeroCheck(shear)){
    00102         errorString.format("TranslationCamera::analyze() "
    00103             "shearが0でない ( %8f, %8f, %8f ) %s",
    00104             shear.x, shear.y, shear.z, name_.getBytes());
    00105         MayaErrorOut(errorString);
    00106         return false;
    00107     }
    00108
    00109     // スケールピボットが0であるかチェック
    00110     MPoint scalePivot = transform.scalePivot(MSpace::kTransform, &result);
    00111     MayaStatusCheck(result);
    00112     if(!zeroCheck(scalePivot)){
    00113         errorString.format("TranslationCamera::analyze() "
    00114             "scalePivotが0でない ( %8f, %8f, %8f ) %s",
    00115             scalePivot.x, scalePivot.y, scalePivot.z,
    00116             name_.getBytes());
    00117         MayaErrorOut(errorString);
    00118         return false;
    00119     }
    00120
    00121     // 回転ピボットが0であるかチェック
    00122     MPoint rotationPivot = transform.rotatePivot(MSpace::kTransform, &result);
    00123     MayaStatusCheck(result);
    00124     if(!zeroCheck(rotationPivot)){
    00125         errorString.format("TranslationCamera::analyze() "
    00126             "rotationPivotTransが0でない ( %8f, %8f, %8f ) %s",
    00127             rotationPivot.x, rotationPivot.y,
    00128             rotationPivot.z, name_.getBytes());
    00129         MayaErrorOut(errorString);
    00130         return false;
    00131     }
    00132
    00133     // スケールが1であるかチェック
    00134     double scale[3];
    00135     result = transform.getScale(scale);
    00136     MayaStatusCheck(result);
    00137     if((scale[0] != 1.0) || (scale[1] != 1.0) || (scale[2] != 1.0)){
    00138         errorString.format("TranslationCamera::analyze() "
    00139             "scaleが1でない ( %8f, %8f, %8f ) %s",
    00140             (float)scale[0], (float)scale[1], (float)scale[2],
    00141             name_.getBytes());
    00142         MayaErrorOut(errorString);
    00143         return false;
    00144     }
    00145
    00146     // 回転
    00147     MEulerRotation rotation;
    00148     result = transform.getRotation(rotation);
    00149     MayaStatusCheck(result);
    00150     if(rotation.order != MEulerRotation::kXYZ){
    00151         errorString.format("TranslationCamera::analyze() "
    00152             "回転順序はXYZしかサポートしていません %s", name_.getBytes());
    00153         MayaErrorOut(errorString);
    00154         return false;
    00155     }
    00156     rotation_.set((float)rotation.x, (float)rotation.y, (float)rotation.z);
    00157
    00158     // 移動
    00159     MVector translation = transform.translation(MSpace::kTransform, &result);
    00160     MayaStatusCheck(result);
    00161     translation_.set(
    00162         (float)translation.x, (float)translation.y, (float)translation.z);
    00163
    00164 // nearClippingPlane , farClippingPlane
    00165 //angle of view = 2 * atan(apertureVertical * 25.4/(2*focal_length))
    00166
    00167 //  DebugOut("\n%s\n", name_.getBytes());
    00168 //  DebugOut("rotation %s\n", rotation_.toString().getBytes());
    00169 //  DebugOut("translation %s\n", translation_.toString().getBytes());
    00170     return true;
    00171 }
    00172 //------------------------------------------------------------------------------
    00173 // ゼロチェック
    00174 bool TranslationCamera::zeroCheck(const MPoint& point){
    00175     if( (Math::abs((float)point.x) > Math::epsilon) ||
    00176         (Math::abs((float)point.y) > Math::epsilon) ||
    00177         (Math::abs((float)point.z) > Math::epsilon)){
    00178         return false;
    00179     }
    00180     return true;
    00181 }
    00182 //------------------------------------------------------------------------------
    00183 // アニメーションの分析
    00184 bool TranslationCamera::analyzeAnimation(){
    00185     hasAnimation_ = false;
    00186     // シーケンスの分析、シーケンスが無ければアニメーション無し
    00187     if(!sequence_.analyze(object_)){ return true; }
    00188     int startTime = sequence_.getStartTime(0);
    00189     int endTime = sequence_.getEndTime(sequence_.getSequenceCount() - 1);
    00190     // 回転アニメーション
    00191     // LookAtは回転がLookAtノードに接続されているのでアニメーションカーブ取得できず
    00192     // アトリビュートをevaluteするような形に持っていきたい
    00193     rotationAnimation_ =
    00194         TranslationAnimationUtility::analyzeRotationAnimation(
    00195         dagPath_.transform(), "rotate", rotation_, startTime, endTime);
    00196     if(rotationAnimation_ != NULL){ hasAnimation_ = true; }
    00197     // 移動アニメーション
    00198     translationAnimation_ =
    00199         TranslationAnimationUtility::analyzeVectorAnimation(
    00200         dagPath_.transform(), "translate", translation_, startTime, endTime);
    00201     if(translationAnimation_ != NULL){ hasAnimation_ = true; }
    00202 //  DebugOut("\nanim %s\n", name_.getBytes());
    00203 //  DebugOut("%d  %d\n", startTime, endTime);
    00204 //  DebugOut("%x  %x\n", rotationAnimation_, translationAnimation_);
    00205     return true;
    00206 }
    00207 //------------------------------------------------------------------------------
    00208 // Lampへの変換
    00209 bool TranslationCamera::convertToLamp(Scene* scene){
    00210     CameraManager* cameraManager = scene->getCameraManager();
    00211     camera_ = cameraManager->createCamera(name_);
    00212     camera_->setTransformation(rotation_, translation_);
    00213     return true;
    00214 }
    00215 //------------------------------------------------------------------------------
    00216 // アニメーションの変換
    00217 bool TranslationCamera::convertAnimation(
    00218     AnimationManager* animationManager, AnimationSet* animationSet){
    00219     if(!hasAnimation_){ return true; }
    00220     if(!sequence_.hasSequence()){ return true; }
    00221     CameraAnimation* animation = animationManager->createCamera(name_);
    00222     if(animation->getName() != name_){
    00223         MayaErrorOut(String("TranslationCamera::convertAnimation() ") +
    00224             name_ + "の名前が重複しています ");
    00225         return false;
    00226     }
    00227     CameraAnimationData* data =
    00228         animationManager->createCameraData(name_);
    00229     if(data->getName() != name_){
    00230         MayaErrorOut(String("TranslationCamera::convertAnimation() ") +
    00231             name_ + "の名前が重複しています ");
    00232         return false;
    00233     }
    00234     // アニメーションの設定
    00235     animation->setTargetName(name_);
    00236     animation->setCameraAnimationData(data);
    00237     animationSet->addAnimation(animation);
    00238     // アニメーションデータの設定
    00239     int sequenceCount = sequence_.getSequenceCount();
    00240     data->setSequenceCount(sequenceCount);
    00241     for(int i = 0; i < sequenceCount; i++){
    00242         int startTime = sequence_.getStartTime(i);
    00243         int endTime = sequence_.getEndTime(i);
    00244         int size = endTime - startTime + 1;
    00245         // 回転アニメーションの準備
    00246         EulerArrayInterpolator* rotation = NULL;
    00247         if(rotationAnimation_ != NULL){
    00248             rotation = new EulerArrayInterpolator();
    00249             rotation->setSize(size);
    00250             for(int j = 0; j < size; j++){
    00251                 rotation->setValue(j,
    00252                     rotationAnimation_->getValue(startTime + j));
    00253             }
    00254         }
    00255         // 移動アニメーションの準備
    00256         VectorArrayInterpolator* translation = NULL;
    00257         if(translationAnimation_ != NULL){
    00258             translation = new VectorArrayInterpolator();
    00259             translation->setSize(size);
    00260             for(int j = 0; j < size; j++){
    00261                 translation->setValue(j,
    00262                     translationAnimation_->getValue(startTime + j));
    00263             }
    00264         }
    00265         // データの設定
    00266         if(rotation != NULL){ data->setRotation(i, rotation); }
    00267         if(translation != NULL){
    00268             data->setTranslation(i, translation);
    00269         }
    00270         data->setLooped(i, sequence_.isLooped(i));
    00271     }
    00272     return true;
    00273 }
    00274 //------------------------------------------------------------------------------
    00275 } // End of namespace LampForMaya
    00276 //------------------------------------------------------------------------------