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

FirstPersonCameraController.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 "Framework/Utility/FirstPersonCameraController.h"
00027 #include "Graphics/Camera/Camera.h"
00028 
00029 namespace Lamp{
00030 
00031 //------------------------------------------------------------------------------
00032 // コンストラクタ
00033 FirstPersonCameraController::FirstPersonCameraController(){
00034     camera_ = NULL;
00035     cameraPosition_ = Vector3::zero;
00036     cameraRotation_ = Vector3::zero;
00037     fovY_ = 60.f;
00038     cameraPositionSensibility_ = 0.05f;
00039     cameraRotationSensibility_ = 0.005f;
00040     keyboardSensibility_ = 25.f;
00041     mouseXPosition_ = 0;
00042     mouseYPosition_ = 0;
00043 }
00044 //------------------------------------------------------------------------------
00045 // デストラクタ
00046 FirstPersonCameraController::~FirstPersonCameraController(){
00047 }
00048 //------------------------------------------------------------------------------
00049 // セットアップ
00050 void FirstPersonCameraController::setup(
00051     float nearClip, float farClip, float aspect){
00052     if(camera_ == NULL){ return; }
00053     camera_->setPerspectiveFovY(
00054         Math::toRadian(fovY_), aspect, nearClip, farClip);
00055     camera_->setTransformation(cameraRotation_, cameraPosition_);
00056     informationString_.format(
00057         "pos { %.1f, %.1f, %.1f } rot { %.1f, %.1f, %.1f } "
00058         "near %.1f far %.1f fovY %.1f aspect %.2f",
00059         cameraPosition_.x, cameraPosition_.y, cameraPosition_.z,
00060         cameraRotation_.x, cameraRotation_.y, cameraRotation_.z,
00061         nearClip, farClip, fovY_, aspect);
00062 }
00063 //------------------------------------------------------------------------------
00064 // ウィンドウプロシージャ
00065 LRESULT FirstPersonCameraController::windowProcedure(
00066     HWND windowHandle, u_int message, WPARAM wParam, LPARAM lParam){
00067     if(camera_ == NULL){ return 0; }
00068     bool cameraChanged = false;
00069     Vector3 cameraAddRotation(0.f, 0.f, 0.f);
00070     Vector3 cameraAddTranslation(0.f, 0.f, 0.f);
00071     switch(message){
00072     // キー入力に対応
00073     case WM_KEYDOWN:
00074         switch(wParam){
00075         case 'F':
00076             cameraAddTranslation.x += cameraPositionSensibility_;
00077             cameraChanged = true;
00078             break;
00079         case 'S':
00080             cameraAddTranslation.x -= cameraPositionSensibility_;
00081             cameraChanged = true;
00082             break;
00083         case 'R':
00084             cameraAddTranslation.y += cameraPositionSensibility_;
00085             cameraChanged = true;
00086             break;
00087         case 'V':
00088             cameraAddTranslation.y -= cameraPositionSensibility_;
00089             cameraChanged = true;
00090             break;
00091         case 'D':
00092             cameraAddTranslation.z += cameraPositionSensibility_;
00093             cameraChanged = true;
00094             break;
00095         case 'E':
00096             cameraAddTranslation.z -= cameraPositionSensibility_;
00097             cameraChanged = true;
00098             break;
00099         }
00100         if(cameraChanged){
00101             cameraAddTranslation *= keyboardSensibility_;
00102             if(::GetKeyState(VK_SHIFT) < 0){ cameraAddTranslation *= 0.1f; }
00103             Matrix33 rotationMatrix;
00104             rotationMatrix.setRotationXYZ(cameraRotation_);
00105             cameraAddTranslation = rotationMatrix * cameraAddTranslation;
00106             cameraPosition_ += cameraAddTranslation;
00107         }
00108         break;
00109     case WM_MOUSEWHEEL:
00110         {
00111             int wheelMove = (int)(((short)HIWORD(wParam)) / WHEEL_DELTA);
00112             fovY_ += wheelMove * -3.f;
00113             if(fovY_ > 179.f){ fovY_ = 179.f; }
00114             else if(fovY_ < 1.f){ fovY_ = 1.f; }
00115             break;
00116         }
00117     case WM_MOUSEMOVE:
00118         // マウス移動値取得
00119         u_int xPosition = LOWORD(lParam);
00120         u_int yPosition = HIWORD(lParam);
00121         int xPositionDistance = xPosition - mouseXPosition_;
00122         int yPositionDistance = yPosition - mouseYPosition_;
00123         if((xPositionDistance == 0) && (yPositionDistance == 0)){ break; }
00124         // カメラパラメータ変更値の算出
00125         bool shift = ((wParam & MK_SHIFT) != 0);
00126         if((wParam & MK_LBUTTON) != 0){
00127             float xDistance = xPositionDistance * cameraRotationSensibility_;
00128             float yDistance = yPositionDistance * cameraRotationSensibility_;
00129             cameraAddRotation.x += -yDistance;
00130             cameraAddRotation.y += -xDistance;
00131             cameraChanged = true;
00132         }else if((wParam & MK_RBUTTON) != 0){
00133             float xDistance = xPositionDistance * cameraPositionSensibility_;
00134             float yDistance = yPositionDistance * cameraPositionSensibility_;
00135             if(shift){
00136                 cameraAddTranslation.x += xDistance;
00137                 cameraAddTranslation.z += yDistance;
00138             }else{
00139                 cameraAddTranslation.x += xDistance;
00140                 cameraAddTranslation.y += -yDistance;
00141             }
00142             cameraChanged = true;
00143         }
00144         mouseXPosition_ = xPosition;
00145         mouseYPosition_ = yPosition;
00146         if(cameraChanged){
00147             // カメラパラメータの変更
00148             cameraRotation_ += cameraAddRotation;
00149             Matrix33 rotationMatrix;
00150             rotationMatrix.setRotationXYZ(cameraRotation_);
00151             cameraAddTranslation = rotationMatrix * cameraAddTranslation;
00152             cameraPosition_ += cameraAddTranslation;
00153         }
00154         break;
00155     }
00156     return 0;
00157 }
00158 //------------------------------------------------------------------------------
00159 } // End of namespace Lamp
00160 //------------------------------------------------------------------------------

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