Visualization Library 2.1.0

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
GhostCameraManipulator.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
33 #include <vlCore/Time.hpp>
34 #include <vlGraphics/Camera.hpp>
35 
36 using namespace vl;
37 
38 //-----------------------------------------------------------------------------
39 // GhostCameraManipulator
40 //-----------------------------------------------------------------------------
42 {
43  VL_DEBUG_SET_OBJECT_NAME()
44  mRotationSpeed = 0.5;
45  mMovementSpeed = 50.0f;
46  mXDegrees = 0;
47  mYDegrees = 0;
48  mLastTime = 0;
49  mPosition = vec3(0,0,0);
50 
57 }
58 //-----------------------------------------------------------------------------
60 {
61  if ( camera() == NULL )
62  return;
63 
65 
66  int cx = (int)camera()->viewport()->center().x();
67  int cy = openglContext()->framebuffer()->height() - camera()->viewport()->height()/2 - camera()->viewport()->y();
68  mXDegrees -= (y - cy) * mRotationSpeed;
69  mYDegrees -= (x - cx) * mRotationSpeed;
72 }
73 //-----------------------------------------------------------------------------
75 {
76  if (camera() == NULL)
77  return;
78 
79  if (mLastTime == 0)
80  {
82  return;
83  }
84  real dt = Time::currentTime() - mLastTime;
86 
88  m *= mat4::getRotation( mYDegrees, vec3(0,1,0), mXDegrees, vec3(1,0,0) );
90 
91  vec3 direction;
92  bool okmodifier;
94 
95  okmodifier = (mKeysLeft[1] == Key_None) ? !modifier : openglContext()->isKeyPressed( mKeysLeft[1] );
96  if ( openglContext()->isKeyPressed(mKeysLeft[0]) && okmodifier )
97  direction.x() = -1;
98 
99  okmodifier = (mKeysRight[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysRight[1]);
100  if ( openglContext()->isKeyPressed(mKeysRight[0]) && okmodifier )
101  direction.x() = +1;
102 
103  okmodifier = (mKeysBackward[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysBackward[1]);
104  if ( openglContext()->isKeyPressed(mKeysBackward[0]) && okmodifier )
105  direction.z() = -1;
106 
107  okmodifier = (mKeysForward[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysForward[1]);
108  if ( openglContext()->isKeyPressed(mKeysForward[0]) && okmodifier )
109  direction.z() = +1;
110 
111  okmodifier = (mKeysUp[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysUp[1]);
112  if ( openglContext()->isKeyPressed(mKeysUp[0]) && okmodifier )
113  direction.y() = +1;
114 
115  okmodifier = (mKeysDown[1] == Key_None) ? !modifier : openglContext()->isKeyPressed(mKeysDown[1]);
116  if ( openglContext()->isKeyPressed(mKeysDown[0]) && okmodifier )
117  direction.y() = -1;
118 
119  vec3 dir;
120  dir += camera()->modelingMatrix().getX() * direction.x();
121  dir += camera()->modelingMatrix().getY() * direction.y();
122  dir -= camera()->modelingMatrix().getZ() * direction.z();
123  dir.normalize();
124  mPosition += dir * (real)(dt * mMovementSpeed);
125 }
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
130 const Camera* GhostCameraManipulator::camera() const { return mCamera.get(); }
131 //-----------------------------------------------------------------------------
133 {
134  if (enabled)
135  {
136  if ( camera() == NULL )
137  return;
138 
139  setPosition( camera()->modelingMatrix().getT() );
140  real x, y;
142  setXDegrees(x);
143  setYDegrees(y);
144 
145  if (openglContext())
146  openglContext()->setMouseVisible(false);
147 
148  if ( openglContext() && openglContext()->framebuffer() )
149  {
150  int cx = (int)camera()->viewport()->center().x();
151  int cy = openglContext()->framebuffer()->height() - camera()->viewport()->height() / 2 - camera()->viewport()->y();
153  openglContext()->setMousePosition(cx, cy);
154  }
155 
156  // requires continuous update
158  }
159 }
160 //-----------------------------------------------------------------------------
void setKeysBackward(EKey key, EKey modifier=Key_None)
Key bindings to move backward (default = Key_S).
int y() const
Definition: Viewport.hpp:67
const Vector3 & normalize(T_Scalar *len=NULL)
Definition: Vector3.hpp:228
void setKeysUp(EKey key, EKey modifier=Key_None)
Key bindings to move up (default = Key_W + Key_Shift).
Vector3< T_Scalar > getZ() const
Definition: Matrix4.hpp:131
void ignoreNextMouseMoveEvent()
Requests not to dispatch the next mouse move event.
const T_Scalar & z() const
Definition: Vector3.hpp:92
Vector3< T_Scalar > getY() const
Definition: Matrix4.hpp:126
void setKeysLeft(EKey key, EKey modifier=Key_None)
Key bindings to move left (default = Key_A).
void setKeysDown(EKey key, EKey modifier=Key_None)
Key bindings to move down (default = Key_S + Key_Shift).
FramebufferObject * framebuffer()
The default render target (always returns leftFramebuffer()).
fvec2 center() const
Definition: Viewport.hpp:72
Viewport * viewport()
The viewport bound to a camera.
Definition: Camera.hpp:140
Visualization Library main namespace.
Vector3< T_Scalar > getX() const
Definition: Matrix4.hpp:121
int height() const
Definition: Viewport.hpp:71
virtual void setContinuousUpdate(bool continuous)
If the OpenGL context is a widget this function sets whether its area is continuously updated at each...
virtual void mouseMoveEvent(int x, int y)
Event generated when the mouse moves.
virtual void setMousePosition(int, int)
If the OpenGL context is a widget this function sets the mouse position.
const mat4 & modelingMatrix() const
Returns the Camera&#39;s modelingMatrix() (inverse of the view matrix).
Definition: Camera.hpp:168
const T_Scalar & y() const
Definition: Vector3.hpp:91
void enableEvent(bool enabled)
Event generated whenever setEnabled() is called.
void setCamera(Camera *camera)
The camera to be manipulated.
void setModelingMatrix(const mat4 &mat)
Sets the Camera&#39;s modelingMatrix() (inverse of the view matrix).
Definition: Camera.hpp:164
#define NULL
Definition: OpenGLDefs.hpp:81
void getYXRotationAngles(T_Scalar &degrees_y, T_Scalar &degrees_x) const
If this matrix can be represented as RY(degrees_y) * RX(degrees_x), where RX and RY are getRotation m...
Definition: Matrix4.hpp:1204
static Matrix4 & getRotation(Matrix4 &out, float degrees, float x, float y, float z)
Definition: Matrix4.hpp:909
bool isKeyPressed(EKey key) const
Returns true if the given key is pressed.
void setKeysForward(EKey key, EKey modifier=Key_None)
Key bindings to move forward (default = Key_W).
fvec3 vec3
Defined as: &#39;typedef fvec3 vec3&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector3.hpp:270
Camera * camera()
The camera to be manipulated.
static Matrix4 & getTranslation(Matrix4 &out, const Vector3< float > &v)
Definition: Matrix4.hpp:553
static real currentTime()
Seconds passed from an arbitrary origin QueryPerformanceFrequency should be called only once in the a...
Definition: Time.cpp:114
void setKeysRight(EKey key, EKey modifier=Key_None)
Key bindings to move right (default = Key_D).
const T_Scalar & x() const
Definition: Vector3.hpp:90
const T_Scalar & x() const
Definition: Vector2.hpp:133
OpenGLContext * openglContext()
Returns the OpenGLContext to which this UIEventListener is bound or NULL if no context is bound...
virtual void updateEvent()
Event generated when the bound OpenGLContext does not have any other message to process and OpenGLCon...
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:49
virtual void setMouseVisible(bool)
If the OpenGL context is a widget this function sets whether the mouse is visible over it or not...
#define VL_CHECK(expr)
Definition: checks.hpp:73
int height() const
The height of a render target.