MRPT  1.9.9
COpenGLViewport.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 "opengl-precomp.h" // Precompiled header
11 
16 #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 
32 
33 //#define OPENGLVIEWPORT_ENABLE_TIMEPROFILING
34 
35 #if defined(OPENGLVIEWPORT_ENABLE_TIMEPROFILING)
36 mrpt::system::CTimeLogger glv_timlog;
37 #endif
38 
39 /*--------------------------------------------------------------
40 
41  IMPLEMENTATION OF COpenGLViewport
42 
43  ---------------------------------------------------------------*/
44 
45 /*--------------------------------------------------------------
46  Constructor
47  ---------------------------------------------------------------*/
48 COpenGLViewport::COpenGLViewport(COpenGLScene* parent, const string& name)
49  : m_camera(),
50  m_parent(parent),
51 
52  m_clonedViewport(),
53  m_name(name),
54 
55  m_background_color(0.6f, 0.6f, 0.6f),
56 
57  m_imageview_img(),
58  m_objects(),
59 
60  m_lights()
61 {
62  // Default: one light from default direction
63  m_lights.emplace_back();
64  m_lights.emplace_back();
65 
66  m_lights[0].setPosition(1, 1, 1, 0);
67  m_lights[0].setDirection(-1, -1, -1);
68 
69  m_lights[1].light_ID = 1;
70  m_lights[1].setPosition(1, 2, -1, 0);
71  m_lights[1].setDirection(1, 2, 1);
72 
73  m_lights[1].color_diffuse[0] = 0.3f;
74  m_lights[1].color_diffuse[1] = 0.3f;
75  m_lights[1].color_diffuse[2] = 0.3f;
76 
77  m_lights[1].color_ambient[0] = 0.3f;
78  m_lights[1].color_ambient[1] = 0.3f;
79  m_lights[1].color_ambient[2] = 0.3f;
80 }
81 
82 /*--------------------------------------------------------------
83  Destructor
84  ---------------------------------------------------------------*/
86 /*--------------------------------------------------------------
87  setCloneView
88  ---------------------------------------------------------------*/
89 void COpenGLViewport::setCloneView(const string& clonedViewport)
90 {
91  clear();
92  m_isCloned = true;
93  m_clonedViewport = clonedViewport;
94 }
95 
96 /*--------------------------------------------------------------
97  setViewportPosition
98  ---------------------------------------------------------------*/
100  const double x, const double y, const double width, const double height)
101 {
102  MRPT_START
103  ASSERT_(m_view_width > 0);
104  ASSERT_(m_view_height > 0);
105 
106  m_view_x = x;
107  m_view_y = y;
110 
111  MRPT_END
112 }
113 
114 /*--------------------------------------------------------------
115  getViewportPosition
116  ---------------------------------------------------------------*/
118  double& x, double& y, double& width, double& height)
119 {
120  x = m_view_x;
121  y = m_view_y;
124 }
125 
126 /*--------------------------------------------------------------
127  clear
128  ---------------------------------------------------------------*/
130 /*--------------------------------------------------------------
131  insert
132  ---------------------------------------------------------------*/
134 {
135  m_objects.push_back(newObject);
136 }
137 
138 /*---------------------------------------------------------------
139  render
140  ---------------------------------------------------------------*/
142  const int render_width, const int render_height) const
143 {
144 #if MRPT_HAS_OPENGL_GLUT
145  const CRenderizable* it =
146  nullptr; // Declared here for usage in the "catch"
147  try
148  {
149  // Change viewport:
150  // -------------------------------------------
151  const GLint vx = m_view_x > 1
152  ? GLint(m_view_x)
153  : (m_view_x < 0 ? GLint(render_width + m_view_x)
154  : GLint(render_width * m_view_x));
155  const GLint vy = m_view_y > 1
156  ? GLint(m_view_y)
157  : (m_view_y < 0 ? GLint(render_height + m_view_y)
158  : GLint(render_height * m_view_y));
159 
160  GLint vw;
161  if (m_view_width > 1) // >1 -> absolute pixels:
162  vw = GLint(m_view_width);
163  else if (m_view_width < 0)
164  { // Negative numbers: Specify the right side coordinates instead of
165  // the width:
166  if (m_view_width >= -1)
167  vw = GLint(-render_width * m_view_width - vx + 1);
168  else
169  vw = GLint(-m_view_width - vx + 1);
170  }
171  else // A factor:
172  {
173  vw = GLint(render_width * m_view_width);
174  }
175 
176  GLint vh;
177  if (m_view_height > 1) // >1 -> absolute pixels:
178  vh = GLint(m_view_height);
179  else if (m_view_height < 0)
180  { // Negative numbers: Specify the right side coordinates instead of
181  // the width:
182  if (m_view_height >= -1)
183  vh = GLint(-render_height * m_view_height - vy + 1);
184  else
185  vh = GLint(-m_view_height - vy + 1);
186  }
187  else // A factor:
188  vh = GLint(render_height * m_view_height);
189 
190  glViewport(vx, vy, vw, vh);
191 
192  // Clear depth&/color buffers:
193  // -------------------------------------------
196 
197  glScissor(vx, vy, vw, vh);
198 
200  if (!m_isTransparent)
201  { // Clear color & depth buffers:
202  // Save?
203  GLdouble old_colors[4];
205  {
206  glGetDoublev(GL_COLOR_CLEAR_VALUE, old_colors);
207  glClearColor(
210  }
211 
212  glClear(
215 
216  // Restore old colors:
218  glClearColor(
219  old_colors[0], old_colors[1], old_colors[2], old_colors[3]);
220  }
221  else
222  { // Clear depth buffer only:
224  }
226 
227  // If we are in "image mode", rendering is much simpler: just set
228  // ortho projection and render the image quad:
229  if (m_isImageView)
230  {
231 #if defined(OPENGLVIEWPORT_ENABLE_TIMEPROFILING)
232  glv_timlog.enter("COpenGLViewport::render imageview");
233 #endif
234  // "Image mode" rendering:
235  // -----------------------------------
236  if (m_imageview_img) // should be ALWAYS true, but just in case!
237  {
238  // Note: The following code is inspired in the implementations:
239  // - libcvd, by Edward Rosten http://www.edwardrosten.com/cvd/
240  // - PTAM, by Klein & Murray
241  // http://www.robots.ox.ac.uk/~gk/PTAM/
242 
244 
245  const int img_w = img->getWidth();
246  const int img_h = img->getHeight();
247 
248  if (img_w != 0 && img_h != 0)
249  {
250  // Prepare an ortho projection:
252  glLoadIdentity();
253 
254  // Need to adjust the aspect ratio?
255  const double ratio = vw * img_h / double(vh * img_w);
256  double ortho_w = img_w;
257  double ortho_h = img_h;
258  if (ratio > 1)
259  ortho_w *= ratio;
260  else if (ratio != 0)
261  ortho_h /= ratio;
262 
263  glOrtho(-0.5, ortho_h - 0.5, ortho_w - 0.5, -0.5, -1, 1);
264 
265  // Prepare raster pos & pixel copy direction in -Y.
266  glRasterPos2f(-0.5f, -0.5f);
267  glPixelZoom(vw / float(ortho_w), -vh / float(ortho_h));
268 
269  // Prepare image data types:
270  const GLenum img_type = GL_UNSIGNED_BYTE;
271  const int nBytesPerPixel = img->isColor() ? 3 : 1;
272  // Reverse RGB <-> BGR order?
273  const bool is_RGB_order =
274  (img->getChannelsOrder() == std::string("RGB"));
275  const GLenum img_format =
276  nBytesPerPixel == 3 ? (is_RGB_order ? GL_RGB : GL_BGR)
277  : GL_LUMINANCE;
278 
279  // Send image data to OpenGL:
281  glPixelStorei(GL_UNPACK_ROW_LENGTH, img->getWidth());
282  glDrawPixels(
283  img_w, img_h, img_format, img_type,
284  img->ptrLine<uint8_t>(0));
285  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); // Reset
287  }
288  }
289 // done.
290 #if defined(OPENGLVIEWPORT_ENABLE_TIMEPROFILING)
291  glv_timlog.leave("COpenGLViewport::render imageview");
292 #endif
293  }
294  else
295  {
296  // Non "image mode" rendering:
297 
298  // Set camera:
299  // -------------------------------------------
301  glLoadIdentity();
302 
303  const CListOpenGLObjects* objectsToRender;
304  COpenGLViewport* viewForGetCamera;
305 
306  if (m_isCloned)
307  { // Clone: render someone's else objects.
308  ASSERT_(m_parent.get() != nullptr);
309 
310  COpenGLViewport::Ptr view =
311  m_parent->getViewport(m_clonedViewport);
312  if (!view)
314  "Cloned viewport '%s' not found in parent COpenGLScene",
315  m_clonedViewport.c_str());
316 
317  objectsToRender = &view->m_objects;
318  viewForGetCamera = m_isClonedCamera
319  ? view.get()
320  : const_cast<COpenGLViewport*>(this);
321  }
322  else
323  { // Normal case: render our own objects:
324  objectsToRender = &m_objects;
325  viewForGetCamera = const_cast<COpenGLViewport*>(this);
326  }
327 
328  // Get camera:
329  // 1st: if there is a CCamera in the scene:
330  CRenderizable::Ptr cam_ptr =
331  viewForGetCamera->getByClass<CCamera>();
332 
333  CCamera* myCamera = nullptr;
334  if (cam_ptr)
335  {
336  myCamera = dynamic_cast<CCamera*>(cam_ptr.get());
337  }
338 
339  // 2nd: the internal camera of all viewports:
340  if (!myCamera) myCamera = &viewForGetCamera->m_camera;
341 
343 
344  m_lastProjMat.azimuth = DEG2RAD(myCamera->m_azimuthDeg);
345  m_lastProjMat.elev = DEG2RAD(myCamera->m_elevationDeg);
346 
347  const float dis = max(0.01f, myCamera->m_distanceZoom);
349  myCamera->m_pointingX +
350  dis * cos(m_lastProjMat.azimuth) * cos(m_lastProjMat.elev);
352  myCamera->m_pointingY +
353  dis * sin(m_lastProjMat.azimuth) * cos(m_lastProjMat.elev);
355  myCamera->m_pointingZ + dis * sin(m_lastProjMat.elev);
356 
357  if (fabs(fabs(myCamera->m_elevationDeg) - 90) > 1e-6)
358  {
359  m_lastProjMat.up.x = 0;
360  m_lastProjMat.up.y = 0;
361  m_lastProjMat.up.z = 1;
362  }
363  else
364  {
365  float sgn = myCamera->m_elevationDeg > 0 ? 1 : -1;
366  m_lastProjMat.up.x =
367  -cos(DEG2RAD(myCamera->m_azimuthDeg)) * sgn;
368  m_lastProjMat.up.y =
369  -sin(DEG2RAD(myCamera->m_azimuthDeg)) * sgn;
370  m_lastProjMat.up.z = 0;
371  }
372 
373  m_lastProjMat.is_projective = myCamera->m_projectiveModel;
374  m_lastProjMat.FOV = myCamera->m_projectiveFOVdeg;
375  m_lastProjMat.pointing.x = myCamera->m_pointingX;
376  m_lastProjMat.pointing.y = myCamera->m_pointingY;
377  m_lastProjMat.pointing.z = myCamera->m_pointingZ;
378  m_lastProjMat.zoom = myCamera->m_distanceZoom;
379 
380  if (myCamera->m_projectiveModel)
381  {
382  gluPerspective(
383  myCamera->m_projectiveFOVdeg, vw / double(vh), m_clip_min,
384  m_clip_max);
386  }
387  else
388  {
389  const double ratio = vw / double(vh);
390  double Ax = myCamera->m_distanceZoom * 0.5;
391  double Ay = myCamera->m_distanceZoom * 0.5;
392 
393  if (ratio > 1)
394  Ax *= ratio;
395  else
396  {
397  if (ratio != 0) Ay /= ratio;
398  }
399 
400  glOrtho(-Ax, Ax, -Ay, Ay, -0.5 * m_clip_max, 0.5 * m_clip_max);
402  }
403 
404  if (myCamera->is6DOFMode())
405  {
406  // In 6DOFMode eye is set viewing towards the direction of the
407  // positive Z axis
408  // Up is set as Y axis
409  mrpt::poses::CPose3D viewDirection, pose, at;
410  viewDirection.z(+1);
411  pose = mrpt::poses::CPose3D(myCamera->getPose());
412  at = pose + viewDirection;
413  gluLookAt(
414  pose.x(), pose.y(), pose.z(), at.x(), at.y(), at.z(),
415  pose.getRotationMatrix()(0, 1),
416  pose.getRotationMatrix()(1, 1),
417  pose.getRotationMatrix()(2, 1));
419  }
420  else
421  {
422  // This command is common to ortho and perspective:
423  gluLookAt(
429  }
430 
431  // Optional pre-Render user code:
432  if (hasSubscribers())
433  {
434  mrptEventGLPreRender ev(this);
435  this->publishEvent(ev);
436  }
437 
438  // Global OpenGL settings:
439  // ---------------------------------
440  glHint(
444 
445  // Render objects:
446  // -------------------------------------------
448  glLoadIdentity();
449 
451  glDepthFunc(GL_LEQUAL); // GL_LESS
452 
453  // Setup lights
454  // -------------------------------------------
460 
461  for (const auto& m_light : m_lights) m_light.sendToOpenGL();
462 
463  // Render all the objects:
464  // -------------------------------------------
466 
467  } // end of non "image mode" rendering
468 
469  // Finally, draw the border:
470  // --------------------------------
471  if (m_borderWidth > 0)
472  {
474  glColor4f(0, 0, 0, 1);
476 
478  glLoadIdentity();
480  glLoadIdentity();
481 
482  glDisable(GL_LIGHTING); // Disable lights when drawing lines
484  glVertex2f(-1, -1);
485  glVertex2f(-1, 1);
486  glVertex2f(1, 1);
487  glVertex2f(1, -1);
488  glEnd();
489  glEnable(GL_LIGHTING); // Disable lights when drawing lines
490 
492  }
493 
494  // Optional post-Render user code:
495  if (hasSubscribers())
496  {
497  mrptEventGLPostRender ev(this);
498  this->publishEvent(ev);
499  }
500  }
501  catch (exception& e)
502  {
503  string msg;
504  if (it != nullptr)
505  msg = format(
506  "Exception while rendering a class '%s'\n%s",
507  it->GetRuntimeClass()->className, e.what());
508  else
509  msg = format("Exception while rendering:\n%s", e.what());
510 
511  THROW_EXCEPTION(msg);
512  }
513  catch (...)
514  {
515  THROW_EXCEPTION("Runtime error!");
516  }
517 #else
518  MRPT_UNUSED_PARAM(render_width);
519  MRPT_UNUSED_PARAM(render_height);
521  "The MRPT has been compiled with MRPT_HAS_OPENGL_GLUT=0! OpenGL "
522  "functions are not implemented");
523 #endif
524 }
525 
528 {
529  // Save data:
533 
534  // Added in v1:
537 
538  // Save objects:
539  uint32_t n;
540  n = (uint32_t)m_objects.size();
541  out << n;
542  for (const auto& m_object : m_objects) out << *m_object;
543 
544  // Added in v2: Global OpenGL settings:
546 
547  // Added in v3: Lights
548  out << m_lights;
549 }
550 
553 {
554  switch (version)
555  {
556  case 0:
557  case 1:
558  case 2:
559  case 3:
560  {
561  // Load data:
566 
567  // in v1:
568  if (version >= 1)
569  {
573  }
574  else
575  {
576  m_custom_backgb_color = false;
577  }
578 
579  // Load objects:
580  uint32_t n;
581  in >> n;
582  clear();
583  m_objects.resize(n);
584 
585  for_each(
586  m_objects.begin(), m_objects.end(), ObjectReadFromStream(&in));
587 
588  // Added in v2: Global OpenGL settings:
589  if (version >= 2)
590  {
592  }
593  else
594  {
595  // Defaults
596  }
597 
598  // Added in v3: Lights
599  if (version >= 3)
600  in >> m_lights;
601  else
602  {
603  // Default: one light from default direction
604  m_lights.clear();
605  m_lights.emplace_back();
606  }
607  }
608  break;
609  default:
611  };
612 }
613 
614 /*---------------------------------------------------------------
615  getByName
616  ---------------------------------------------------------------*/
618 {
619  for (auto& m_object : m_objects)
620  {
621  if (m_object->m_name == str)
622  return m_object;
623  else if (
624  m_object->GetRuntimeClass() ==
626  {
627  CRenderizable::Ptr ret =
628  std::dynamic_pointer_cast<CSetOfObjects>(m_object)->getByName(
629  str);
630  if (ret) return ret;
631  }
632  }
633  return CRenderizable::Ptr();
634 }
635 
636 /*---------------------------------------------------------------
637  initializeAllTextures
638  ---------------------------------------------------------------*/
640 {
641 #if MRPT_HAS_OPENGL_GLUT
642  for (auto it = m_objects.begin(); it != m_objects.end(); ++it)
643  {
644  if (IS_DERIVED(*it, CTexturedObject))
645  std::dynamic_pointer_cast<CTexturedObject>(*it)
646  ->loadTextureInOpenGL();
647  else if (IS_CLASS(*it, CSetOfObjects))
648  std::dynamic_pointer_cast<CSetOfObjects>(*it)
650  }
651 #endif
652 }
653 
654 void COpenGLViewport::dumpListOfObjects(std::vector<std::string>& lst)
655 {
656  for (auto& m_object : m_objects)
657  {
658  // Single obj:
659  string s(m_object->GetRuntimeClass()->className);
660  if (m_object->m_name.size())
661  s += string(" (") + m_object->m_name + string(")");
662  lst.emplace_back(s);
663 
664  if (m_object->GetRuntimeClass() ==
666  {
667  std::vector<std::string> auxLst;
668 
669  dynamic_cast<CSetOfObjects*>(m_object.get())
670  ->dumpListOfObjects(auxLst);
671 
672  for (const auto& i : auxLst) lst.emplace_back(string(" ") + i);
673  }
674  }
675 }
676 
677 /*--------------------------------------------------------------
678  removeObject
679  ---------------------------------------------------------------*/
681 {
682  for (auto it = m_objects.begin(); it != m_objects.end(); ++it)
683  if (*it == obj)
684  {
685  m_objects.erase(it);
686  return;
687  }
688  else if (
689  (*it)->GetRuntimeClass() ==
691  dynamic_cast<CSetOfObjects*>(it->get())->removeObject(obj);
692 }
693 
694 /*--------------------------------------------------------------
695  setViewportClipDistances
696  ---------------------------------------------------------------*/
698  const double clip_min, const double clip_max)
699 {
700  ASSERT_(clip_max > clip_min);
701 
702  m_clip_min = clip_min;
703  m_clip_max = clip_max;
704 }
705 
706 /*--------------------------------------------------------------
707  getViewportClipDistances
708  ---------------------------------------------------------------*/
710  double& clip_min, double& clip_max) const
711 {
712  clip_min = m_clip_min;
713  clip_max = m_clip_max;
714 }
715 
716 /*--------------------------------------------------------------
717  get3DRayForPixelCoord
718  ---------------------------------------------------------------*/
720  const double x_coord, const double y_coord, mrpt::math::TLine3D& out_ray,
721  mrpt::poses::CPose3D* out_cameraPose) const
722 {
723  ASSERTDEB_(
725 
726  const double ASPECT =
728 
729  // unitary vector between (eye) -> (pointing):
730  TPoint3D pointing_dir;
731  pointing_dir.x = -cos(m_lastProjMat.azimuth) * cos(m_lastProjMat.elev);
732  pointing_dir.y = -sin(m_lastProjMat.azimuth) * cos(m_lastProjMat.elev);
733  pointing_dir.z = -sin(m_lastProjMat.elev);
734 
735  // The camera X vector (in 3D) can be computed from the camera azimuth
736  // angle:
737  TPoint3D cam_x_3d;
738  cam_x_3d.x = -sin(m_lastProjMat.azimuth);
739  cam_x_3d.y = cos(m_lastProjMat.azimuth);
740  cam_x_3d.z = 0;
741 
742  // The camera real UP vector (in 3D) is the cross product:
743  // X3d x pointing_dir:
744  TPoint3D cam_up_3d;
745  crossProduct3D(cam_x_3d, pointing_dir, cam_up_3d);
746 
748  {
749  // Ortho projection:
750  // -------------------------------
751  double Ax = m_lastProjMat.zoom * 0.5;
752  double Ay = Ax;
753 
754  if (ASPECT > 1)
755  Ax *= ASPECT;
756  else
757  {
758  if (ASPECT != 0) Ay /= ASPECT;
759  }
760 
761  const double point_lx =
762  (-0.5 + x_coord / m_lastProjMat.viewport_width) * 2 * Ax;
763  const double point_ly =
764  -(-0.5 + y_coord / m_lastProjMat.viewport_height) * 2 * Ay;
765 
766  const TPoint3D ray_origin(
767  m_lastProjMat.eye.x + point_lx * cam_x_3d.x +
768  point_ly * cam_up_3d.x,
769  m_lastProjMat.eye.y + point_lx * cam_x_3d.y +
770  point_ly * cam_up_3d.y,
771  m_lastProjMat.eye.z + point_lx * cam_x_3d.z +
772  point_ly * cam_up_3d.z);
773 
774  out_ray.pBase = ray_origin;
775  out_ray.director[0] = pointing_dir.x;
776  out_ray.director[1] = pointing_dir.y;
777  out_ray.director[2] = pointing_dir.z;
778  }
779  else
780  {
781  // Perspective camera
782  // -------------------------------
783 
784  // JL: This can derived from:
785  // http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml
786  // where one arrives to:
787  // tan(FOVx/2) = ASPECT_RATIO * tan(FOVy/2)
788  //
789  const double FOVy = DEG2RAD(m_lastProjMat.FOV);
790  const double FOVx = 2.0 * atan(ASPECT * tan(FOVy * 0.5));
791 
792  const double ang_horz =
793  (-0.5 + x_coord / m_lastProjMat.viewport_width) * FOVx;
794  const double ang_vert =
795  -(-0.5 + y_coord / m_lastProjMat.viewport_height) * FOVy;
796 
797  const TPoint3D l(
798  tan(ang_horz), tan(ang_vert),
799  1.0); // Point in camera local reference frame
800 
801  const TPoint3D ray_director(
802  l.x * cam_x_3d.x + l.y * cam_up_3d.x + l.z * pointing_dir.x,
803  l.x * cam_x_3d.y + l.y * cam_up_3d.y + l.z * pointing_dir.y,
804  l.x * cam_x_3d.z + l.y * cam_up_3d.z + l.z * pointing_dir.z);
805 
806  // Set out ray:
807  out_ray.pBase = m_lastProjMat.eye;
808  out_ray.director[0] = ray_director.x;
809  out_ray.director[1] = ray_director.y;
810  out_ray.director[2] = ray_director.z;
811 
812  } // end projective
813 
814  // Camera pose:
815  if (out_cameraPose)
816  {
818  M.get_unsafe(0, 0) = cam_x_3d.x;
819  M.get_unsafe(1, 0) = cam_x_3d.y;
820  M.get_unsafe(2, 0) = cam_x_3d.z;
821  M.get_unsafe(3, 0) = 0;
822 
823  M.get_unsafe(0, 1) = cam_up_3d.x;
824  M.get_unsafe(1, 1) = cam_up_3d.y;
825  M.get_unsafe(2, 1) = cam_up_3d.z;
826  M.get_unsafe(3, 1) = 0;
827 
828  M.get_unsafe(0, 2) = pointing_dir.x;
829  M.get_unsafe(1, 2) = pointing_dir.y;
830  M.get_unsafe(2, 2) = pointing_dir.z;
831  M.get_unsafe(3, 2) = 0;
832 
833  M.get_unsafe(0, 3) = m_lastProjMat.eye.x;
834  M.get_unsafe(1, 3) = m_lastProjMat.eye.y;
835  M.get_unsafe(2, 3) = m_lastProjMat.eye.z;
836  M.get_unsafe(3, 3) = 1;
837 
838  *out_cameraPose = CPose3D(M);
839  }
840 }
841 
842 MRPT_TODO("Implement a setCurrentCameraFromPose() method")
843 
844 void COpenGLViewport::getCurrentCameraPose(
845  mrpt::poses::CPose3D& out_cameraPose) const
846 {
848  get3DRayForPixelCoord(0, 0, dum, &out_cameraPose);
849 }
850 
851 /** Resets the viewport to a normal 3D viewport \sa setCloneView, setImageView
852  */
854 {
855  // If this was a m_isImageView, remove the quad object:
857 
858  m_isCloned = false;
859  m_isClonedCamera = false;
860  m_isImageView = false;
861 }
862 
864 {
866  *m_imageview_img = img;
867 }
869 {
871  *m_imageview_img = std::move(img);
872 }
873 
875 {
876  // If this is the first time, we have to create the quad object:
879  m_isImageView = true;
880 }
881 
882 /** Evaluates the bounding box of this object (including possible children) in
883  * the coordinate frame of the object parent. */
885  mrpt::math::TPoint3D& bb_min, mrpt::math::TPoint3D& bb_max) const
886 {
887  bb_min = TPoint3D(
888  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
889  std::numeric_limits<double>::max());
890  bb_max = TPoint3D(
891  -std::numeric_limits<double>::max(),
892  -std::numeric_limits<double>::max(),
893  -std::numeric_limits<double>::max());
894 
895  for (const auto& m_object : m_objects)
896  {
897  TPoint3D child_bbmin(
898  std::numeric_limits<double>::max(),
899  std::numeric_limits<double>::max(),
900  std::numeric_limits<double>::max());
901  TPoint3D child_bbmax(
902  -std::numeric_limits<double>::max(),
903  -std::numeric_limits<double>::max(),
904  -std::numeric_limits<double>::max());
905  m_object->getBoundingBox(child_bbmin, child_bbmax);
906 
907  keep_min(bb_min.x, child_bbmin.x);
908  keep_min(bb_min.y, child_bbmin.y);
909  keep_min(bb_min.z, child_bbmin.z);
910 
911  keep_max(bb_max.x, child_bbmax.x);
912  keep_max(bb_max.y, child_bbmax.y);
913  keep_max(bb_max.z, child_bbmax.z);
914  }
915 }
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:1262
An object for reading objects from a stream, intended for being used in STL algorithms.
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:282
GLAPI void GLAPIENTRY glMatrixMode(GLenum mode)
double GLdouble
Definition: glew.h:220
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:26
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:108
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.
static Ptr Create(Args &&... args)
Definition: img/CImage.h:149
#define GL_STENCIL_BUFFER_BIT
Definition: glew.h:262
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:611
#define GL_FRONT_AND_BACK
Definition: glew.h:322
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:5136
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:40
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:42
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:303
mrpt::img::CImage::Ptr m_imageview_img
The image to display, after calling setImageView()
#define GL_PERSPECTIVE_CORRECTION_HINT
Definition: glew.h:451
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:393
#define GL_DEPTH_TEST
Definition: glew.h:402
#define GL_SMOOTH
Definition: glew.h:636
mrpt::math::TPoint3D eye
The camera is here.
GLdouble s
Definition: glext.h:3682
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
#define GL_LIGHTING
Definition: glew.h:386
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
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:3535
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:801
unsigned char uint8_t
Definition: rptypes.h:44
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:138
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:161
GLAPI void GLAPIENTRY glGetDoublev(GLenum pname, GLdouble *params)
#define GL_DEPTH_BUFFER_BIT
Definition: glew.h:260
#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:108
#define GL_COLOR_BUFFER_BIT
Definition: glew.h:266
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:444
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:247
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:3769
~COpenGLViewport() override
Destructor: clears all objects.
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:612
#define GL_RGB
Definition: glew.h:624
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:4116
#define GL_LINE_LOOP
Definition: glew.h:275
GLAPI void GLAPIENTRY glHint(GLenum target, GLenum mode)
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
virtual const mrpt::rtti::TRuntimeClassId * GetRuntimeClass() const override
Returns information about the class of an object in runtime.
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:87
unsigned int GLenum
Definition: glew.h:207
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:482
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:52
void clear()
Delete all internal obejcts.
#define MRPT_TODO(x)
Definition: common.h:129
#define GL_UNPACK_ALIGNMENT
Definition: glew.h:485
#define GL_NICEST
Definition: glew.h:570
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:82
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:231
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
#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:102
An event sent by an mrpt::opengl::COpenGLViewport after calling the scene OpenGL drawing primitives a...
#define GL_SCISSOR_TEST
Definition: glew.h:441
GLAPI void GLAPIENTRY glClear(GLbitfield mask)
#define MRPT_END
Definition: exceptions.h:286
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:53
GLuint in
Definition: glext.h:7391
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLuint const GLchar * name
Definition: glext.h:4068
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:58
GLAPI void GLAPIENTRY glEnd(void)
std::string m_clonedViewport
Only if m_isCloned=true.
#define GL_POLYGON_SMOOTH_HINT
Definition: glew.h:454
mrpt::img::TColorf m_background_color
used only if m_custom_backgb_color
GLenum GLint GLint y
Definition: glext.h:3542
#define GL_LIGHT_MODEL_TWO_SIDE
Definition: glew.h:388
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:210
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:3542
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:227
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:27
GLenum GLsizei GLsizei height
Definition: glext.h:3558
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:110
unsigned __int32 uint32_t
Definition: rptypes.h:50
GLAPI void GLAPIENTRY glVertex2f(GLfloat x, GLfloat y)
GLAPI void GLAPIENTRY glDisable(GLenum cap)
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:609
#define GL_ACCUM_BUFFER_BIT
Definition: glew.h:261
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.
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:626
#define GL_TRUE
Definition: glew.h:294
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:147
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:569



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 5887d2b31 Wed Apr 24 13:03:27 2019 +0200 at miƩ abr 24 13:10:13 CEST 2019