Main MRPT website > C++ reference for MRPT 1.9.9
test.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 
11 #include <mrpt/system/os.h>
12 #include <mrpt/system/CTicTac.h>
13 #include <mrpt/system/CObserver.h>
14 #include <mrpt/opengl/gl_utils.h>
16 #include <mrpt/opengl/CText.h>
18 #include <mrpt/opengl/CAxis.h>
19 #include <mrpt/opengl/CBox.h>
20 #include <mrpt/opengl/CSphere.h>
21 #include <iostream>
22 
23 using namespace std;
24 using namespace mrpt;
25 using namespace mrpt::gui;
26 using namespace mrpt::opengl;
27 using namespace mrpt::system;
28 
29 // This is my custom class to handle the pre/post render events:
31 {
32  opengl::CSphere::Ptr ball_obj; // The ball moving in the scene
33  bool showing_help, hiding_help;
34  mrpt::system::CTicTac tim_show_start, tim_show_end;
35 
36  TMyExtraRenderingStuff() : showing_help(false), hiding_help(false) {}
37  virtual void OnEvent(const mrptEvent& e)
38  {
40  {
41  // const mrptEventGLPreRender* ev = e.getAs<mrptEventGLPreRender>();
42  // ev-> ...
43  }
44  else if (e.isOfType<mrptEventGLPostRender>())
45  {
46  // const mrptEventGLPostRender* ev =
47  // e.getAs<mrptEventGLPostRender>();
48 
49  // Show small message in the corner:
51  0.7f, 0.9f, // x,y (in screen "ratios")
52  0.29f, 0.09f, // width, height (in screen "ratios")
53  "Press 'h' for help",
54  0.02f // text size
55  );
56 
57  // Also showing help?
58  if (showing_help || hiding_help)
59  {
60  static const double TRANSP_ANIMATION_TIME_SEC = 0.5;
61 
62  const double show_tim = tim_show_start.Tac();
63  const double hide_tim = tim_show_end.Tac();
64 
65  const double tranparency =
66  hiding_help
67  ? 1.0 - std::min(
68  1.0, hide_tim / TRANSP_ANIMATION_TIME_SEC)
69  : std::min(1.0, show_tim / TRANSP_ANIMATION_TIME_SEC);
70 
72  0.05f, 0.05f, // x,y (in screen "ratios")
73  0.90f, 0.90f, // width, height (in screen "ratios")
74  "These are the supported commands:\n"
75  " - 'h': Toogle help view\n"
76  " - '<-' and '->': Rotate camera\n"
77  " - 'Alt+Enter': Toogle fullscreen\n"
78  " - 'ESC': Quit",
79  0.05f, // text size
81  190, 190, 190, 200 * tranparency), // background
82  mrpt::img::TColor(0, 0, 0, 200 * tranparency), // border
83  mrpt::img::TColor(200, 0, 0, 150 * tranparency), // text
84  6.0f, // border width
85  "serif", // text font
86  mrpt::opengl::NICE // text style
87  );
88 
89  if (hide_tim > TRANSP_ANIMATION_TIME_SEC && hiding_help)
90  hiding_help = false;
91  }
92  }
93  }
94 };
95 
96 // ------------------------------------------------------
97 // TestDisplay3D
98 // ------------------------------------------------------
99 void TestDisplay3D()
100 {
101  CDisplayWindow3D win("Example of 3D Scene Visualization - MRPT", 640, 480);
102 
103  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
104 
105  // The unique instance of the observer class:
106  TMyExtraRenderingStuff my_extra_rendering;
107 
108  // And start subscribing to the viewport events:
109  opengl::COpenGLViewport::Ptr the_main_view = theScene->getViewport("main");
110  my_extra_rendering.observeBegin(*the_main_view);
111 
112  // Modify the scene:
113  // ------------------------------------------------------
114  {
116  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
117  -20, 20, -20, 20, 0, 1);
118  obj->setColor(0.8, 0.8, 0.8);
119  theScene->insert(obj);
120  }
121 
122  theScene->insert(mrpt::opengl::stock_objects::CornerXYZ());
123 
124  if (1)
125  {
126  opengl::CAxis::Ptr obj = mrpt::make_aligned_shared<opengl::CAxis>();
127  obj->setFrequency(5);
128  obj->enableTickMarks();
129  obj->setAxisLimits(-10, -10, -10, 10, 10, 10);
130  theScene->insert(obj);
131  }
132 
133  {
134  opengl::CSphere::Ptr obj = mrpt::make_aligned_shared<opengl::CSphere>();
135  obj->setColor(0, 0, 1);
136  obj->setRadius(0.3f);
137  obj->setLocation(0, 0, 1);
138  obj->setName("ball_1");
139  theScene->insert(obj);
140 
141  // And also let my rendering object access this ball properties:
142  my_extra_rendering.ball_obj = obj;
143  }
144 
145  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
146  win.unlockAccess3DScene();
147 
148  // Texts:
149  win.addTextMessage(
150  0.01, 0.85, "This is a 2D message", mrpt::img::TColorf(1, 1, 1), 0,
152 
153  win.setCameraElevationDeg(25.0f);
154  // win.setCameraProjective(false);
155 
156  cout << endl;
157  cout << "Control with mouse or keyboard. Valid keys:" << endl;
158  cout << " ESC -> Exit" << endl;
159  cout << " Left/right cursor arrow -> Camera azimuth" << endl;
160  cout << endl;
161 
162  bool end = false;
163 
164  CTicTac timer;
165  timer.Tic();
166 
167  while (!end && win.isOpen())
168  {
169  // Move the scene:
170  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
171 
172  opengl::CRenderizable::Ptr obj1 = theScene->getByName("ball_1");
173  const double t = timer.Tac();
174  const double R = 8;
175  const double W = 5.0, Q = 3.3;
176  obj1->setLocation(
177  R * cos(W * t) * sin(Q * t), R * sin(W * t),
178  R * cos(W * t) * cos(Q * t));
179 
180  // Update the texts on the gl display:
181  win.addTextMessage(
182  5, 5, mrpt::format("FPS=%5.02f", win.getRenderingFPS()),
184 
185  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
186  win.unlockAccess3DScene();
187 
188  // Update window:
189  win.forceRepaint();
190  std::this_thread::sleep_for(1ms);
191 
192  if (mrpt::system::os::kbhit()) end = true;
193  if (win.keyHit())
194  {
195  mrptKeyModifier kmods;
196  int key = win.getPushedKey(&kmods);
197  // printf("Key pushed: %c (%i) - modifiers:
198  // 0x%04X\n",char(key),key,kmods);
199 
200  if (key == MRPTK_ESCAPE) end = true;
201 
202  if (key == 'h' || key == 'H')
203  {
204  if (!my_extra_rendering.showing_help)
205  {
206  my_extra_rendering.tim_show_start.Tic();
207  my_extra_rendering.showing_help = true;
208  }
209  else
210  {
211  my_extra_rendering.tim_show_end.Tic();
212  my_extra_rendering.showing_help = false;
213  my_extra_rendering.hiding_help = true;
214  }
215  }
216 
217  if (key == MRPTK_RIGHT)
218  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() + 5);
219  if (key == MRPTK_LEFT)
220  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() - 5);
221  }
222  };
223 }
224 
225 // ------------------------------------------------------
226 // MAIN
227 // ------------------------------------------------------
228 int main()
229 {
230  try
231  {
232  TestDisplay3D();
233 
234  std::this_thread::sleep_for(
235  50ms); // leave time for the window to close
236  return 0;
237  }
238  catch (std::exception& e)
239  {
240  std::cout << "MRPT exception caught: " << e.what() << std::endl;
241  return -1;
242  }
243  catch (...)
244  {
245  printf("Untyped exception!!");
246  return -1;
247  }
248 }
mrpt::system::os::kbhit
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:390
os.h
mrpt::opengl::CGridPlaneXY::Ptr
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34
mrpt::system::mrptEvent
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:33
t
GLdouble GLdouble t
Definition: glext.h:3689
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
CSphere.h
mrpt::opengl::CSphere::Ptr
std::shared_ptr< CSphere > Ptr
Definition: CSphere.h:33
mrpt::opengl::NICE
@ NICE
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:40
mrpt::opengl::gl_utils::renderMessageBox
void renderMessageBox(const float msg_x, const float msg_y, const float msg_w, const float msg_h, const std::string &text, float text_scale, const mrpt::img::TColor &back_col=mrpt::img::TColor(0, 0, 50, 150), const mrpt::img::TColor &border_col=mrpt::img::TColor(0, 0, 0, 140), const mrpt::img::TColor &text_col=mrpt::img::TColor(255, 255, 255, 220), const float border_width=4.0f, const std::string &text_font=std::string("sans"), mrpt::opengl::TOpenGLFontStyle text_style=mrpt::opengl::FILL, const double text_spacing=1.5, const double text_kerning=0.1)
Draws a message box with a centered (possibly multi-lined) text.
Definition: gl_utils.cpp:387
end
GLuint GLuint end
Definition: glext.h:3528
mrpt::opengl::mrptEventGLPostRender
An event sent by an mrpt::opengl::COpenGLViewport after calling the scene OpenGL drawing primitives a...
Definition: COpenGLViewport.h:506
TMyExtraRenderingStuff::tim_show_start
mrpt::system::CTicTac tim_show_start
Definition: vision_stereo_rectify/test.cpp:34
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::system::CObserver::observeBegin
void observeBegin(CObservable &obj)
Starts the subscription of this observer to the given object.
Definition: CObserver.cpp:26
mrpt::opengl::CRenderizable::Ptr
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
R
const float R
Definition: CKinematicChain.cpp:138
stock_objects.h
TMyExtraRenderingStuff::tim_show_end
mrpt::system::CTicTac tim_show_end
Definition: vision_stereo_rectify/test.cpp:34
CDisplayWindow3D.h
mrpt::system::mrptEvent::isOfType
bool isOfType() const
Definition: mrptEvent.h:42
mrpt::gui::MRPTK_RIGHT
@ MRPTK_RIGHT
Definition: keycodes.h:50
mrpt::opengl::MRPT_GLUT_BITMAP_HELVETICA_18
@ MRPT_GLUT_BITMAP_HELVETICA_18
Definition: opengl_fonts.h:32
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
TMyExtraRenderingStuff::hiding_help
bool hiding_help
Definition: vision_stereo_rectify/test.cpp:33
mrpt::opengl::mrptEventGLPreRender
An event sent by an mrpt::opengl::COpenGLViewport just after clearing the viewport and setting the GL...
Definition: COpenGLViewport.h:481
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
TMyExtraRenderingStuff::showing_help
bool showing_help
Definition: vision_stereo_rectify/test.cpp:33
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
CObserver.h
mrpt::opengl::CAxis::Ptr
std::shared_ptr< CAxis > Ptr
Definition: CAxis.h:33
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:16
CBox.h
gl_utils.h
mrpt::opengl::MRPT_GLUT_BITMAP_TIMES_ROMAN_10
@ MRPT_GLUT_BITMAP_TIMES_ROMAN_10
Definition: opengl_fonts.h:28
CAxis.h
mrpt::opengl::stock_objects::CornerXYZ
CSetOfObjects::Ptr CornerXYZ(float scale=1.0)
Returns three arrows representing a X,Y,Z 3D corner.
Definition: StockObjects.cpp:209
min
#define min(a, b)
Definition: rplidar_driver.cpp:42
mrpt::system::CObserver
Inherit from this class to get notified about events from any CObservable object after subscribing to...
Definition: CObserver.h:36
CTicTac.h
mrpt::gui::MRPTK_ESCAPE
@ MRPTK_ESCAPE
Definition: keycodes.h:30
mrpt::gui::mrptKeyModifier
mrptKeyModifier
Definition: keycodes.h:159
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
TMyExtraRenderingStuff::ball_obj
opengl::CSphere::Ptr ball_obj
Definition: vision_stereo_rectify/test.cpp:32
TestDisplay3D
void TestDisplay3D()
Definition: vision_stereo_rectify/test.cpp:26
mrpt::gui::MRPTK_LEFT
@ MRPTK_LEFT
Definition: keycodes.h:48
CGridPlaneXY.h
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::opengl::COpenGLViewport::Ptr
std::shared_ptr< COpenGLViewport > Ptr
Definition: COpenGLViewport.h:63
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
CText.h
TMyExtraRenderingStuff
Definition: vision_stereo_rectify/test.cpp:30



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