MRPT  1.9.9
CGlCanvasBase.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "gui-precomp.h" // Precompiled headers
11 
12 #include <mrpt/gui/CGlCanvasBase.h>
13 #include <mrpt/opengl/opengl_api.h>
14 #include <mrpt/system/CTicTac.h>
15 #include <cstdlib>
16 
17 #if MRPT_HAS_OPENGL_GLUT
18 #ifdef _WIN32
19 // Windows:
20 #include <windows.h>
21 #endif
22 
23 #ifdef __APPLE__
24 #include <GLUT/glut.h>
25 #include <OpenGL/gl.h>
26 #include <OpenGL/glu.h>
27 #else
28 #include <GL/gl.h>
29 #include <GL/glu.h>
30 #include <GL/glut.h>
31 #ifdef HAVE_FREEGLUT_EXT_H
32 #include <GL/freeglut_ext.h>
33 #endif
34 #endif
35 #endif // MRPT_HAS_OPENGL_GLUT
36 
37 using namespace mrpt;
38 using namespace mrpt::gui;
39 using namespace mrpt::opengl;
40 using namespace std;
42 
44 
46 {
47 #if defined(MRPT_OS_LINUX)
48  // Workaround to enfore wxWidgets to use GLSL>=3.3 even for wxWidgets<3.1
49  // See CWxGLCanvasBase::CWxGLCanvasBase.
50  if (!::getenv("MESA_GL_VERSION_OVERRIDE"))
51  {
52  ::putenv("MESA_GL_VERSION_OVERRIDE=3.3");
53  }
54 #endif
55 }
56 
58 {
59  // Ensure all OpenGL resources are freed before the opengl context is gone:
60  if (m_openGLScene) m_openGLScene->unloadShaders();
61 }
62 
63 void CGlCanvasBase::setMinimumZoom(float zoom) { m_minZoom = zoom; }
64 void CGlCanvasBase::setMaximumZoom(float zoom) { m_maxZoom = zoom; }
65 void CGlCanvasBase::setMousePos(int x, int y)
66 {
67  m_mouseClickX = x;
68  m_mouseClickY = y;
69 }
70 
71 void CGlCanvasBase::setMouseClicked(bool is) { mouseClicked = is; }
72 void CGlCanvasBase::updateZoom(CamaraParams& params, int x, int y) const
73 {
74  float zoom = params.cameraZoomDistance * exp(0.01f * (y - m_mouseClickY));
75  if (zoom <= m_minZoom || (m_maxZoom != -1.0f && m_maxZoom <= zoom)) return;
76  params.cameraZoomDistance = zoom;
77  if (params.cameraZoomDistance < 0.01f) params.cameraZoomDistance = 0.01f;
78 
79  float Az = -0.05f * (x - m_mouseClickX);
80  float D = 0.001f * params.cameraZoomDistance;
81  params.cameraPointingZ += D * Az;
82 }
83 
85 {
86  float zoom = params.cameraZoomDistance * (1 - 0.03f * (delta / 120.0f));
87  if (zoom <= m_minZoom || (m_maxZoom != -1.0f && m_maxZoom <= zoom)) return;
88 
89  params.cameraZoomDistance = zoom;
90 }
91 
93 {
94  const float dis = max(0.01f, (params.cameraZoomDistance));
95  float eye_x =
96  params.cameraPointingX + dis * cos(DEG2RAD(params.cameraAzimuthDeg)) *
97  cos(DEG2RAD(params.cameraElevationDeg));
98  float eye_y =
99  params.cameraPointingY + dis * sin(DEG2RAD(params.cameraAzimuthDeg)) *
100  cos(DEG2RAD(params.cameraElevationDeg));
101  float eye_z =
102  params.cameraPointingZ + dis * sin(DEG2RAD(params.cameraElevationDeg));
103 
104  // Orbit camera:
105  float A_AzimuthDeg = -SENSIBILITY_DEG_PER_PIXEL * (x - m_mouseClickX);
106  params.cameraAzimuthDeg += A_AzimuthDeg;
107 
108  float A_ElevationDeg = SENSIBILITY_DEG_PER_PIXEL * (y - m_mouseClickY);
109  params.setElevationDeg(params.cameraElevationDeg + A_ElevationDeg);
110 
111  // Move cameraPointing pos:
112  params.cameraPointingX =
113  eye_x - dis * cos(DEG2RAD(params.cameraAzimuthDeg)) *
114  cos(DEG2RAD(params.cameraElevationDeg));
115  params.cameraPointingY =
116  eye_y - dis * sin(DEG2RAD(params.cameraAzimuthDeg)) *
117  cos(DEG2RAD(params.cameraElevationDeg));
118  params.cameraPointingZ =
119  eye_z - dis * sin(DEG2RAD(params.cameraElevationDeg));
120 }
121 
123 {
124  params.cameraAzimuthDeg -= 0.2f * (x - m_mouseClickX);
125  params.setElevationDeg(
126  params.cameraElevationDeg + 0.2f * (y - m_mouseClickY));
127 }
128 
130 {
131  m_mouseLastX = x;
132  m_mouseLastY = y;
133 }
134 
136 {
137 #if MRPT_HAS_OPENGL_GLUT
138  if (w == -1 || h == -1) return;
139 
140  glViewport(0, 0, (GLint)w, (GLint)h);
141 #endif
142 }
143 
145 {
146 #if MRPT_HAS_OPENGL_GLUT
147  glClearColor(clearColorR, clearColorG, clearColorB, clearColorA);
148 #endif
149 }
150 
151 void CGlCanvasBase::updatePan(CamaraParams& params, int x, int y) const
152 {
153  float Ay = -(x - m_mouseClickX);
154  float Ax = -(y - m_mouseClickY);
155  float D = 0.001f * params.cameraZoomDistance;
156  params.cameraPointingX += D * (Ax * cos(DEG2RAD(params.cameraAzimuthDeg)) -
157  Ay * sin(DEG2RAD(params.cameraAzimuthDeg)));
158  params.cameraPointingY += D * (Ax * sin(DEG2RAD(params.cameraAzimuthDeg)) +
159  Ay * cos(DEG2RAD(params.cameraAzimuthDeg)));
160 }
161 
163 {
164  return m_cameraParams;
165 }
166 
168 {
169  return m_cameraParams;
170 }
171 
173 {
174  m_cameraParams = params;
175 }
176 
178 {
179  return m_cameraParams.cameraZoomDistance;
180 }
181 
183 {
184  m_cameraParams.cameraZoomDistance = zoom;
185 }
186 
188 {
189  cam.setPointingAt(
190  m_cameraParams.cameraPointingX, m_cameraParams.cameraPointingY,
191  m_cameraParams.cameraPointingZ);
192  cam.setZoomDistance(m_cameraParams.cameraZoomDistance);
193  cam.setAzimuthDegrees(m_cameraParams.cameraAzimuthDeg);
194  cam.setElevationDegrees(m_cameraParams.cameraElevationDeg);
195  cam.setProjectiveModel(m_cameraParams.cameraIsProjective);
196  cam.setProjectiveFOVdeg(m_cameraParams.cameraFOV);
197 
198  return cam;
199 }
200 
201 void CGlCanvasBase::setUseCameraFromScene(bool is) { useCameraFromScene = is; }
202 bool CGlCanvasBase::getUseCameraFromScene() const { return useCameraFromScene; }
204 {
205  m_cameraParams.cameraAzimuthDeg = ang;
206 }
207 
209 {
210  m_cameraParams.cameraElevationDeg = ang;
211 }
212 
214 {
215  return m_cameraParams.cameraAzimuthDeg;
216 }
217 
219 {
220  return m_cameraParams.cameraElevationDeg;
221 }
222 
224 {
225  m_cameraParams.cameraIsProjective = is;
226 }
227 
229 {
230  return m_cameraParams.cameraIsProjective;
231 }
232 
233 void CGlCanvasBase::setCameraFOV(float FOV) { m_cameraParams.cameraFOV = FOV; }
234 float CGlCanvasBase::cameraFOV() const { return m_cameraParams.cameraFOV; }
235 void CGlCanvasBase::setClearColors(float r, float g, float b, float a)
236 {
237  clearColorR = r;
238  clearColorG = g;
239  clearColorB = b;
240  clearColorA = a;
241 }
242 
243 float CGlCanvasBase::getClearColorR() const { return clearColorR; }
244 float CGlCanvasBase::getClearColorG() const { return clearColorG; }
245 float CGlCanvasBase::getClearColorB() const { return clearColorB; }
246 float CGlCanvasBase::getClearColorA() const { return clearColorA; }
248 {
249  m_openGLScene = scene;
250 }
251 
252 void CGlCanvasBase::setCameraPointing(float pointX, float pointY, float pointZ)
253 {
254  m_cameraParams.cameraPointingX = pointX;
255  m_cameraParams.cameraPointingY = pointY;
256  m_cameraParams.cameraPointingZ = pointZ;
257 }
258 
260 {
261  return m_cameraParams.cameraPointingX;
262 }
263 
265 {
266  return m_cameraParams.cameraPointingY;
267 }
268 
270 {
271  return m_cameraParams.cameraPointingZ;
272 }
273 
274 double CGlCanvasBase::renderCanvas(int width, int height)
275 {
276 #if MRPT_HAS_OPENGL_GLUT
277  CTicTac tictac;
278  double At = 0.1;
279 
280  try
281  {
282  // Call PreRender user code:
283  preRender();
284  CHECK_OPENGL_ERROR();
285 
286  // Set static configs:
287  glEnable(GL_DEPTH_TEST);
288  CHECK_OPENGL_ERROR();
289 
290  // Set the viewport
291  resizeViewport((GLsizei)width, (GLsizei)height);
292 
293  // Set the background color:
294  clearColors();
295 
296  if (m_openGLScene)
297  {
298  // Set the camera params in the scene:
299  if (!useCameraFromScene)
300  {
301  COpenGLViewport::Ptr view = m_openGLScene->getViewport("main");
302  if (!view)
303  {
305  "Fatal error: there is no 'main' viewport in the 3D "
306  "scene!");
307  }
308 
309  mrpt::opengl::CCamera& cam = view->getCamera();
310  updateCameraParams(cam);
311  }
312 
313  tictac.Tic();
314 
315  // Draw primitives:
316  m_openGLScene->render();
317 
318  } // end if "m_openGLScene!=nullptr"
319 
320  postRender();
321 
322  // Flush & swap buffers to disply new image:
323  glFinish();
324  swapBuffers();
325  CHECK_OPENGL_ERROR();
326 
327  At = tictac.Tac();
328  }
329  catch (const std::exception& e)
330  {
331  const std::string err_msg =
332  std::string("[CGLCanvasBase::Render] Exception:\n") +
334  std::cerr << err_msg;
335  renderError(err_msg);
336  }
337 
338  return At;
339 #else
340  THROW_EXCEPTION("Cant render: MRPT was built without OpenGL");
341 #endif
342 }
343 
345 {
346  cameraElevationDeg = deg;
347 
348  if (cameraElevationDeg < -90.0f)
349  cameraElevationDeg = -90.0f;
350  else if (cameraElevationDeg > 90.0f)
351  cameraElevationDeg = 90.0f;
352 }
353 
354 void CGlCanvasBaseHeadless::renderError(const std::string& e)
355 {
356  std::cerr << "[CGlCanvasBaseHeadless::renderError] Error:" << e
357  << std::endl;
358 }
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:86
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...
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
CamaraParams cameraParams() const
Returns a copy of CamaraParams See also getRefCameraParams(), setCameraParams(const CamaraParams &) ...
A high-performance stopwatch, with typical resolution of nanoseconds.
void setProjectiveModel(bool v=true)
Enable/Disable projective mode (vs.
Definition: CCamera.h:70
void setMaximumZoom(float zoom)
Sets the maximum of the zoom See also setMinimumZoom(float)
float getZoomDistance() const
Returns a zoom See also setZoomDistance(float)
mrpt::vision::TStereoCalibParams params
STL namespace.
virtual void setCameraParams(const CamaraParams &params)
Sets the CamaraParams See also cameraParams(), getRefCameraParams()
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 &) ...
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()
void setZoomDistance(float z)
Definition: CCamera.h:63
mrpt::system::CTicTac CTicTac
Definition: utils/CTicTac.h:5
void setAzimuthDegrees(float ang)
Definition: CCamera.h:67
virtual void renderError(const std::string &e) override
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)
constexpr double DEG2RAD(const double x)
Degrees to radians.
virtual void setCameraPointing(float pointX, float pointY, float pointZ)
Saves the pointing of the camera See also getCameraPointingX(), getCameraPointingY(), getCameraPointingZ()
void clearColors()
Calls the glClearColor function See also setClearColors(float, float, float, float) ...
void setProjectiveFOVdeg(float ang)
Vertical field-of-View in degs, only when projectiveModel=true (default=30 deg).
Definition: CCamera.h:87
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()
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) ...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void setElevationDegrees(float ang)
Definition: CCamera.h:68
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:13
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
void setUseCameraFromScene(bool is)
If set to true (default=false), the cameraPointingX,...
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:33
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 ...
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:41



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020