Main MRPT website > C++ reference for 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 }
mrpt::gui::CGlCanvasBase::getRefCameraParams
const CamaraParams & getRefCameraParams() const
Returns a reference to CamaraParams See also cameraParams(), setCameraParams(const CamaraParams &)
Definition: CGlCanvasBase.cpp:146
glViewport
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
mrpt::gui::CGlCanvasBase::updateOrbitCamera
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...
Definition: CGlCanvasBase.cpp:101
mrpt::gui::CGlCanvasBase::updateZoom
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...
Definition: CGlCanvasBase.cpp:51
mrpt::gui::CGlCanvasBase::updateCameraParams
mrpt::opengl::CCamera & updateCameraParams(mrpt::opengl::CCamera &cam) const
This function gets a reference to mrpt::opengl::CCamera and updates the camera parameters(pointing,...
Definition: CGlCanvasBase.cpp:166
mrpt::opengl::CCamera::setAzimuthDegrees
void setAzimuthDegrees(float ang)
Definition: CCamera.h:76
mrpt::gui::CGlCanvasBase::getUseCameraFromScene
bool getUseCameraFromScene() const
See also void setUseCameraFromScene(bool)
Definition: CGlCanvasBase.cpp:181
mrpt::gui::CGlCanvasBase::getCameraPointingZ
float getCameraPointingZ() const
Returns the z pointing of the camera See also setCameraPointing(float, float, float)
Definition: CGlCanvasBase.cpp:248
mrpt::gui::CGlCanvasBase::getAzimuthDegrees
float getAzimuthDegrees() const
Returns a azimuth degrees See also setAzimuthDegrees(float)
Definition: CGlCanvasBase.cpp:192
mrpt::gui::CGlCanvasBase::setCameraProjective
virtual void setCameraProjective(bool is)
Definition: CGlCanvasBase.cpp:202
mrpt::gui::CGlCanvasBase::SENSIBILITY_DEG_PER_PIXEL
static float SENSIBILITY_DEG_PER_PIXEL
Definition: CGlCanvasBase.h:191
mrpt::gui::CGlCanvasBase::getClearColorG
float getClearColorG() const
Definition: CGlCanvasBase.cpp:223
mrpt::opengl::CCamera::setProjectiveModel
void setProjectiveModel(bool v=true)
Enable/Disable projective mode (vs.
Definition: CCamera.h:79
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::opengl::CCamera::setProjectiveFOVdeg
void setProjectiveFOVdeg(float ang)
Field-of-View in degs, only when projectiveModel=true (default=30 deg).
Definition: CCamera.h:95
mrpt::gui::CGlCanvasBase::updateRotate
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...
Definition: CGlCanvasBase.cpp:71
mrpt::gui::CGlCanvasBase::updatePan
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 ...
Definition: CGlCanvasBase.cpp:130
mrpt::gui::CGlCanvasBase::setMaximumZoom
void setMaximumZoom(float zoom)
Sets the maximum of the zoom See also setMinimumZoom(float)
Definition: CGlCanvasBase.cpp:43
GL_TEXTURE_2D
#define GL_TEXTURE_2D
Definition: glew.h:7238
mrpt::gui::CGlCanvasBase::CamaraParams::setElevationDeg
void setElevationDeg(float deg)
Definition: CGlCanvasBase.cpp:339
glEnable
GLAPI void GLAPIENTRY glEnable(GLenum cap)
GL_ALPHA_TEST
#define GL_ALPHA_TEST
Definition: glew.h:426
mrpt::gui::CGlCanvasBase::setCameraParams
virtual void setCameraParams(const CamaraParams &params)
Sets the CamaraParams See also cameraParams(), getRefCameraParams()
Definition: CGlCanvasBase.cpp:151
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
g
GLubyte g
Definition: glext.h:6279
w
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
mrpt::gui::CGlCanvasBase::setClearColors
void setClearColors(float r, float g, float b, float a=1.0f)
Sets the RGBA colors for glClearColor See also clearColors(), getClearColorR(), getClearColorG(),...
Definition: CGlCanvasBase.cpp:214
CGlCanvasBase.h
mrpt::opengl::CCamera::setPointingAt
void setPointingAt(float x, float y, float z)
Definition: CCamera.h:50
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::gui::CGlCanvasBase::isCameraProjective
bool isCameraProjective() const
Definition: CGlCanvasBase.cpp:207
mrpt::gui::CGlCanvasBase::setAzimuthDegrees
virtual void setAzimuthDegrees(float ang)
Saves the degrees of the azimuth camera See also getAzimuthDegrees()
Definition: CGlCanvasBase.cpp:182
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
GLsizei
int GLsizei
Definition: glew.h:210
r
GLdouble GLdouble GLdouble r
Definition: glext.h:3705
mrpt::gui::CGlCanvasBase::getCameraPointingY
float getCameraPointingY() const
Returns the y pointing of the camera See also setCameraPointing(float, float, float)
Definition: CGlCanvasBase.cpp:243
glFlush
GLAPI void GLAPIENTRY glFlush(void)
glPushAttrib
GLAPI void GLAPIENTRY glPushAttrib(GLbitfield mask)
mrpt::utils::CTicTac
mrpt::system::CTicTac CTicTac
Definition: utils/CTicTac.h:7
b
GLubyte GLubyte b
Definition: glext.h:6279
mrpt::gui::CGlCanvasBase::updateLastPos
void updateLastPos(int x, int y)
Sets the last mouse position.
Definition: CGlCanvasBase.cpp:108
mrpt::opengl::CCamera
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:30
mrpt::gui::CGlCanvasBase::getZoomDistance
float getZoomDistance() const
Returns a zoom See also setZoomDistance(float)
Definition: CGlCanvasBase.cpp:156
gui-precomp.h
mrpt::opengl::CCamera::setZoomDistance
void setZoomDistance(float z)
Definition: CCamera.h:72
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::gui::CGlCanvasBase::clearColors
void clearColors()
Calls the glClearColor function See also setClearColors(float, float, float, float)
Definition: CGlCanvasBase.cpp:123
GL_DEPTH_TEST
#define GL_DEPTH_TEST
Definition: glew.h:401
mrpt::gui::CGlCanvasBase::cameraFOV
float cameraFOV() const
Definition: CGlCanvasBase.cpp:213
mrpt::gui::CGlCanvasBase::getClearColorA
float getClearColorA() const
Definition: CGlCanvasBase.cpp:225
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
GL_MODELVIEW
#define GL_MODELVIEW
Definition: glew.h:610
height
GLenum GLsizei GLsizei height
Definition: glext.h:3554
mrpt::gui::CGlCanvasBase::getElevationDegrees
float getElevationDegrees() const
Returns a elevation degrees See also setElevationDegrees(float)
Definition: CGlCanvasBase.cpp:197
glLoadIdentity
GLAPI void GLAPIENTRY glLoadIdentity(void)
GL_ALL_ATTRIB_BITS
#define GL_ALL_ATTRIB_BITS
Definition: glew.h:271
glClearColor
GLAPI void GLAPIENTRY glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
mrpt::gui::CGlCanvasBase::CamaraParams
Definition: CGlCanvasBase.h:31
mrpt::gui::CGlCanvasBase::setOpenGLSceneRef
void setOpenGLSceneRef(mrpt::opengl::COpenGLScene::Ptr scene)
Definition: CGlCanvasBase.cpp:226
CTicTac.h
mrpt::opengl::CCamera::setElevationDegrees
void setElevationDegrees(float ang)
Definition: CCamera.h:77
mrpt::gui::CGlCanvasBase::setCameraFOV
virtual void setCameraFOV(float FOV)
Definition: CGlCanvasBase.cpp:212
glMatrixMode
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
glPopAttrib
GLAPI void GLAPIENTRY glPopAttrib(void)
width
GLenum GLsizei width
Definition: glext.h:3531
mrpt::gui::CGlCanvasBase::getClearColorB
float getClearColorB() const
Definition: CGlCanvasBase.cpp:224
mrpt::gui::CGlCanvasBase::renderCanvas
virtual double renderCanvas(int width=-1, int height=-1)
Definition: CGlCanvasBase.cpp:253
mrpt::gui::CGlCanvasBase::getCameraPointingX
float getCameraPointingX() const
Returns the x pointing of the camera See also setCameraPointing(float, float, float)
Definition: CGlCanvasBase.cpp:238
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
mrpt::gui::CGlCanvasBase::setCameraPointing
virtual void setCameraPointing(float pointX, float pointY, float pointZ)
Saves the pointing of the camera See also getCameraPointingX(), getCameraPointingY(),...
Definition: CGlCanvasBase.cpp:231
mrpt::gui::CGlCanvasBase::resizeViewport
void resizeViewport(int w, int h)
Calls the glViewport function.
Definition: CGlCanvasBase.cpp:114
GLint
int GLint
Definition: glew.h:209
mrpt::gui::CGlCanvasBase::setMousePos
void setMousePos(int x, int y)
Saves the click position of the mouse See also setMouseClicked(bool)
Definition: CGlCanvasBase.cpp:44
mrpt::gui::CGlCanvasBase::setElevationDegrees
virtual void setElevationDegrees(float ang)
Saves the degrees of the elevation camera See also getElevationDegrees()
Definition: CGlCanvasBase.cpp:187
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::gui::CGlCanvasBase::setMouseClicked
void setMouseClicked(bool is)
Sets the property mouseClicked By default, this property is false.
Definition: CGlCanvasBase.cpp:50
mrpt::gui::CGlCanvasBase::getClearColorR
float getClearColorR() const
Definition: CGlCanvasBase.cpp:222
mrpt::opengl::COpenGLViewport::Ptr
std::shared_ptr< COpenGLViewport > Ptr
Definition: COpenGLViewport.h:63
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::gui::CGlCanvasBase::setMinimumZoom
void setMinimumZoom(float zoom)
Sets the minimum of the zoom See also setMaximumZoom(float)
Definition: CGlCanvasBase.cpp:42
x
GLenum GLint x
Definition: glext.h:3538
mrpt::gui::CGlCanvasBase::setUseCameraFromScene
void setUseCameraFromScene(bool is)
If set to true (default=false), the cameraPointingX,...
Definition: CGlCanvasBase.cpp:180
params
GLenum const GLfloat * params
Definition: glext.h:3534
a
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
mrpt::gui::CGlCanvasBase::cameraParams
CamaraParams cameraParams() const
Returns a copy of CamaraParams See also getRefCameraParams(), setCameraParams(const CamaraParams &)
Definition: CGlCanvasBase.cpp:141
mrpt::gui::CGlCanvasBase::setZoomDistance
virtual void setZoomDistance(float zoom)
Saves camera zooming See also getZoomDistance()
Definition: CGlCanvasBase.cpp:161
mrpt::DEG2RAD
double DEG2RAD(const double x)
Degrees to radians.
Definition: core/include/mrpt/core/bits_math.h:42



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST