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]
FlatManipulator.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 
34 
35 using namespace vl;
36 
38  mTranslationButton(MiddleButton), mZoomButton(RightButton),
39  mZoomSpeed(1.0f)
40  {
41  VL_DEBUG_SET_OBJECT_NAME()
42  }
43 
44 //-----------------------------------------------------------------------------
46 {
47  mCamera = camera;
48 }
49 //-----------------------------------------------------------------------------
51 {
52  if ( camera() == NULL )
53  return;
54 
55  // if already busy ignore the event, wadda heck!
56  if (mode() != NoMode)
57  return;
58 
59  //if the rodent is out of cage, kisses and bye-bye
60  int vx, vy;
61  if(mouseInViewport(x, y, vx, vy) == false)
62  return;
63  //store the rodent's position in the cage
65 
66  // enter new mode
67  if (btn == translationButton())
68  {//we need world pixel size only in translation mode
70  //now let's find the pixel size, assuming an orthographic projection
71  mPixelSize.x() = vec2::scalar_type( 2.0 / camera()->projectionMatrix().e( 0, 0 ) / camera()->viewport()->width() );
72  mPixelSize.y() = vec2::scalar_type( 2.0 / camera()->projectionMatrix().e( 1, 1 ) / camera()->viewport()->height() );
73  }
74  else
75  if (btn == zoomButton())
76  {
77  mMode = ZoomMode;
78  }
79 
80  VL_CHECK(openglContext()->framebuffer())
81 
82 }
83 //-----------------------------------------------------------------------------
85 {
86  if ( camera() == NULL )
87  return;
88 
89  // ignore the event if the manipulator is not in any mode
90  if (mode() == NoMode)
91  return;
92 
93  VL_CHECK(openglContext()->framebuffer())
94 
95  if (mode() == ZoomMode)
96  {
97  float scale = 1.0f - ( y - mMouseStart.y() ) * zoomSpeed() / 100.0f;
99  mat4::getScaling(scale, scale,1.0)*
100  camera()->viewMatrix());
102  }
103  else
104  if (mode() == TranslationMode)
105  {
108  mat4::getTranslation(shift.x(), -shift.y(),0)*
109  camera()->viewMatrix());
111  }
112 
113  // update the view
114  openglContext()->update();
115 }
116 //-----------------------------------------------------------------------------
117 void FlatManipulator::mouseUpEvent(EMouseButton btn, int /*x*/, int /*y*/)
118 {
119  if ( camera() == NULL )
120  return;
121 
122  // if the manipulator is not doing anything ignore the event
123  if (mode() == NoMode)
124  return;
125 
126  // leave the mode
127  if (btn == translationButton() && mMode == TranslationMode)
128  mMode = NoMode;
129  else
130  if (btn == zoomButton() && mMode == ZoomMode)
131  mMode = NoMode;
132 }
133 //-----------------------------------------------------------------------------
135 {
136  if (enabled)
137  {
138  mMode = NoMode;
139  if ( openglContext() )
140  {
143  }
144  }
145 }
146 //-----------------------------------------------------------------------------
147 bool FlatManipulator::mouseInViewport(int mx, int my, int& vx, int& vy)
148 {
149  vx = mx - camera()->viewport()->x();
150  vy = openglContext()->height() - my - camera()->viewport()->y();
151 
152  //if outside camera's viewport, return false
153  if (vx<0 || vy<0 ||
154  vx >= camera()->viewport()->width() ||
155  vy >= camera()->viewport()->height())
156  {
157  return false;
158  }
159  return true;
160 }
161 //-----------------------------------------------------------------------------
162 vl::ref<vl::Geometry> makeScales(bool X, bool Y, bool Z, int numArmTicks, float mmStep, float mmTickSize)
163 {
164  vl::ref<vl::Geometry> scales = new vl::Geometry();
165  int numRulers = (X?1:0)+(Y?1:0)+(Z?1:0);
166  if(numRulers == 0) return scales;
167 
169  vl::ref<vl::ArrayFloat3> scalesPoints(new vl::ArrayFloat3());
170 
171  //the length of one arm of scales
172  const float mmArmLength = numArmTicks*mmStep;
173 
174  //calculate the number of points, having marks at every 10 mm
175  int numAxisPoints = ( 2*numArmTicks + 1)*2;
176 
177  //resize the array to accomodate a ruler
178  points->resize(numAxisPoints);
179  //room for up to three orthogonal rulers
180  scalesPoints->resize(numRulers*points->size());
181 
182  float xpos = mmStep;
183  int t;
184  //generate the X ruler
185  for(t = 0; t < numArmTicks; t++)
186  {
187  float height = mmTickSize;
188 
189  if((t+1)%10 == 0)//takes priority over '5'
190  height *= 2;
191  else
192  if((t+1)%5 == 0)
193  height *= 1.5;
194 
195  points->at(4*t) = vl::fvec3(-xpos, -height,0);
196  points->at(4*t+1) = vl::fvec3(-xpos, +height,0);
197  points->at(4*t+2) = vl::fvec3(xpos, -height,0);
198  points->at(4*t+3) = vl::fvec3(xpos, +height,0);
199 
200  xpos += mmStep;
201  }
202  //now add the axis line
203  points->at(4*t) = vl::fvec3(-mmArmLength, 0.0,0);
204  points->at(4*t+1) = vl::fvec3(mmArmLength, 0.0,0);
205 
206  int i=0;
207 
208  if(X)//transfer the X ruler into the main array
209  {
210  for(size_t p=0; p < points->size(); i++, p++)
211  {
212  scalesPoints->at(i) = points->at(p);
213  }
214  }
215  if(Y)//transfer the Y ruler into the main array
216  {
217  //rotate the generic ruler around Z axis to create the Y ruler
218  points->transform(vl::mat4::getRotation(90, 0, 0, 1));
219 
220  //copy the Y ruler into the scales array
221  for(size_t p=0; p < points->size(); i++, p++)
222  {
223  scalesPoints->at(i) = points->at(p);
224  }
225  }
226  if(Z)//transfer the Z ruler into the main array
227  {
228  //rotate the ruler based on the last rotation
229  vl::vec3 rotaxis = Y ? vl::vec3(1,0,0) : vl::vec3(0,1,0);
230  points->transform(vl::mat4::getRotation(90,rotaxis));
231 
232  //copy the Y ruler into the scales array
233  for(size_t p=0; p < points->size(); i++, p++)
234  {
235  scalesPoints->at(i) = points->at(p);
236  }
237  }
238  scales->setVertexArray(scalesPoints.get());
239  //every consecutive pair of points make a line
240  scales->drawCalls().push_back( new vl::DrawArrays( vl::PT_LINES, 0, numRulers*points->size() ) );
241  return scales;
242 }
int y() const
Definition: Viewport.hpp:67
bool mouseInViewport(int mx, int my, int &vx, int &vy)
const mat4 & viewMatrix() const
Returns the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:160
Vector3< float > fvec3
A 3 components vector with float precision.
Definition: Vector3.hpp:253
fvec2 vec2
Defined as: &#39;typedef fvec2 vec2&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector2.hpp:300
const T * get() const
Definition: Object.hpp:128
float zoomSpeed() const
Zoom speed multiplicative factor (default = 1).
virtual void mouseMoveEvent(int x, int y)
Event generated when the mouse moves.
void setVertexArray(ArrayAbstract *data)
Conventional vertex array.
Definition: Geometry.cpp:155
void resize(size_t dim)
Definition: Array.hpp:233
virtual void enableEvent(bool enabled)
Event generated whenever setEnabled() is called.
Camera * camera()
The camera through which the manipulator is used.
virtual void mouseUpEvent(EMouseButton, int x, int y)
Event generated when one of the mouse buttons is released.
void transform(const mat4 &m)
Transforms the vectors contained in the buffer.
Definition: Array.hpp:322
size_t size() const
Returns the number of elements of an array.
Definition: Array.hpp:235
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
Definition: Geometry.hpp:66
Viewport * viewport()
The viewport bound to a camera.
Definition: Camera.hpp:140
void setCamera(Camera *camera)
The camera through which the manipulator is used.
Visualization Library main namespace.
virtual void setContinuousUpdate(bool continuous)
If the OpenGL context is a widget this function sets whether its area is continuously updated at each...
int height() const
Returns the height in pixels of an OpenGLContext.
FlatManipulator()
Constructor.
EMouseButton
int x() const
Definition: Viewport.hpp:65
#define NULL
Definition: OpenGLDefs.hpp:81
void setViewMatrix(const mat4 &mat)
Sets the Camera&#39;s view matrix (inverse of the modeling matrix).
Definition: Camera.hpp:156
static Matrix4 & getRotation(Matrix4 &out, float degrees, float x, float y, float z)
Definition: Matrix4.hpp:909
T_VectorType & at(size_t i)
Definition: Array.hpp:255
fvec3 vec3
Defined as: &#39;typedef fvec3 vec3&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector3.hpp:270
static Matrix4 & getTranslation(Matrix4 &out, const Vector3< float > &v)
Definition: Matrix4.hpp:553
virtual void mouseDownEvent(EMouseButton, int x, int y)
Event generated when one of the mouse buttons is pressed.
int zoomButton() const
Mouse button used to zoom.
static Matrix4 & getScaling(Matrix4 &out, const Vector3< float > &v)
Definition: Matrix4.hpp:589
vl::ref< vl::Geometry > makeScales(bool X=true, bool Y=true, bool Z=true, int numArmTicks=50, float mmStep=10, float mmTickSize=4, vl::fvec4 color=vl::fvec4(1, 1, 1, 1))
Makes an orthogonal cross-hair scales (reticle).
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...
An array of vl::fvec3.
Definition: Array.hpp:414
EManipMode mode() const
Returns the current manipulator state.
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...
Wraps the OpenGL function glDrawArrays().
Definition: DrawArrays.hpp:57
const T_Scalar & y() const
Definition: Vector2.hpp:134
#define VL_CHECK(expr)
Definition: checks.hpp:73
int translationButton() const
Mouse button used to translate the view.
virtual void update()=0
If the OpenGLContext is a widget this function requests a redraw and generates an updateEvent()...
Collection< DrawCall > & drawCalls()
Returns the list of DrawCall objects bound to a Geometry.
Definition: Geometry.hpp:102