MRPT  1.9.9
test.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 
11 #include <mrpt/opengl/CAxis.h>
12 #include <mrpt/opengl/CBox.h>
14 #include <mrpt/opengl/CSphere.h>
15 #include <mrpt/opengl/CText.h>
16 #include <mrpt/opengl/gl_utils.h>
18 #include <mrpt/system/CObserver.h>
19 #include <mrpt/system/CTicTac.h>
20 #include <mrpt/system/os.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{false}, hiding_help{false};
34  mrpt::system::CTicTac tim_show_start, tim_show_end;
35 
37  void OnEvent(const mrptEvent& e) override
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  opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 1);
117  obj->setColor(0.8, 0.8, 0.8);
118  theScene->insert(obj);
119  }
120 
121  theScene->insert(mrpt::opengl::stock_objects::CornerXYZ());
122 
123  if (true)
124  {
125  opengl::CAxis::Ptr obj = opengl::CAxis::Create();
126  obj->setFrequency(5);
127  obj->enableTickMarks();
128  obj->setAxisLimits(-10, -10, -10, 10, 10, 10);
129  theScene->insert(obj);
130  }
131 
132  {
133  opengl::CSphere::Ptr obj = opengl::CSphere::Create();
134  obj->setColor(0, 0, 1);
135  obj->setRadius(0.3f);
136  obj->setLocation(0, 0, 1);
137  obj->setName("ball_1");
138  theScene->insert(obj);
139 
140  // And also let my rendering object access this ball properties:
141  my_extra_rendering.ball_obj = obj;
142  }
143 
144  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
145  win.unlockAccess3DScene();
146 
147  // Texts:
148  win.addTextMessage(
149  0.01, 0.85, "This is a 2D message", mrpt::img::TColorf(1, 1, 1), 0,
151 
152  win.setCameraElevationDeg(25.0f);
153  // win.setCameraProjective(false);
154 
155  cout << endl;
156  cout << "Control with mouse or keyboard. Valid keys:" << endl;
157  cout << " ESC -> Exit" << endl;
158  cout << " Left/right cursor arrow -> Camera azimuth" << endl;
159  cout << endl;
160 
161  bool end = false;
162 
163  CTicTac timer;
164  timer.Tic();
165 
166  while (!end && win.isOpen())
167  {
168  // Move the scene:
169  COpenGLScene::Ptr& theScene = win.get3DSceneAndLock();
170 
171  opengl::CRenderizable::Ptr obj1 = theScene->getByName("ball_1");
172  const double t = timer.Tac();
173  const double R = 8;
174  const double W = 5.0, Q = 3.3;
175  obj1->setLocation(
176  R * cos(W * t) * sin(Q * t), R * sin(W * t),
177  R * cos(W * t) * cos(Q * t));
178 
179  // Update the texts on the gl display:
180  win.addTextMessage(
181  5, 5, mrpt::format("FPS=%5.02f", win.getRenderingFPS()),
183 
184  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
185  win.unlockAccess3DScene();
186 
187  // Update window:
188  win.forceRepaint();
189  std::this_thread::sleep_for(1ms);
190 
191  if (mrpt::system::os::kbhit()) end = true;
192  if (win.keyHit())
193  {
194  mrptKeyModifier kmods;
195  int key = win.getPushedKey(&kmods);
196  // printf("Key pushed: %c (%i) - modifiers:
197  // 0x%04X\n",char(key),key,kmods);
198 
199  if (key == MRPTK_ESCAPE) end = true;
200 
201  if (key == 'h' || key == 'H')
202  {
203  if (!my_extra_rendering.showing_help)
204  {
205  my_extra_rendering.tim_show_start.Tic();
206  my_extra_rendering.showing_help = true;
207  }
208  else
209  {
210  my_extra_rendering.tim_show_end.Tic();
211  my_extra_rendering.showing_help = false;
212  my_extra_rendering.hiding_help = true;
213  }
214  }
215 
216  if (key == MRPTK_RIGHT)
217  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() + 5);
218  if (key == MRPTK_LEFT)
219  win.setCameraAzimuthDeg(win.getCameraAzimuthDeg() - 5);
220  }
221  };
222 }
223 
224 // ------------------------------------------------------
225 // MAIN
226 // ------------------------------------------------------
227 int main()
228 {
229  try
230  {
231  TestDisplay3D();
232 
233  std::this_thread::sleep_for(
234  50ms); // leave time for the window to close
235  return 0;
236  }
237  catch (const std::exception& e)
238  {
239  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
240  return -1;
241  }
242  catch (...)
243  {
244  printf("Untyped exception!!");
245  return -1;
246  }
247 }
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:86
GLdouble GLdouble t
Definition: glext.h:3695
#define min(a, b)
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:31
mrpt::system::CTicTac tim_show_end
A high-performance stopwatch, with typical resolution of nanoseconds.
mrptKeyModifier
Definition: keycodes.h:156
STL namespace.
mrpt::system::CTicTac tim_show_start
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
Inherit from this class to get notified about events from any CObservable object after subscribing to...
Definition: CObserver.h:34
GLuint GLuint end
Definition: glext.h:3532
mrpt::gui::CDisplayWindow3D::Ptr win
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
An event sent by an mrpt::opengl::COpenGLViewport just after clearing the viewport and setting the GL...
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:400
const float R
CSetOfObjects::Ptr CornerXYZ(float scale=1.0)
Returns three arrows representing a X,Y,Z 3D corner.
bool isOfType() const
Definition: mrptEvent.h:41
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
An event sent by an mrpt::opengl::COpenGLViewport after calling the scene OpenGL drawing primitives a...
A RGB color - floats in the range [0,1].
Definition: TColor.h:77
renders glyphs filled with antialiased outlines
Definition: opengl_fonts.h:37
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:394
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
void observeBegin(CObservable &obj)
Starts the subscription of this observer to the given object.
Definition: CObserver.cpp:26
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
A RGB color - 8bit.
Definition: TColor.h:20
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:75
void TestDisplay3D()
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.



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