MRPT  1.9.9
CGlCanvasBase.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "gui-precomp.h" // Precompiled headers
11 #include <mrpt/gui/CGlCanvasBase.h>
12 #include <mrpt/system/CTicTac.h>
13 
14 #if MRPT_HAS_OPENGL_GLUT
15 #ifdef _WIN32
16 // Windows:
17 #include <windows.h>
18 #endif
19 
20 #ifdef __APPLE__
21 #include <OpenGL/gl.h>
22 #include <OpenGL/glu.h>
23 #include <GLUT/glut.h>
24 #else
25 #include <GL/gl.h>
26 #include <GL/glu.h>
27 #include <GL/glut.h>
28 #ifdef HAVE_FREEGLUT_EXT_H
29 #include <GL/freeglut_ext.h>
30 #endif
31 #endif
32 #endif // MRPT_HAS_OPENGL_GLUT
33 
34 using namespace mrpt;
35 using namespace mrpt::gui;
36 using namespace mrpt::opengl;
37 using namespace std;
39 
41 
42 void CGlCanvasBase::setMinimumZoom(float zoom) { m_minZoom = zoom; }
43 void CGlCanvasBase::setMaximumZoom(float zoom) { m_maxZoom = zoom; }
45 {
46  m_mouseClickX = x;
47  m_mouseClickY = y;
48 }
49 
50 void CGlCanvasBase::setMouseClicked(bool is) { mouseClicked = is; }
52 {
53  float zoom = params.cameraZoomDistance * exp(0.01 * (y - m_mouseClickY));
54  if (zoom <= m_minZoom || (m_maxZoom != -1.0f && m_maxZoom <= zoom)) return;
55  params.cameraZoomDistance = zoom;
56  if (params.cameraZoomDistance < 0.01) params.cameraZoomDistance = 0.01f;
57 
58  float Az = -0.05 * (x - m_mouseClickX);
59  float D = 0.001 * params.cameraZoomDistance;
60  params.cameraPointingZ += D * Az;
61 }
62 
64 {
65  float zoom = params.cameraZoomDistance * (1 - 0.03f * (delta / 120.0f));
66  if (zoom <= m_minZoom || (m_maxZoom != -1.0f && m_maxZoom <= zoom)) return;
67 
68  params.cameraZoomDistance = zoom;
69 }
70 
72 {
73  const float dis = max(0.01f, (params.cameraZoomDistance));
74  float eye_x = params.cameraPointingX +
75  dis * cos(DEG2RAD(params.cameraAzimuthDeg)) *
76  cos(DEG2RAD(params.cameraElevationDeg));
77  float eye_y = params.cameraPointingY +
78  dis * sin(DEG2RAD(params.cameraAzimuthDeg)) *
79  cos(DEG2RAD(params.cameraElevationDeg));
80  float eye_z =
81  params.cameraPointingZ + dis * sin(DEG2RAD(params.cameraElevationDeg));
82 
83  // Orbit camera:
84  float A_AzimuthDeg = -SENSIBILITY_DEG_PER_PIXEL * (x - m_mouseClickX);
85  params.cameraAzimuthDeg += A_AzimuthDeg;
86 
87  float A_ElevationDeg = SENSIBILITY_DEG_PER_PIXEL * (y - m_mouseClickY);
88  params.setElevationDeg(params.cameraElevationDeg + A_ElevationDeg);
89 
90  // Move cameraPointing pos:
91  params.cameraPointingX = eye_x -
92  dis * cos(DEG2RAD(params.cameraAzimuthDeg)) *
93  cos(DEG2RAD(params.cameraElevationDeg));
94  params.cameraPointingY = eye_y -
95  dis * sin(DEG2RAD(params.cameraAzimuthDeg)) *
96  cos(DEG2RAD(params.cameraElevationDeg));
97  params.cameraPointingZ =
98  eye_z - dis * sin(DEG2RAD(params.cameraElevationDeg));
99 }
100 
102 {
103  params.cameraAzimuthDeg -= 0.2 * (x - m_mouseClickX);
104  params.setElevationDeg(
105  params.cameraElevationDeg + 0.2 * (y - m_mouseClickY));
106 }
107 
109 {
110  m_mouseLastX = x;
111  m_mouseLastY = y;
112 }
113 
115 {
116 #if MRPT_HAS_OPENGL_GLUT
117  if (w == -1 || h == -1) return;
118 
119  glViewport(0, 0, (GLint)w, (GLint)h);
120 #endif
121 }
122 
124 {
125 #if MRPT_HAS_OPENGL_GLUT
126  glClearColor(clearColorR, clearColorG, clearColorB, clearColorA);
127 #endif
128 }
129 
131 {
132  float Ay = -(x - m_mouseClickX);
133  float Ax = -(y - m_mouseClickY);
134  float D = 0.001 * params.cameraZoomDistance;
135  params.cameraPointingX += D * (Ax * cos(DEG2RAD(params.cameraAzimuthDeg)) -
136  Ay * sin(DEG2RAD(params.cameraAzimuthDeg)));
137  params.cameraPointingY += D * (Ax * sin(DEG2RAD(params.cameraAzimuthDeg)) +
138  Ay * cos(DEG2RAD(params.cameraAzimuthDeg)));
139 }
140 
142 {
143  return m_cameraParams;
144 }
145 
147 {
148  return m_cameraParams;
149 }
150 
152 {
153  m_cameraParams = params;
154 }
155 
157 {
158  return m_cameraParams.cameraZoomDistance;
159 }
160 
162 {
163  m_cameraParams.cameraZoomDistance = zoom;
164 }
165 
167 {
168  cam.setPointingAt(
169  m_cameraParams.cameraPointingX, m_cameraParams.cameraPointingY,
170  m_cameraParams.cameraPointingZ);
171  cam.setZoomDistance(m_cameraParams.cameraZoomDistance);
172  cam.setAzimuthDegrees(m_cameraParams.cameraAzimuthDeg);
173  cam.setElevationDegrees(m_cameraParams.cameraElevationDeg);
174  cam.setProjectiveModel(m_cameraParams.cameraIsProjective);
175  cam.setProjectiveFOVdeg(m_cameraParams.cameraFOV);
176 
177  return cam;
178 }
179 
180 void CGlCanvasBase::setUseCameraFromScene(bool is) { useCameraFromScene = is; }
181 bool CGlCanvasBase::getUseCameraFromScene() const { return useCameraFromScene; }
183 {
184  m_cameraParams.cameraAzimuthDeg = ang;
185 }
186 
188 {
189  m_cameraParams.cameraElevationDeg = ang;
190 }
191 
193 {
194  return m_cameraParams.cameraAzimuthDeg;
195 }
196 
198 {
199  return m_cameraParams.cameraElevationDeg;
200 }
201 
203 {
204  m_cameraParams.cameraIsProjective = is;
205 }
206 
208 {
209  return m_cameraParams.cameraIsProjective;
210 }
211 
212 void CGlCanvasBase::setCameraFOV(float FOV) { m_cameraParams.cameraFOV = FOV; }
213 float CGlCanvasBase::cameraFOV() const { return m_cameraParams.cameraFOV; }
214 void CGlCanvasBase::setClearColors(float r, float g, float b, float a)
215 {
216  clearColorR = r;
217  clearColorG = g;
218  clearColorB = b;
219  clearColorA = a;
220 }
221 
222 float CGlCanvasBase::getClearColorR() const { return clearColorR; }
223 float CGlCanvasBase::getClearColorG() const { return clearColorG; }
224 float CGlCanvasBase::getClearColorB() const { return clearColorB; }
225 float CGlCanvasBase::getClearColorA() const { return clearColorA; }
227 {
228  m_openGLScene = scene;
229 }
230 
231 void CGlCanvasBase::setCameraPointing(float pointX, float pointY, float pointZ)
232 {
233  m_cameraParams.cameraPointingX = pointX;
234  m_cameraParams.cameraPointingY = pointY;
235  m_cameraParams.cameraPointingZ = pointZ;
236 }
237 
239 {
240  return m_cameraParams.cameraPointingX;
241 }
242 
244 {
245  return m_cameraParams.cameraPointingY;
246 }
247 
249 {
250  return m_cameraParams.cameraPointingZ;
251 }
252 
254 {
255 #if MRPT_HAS_OPENGL_GLUT
256  CTicTac tictac;
257  double At = 0.1;
258 
259  try
260  {
261  // Call PreRender user code:
262  preRender();
263 
265 
266  // Set static configs:
270 
271  // PART 1a: Set the viewport
272  // --------------------------------------
273  resizeViewport((GLsizei)width, (GLsizei)height);
274 
275  // Set the background color:
276  clearColors();
277 
278  if (m_openGLScene)
279  {
280  // Set the camera params in the scene:
281  if (!useCameraFromScene)
282  {
283  COpenGLViewport::Ptr view = m_openGLScene->getViewport("main");
284  if (!view)
285  {
287  "Fatal error: there is no 'main' viewport in the 3D "
288  "scene!");
289  }
290 
291  mrpt::opengl::CCamera& cam = view->getCamera();
292  updateCameraParams(cam);
293  }
294 
295  // PART 2: Set the MODELVIEW matrix
296  // --------------------------------------
298  glLoadIdentity();
299 
300  tictac.Tic();
301 
302  // PART 3: Draw primitives:
303  // --------------------------------------
304  m_openGLScene->render();
305 
306  } // end if "m_openGLScene!=nullptr"
307 
308  postRender();
309 
310  // Flush & swap buffers to disply new image:
311  glFlush();
312  swapBuffers();
313 
314  At = tictac.Tac();
315 
316  glPopAttrib();
317  }
318  catch (std::exception& e)
319  {
320  glPopAttrib();
321  const std::string err_msg =
322  std::string("[CWxGLCanvasBase::Render] Exception!: ") +
323  std::string(e.what());
324  std::cerr << err_msg;
325  renderError(err_msg);
326  }
327  catch (...)
328  {
329  glPopAttrib();
330  std::cerr << "Runtime error!" << std::endl;
331  }
332 
333  return At;
334 #else
335  THROW_EXCEPTION("Cant render: MRPT was built without OpenGL");
336 #endif
337 }
338 
340 {
341  cameraElevationDeg = deg;
342 
343  if (cameraElevationDeg < -90.0f)
344  cameraElevationDeg = -90.0f;
345  else if (cameraElevationDeg > 90.0f)
346  cameraElevationDeg = 90.0f;
347 }
GLAPI void GLAPIENTRY glFlush(void)
float getCameraPointingX() const
Returns the x pointing of the camera See also setCameraPointing(float, float, float) ...
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
GLAPI void GLAPIENTRY glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
virtual void setCameraProjective(bool is)
void updateOrbitCamera(CamaraParams &params, int x, int y) const
This function for the mouse event It gets a reference to CamaraParams, x, y and updates the elevation...
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
CamaraParams cameraParams() const
Returns a copy of CamaraParams See also getRefCameraParams(), setCameraParams(const CamaraParams &) ...
#define GL_MODELVIEW
Definition: glew.h:610
double DEG2RAD(const double x)
Degrees to radians.
A high-performance stopwatch, with typical resolution of nanoseconds.
void setProjectiveModel(bool v=true)
Enable/Disable projective mode (vs.
Definition: CCamera.h:77
void setMaximumZoom(float zoom)
Sets the maximum of the zoom See also setMinimumZoom(float)
float getZoomDistance() const
Returns a zoom See also setZoomDistance(float)
STL namespace.
virtual void setCameraParams(const CamaraParams &params)
Sets the CamaraParams See also cameraParams(), getRefCameraParams()
#define GL_DEPTH_TEST
Definition: glew.h:401
GLAPI void GLAPIENTRY glPopAttrib(void)
void resizeViewport(int w, int h)
Calls the glViewport function.
const CamaraParams & getRefCameraParams() const
Returns a reference to CamaraParams See also cameraParams(), setCameraParams(const CamaraParams &) ...
GLAPI void GLAPIENTRY glLoadIdentity(void)
void setMousePos(int x, int y)
Saves the click position of the mouse See also setMouseClicked(bool)
virtual void setElevationDegrees(float ang)
Saves the degrees of the elevation camera See also getElevationDegrees()
GLenum GLsizei width
Definition: glext.h:3531
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
void setZoomDistance(float z)
Definition: CCamera.h:70
mrpt::system::CTicTac CTicTac
Definition: utils/CTicTac.h:5
void setAzimuthDegrees(float ang)
Definition: CCamera.h:74
void setOpenGLSceneRef(mrpt::opengl::COpenGLScene::Ptr scene)
virtual void setAzimuthDegrees(float ang)
Saves the degrees of the azimuth camera See also getAzimuthDegrees()
virtual double renderCanvas(int width=-1, int height=-1)
virtual void setCameraPointing(float pointX, float pointY, float pointZ)
Saves the pointing of the camera See also getCameraPointingX(), getCameraPointingY(), getCameraPointingZ()
GLubyte g
Definition: glext.h:6279
void clearColors()
Calls the glClearColor function See also setClearColors(float, float, float, float) ...
void setProjectiveFOVdeg(float ang)
Field-of-View in degs, only when projectiveModel=true (default=30 deg).
Definition: CCamera.h:93
GLubyte GLubyte b
Definition: glext.h:6279
float getCameraPointingZ() const
Returns the z pointing of the camera See also setCameraPointing(float, float, float) ...
virtual void setCameraFOV(float FOV)
void setClearColors(float r, float g, float b, float a=1.0f)
Sets the RGBA colors for glClearColor See also clearColors(), getClearColorR(), getClearColorG(),getClearColorB(), getClearColorA()
virtual void setZoomDistance(float zoom)
Saves camera zooming See also getZoomDistance()
GLsizei const GLchar ** string
Definition: glext.h:4101
bool getUseCameraFromScene() const
See also void setUseCameraFromScene(bool)
float getAzimuthDegrees() const
Returns a azimuth degrees See also setAzimuthDegrees(float)
float getCameraPointingY() const
Returns the y pointing of the camera See also setCameraPointing(float, float, float) ...
#define GL_ALPHA_TEST
Definition: glew.h:426
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
void setElevationDegrees(float ang)
Definition: CCamera.h:75
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
GLAPI void GLAPIENTRY glPushAttrib(GLbitfield mask)
void setMouseClicked(bool is)
Sets the property mouseClicked By default, this property is false.
void updateLastPos(int x, int y)
Sets the last mouse position.
float getElevationDegrees() const
Returns a elevation degrees See also setElevationDegrees(float)
void updateRotate(CamaraParams &params, int x, int y) const
This function for the mouse event It gets a reference to CamaraParams, x, y and updates the elevation...
void setMinimumZoom(float zoom)
Sets the minimum of the zoom See also setMaximumZoom(float)
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
void setUseCameraFromScene(bool is)
If set to true (default=false), the cameraPointingX,...
GLenum GLint GLint y
Definition: glext.h:3538
#define GL_ALL_ATTRIB_BITS
Definition: glew.h:271
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
int GLint
Definition: glew.h:209
GLenum GLint x
Definition: glext.h:3538
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:28
GLenum GLsizei GLsizei height
Definition: glext.h:3554
static float SENSIBILITY_DEG_PER_PIXEL
void updatePan(CamaraParams &params, int x, int y) const
This function for the mouse event It gets a reference to CamaraParams, x, y and updates the pointing ...
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
GLenum const GLfloat * params
Definition: glext.h:3534
#define GL_TEXTURE_2D
Definition: glew.h:7238
int GLsizei
Definition: glew.h:210
mrpt::opengl::CCamera & updateCameraParams(mrpt::opengl::CCamera &cam) const
This function gets a reference to mrpt::opengl::CCamera and updates the camera parameters(pointing, zoom, azimuth, elevation, IsProjective, FOV)
void updateZoom(CamaraParams &params, int x, int y) const
This function for the mouse event It gets a reference to CamaraParams, x, y and updates the zoom of t...
void setPointingAt(float x, float y, float z)
Definition: CCamera.h:48



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020