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-2019, 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/system/CTicTac.h>
14 
15 #if MRPT_HAS_OPENGL_GLUT
16 #ifdef _WIN32
17 // Windows:
18 #include <windows.h>
19 #endif
20 
21 #ifdef __APPLE__
22 #include <GLUT/glut.h>
23 #include <OpenGL/gl.h>
24 #include <OpenGL/glu.h>
25 #else
26 #include <GL/gl.h>
27 #include <GL/glu.h>
28 #include <GL/glut.h>
29 #ifdef HAVE_FREEGLUT_EXT_H
30 #include <GL/freeglut_ext.h>
31 #endif
32 #endif
33 #endif // MRPT_HAS_OPENGL_GLUT
34 
35 using namespace mrpt;
36 using namespace mrpt::gui;
37 using namespace mrpt::opengl;
38 using namespace std;
40 
42 
43 void CGlCanvasBase::setMinimumZoom(float zoom) { m_minZoom = zoom; }
44 void CGlCanvasBase::setMaximumZoom(float zoom) { m_maxZoom = zoom; }
46 {
47  m_mouseClickX = x;
48  m_mouseClickY = y;
49 }
50 
51 void CGlCanvasBase::setMouseClicked(bool is) { mouseClicked = is; }
53 {
54  float zoom = params.cameraZoomDistance * exp(0.01 * (y - m_mouseClickY));
55  if (zoom <= m_minZoom || (m_maxZoom != -1.0f && m_maxZoom <= zoom)) return;
56  params.cameraZoomDistance = zoom;
57  if (params.cameraZoomDistance < 0.01) params.cameraZoomDistance = 0.01f;
58 
59  float Az = -0.05 * (x - m_mouseClickX);
60  float D = 0.001 * params.cameraZoomDistance;
61  params.cameraPointingZ += D * Az;
62 }
63 
65 {
66  float zoom = params.cameraZoomDistance * (1 - 0.03f * (delta / 120.0f));
67  if (zoom <= m_minZoom || (m_maxZoom != -1.0f && m_maxZoom <= zoom)) return;
68 
69  params.cameraZoomDistance = zoom;
70 }
71 
73 {
74  const float dis = max(0.01f, (params.cameraZoomDistance));
75  float eye_x =
76  params.cameraPointingX + dis * cos(DEG2RAD(params.cameraAzimuthDeg)) *
77  cos(DEG2RAD(params.cameraElevationDeg));
78  float eye_y =
79  params.cameraPointingY + dis * sin(DEG2RAD(params.cameraAzimuthDeg)) *
80  cos(DEG2RAD(params.cameraElevationDeg));
81  float eye_z =
82  params.cameraPointingZ + dis * sin(DEG2RAD(params.cameraElevationDeg));
83 
84  // Orbit camera:
85  float A_AzimuthDeg = -SENSIBILITY_DEG_PER_PIXEL * (x - m_mouseClickX);
86  params.cameraAzimuthDeg += A_AzimuthDeg;
87 
88  float A_ElevationDeg = SENSIBILITY_DEG_PER_PIXEL * (y - m_mouseClickY);
89  params.setElevationDeg(params.cameraElevationDeg + A_ElevationDeg);
90 
91  // Move cameraPointing pos:
92  params.cameraPointingX =
93  eye_x - dis * cos(DEG2RAD(params.cameraAzimuthDeg)) *
94  cos(DEG2RAD(params.cameraElevationDeg));
95  params.cameraPointingY =
96  eye_y - dis * sin(DEG2RAD(params.cameraAzimuthDeg)) *
97  cos(DEG2RAD(params.cameraElevationDeg));
98  params.cameraPointingZ =
99  eye_z - dis * sin(DEG2RAD(params.cameraElevationDeg));
100 }
101 
103 {
104  params.cameraAzimuthDeg -= 0.2 * (x - m_mouseClickX);
105  params.setElevationDeg(
106  params.cameraElevationDeg + 0.2 * (y - m_mouseClickY));
107 }
108 
110 {
111  m_mouseLastX = x;
112  m_mouseLastY = y;
113 }
114 
116 {
117 #if MRPT_HAS_OPENGL_GLUT
118  if (w == -1 || h == -1) return;
119 
120  glViewport(0, 0, (GLint)w, (GLint)h);
121 #endif
122 }
123 
125 {
126 #if MRPT_HAS_OPENGL_GLUT
127  glClearColor(clearColorR, clearColorG, clearColorB, clearColorA);
128 #endif
129 }
130 
132 {
133  float Ay = -(x - m_mouseClickX);
134  float Ax = -(y - m_mouseClickY);
135  float D = 0.001 * params.cameraZoomDistance;
136  params.cameraPointingX += D * (Ax * cos(DEG2RAD(params.cameraAzimuthDeg)) -
137  Ay * sin(DEG2RAD(params.cameraAzimuthDeg)));
138  params.cameraPointingY += D * (Ax * sin(DEG2RAD(params.cameraAzimuthDeg)) +
139  Ay * cos(DEG2RAD(params.cameraAzimuthDeg)));
140 }
141 
143 {
144  return m_cameraParams;
145 }
146 
148 {
149  return m_cameraParams;
150 }
151 
153 {
154  m_cameraParams = params;
155 }
156 
158 {
159  return m_cameraParams.cameraZoomDistance;
160 }
161 
163 {
164  m_cameraParams.cameraZoomDistance = zoom;
165 }
166 
168 {
169  cam.setPointingAt(
170  m_cameraParams.cameraPointingX, m_cameraParams.cameraPointingY,
171  m_cameraParams.cameraPointingZ);
172  cam.setZoomDistance(m_cameraParams.cameraZoomDistance);
173  cam.setAzimuthDegrees(m_cameraParams.cameraAzimuthDeg);
174  cam.setElevationDegrees(m_cameraParams.cameraElevationDeg);
175  cam.setProjectiveModel(m_cameraParams.cameraIsProjective);
176  cam.setProjectiveFOVdeg(m_cameraParams.cameraFOV);
177 
178  return cam;
179 }
180 
181 void CGlCanvasBase::setUseCameraFromScene(bool is) { useCameraFromScene = is; }
182 bool CGlCanvasBase::getUseCameraFromScene() const { return useCameraFromScene; }
184 {
185  m_cameraParams.cameraAzimuthDeg = ang;
186 }
187 
189 {
190  m_cameraParams.cameraElevationDeg = ang;
191 }
192 
194 {
195  return m_cameraParams.cameraAzimuthDeg;
196 }
197 
199 {
200  return m_cameraParams.cameraElevationDeg;
201 }
202 
204 {
205  m_cameraParams.cameraIsProjective = is;
206 }
207 
209 {
210  return m_cameraParams.cameraIsProjective;
211 }
212 
213 void CGlCanvasBase::setCameraFOV(float FOV) { m_cameraParams.cameraFOV = FOV; }
214 float CGlCanvasBase::cameraFOV() const { return m_cameraParams.cameraFOV; }
215 void CGlCanvasBase::setClearColors(float r, float g, float b, float a)
216 {
217  clearColorR = r;
218  clearColorG = g;
219  clearColorB = b;
220  clearColorA = a;
221 }
222 
223 float CGlCanvasBase::getClearColorR() const { return clearColorR; }
224 float CGlCanvasBase::getClearColorG() const { return clearColorG; }
225 float CGlCanvasBase::getClearColorB() const { return clearColorB; }
226 float CGlCanvasBase::getClearColorA() const { return clearColorA; }
228 {
229  m_openGLScene = scene;
230 }
231 
232 void CGlCanvasBase::setCameraPointing(float pointX, float pointY, float pointZ)
233 {
234  m_cameraParams.cameraPointingX = pointX;
235  m_cameraParams.cameraPointingY = pointY;
236  m_cameraParams.cameraPointingZ = pointZ;
237 }
238 
240 {
241  return m_cameraParams.cameraPointingX;
242 }
243 
245 {
246  return m_cameraParams.cameraPointingY;
247 }
248 
250 {
251  return m_cameraParams.cameraPointingZ;
252 }
253 
255 {
256 #if MRPT_HAS_OPENGL_GLUT
257  CTicTac tictac;
258  double At = 0.1;
259 
260  try
261  {
262  // Call PreRender user code:
263  preRender();
264 
266 
267  // Set static configs:
271 
272  // PART 1a: Set the viewport
273  // --------------------------------------
274  resizeViewport((GLsizei)width, (GLsizei)height);
275 
276  // Set the background color:
277  clearColors();
278 
279  if (m_openGLScene)
280  {
281  // Set the camera params in the scene:
282  if (!useCameraFromScene)
283  {
284  COpenGLViewport::Ptr view = m_openGLScene->getViewport("main");
285  if (!view)
286  {
288  "Fatal error: there is no 'main' viewport in the 3D "
289  "scene!");
290  }
291 
292  mrpt::opengl::CCamera& cam = view->getCamera();
293  updateCameraParams(cam);
294  }
295 
296  // PART 2: Set the MODELVIEW matrix
297  // --------------------------------------
299  glLoadIdentity();
300 
301  tictac.Tic();
302 
303  // PART 3: Draw primitives:
304  // --------------------------------------
305  m_openGLScene->render();
306 
307  } // end if "m_openGLScene!=nullptr"
308 
309  postRender();
310 
311  // Flush & swap buffers to disply new image:
312  glFlush();
313  swapBuffers();
314 
315  At = tictac.Tac();
316 
317  glPopAttrib();
318  }
319  catch (const std::exception& e)
320  {
321  glPopAttrib();
322  const std::string err_msg =
323  std::string("[CWxGLCanvasBase::Render] Exception!: ") +
324  std::string(e.what());
325  std::cerr << err_msg;
326  renderError(err_msg);
327  }
328  catch (...)
329  {
330  glPopAttrib();
331  std::cerr << "Runtime error!" << std::endl;
332  }
333 
334  return At;
335 #else
336  THROW_EXCEPTION("Cant render: MRPT was built without OpenGL");
337 #endif
338 }
339 
341 {
342  cameraElevationDeg = deg;
343 
344  if (cameraElevationDeg < -90.0f)
345  cameraElevationDeg = -90.0f;
346  else if (cameraElevationDeg > 90.0f)
347  cameraElevationDeg = 90.0f;
348 }
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:86
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:67
CamaraParams cameraParams() const
Returns a copy of CamaraParams See also getRefCameraParams(), setCameraParams(const CamaraParams &) ...
#define GL_MODELVIEW
Definition: glew.h:611
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:76
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:402
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:3535
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4199
void setZoomDistance(float z)
Definition: CCamera.h:69
mrpt::system::CTicTac CTicTac
Definition: utils/CTicTac.h:5
void setAzimuthDegrees(float ang)
Definition: CCamera.h:73
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:6372
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:92
GLubyte GLubyte b
Definition: glext.h:6372
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:4116
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:427
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:74
GLdouble GLdouble GLdouble r
Definition: glext.h:3711
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:3542
#define GL_ALL_ATTRIB_BITS
Definition: glew.h:272
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
int GLint
Definition: glew.h:210
GLenum GLint x
Definition: glext.h:3542
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:27
GLenum GLsizei GLsizei height
Definition: glext.h:3558
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:6372
GLenum const GLfloat * params
Definition: glext.h:3538
#define GL_TEXTURE_2D
Definition: glew.h:7505
int GLsizei
Definition: glew.h:211
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:47



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019