# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/GhostCameraManipulator.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    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 // GhostCameraManipulator
    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()->renderTarget()->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()->setInverseViewMatrix(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()->inverseViewMatrix().getX() * direction.x();
    00121   dir += camera()->inverseViewMatrix().getY() * direction.y();
    00122   dir -= camera()->inverseViewMatrix().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()->inverseViewMatrix().getT() );
    00140     Real x, y;
    00141     camera()->inverseViewMatrix().getYXRotationAngles( y, x );
    00142     setXDegrees(x);
    00143     setYDegrees(y);
    00144
    00145     if (openglContext())
    00146       openglContext()->setMouseVisible(false);
    00147
    00148     if ( openglContext() && openglContext()->renderTarget() )
    00149     {
    00150       int cx = (int)camera()->viewport()->center().x();
    00151       int cy = openglContext()->renderTarget()->height() - camera()->viewport()->height() / 2 - camera()->viewport()->y();
    00152       openglContext()->ignoreNextMouseMoveEvent();
    00153       openglContext()->setMousePosition(cx, cy);
    00154     }
    00155
    00156     // requires continuous update
    00157     openglContext()->setContinuousUpdate(true);
    00158   }
    00159 }
    00160 //-----------------------------------------------------------------------------

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:23.
Permission is granted to use this page to write and publish articles regarding Visualization Library.