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

Camera.h

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 #ifndef CAMERA_H_
00026 #define CAMERA_H_
00027 
00028 #include <Graphics/Scene/SceneObject.h>
00029 #include <Graphics/Renderer/Clipping.h>
00030 
00031 namespace Lamp{
00032 
00033 //------------------------------------------------------------------------------
00034 /**
00035  * カメラ
00036  */
00037 class Camera : public SceneObject{
00038 friend class SceneObjectManagerTemplate<Camera>;
00039 friend class CameraManager;
00040 public:
00041     /**
00042      * リファレンスカウントの取得
00043      * @return リファレンスカウント
00044      */
00045     virtual int getReferenceCount() const{ return 0; }
00046 
00047     /**
00048      * コピー
00049      * @return コピーされたカメラ
00050      */
00051     virtual Camera* copy() const;
00052 
00053     //--------------------------------------------------------------------------
00054     // 射影行列
00055     //--------------------------------------------------------------------------
00056     /**
00057      * 射影行列の取得
00058      * @return 射影行列
00059      */
00060     virtual const Matrix44& getProjectionMatrix(){ return projectionMatrix_; }
00061 
00062     //--------------------------------------------------------------------------
00063     /**
00064      * 中心のずれたパースペクティブ設定
00065      * @param left 最小X値
00066      * @param right 最大X値
00067      * @param bottom 最小Y値
00068      * @param top 最大Y値
00069      * @param nearClip 最小Z値
00070      * @param farClip 最大Z値
00071      */
00072     virtual void setPerspectiveOffCenter(
00073         float left, float right, float bottom, float top,
00074         float nearClip, float farClip){
00075         left_ = left;
00076         right_  = right;
00077         bottom_ = bottom;
00078         top_ = top;
00079         nearClip_ = nearClip;
00080         farClip_ = farClip;
00081         isPerspective_ = true;
00082         buildPerspectiveMatrix();
00083     }
00084 
00085     /**
00086      * パースペクティブ設定
00087      * @param width 幅
00088      * @param height 高さ
00089      * @param nearClip 最小Z値
00090      * @param farClip 最大Z値
00091      */
00092     virtual void setPerspective(
00093         float width, float height, float nearClip, float farClip){
00094         left_ = -width * 0.5f;
00095         right_  = -left_;
00096         bottom_ = -height * 0.5f;
00097         top_ = -bottom_;
00098         nearClip_ = nearClip;
00099         farClip_ = farClip;
00100         isPerspective_ = true;
00101         buildPerspectiveMatrix();
00102     }
00103 
00104     /**
00105      * 画角によるパースペクティブ設定
00106      * @param fovY Y軸方向の画角
00107      * @param aspect アスペクト比(width / height)
00108      * @param nearClip 最小Z値
00109      * @param farClip 最大Z値
00110      */
00111     virtual void setPerspectiveFovY(
00112         float fovY, float aspect, float nearClip, float farClip){
00113         bottom_ = -nearClip / (1.f / Math::tan(fovY * 0.5f));
00114         top_ = -bottom_;
00115         left_ = bottom_ * aspect;
00116         right_  = -left_;
00117         nearClip_ = nearClip;
00118         farClip_ = farClip;
00119         isPerspective_ = true;
00120         buildPerspectiveMatrix();
00121     }
00122 
00123     /**
00124      * 中心のずれた並行投影設定
00125      * @param left 最小X値
00126      * @param right 最大X値
00127      * @param bottom 最小Y値
00128      * @param top 最大Y値
00129      * @param nearClip 最小Z値
00130      * @param farClip 最大Z値
00131      */
00132     virtual void setOrthoOffCenter(
00133         float left, float right, float bottom, float top,
00134         float nearClip, float farClip){
00135         left_ = left;
00136         right_  = right;
00137         bottom_ = bottom;
00138         top_ = top;
00139         nearClip_ = nearClip;
00140         farClip_ = farClip;
00141         isPerspective_ = false;
00142         buildPerspectiveMatrix();
00143     }
00144 
00145     /**
00146      * 並行投影設定
00147      * @param width 幅
00148      * @param height 高さ
00149      * @param nearClip 最小Z値
00150      * @param farClip 最大Z値
00151      */
00152     virtual void setOrtho(
00153         float width, float height, float nearClip, float farClip){
00154         left_ = -width * 0.5f;
00155         right_  = -left_;
00156         bottom_ = -height * 0.5f;
00157         top_ = -bottom_;
00158         nearClip_ = nearClip;
00159         farClip_ = farClip;
00160         isPerspective_ = false;
00161         buildPerspectiveMatrix();
00162     }
00163 
00164     //--------------------------------------------------------------------------
00165     /**
00166      * 左の取得
00167      * @return 左
00168      */
00169     virtual float getLeft(){ return left_; }
00170 
00171     /**
00172      * 右の取得
00173      * @return 右
00174      */
00175     virtual float getRight(){ return right_; }
00176 
00177     /**
00178      * 下の取得
00179      * @return 下
00180      */
00181     virtual float getBottom(){ return bottom_; }
00182 
00183     /**
00184      * 上の取得
00185      * @return 上
00186      */
00187     virtual float getTop(){ return top_; }
00188 
00189     //--------------------------------------------------------------------------
00190     /**
00191      * ニアクリップの取得
00192      * @return ニアクリップ
00193      */
00194     virtual float getNearClip(){ return nearClip_; }
00195 
00196     /**
00197      * ニアクリップの設定
00198      * @param nearClip ニアクリップ
00199      */
00200     virtual void setNearClip(float nearClip){
00201         if(isPerspective_){
00202             setPerspectiveOffCenter(getLeft(), getRight(),
00203                 getBottom(), getTop(), nearClip, getFarClip());
00204         }else{
00205             setOrthoOffCenter(getLeft(), getRight(),
00206                 getBottom(), getTop(), nearClip, getFarClip());
00207         }
00208     }
00209 
00210     //--------------------------------------------------------------------------
00211     /**
00212      * ファークリップの取得
00213      * @return ファークリップ
00214      */
00215     virtual float getFarClip(){ return farClip_; }
00216 
00217     /**
00218      * ファークリップの設定
00219      * @param farClip ファークリップ
00220      */
00221     virtual void setFarClip(float farClip){
00222         if(isPerspective_){
00223             setPerspectiveOffCenter(getLeft(), getRight(),
00224                 getBottom(), getTop(), getNearClip(), farClip);
00225         }else{
00226             setOrthoOffCenter(getLeft(), getRight(),
00227                 getBottom(), getTop(), getNearClip(), farClip);
00228         }
00229     }
00230 
00231     //--------------------------------------------------------------------------
00232     /**
00233      * パースペクティブかどうか
00234      * @return パースペクティブならtrue
00235      */
00236     virtual bool isPerspective(){ return isPerspective_; }
00237 
00238     //--------------------------------------------------------------------------
00239     /**
00240      * 幅の取得
00241      * @return 幅
00242      */
00243     virtual float getWidth(){ return right_ - left_; }
00244 
00245     /**
00246      * 高さの取得
00247      * @return 高さ
00248      */
00249     virtual float getHeight(){ return top_ - bottom_; }
00250 
00251     //--------------------------------------------------------------------------
00252     /**
00253      * 画角の取得
00254      * @return 画角
00255      */
00256     virtual float getFovY(){
00257         if(!isPerspective_){ return 0.f; }
00258         return 2.f * Math::atan(1.f / (2.f * nearClip_ / (top_ - bottom_)));
00259     }
00260 
00261     /**
00262      * 画角の設定
00263      * @param fovY 画角
00264      */
00265     virtual void setFovY(float fovY){
00266 // OffCenterで設定されているとおかしくなる
00267         Assert(isPerspective_);
00268         setPerspectiveFovY(fovY, getAspect(), getNearClip(), getFarClip());
00269     }
00270 
00271     //--------------------------------------------------------------------------
00272     /**
00273      * アスペクト比の取得
00274      * @return アスペクト比
00275      */
00276     virtual float getAspect(){ return (right_ - left_) / (top_ - bottom_); }
00277 
00278     /**
00279      * アスペクト比の設定
00280      * @param aspect アスペクト比
00281      */
00282     virtual void setAspect(float aspect){
00283 // OffCenterで設定されているとおかしくなる
00284         Assert(isPerspective_);
00285         setPerspectiveFovY(getFovY(), aspect, getNearClip(), getFarClip());
00286     }
00287 
00288     //--------------------------------------------------------------------------
00289     // ビュー行列
00290     //--------------------------------------------------------------------------
00291     /**
00292      * ビュー行列の取得
00293      * @return ビュー行列
00294      */
00295     virtual const Matrix44& getViewMatrix(){ return viewMatrix_; }
00296 
00297     /**
00298      * カメラ位置の取得
00299      * @return カメラ位置
00300      */
00301     virtual const Vector3& getPosition(){ return position_; }
00302 
00303     /**
00304      * カメラ回転の取得
00305      * @return カメラ回転
00306      */
00307     virtual const Vector3& getRotation(){ return rotation_; }
00308 
00309     //--------------------------------------------------------------------------
00310     /**
00311      * ビュー行列の設定
00312      * @param viewMatrix ビュー行列
00313      */
00314     virtual void setViewMatrix(const Matrix44& viewMatrix);
00315 
00316     /**
00317      * トランスフォーメーションの設定
00318      * @param rotationXYZ ビューのXYZ回転
00319      * @param position ビューの位置
00320      */
00321     virtual void setTransformation(
00322         const Vector3& rotationXYZ, const Vector3& position);
00323 
00324     /**
00325      * トランスフォーメーションの設定
00326      * @param rotation ビューの四元数回転
00327      * @param position ビューの位置
00328      */
00329     virtual void setTransformation(
00330         const Quaternion& rotation, const Vector3& position);
00331 
00332     /**
00333      * ルックアットの設定
00334      * @param position ビュー位置
00335      * @param target 目標
00336      * @param up 上ベクトル
00337      */
00338     virtual void setLookAt(
00339         const Vector3& position, const Vector3& target, const Vector3& up);
00340 
00341     //--------------------------------------------------------------------------
00342     // クリップ関係
00343     //--------------------------------------------------------------------------
00344     /**
00345      * クリッピング
00346      * @param boundingSphere バウンディングスフィア
00347      * @return クリッピングステート
00348      */
00349     virtual Clipping::State clipping(const Sphere& boundingSphere);
00350 
00351     /**
00352      * クリッピング
00353      * @param boundingBox バウンディングボックス
00354      * @return クリッピングステート
00355      */
00356     virtual Clipping::State clipping(const AxisAlignedBox& boundingBox);
00357 
00358     /**
00359      * コーナーの取得
00360      *
00361      * 以下の図のインデックスに沿ってコーナーを取得します。
00362      * <pre>
00363      *    y+
00364      *    |  5----4
00365      *    | /|   /|
00366      *    |1-+--2 |
00367      *    || 6--+-7
00368      *    ||/   |/
00369      *    |0----3
00370      *    +-------- x+
00371      *   /
00372      *  z+
00373      * </pre>
00374      * @param index コーナーインデックス
00375      * @return コーナー
00376      */
00377     virtual const Vector3& getCorner(int index){
00378         Assert(index >= 0);
00379         Assert(index < 8);
00380         return corner_[index];
00381     }
00382 
00383     /**
00384      * バウンディングスフィアの取得
00385      * @return バウンディングスフィア
00386      */
00387     virtual const Sphere& getBoundingSphere(){ return boundingSphere_; }
00388 
00389     //--------------------------------------------------------------------------
00390     // RTTI
00391     //--------------------------------------------------------------------------
00392     /**
00393      * カメラかどうか
00394      * @return カメラならtrue
00395      */
00396     virtual bool isCamera() const{ return true; }
00397 
00398     //--------------------------------------------------------------------------
00399 protected:
00400     /**
00401      * コンストラクタ
00402      * @param name 名前
00403      * @param scene シーン
00404      */
00405     Camera(const String& name, Scene* scene);
00406 
00407     /**
00408      * デストラクタ
00409      */
00410     virtual ~Camera();
00411 
00412     //--------------------------------------------------------------------------
00413     /**
00414      * 射影行列の構築
00415      */
00416     virtual void buildPerspectiveMatrix();
00417 
00418     /**
00419      * クリッピングのセットアップ
00420      */
00421     virtual void clippingSetup();
00422 
00423     //--------------------------------------------------------------------------
00424 private:
00425     // 射影行列
00426     Matrix44 projectionMatrix_;
00427     // ビュー行列
00428     Matrix44 viewMatrix_;
00429     // クリップ面
00430     Plane clipPlane_[6];
00431     // バウンディングスフィア
00432     Sphere boundingSphere_;
00433     // コーナー
00434     Vector3 corner_[8];
00435     // 位置
00436     Vector3 position_;
00437     // 回転
00438     Vector3 rotation_;
00439 
00440     // 左
00441     float left_;
00442     // 右
00443     float right_;
00444     // 下
00445     float bottom_;
00446     // 上
00447     float top_;
00448     // ニアクリップ
00449     float nearClip_;
00450     // ファークリップ
00451     float farClip_;
00452     // パースペクティブかどうか
00453     bool isPerspective_;
00454 };
00455 
00456 //------------------------------------------------------------------------------
00457 } // End of namespace Lamp
00458 #endif // End of CAMERA_H_
00459 //------------------------------------------------------------------------------

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