Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <vlGraphics/GhostCameraManipulator.hpp>
00033 #include <vlCore/Time.hpp>
00034 #include <vlGraphics/Camera.hpp>
00035
00036 using namespace vl;
00037
00038
00039
00040
00041 GhostCameraManipulator::GhostCameraManipulator()
00042 {
00043 VL_DEBUG_SET_OBJECT_NAME()
00044 mRotationSpeed = 0.5;
00045 mMovementSpeed = 50.0f;
00046 mXDegrees = 0;
00047 mYDegrees = 0;
00048 mLastTime = 0;
00049 mPosition = vec3(0,0,0);
00050
00051 setKeysForward(Key_W);
00052 setKeysBackward(Key_S);
00053 setKeysLeft(Key_A);
00054 setKeysRight(Key_D);
00055 setKeysUp(Key_W, Key_Shift);
00056 setKeysDown(Key_S, Key_Shift);
00057 }
00058
00059 void GhostCameraManipulator::mouseMoveEvent(int x, int y)
00060 {
00061 if ( camera() == NULL )
00062 return;
00063
00064 VL_CHECK(openglContext());
00065
00066 int cx = (int)camera()->viewport()->center().x();
00067 int cy = openglContext()->framebuffer()->height() - camera()->viewport()->height()/2 - camera()->viewport()->y();
00068 mXDegrees -= (y - cy) * mRotationSpeed;
00069 mYDegrees -= (x - cx) * mRotationSpeed;
00070 openglContext()->ignoreNextMouseMoveEvent();
00071 openglContext()->setMousePosition(cx, cy);
00072 }
00073
00074 void GhostCameraManipulator::updateEvent()
00075 {
00076 if (camera() == NULL)
00077 return;
00078
00079 if (mLastTime == 0)
00080 {
00081 mLastTime = Time::currentTime();
00082 return;
00083 }
00084 real dt = Time::currentTime() - mLastTime;
00085 mLastTime = Time::currentTime();
00086
00087 mat4 m = mat4::getTranslation(mPosition);
00088 m *= mat4::getRotation( mYDegrees, vec3(0,1,0), mXDegrees, vec3(1,0,0) );
00089 camera()->setModelingMatrix(m);
00090
00091 vec3 direction;
00092 bool okmodifier;
00093 bool modifier = openglContext()->isKeyPressed( Key_Alt ) || openglContext()->isKeyPressed( Key_Ctrl ) || openglContext()->isKeyPressed( Key_Shift );
00094
00095 okmodifier = (mKeysLeft[1] == Key_None) ? !modifier : openglContext()->isKeyPressed( mKeysLeft[1] );
00096 if ( openglContext()->isKeyPressed(mKeysLeft[0]) && okmodifier )
00097 direction.x() = -1;
00098
00099 okmodifier = (mKeysRight[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysRight[1]);
00100 if ( openglContext()->isKeyPressed(mKeysRight[0]) && okmodifier )
00101 direction.x() = +1;
00102
00103 okmodifier = (mKeysBackward[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysBackward[1]);
00104 if ( openglContext()->isKeyPressed(mKeysBackward[0]) && okmodifier )
00105 direction.z() = -1;
00106
00107 okmodifier = (mKeysForward[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysForward[1]);
00108 if ( openglContext()->isKeyPressed(mKeysForward[0]) && okmodifier )
00109 direction.z() = +1;
00110
00111 okmodifier = (mKeysUp[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysUp[1]);
00112 if ( openglContext()->isKeyPressed(mKeysUp[0]) && okmodifier )
00113 direction.y() = +1;
00114
00115 okmodifier = (mKeysDown[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysDown[1]);
00116 if ( openglContext()->isKeyPressed(mKeysDown[0]) && okmodifier )
00117 direction.y() = -1;
00118
00119 vec3 dir;
00120 dir += camera()->modelingMatrix().getX() * direction.x();
00121 dir += camera()->modelingMatrix().getY() * direction.y();
00122 dir -= camera()->modelingMatrix().getZ() * direction.z();
00123 dir.normalize();
00124 mPosition += dir * (real)(dt * mMovementSpeed);
00125 }
00126
00127 void GhostCameraManipulator::setCamera(Camera* camera) { mCamera = camera; }
00128
00129 Camera* GhostCameraManipulator::camera() { return mCamera.get(); }
00130 const Camera* GhostCameraManipulator::camera() const { return mCamera.get(); }
00131
00132 void GhostCameraManipulator::enableEvent(bool enabled)
00133 {
00134 if (enabled)
00135 {
00136 if ( camera() == NULL )
00137 return;
00138
00139 setPosition( camera()->modelingMatrix().getT() );
00140 real x, y;
00141 camera()->modelingMatrix().getYXRotationAngles( y, x );
00142 setXDegrees(x);
00143 setYDegrees(y);
00144
00145 if (openglContext())
00146 openglContext()->setMouseVisible(false);
00147
00148 if ( openglContext() && openglContext()->framebuffer() )
00149 {
00150 int cx = (int)camera()->viewport()->center().x();
00151 int cy = openglContext()->framebuffer()->height() - camera()->viewport()->height() / 2 - camera()->viewport()->y();
00152 openglContext()->ignoreNextMouseMoveEvent();
00153 openglContext()->setMousePosition(cx, cy);
00154 }
00155
00156
00157 openglContext()->setContinuousUpdate(true);
00158 }
00159 }
00160