Main MRPT website > C++ reference for MRPT 1.9.9
COpenGLViewport.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 "opengl-precomp.h" // Precompiled header
11 
20 #include <mrpt/opengl/gl_utils.h>
21 
22 #include "opengl_internals.h"
23 
24 using namespace mrpt;
25 using namespace mrpt::poses;
26 using namespace mrpt::opengl;
27 using namespace mrpt::math;
29 using namespace std;
30 
31 //// using namespace mrpt::utils::metaprogramming;
32 
34 
35 //#define OPENGLVIEWPORT_ENABLE_TIMEPROFILING
36 
37 #if defined(OPENGLVIEWPORT_ENABLE_TIMEPROFILING)
38 mrpt::system::CTimeLogger glv_timlog;
39 #endif
40 
41 /*--------------------------------------------------------------
42 
43  IMPLEMENTATION OF COpenGLViewport
44 
45  ---------------------------------------------------------------*/
46 
47 /*--------------------------------------------------------------
48  Constructor
49  ---------------------------------------------------------------*/
50 COpenGLViewport::COpenGLViewport(COpenGLScene* parent, const string& name)
51  : m_camera(),
52  m_parent(parent),
53  m_isCloned(false),
54  m_isClonedCamera(false),
55  m_clonedViewport(),
56  m_name(name),
57  m_isTransparent(false),
58  m_borderWidth(0),
59  m_view_x(0),
60  m_view_y(0),
61  m_view_width(1),
62  m_view_height(1),
63  m_clip_min(0.1),
64  m_clip_max(10000),
65  m_custom_backgb_color(false),
66  m_background_color(0.6f, 0.6f, 0.6f),
67  m_isImageView(false),
68  m_imageview_img(),
69  m_objects(),
70  // OpenGL settings:
71  m_OpenGL_enablePolygonNicest(true),
72  m_lights()
73 {
74  // Default: one light from default direction
75  m_lights.push_back(CLight());
76  m_lights.push_back(CLight());
77 
78  m_lights[0].setPosition(1, 1, 1, 0);
79  m_lights[0].setDirection(-1, -1, -1);
80 
81  m_lights[1].light_ID = 1;
82  m_lights[1].setPosition(1, 2, -1, 0);
83  m_lights[1].setDirection(1, 2, 1);
84 
85  m_lights[1].color_diffuse[0] = 0.3f;
86  m_lights[1].color_diffuse[1] = 0.3f;
87  m_lights[1].color_diffuse[2] = 0.3f;
88 
89  m_lights[1].color_ambient[0] = 0.3f;
90  m_lights[1].color_ambient[1] = 0.3f;
91  m_lights[1].color_ambient[2] = 0.3f;
92 }
93 
94 /*--------------------------------------------------------------
95  Destructor
96  ---------------------------------------------------------------*/
98 /*--------------------------------------------------------------
99  setCloneView
100  ---------------------------------------------------------------*/
101 void COpenGLViewport::setCloneView(const string& clonedViewport)
102 {
103  clear();
104  m_isCloned = true;
105  m_clonedViewport = clonedViewport;
106 }
107 
108 /*--------------------------------------------------------------
109  setViewportPosition
110  ---------------------------------------------------------------*/
112  const double x, const double y, const double width, const double height)
113 {
114  MRPT_START
115  ASSERT_(m_view_width > 0);
116  ASSERT_(m_view_height > 0);
117 
118  m_view_x = x;
119  m_view_y = y;
122 
123  MRPT_END
124 }
125 
126 /*--------------------------------------------------------------
127  getViewportPosition
128  ---------------------------------------------------------------*/
130  double& x, double& y, double& width, double& height)
131 {
132  x = m_view_x;
133  y = m_view_y;
136 }
137 
138 /*--------------------------------------------------------------
139  clear
140  ---------------------------------------------------------------*/
142 /*--------------------------------------------------------------
143  insert
144  ---------------------------------------------------------------*/
146 {
147  m_objects.push_back(newObject);
148 }
149 
150 /*---------------------------------------------------------------
151  render
152  ---------------------------------------------------------------*/
154  const int render_width, const int render_height) const
155 {
156 #if MRPT_HAS_OPENGL_GLUT
157  const CRenderizable* it =
158  nullptr; // Declared here for usage in the "catch"
159  try
160  {
161  // Change viewport:
162  // -------------------------------------------
163  const GLint vx = m_view_x > 1
164  ? GLint(m_view_x)
165  : (m_view_x < 0 ? GLint(render_width + m_view_x)
166  : GLint(render_width * m_view_x));
167  const GLint vy = m_view_y > 1
168  ? GLint(m_view_y)
169  : (m_view_y < 0 ? GLint(render_height + m_view_y)
170  : GLint(render_height * m_view_y));
171 
172  GLint vw;
173  if (m_view_width > 1) // >1 -> absolute pixels:
174  vw = GLint(m_view_width);
175  else if (m_view_width < 0)
176  { // Negative numbers: Specify the right side coordinates instead of
177  // the width:
178  if (m_view_width >= -1)
179  vw = GLint(-render_width * m_view_width - vx + 1);
180  else
181  vw = GLint(-m_view_width - vx + 1);
182  }
183  else // A factor:
184  {
185  vw = GLint(render_width * m_view_width);
186  }
187 
188  GLint vh;
189  if (m_view_height > 1) // >1 -> absolute pixels:
190  vh = GLint(m_view_height);
191  else if (m_view_height < 0)
192  { // Negative numbers: Specify the right side coordinates instead of
193  // the width:
194  if (m_view_height >= -1)
195  vh = GLint(-render_height * m_view_height - vy + 1);
196  else
197  vh = GLint(-m_view_height - vy + 1);
198  }
199  else // A factor:
200  vh = GLint(render_height * m_view_height);
201 
202  glViewport(vx, vy, vw, vh);
203 
204  // Clear depth&/color buffers:
205  // -------------------------------------------
208 
209  glScissor(vx, vy, vw, vh);
210 
212  if (!m_isTransparent)
213  { // Clear color & depth buffers:
214  // Save?
215  GLdouble old_colors[4];
217  {
218  glGetDoublev(GL_COLOR_CLEAR_VALUE, old_colors);
219  glClearColor(
222  }
223 
224  glClear(
227 
228  // Restore old colors:
230  glClearColor(
231  old_colors[0], old_colors[1], old_colors[2], old_colors[3]);
232  }
233  else
234  { // Clear depth buffer only:
236  }
238 
239  // If we are in "image mode", rendering is much simpler: just set
240  // ortho projection and render the image quad:
241  if (m_isImageView)
242  {
243 #if defined(OPENGLVIEWPORT_ENABLE_TIMEPROFILING)
244  glv_timlog.enter("COpenGLViewport::render imageview");
245 #endif
246  // "Image mode" rendering:
247  // -----------------------------------
248  if (m_imageview_img) // should be ALWAYS true, but just in case!
249  {
250  // Note: The following code is inspired in the implementations:
251  // - libcvd, by Edward Rosten http://www.edwardrosten.com/cvd/
252  // - PTAM, by Klein & Murray
253  // http://www.robots.ox.ac.uk/~gk/PTAM/
254 
255  const mrpt::img::CImage* img = m_imageview_img.get();
256 
257  const int img_w = img->getWidth();
258  const int img_h = img->getHeight();
259 
260  if (img_w != 0 && img_h != 0)
261  {
262  // Prepare an ortho projection:
264  glLoadIdentity();
265 
266  // Need to adjust the aspect ratio?
267  const double ratio = vw * img_h / double(vh * img_w);
268  double ortho_w = img_w;
269  double ortho_h = img_h;
270  if (ratio > 1)
271  ortho_w *= ratio;
272  else if (ratio != 0)
273  ortho_h /= ratio;
274 
275  glOrtho(-0.5, ortho_h - 0.5, ortho_w - 0.5, -0.5, -1, 1);
276 
277  // Prepare raster pos & pixel copy direction in -Y.
278  glRasterPos2f(-0.5f, -0.5f);
279  glPixelZoom(vw / float(ortho_w), -vh / float(ortho_h));
280 
281  // Prepare image data types:
282  const GLenum img_type = GL_UNSIGNED_BYTE;
283  const int nBytesPerPixel = img->isColor() ? 3 : 1;
284  const bool is_RGB_order =
285  (!::strcmp(
286  img->getChannelsOrder(),
287  "RGB")); // Reverse RGB <-> BGR order?
288  const GLenum img_format =
289  nBytesPerPixel == 3 ? (is_RGB_order ? GL_RGB : GL_BGR)
290  : GL_LUMINANCE;
291 
292  // Send image data to OpenGL:
295  img->getRowStride() / nBytesPerPixel);
296  glDrawPixels(
297  img_w, img_h, img_format, img_type,
298  img->get_unsafe(0, 0));
299  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); // Reset
301  }
302  }
303 // done.
304 #if defined(OPENGLVIEWPORT_ENABLE_TIMEPROFILING)
305  glv_timlog.leave("COpenGLViewport::render imageview");
306 #endif
307  }
308  else
309  {
310  // Non "image mode" rendering:
311 
312  // Set camera:
313  // -------------------------------------------
315  glLoadIdentity();
316 
317  const CListOpenGLObjects* objectsToRender;
318  COpenGLViewport* viewForGetCamera;
319 
320  if (m_isCloned)
321  { // Clone: render someone's else objects.
322  ASSERT_(m_parent.get() != nullptr);
323 
324  COpenGLViewport::Ptr view =
325  m_parent->getViewport(m_clonedViewport);
326  if (!view)
328  "Cloned viewport '%s' not found in parent COpenGLScene",
329  m_clonedViewport.c_str());
330 
331  objectsToRender = &view->m_objects;
332  viewForGetCamera = m_isClonedCamera
333  ? view.get()
334  : const_cast<COpenGLViewport*>(this);
335  }
336  else
337  { // Normal case: render our own objects:
338  objectsToRender = &m_objects;
339  viewForGetCamera = const_cast<COpenGLViewport*>(this);
340  }
341 
342  // Get camera:
343  // 1st: if there is a CCamera in the scene:
344  CRenderizable::Ptr cam_ptr =
345  viewForGetCamera->getByClass<CCamera>();
346 
347  CCamera* myCamera = nullptr;
348  if (cam_ptr)
349  {
350  myCamera = dynamic_cast<CCamera*>(cam_ptr.get());
351  }
352 
353  // 2nd: the internal camera of all viewports:
354  if (!myCamera) myCamera = &viewForGetCamera->m_camera;
355 
357 
358  m_lastProjMat.azimuth = DEG2RAD(myCamera->m_azimuthDeg);
359  m_lastProjMat.elev = DEG2RAD(myCamera->m_elevationDeg);
360 
361  const float dis = max(0.01f, myCamera->m_distanceZoom);
363  myCamera->m_pointingX +
364  dis * cos(m_lastProjMat.azimuth) * cos(m_lastProjMat.elev);
366  myCamera->m_pointingY +
367  dis * sin(m_lastProjMat.azimuth) * cos(m_lastProjMat.elev);
369  myCamera->m_pointingZ + dis * sin(m_lastProjMat.elev);
370 
371  if (fabs(fabs(myCamera->m_elevationDeg) - 90) > 1e-6)
372  {
373  m_lastProjMat.up.x = 0;
374  m_lastProjMat.up.y = 0;
375  m_lastProjMat.up.z = 1;
376  }
377  else
378  {
379  float sgn = myCamera->m_elevationDeg > 0 ? 1 : -1;
380  m_lastProjMat.up.x =
381  -cos(DEG2RAD(myCamera->m_azimuthDeg)) * sgn;
382  m_lastProjMat.up.y =
383  -sin(DEG2RAD(myCamera->m_azimuthDeg)) * sgn;
384  m_lastProjMat.up.z = 0;
385  }
386 
387  m_lastProjMat.is_projective = myCamera->m_projectiveModel;
388  m_lastProjMat.FOV = myCamera->m_projectiveFOVdeg;
389  m_lastProjMat.pointing.x = myCamera->m_pointingX;
390  m_lastProjMat.pointing.y = myCamera->m_pointingY;
391  m_lastProjMat.pointing.z = myCamera->m_pointingZ;
392  m_lastProjMat.zoom = myCamera->m_distanceZoom;
393 
394  if (myCamera->m_projectiveModel)
395  {
396  gluPerspective(
397  myCamera->m_projectiveFOVdeg, vw / double(vh), m_clip_min,
398  m_clip_max);
400  }
401  else
402  {
403  const double ratio = vw / double(vh);
404  double Ax = myCamera->m_distanceZoom * 0.5;
405  double Ay = myCamera->m_distanceZoom * 0.5;
406 
407  if (ratio > 1)
408  Ax *= ratio;
409  else
410  {
411  if (ratio != 0) Ay /= ratio;
412  }
413 
414  glOrtho(-Ax, Ax, -Ay, Ay, -0.5 * m_clip_max, 0.5 * m_clip_max);
416  }
417 
418  if (myCamera->is6DOFMode())
419  {
420  // In 6DOFMode eye is set viewing towards the direction of the
421  // positive Z axis
422  // Up is set as Y axis
423  mrpt::poses::CPose3D viewDirection, pose, at;
424  viewDirection.z(+1);
425  pose = mrpt::poses::CPose3D(myCamera->getPose());
426  at = pose + viewDirection;
427  gluLookAt(
428  pose.x(), pose.y(), pose.z(), at.x(), at.y(), at.z(),
429  pose.getRotationMatrix()(0, 1),
430  pose.getRotationMatrix()(1, 1),
431  pose.getRotationMatrix()(2, 1));
433  }
434  else
435  {
436  // This command is common to ortho and perspective:
437  gluLookAt(
443  }
444 
445  // Optional pre-Render user code:
446  if (hasSubscribers())
447  {
448  mrptEventGLPreRender ev(this);
449  this->publishEvent(ev);
450  }
451 
452  // Global OpenGL settings:
453  // ---------------------------------
454  glHint(
458 
459  // Render objects:
460  // -------------------------------------------
462  glLoadIdentity();
463 
465  glDepthFunc(GL_LEQUAL); // GL_LESS
466 
467  // Setup lights
468  // -------------------------------------------
474 
475  for (size_t i = 0; i < m_lights.size(); i++)
476  m_lights[i].sendToOpenGL();
477 
478  // Render all the objects:
479  // -------------------------------------------
481 
482  } // end of non "image mode" rendering
483 
484  // Finally, draw the border:
485  // --------------------------------
486  if (m_borderWidth > 0)
487  {
489  glColor4f(0, 0, 0, 1);
491 
493  glLoadIdentity();
495  glLoadIdentity();
496 
497  glDisable(GL_LIGHTING); // Disable lights when drawing lines
499  glVertex2f(-1, -1);
500  glVertex2f(-1, 1);
501  glVertex2f(1, 1);
502  glVertex2f(1, -1);
503  glEnd();
504  glEnable(GL_LIGHTING); // Disable lights when drawing lines
505 
507  }
508 
509  // Optional post-Render user code:
510  if (hasSubscribers())
511  {
512  mrptEventGLPostRender ev(this);
513  this->publishEvent(ev);
514  }
515  }
516  catch (exception& e)
517  {
518  string msg;
519  if (it != nullptr)
520  msg = format(
521  "Exception while rendering a class '%s'\n%s",
522  it->GetRuntimeClass()->className, e.what());
523  else
524  msg = format("Exception while rendering:\n%s", e.what());
525 
526  THROW_EXCEPTION(msg);
527  }
528  catch (...)
529  {
530  THROW_EXCEPTION("Runtime error!");
531  }
532 #else
533  MRPT_UNUSED_PARAM(render_width);
534  MRPT_UNUSED_PARAM(render_height);
536  "The MRPT has been compiled with MRPT_HAS_OPENGL_GLUT=0! OpenGL "
537  "functions are not implemented");
538 #endif
539 }
540 
543 {
544  // Save data:
548 
549  // Added in v1:
552 
553  // Save objects:
554  uint32_t n;
555  n = (uint32_t)m_objects.size();
556  out << n;
558  it != m_objects.end(); ++it)
559  out << **it;
560 
561  // Added in v2: Global OpenGL settings:
563 
564  // Added in v3: Lights
565  out << m_lights;
566 }
567 
570 {
571  switch (version)
572  {
573  case 0:
574  case 1:
575  case 2:
576  case 3:
577  {
578  // Load data:
583 
584  // in v1:
585  if (version >= 1)
586  {
590  }
591  else
592  {
593  m_custom_backgb_color = false;
594  }
595 
596  // Load objects:
597  uint32_t n;
598  in >> n;
599  clear();
600  m_objects.resize(n);
601 
602  for_each(
603  m_objects.begin(), m_objects.end(), ObjectReadFromStream(&in));
604 
605  // Added in v2: Global OpenGL settings:
606  if (version >= 2)
607  {
609  }
610  else
611  {
612  // Defaults
613  }
614 
615  // Added in v3: Lights
616  if (version >= 3)
617  in >> m_lights;
618  else
619  {
620  // Default: one light from default direction
621  m_lights.clear();
622  m_lights.push_back(CLight());
623  }
624  }
625  break;
626  default:
628  };
629 }
630 
631 /*---------------------------------------------------------------
632  getByName
633  ---------------------------------------------------------------*/
635 {
636  for (CListOpenGLObjects::iterator it = m_objects.begin();
637  it != m_objects.end(); ++it)
638  {
639  if ((*it)->m_name == str)
640  return *it;
641  else if (
642  (*it)->GetRuntimeClass() ==
644  {
645  CRenderizable::Ptr ret =
646  std::dynamic_pointer_cast<CSetOfObjects>(*it)->getByName(str);
647  if (ret) return ret;
648  }
649  }
650  return CRenderizable::Ptr();
651 }
652 
653 /*---------------------------------------------------------------
654  initializeAllTextures
655  ---------------------------------------------------------------*/
657 {
658 #if MRPT_HAS_OPENGL_GLUT
659  for (CListOpenGLObjects::iterator it = m_objects.begin();
660  it != m_objects.end(); ++it)
661  {
662  if (IS_DERIVED(*it, CTexturedObject))
663  std::dynamic_pointer_cast<CTexturedObject>(*it)
664  ->loadTextureInOpenGL();
665  else if (IS_CLASS(*it, CSetOfObjects))
666  std::dynamic_pointer_cast<CSetOfObjects>(*it)
668  }
669 #endif
670 }
671 
672 void COpenGLViewport::dumpListOfObjects(std::vector<std::string>& lst)
673 {
674  for (CListOpenGLObjects::iterator it = m_objects.begin();
675  it != m_objects.end(); ++it)
676  {
677  // Single obj:
678  string s((*it)->GetRuntimeClass()->className);
679  if ((*it)->m_name.size())
680  s += string(" (") + (*it)->m_name + string(")");
681  lst.emplace_back(s);
682 
683  if ((*it)->GetRuntimeClass() ==
685  {
686  std::vector<std::string> auxLst;
687 
688  dynamic_cast<CSetOfObjects*>(it->get())->dumpListOfObjects(auxLst);
689 
690  for (size_t i = 0; i < auxLst.size(); i++)
691  lst.emplace_back(string(" ") + auxLst[i]);
692  }
693  }
694 }
695 
696 /*--------------------------------------------------------------
697  removeObject
698  ---------------------------------------------------------------*/
700 {
701  for (CListOpenGLObjects::iterator it = m_objects.begin();
702  it != m_objects.end(); ++it)
703  if (*it == obj)
704  {
705  m_objects.erase(it);
706  return;
707  }
708  else if (
709  (*it)->GetRuntimeClass() ==
711  dynamic_cast<CSetOfObjects*>(it->get())->removeObject(obj);
712 }
713 
714 /*--------------------------------------------------------------
715  setViewportClipDistances
716  ---------------------------------------------------------------*/
718  const double clip_min, const double clip_max)
719 {
720  ASSERT_(clip_max > clip_min);
721 
722  m_clip_min = clip_min;
723  m_clip_max = clip_max;
724 }
725 
726 /*--------------------------------------------------------------
727  getViewportClipDistances
728  ---------------------------------------------------------------*/
730  double& clip_min, double& clip_max) const
731 {
732  clip_min = m_clip_min;
733  clip_max = m_clip_max;
734 }
735 
736 /*--------------------------------------------------------------
737  get3DRayForPixelCoord
738  ---------------------------------------------------------------*/
740  const double x_coord, const double y_coord, mrpt::math::TLine3D& out_ray,
741  mrpt::poses::CPose3D* out_cameraPose) const
742 {
743  ASSERTDEB_(
745 
746  const double ASPECT =
748 
749  // unitary vector between (eye) -> (pointing):
750  TPoint3D pointing_dir;
751  pointing_dir.x = -cos(m_lastProjMat.azimuth) * cos(m_lastProjMat.elev);
752  pointing_dir.y = -sin(m_lastProjMat.azimuth) * cos(m_lastProjMat.elev);
753  pointing_dir.z = -sin(m_lastProjMat.elev);
754 
755  // The camera X vector (in 3D) can be computed from the camera azimuth
756  // angle:
757  TPoint3D cam_x_3d;
758  cam_x_3d.x = -sin(m_lastProjMat.azimuth);
759  cam_x_3d.y = cos(m_lastProjMat.azimuth);
760  cam_x_3d.z = 0;
761 
762  // The camera real UP vector (in 3D) is the cross product:
763  // X3d x pointing_dir:
764  TPoint3D cam_up_3d;
765  crossProduct3D(cam_x_3d, pointing_dir, cam_up_3d);
766 
768  {
769  // Ortho projection:
770  // -------------------------------
771  double Ax = m_lastProjMat.zoom * 0.5;
772  double Ay = Ax;
773 
774  if (ASPECT > 1)
775  Ax *= ASPECT;
776  else
777  {
778  if (ASPECT != 0) Ay /= ASPECT;
779  }
780 
781  const double point_lx =
782  (-0.5 + x_coord / m_lastProjMat.viewport_width) * 2 * Ax;
783  const double point_ly =
784  -(-0.5 + y_coord / m_lastProjMat.viewport_height) * 2 * Ay;
785 
786  const TPoint3D ray_origin(
787  m_lastProjMat.eye.x + point_lx * cam_x_3d.x +
788  point_ly * cam_up_3d.x,
789  m_lastProjMat.eye.y + point_lx * cam_x_3d.y +
790  point_ly * cam_up_3d.y,
791  m_lastProjMat.eye.z + point_lx * cam_x_3d.z +
792  point_ly * cam_up_3d.z);
793 
794  out_ray.pBase = ray_origin;
795  out_ray.director[0] = pointing_dir.x;
796  out_ray.director[1] = pointing_dir.y;
797  out_ray.director[2] = pointing_dir.z;
798  }
799  else
800  {
801  // Perspective camera
802  // -------------------------------
803 
804  // JL: This can derived from:
805  // http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml
806  // where one arrives to:
807  // tan(FOVx/2) = ASPECT_RATIO * tan(FOVy/2)
808  //
809  const double FOVy = DEG2RAD(m_lastProjMat.FOV);
810  const double FOVx = 2.0 * atan(ASPECT * tan(FOVy * 0.5));
811 
812  const double ang_horz =
813  (-0.5 + x_coord / m_lastProjMat.viewport_width) * FOVx;
814  const double ang_vert =
815  -(-0.5 + y_coord / m_lastProjMat.viewport_height) * FOVy;
816 
817  const TPoint3D l(
818  tan(ang_horz), tan(ang_vert),
819  1.0); // Point in camera local reference frame
820 
821  const TPoint3D ray_director(
822  l.x * cam_x_3d.x + l.y * cam_up_3d.x + l.z * pointing_dir.x,
823  l.x * cam_x_3d.y + l.y * cam_up_3d.y + l.z * pointing_dir.y,
824  l.x * cam_x_3d.z + l.y * cam_up_3d.z + l.z * pointing_dir.z);
825 
826  // Set out ray:
827  out_ray.pBase = m_lastProjMat.eye;
828  out_ray.director[0] = ray_director.x;
829  out_ray.director[1] = ray_director.y;
830  out_ray.director[2] = ray_director.z;
831 
832  } // end projective
833 
834  // Camera pose:
835  if (out_cameraPose)
836  {
838  M.get_unsafe(0, 0) = cam_x_3d.x;
839  M.get_unsafe(1, 0) = cam_x_3d.y;
840  M.get_unsafe(2, 0) = cam_x_3d.z;
841  M.get_unsafe(3, 0) = 0;
842 
843  M.get_unsafe(0, 1) = cam_up_3d.x;
844  M.get_unsafe(1, 1) = cam_up_3d.y;
845  M.get_unsafe(2, 1) = cam_up_3d.z;
846  M.get_unsafe(3, 1) = 0;
847 
848  M.get_unsafe(0, 2) = pointing_dir.x;
849  M.get_unsafe(1, 2) = pointing_dir.y;
850  M.get_unsafe(2, 2) = pointing_dir.z;
851  M.get_unsafe(3, 2) = 0;
852 
853  M.get_unsafe(0, 3) = m_lastProjMat.eye.x;
854  M.get_unsafe(1, 3) = m_lastProjMat.eye.y;
855  M.get_unsafe(2, 3) = m_lastProjMat.eye.z;
856  M.get_unsafe(3, 3) = 1;
857 
858  *out_cameraPose = CPose3D(M);
859  }
860 }
861 
862 MRPT_TODO("Implement a setCurrentCameraFromPose() method")
863 
864 void COpenGLViewport::getCurrentCameraPose(
865  mrpt::poses::CPose3D& out_cameraPose) const
866 {
868  get3DRayForPixelCoord(0, 0, dum, &out_cameraPose);
869 }
870 
871 /** Resets the viewport to a normal 3D viewport \sa setCloneView, setImageView
872  */
874 {
875  // If this was a m_isImageView, remove the quad object:
877 
878  m_isCloned = false;
879  m_isClonedCamera = false;
880  m_isImageView = false;
881 }
882 
884 {
886 }
888 {
890 }
891 
893  const mrpt::img::CImage& img, bool is_fast)
894 {
895  // If this is the first time, we have to create the quad object:
897  m_imageview_img = mrpt::make_aligned_shared<mrpt::img::CImage>();
898  m_isImageView = true;
899 
900  // Update texture image:
901  mrpt::img::CImage* my_img = m_imageview_img.get();
902 
903  if (!is_fast)
904  *my_img = img;
905  else
906  my_img->copyFastFrom(*const_cast<mrpt::img::CImage*>(&img));
907 }
908 
909 /** Evaluates the bounding box of this object (including possible children) in
910  * the coordinate frame of the object parent. */
912  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
913 {
914  bb_min = TPoint3D(
915  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
916  std::numeric_limits<double>::max());
917  bb_max = TPoint3D(
918  -std::numeric_limits<double>::max(),
919  -std::numeric_limits<double>::max(),
920  -std::numeric_limits<double>::max());
921 
923  it != m_objects.end(); ++it)
924  {
925  TPoint3D child_bbmin(
926  std::numeric_limits<double>::max(),
927  std::numeric_limits<double>::max(),
928  std::numeric_limits<double>::max());
929  TPoint3D child_bbmax(
930  -std::numeric_limits<double>::max(),
931  -std::numeric_limits<double>::max(),
932  -std::numeric_limits<double>::max());
933  (*it)->getBoundingBox(child_bbmin, child_bbmax);
934 
935  keep_min(bb_min.x, child_bbmin.x);
936  keep_min(bb_min.y, child_bbmin.y);
937  keep_min(bb_min.z, child_bbmin.z);
938 
939  keep_max(bb_max.x, child_bbmax.x);
940  keep_max(bb_max.y, child_bbmax.y);
941  keep_max(bb_max.z, child_bbmax.z);
942  }
943 }
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:140
#define GL_BGR
Definition: glew.h:1239
An object for reading objects from a stream, intended for being used in STL algorithms.
Scalar * iterator
Definition: eigen_plugins.h:26
opengl::CListOpenGLObjects m_objects
The list of objects that comprise the 3D scene.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
CRenderizable::Ptr getByName(const std::string &str)
Returns the first object with a given name, or nullptr if not found.
void get3DRayForPixelCoord(const double x_coord, const double y_coord, mrpt::math::TLine3D &out_ray, mrpt::poses::CPose3D *out_cameraPose=nullptr) const
Compute the 3D ray corresponding to a given pixel; this can be used to allow the user to pick and sel...
#define MRPT_START
Definition: exceptions.h:262
void setImageView_fast(mrpt::img::CImage &img)
Just like setImageView but moves the internal image memory instead of making a copy, so it&#39;s faster but empties the input image.
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
double GLdouble
Definition: glew.h:219
GLAPI void GLAPIENTRY glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)
A base class for all OpenGL objects with loadable textures.
bool m_isTransparent
Whether to clear color buffer.
A set of object, which are referenced to the coordinates framework established in this object...
Definition: CSetOfObjects.h:28
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
uint32_t m_borderWidth
Default=0, the border around the viewport.
void setCloneView(const std::string &clonedViewport)
Set this viewport as a clone of some other viewport, given its name - as a side effect, current list of internal OpenGL objects is cleared.
#define GL_STENCIL_BUFFER_BIT
Definition: glew.h:261
float azimuth
Camera elev & azimuth, in radians.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
#define GL_MODELVIEW
Definition: glew.h:610
#define GL_FRONT_AND_BACK
Definition: glew.h:321
double DEG2RAD(const double x)
Degrees to radians.
std::deque< CRenderizable::Ptr > CListOpenGLObjects
A list of objects pointers, automatically managing memory free at destructor, and managing copies cor...
std::vector< CLight > m_lights
GLenum GLsizei n
Definition: glext.h:5074
void getViewportClipDistances(double &clip_min, double &clip_max) const
Get the current min/max clip depth distances of the rendering frustum (default: 0.1 - 10000)
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:43
void setImageView(const mrpt::img::CImage &img)
Set this viewport into "image view"-mode, where an image is efficiently drawn (fitting the viewport a...
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
void setViewportClipDistances(const double clip_min, const double clip_max)
Set the min/max clip depth distances of the rendering frustum (default: 0.1 - 10000) ...
TPoint3D pBase
Base point.
STL namespace.
#define GL_UNSIGNED_BYTE
Definition: glew.h:302
mrpt::img::CImage::Ptr m_imageview_img
The image to display, after calling setImageView()
#define GL_PERSPECTIVE_CORRECTION_HINT
Definition: glew.h:450
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
#define GL_COLOR_MATERIAL
Definition: glew.h:392
#define GL_DEPTH_TEST
Definition: glew.h:401
#define GL_SMOOTH
Definition: glew.h:635
mrpt::math::TPoint3D eye
The camera is here.
GLdouble s
Definition: glext.h:3676
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
#define GL_LIGHTING
Definition: glew.h:385
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
A viewport within a COpenGLScene, containing a set of OpenGL objects to render.
GLAPI void GLAPIENTRY glLoadIdentity(void)
mrpt::safe_ptr< COpenGLScene > m_parent
The scene that contains this viewport.
GLenum GLsizei width
Definition: glext.h:3531
void crossProduct3D(const T &v0, const U &v1, V &vOut)
Computes the cross product of two 3D vectors, returning a vector normal to both.
Definition: geometry.h:803
Each of the possible lights of a 3D scene.
Definition: CLight.h:24
unsigned char uint8_t
Definition: rptypes.h:41
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:90
bool m_isClonedCamera
Set by setCloneCamera.
double m_clip_min
The min/max clip depth distances (default: 0.1 - 10000)
GLAPI void GLAPIENTRY glDrawPixels(GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels)
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
GLAPI void GLAPIENTRY glGetDoublev(GLenum pname, GLdouble *params)
#define GL_DEPTH_BUFFER_BIT
Definition: glew.h:259
#define IS_DERIVED(ptrObj, class_name)
Evaluates to true if a pointer to an object (derived from mrpt::rtti::CObject) is an instance of the ...
Definition: CObject.h:109
#define GL_COLOR_BUFFER_BIT
Definition: glew.h:265
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
bool m_isImageView
Set by setImageView.
#define GL_COLOR_CLEAR_VALUE
Definition: glew.h:443
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
GLAPI void GLAPIENTRY glDepthFunc(GLenum func)
const char * className
Definition: CObject.h:33
#define GL_LEQUAL
Definition: glew.h:246
void render(const int render_width, const int render_height) const
Render the objects in this viewport (called from COpenGLScene only)
GLint GLvoid * img
Definition: glext.h:3763
GLAPI void GLAPIENTRY glColorMaterial(GLenum face, GLenum mode)
GLAPI void GLAPIENTRY glPixelZoom(GLfloat xfactor, GLfloat yfactor)
void removeObject(const CRenderizable::Ptr &obj)
Removes the given object from the scene (it also deletes the object to free its memory).
#define GL_PROJECTION
Definition: glew.h:611
void internal_setImageView_fast(const mrpt::img::CImage &img, bool is_fast)
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
#define GL_RGB
Definition: glew.h:623
double x
X,Y,Z coordinates.
mrpt::math::TPoint3D up
Up vector of the camera.
TLastProjectiveMatrixInfo m_lastProjMat
Info updated with each "render()" and used in "get3DRayForPixelCoord".
void setNormalMode()
Resets the viewport to a normal 3D viewport.
GLAPI void GLAPIENTRY glBegin(GLenum mode)
GLsizei const GLchar ** string
Definition: glext.h:4101
#define GL_LINE_LOOP
Definition: glew.h:274
GLAPI void GLAPIENTRY glHint(GLenum target, GLenum mode)
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
void copyFastFrom(CImage &o)
Moves an image from another object, erasing the origin image in the process (this is much faster than...
Definition: CImage.cpp:168
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
virtual ~COpenGLViewport()
Destructor: clears all objects.
mrpt::math::TPoint3D pointing
The camera points to here.
double director[3]
Director vector.
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) ...
#define CLASS_ID_NAMESPACE(class_name, namespaceName)
Definition: CObject.h:88
unsigned int GLenum
Definition: glew.h:206
GLAPI void GLAPIENTRY glLightModeli(GLenum pname, GLint param)
static void checkOpenGLError()
Checks glGetError and throws an exception if an error situation is found.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLAPI void GLAPIENTRY glScissor(GLint x, GLint y, GLsizei width, GLsizei height)
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
#define GL_UNPACK_ROW_LENGTH
Definition: glew.h:481
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
void clear()
Delete all internal obejcts.
#define MRPT_TODO(x)
Definition: common.h:129
#define GL_NICEST
Definition: glew.h:569
An event sent by an mrpt::opengl::COpenGLViewport just after clearing the viewport and setting the GL...
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
Definition: exceptions.h:205
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::rtti::CObject) is of the give...
Definition: CObject.h:103
An event sent by an mrpt::opengl::COpenGLViewport after calling the scene OpenGL drawing primitives a...
#define GL_SCISSOR_TEST
Definition: glew.h:440
GLAPI void GLAPIENTRY glClear(GLbitfield mask)
#define MRPT_END
Definition: exceptions.h:266
void publishEvent(const mrptEvent &e) const
Called when you want this object to emit an event to all the observers currently subscribed to this o...
Definition: CObservable.cpp:48
bool hasSubscribers() const
Can be called by a derived class before preparing an event for publishing with publishEvent to determ...
Definition: CObservable.h:55
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLuint const GLchar * name
Definition: glext.h:4054
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:59
GLAPI void GLAPIENTRY glEnd(void)
std::string m_clonedViewport
Only if m_isCloned=true.
#define GL_POLYGON_SMOOTH_HINT
Definition: glew.h:453
mrpt::img::TColorf m_background_color
used only if m_custom_backgb_color
GLenum GLint GLint y
Definition: glext.h:3538
#define GL_LIGHT_MODEL_TWO_SIDE
Definition: glew.h:387
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
T::Ptr getByClass(const size_t &ith=0) const
Returns the i&#39;th object of a given class (or of a descendant class), or nullptr (an empty smart point...
int GLint
Definition: glew.h:209
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
GLAPI void GLAPIENTRY glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)
GLenum GLint x
Definition: glext.h:3538
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL) ...
void getRotationMatrix(mrpt::math::CMatrixDouble33 &ROT) const
Get the 3x3 rotation matrix.
Definition: CPose3D.h:232
Lightweight 3D point.
void renderSetOfObjects(const mrpt::opengl::CListOpenGLObjects &objs)
For each object in the list:
Definition: gl_utils.cpp:35
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:30
GLenum GLsizei GLsizei height
Definition: glext.h:3554
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLAPI void GLAPIENTRY glVertex2f(GLfloat x, GLfloat y)
GLAPI void GLAPIENTRY glDisable(GLenum cap)
CRenderizable::Ptr getByName(const std::string &str)
Returns the first object with a given name, or a nullptr pointer if not found.
void setViewportPosition(const double x, const double y, const double width, const double height)
Change the viewport position and dimension on the rendering window.
void dumpListOfObjects(std::vector< std::string > &lst)
Retrieves a list of all objects in text form.
#define GL_AMBIENT_AND_DIFFUSE
Definition: glew.h:608
#define GL_ACCUM_BUFFER_BIT
Definition: glew.h:260
GLAPI void GLAPIENTRY glPixelStorei(GLenum pname, GLint param)
void getViewportPosition(double &x, double &y, double &width, double &height)
Get the current viewport position and dimension on the rendering window.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
void insert(const CRenderizable::Ptr &newObject)
Insert a new object into the list.
double m_view_x
The viewport position [0,1].
GLAPI void GLAPIENTRY glRasterPos2f(GLfloat x, GLfloat y)
#define GL_LUMINANCE
Definition: glew.h:625
#define GL_TRUE
Definition: glew.h:293
opengl::CCamera m_camera
The camera associated to the viewport.
std::string m_name
The viewport&#39;s name.
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
bool m_isCloned
Set by setCloneView.
3D line, represented by a base point and a director vector.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
#define GL_FASTEST
Definition: glew.h:568



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019