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/img/CImage.h>
12 #include <mrpt/opengl/CAxis.h>
13 #include <mrpt/opengl/CFBORender.h>
16 #include <mrpt/opengl/CSphere.h>
17 
18 #include <chrono>
19 #include <iostream>
20 #include <thread>
21 
22 using namespace mrpt;
23 using namespace mrpt::gui;
24 using namespace mrpt::opengl;
25 using namespace mrpt::img;
26 
27 using namespace std::literals;
28 
29 // ------------------------------------------------------
30 // TestDisplay3D
31 // ------------------------------------------------------
32 void TestDisplay3D()
33 {
34  COpenGLScene scene;
35 
36  // Modify the scene:
37  // ------------------------------------------------------
38  {
40  opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 1);
41  obj->setColor(0.4, 0.4, 0.4);
42  scene.insert(obj);
43  }
44  {
46  obj->setFrequency(5);
47  obj->enableTickMarks();
48  obj->setAxisLimits(-10, -10, -10, 10, 10, 10);
49  scene.insert(obj);
50  }
51  {
53  obj->setColor(0, 0, 1);
54  obj->setRadius(0.3);
55  obj->setLocation(0, 0, 1);
56  obj->setName("ball_1");
57  scene.insert(obj);
58  }
59  {
61  obj->setColor(1, 0, 0);
62  obj->setRadius(0.3);
63  obj->setLocation(-1, -1, 1);
64  obj->setName("ball_2");
65  scene.insert(obj);
66  }
67 
68  CDisplayWindow win("output");
69 
70  int c = 0, width = 640, height = 480;
71 
72  CFBORender render(width, height);
73  CImage frame(width, height, CH_RGB);
74 
75  {
76  CCamera& camera = render.getCamera(scene);
77  camera.setZoomDistance(50);
78  }
79 
80  while (!mrpt::system::os::kbhit())
81  {
82  CRenderizable::Ptr obj = scene.getByName("ball_1");
83  obj->setLocation(
84  obj->getPoseX() + cos(obj->getPoseY() / 2) * 0.05,
85  obj->getPoseY() - sin(obj->getPoseX() / 2) * 0.09,
86  obj->getPoseZ() - sin(obj->getPoseX() / 2) * 0.08);
87 
88  obj = scene.getByName("ball_2");
89  obj->setLocation(
90  obj->getPoseX() + cos(obj->getPoseY() / 2) * 0.05,
91  obj->getPoseY() - sin(obj->getPoseX() / 2) * 0.09,
92  obj->getPoseZ() - sin(obj->getPoseX() / 2) * 0.08);
93 
94  // change the size
95  if (++c > 100)
96  {
97  width = 800, height = 600;
98  frame.resize(width, height, CH_RGB);
99  render.resize(width, height);
100  }
101 
102  // render the scene
103  render.getFrame2(scene, frame);
104 
105  // show the redered image
106  win.showImage(frame);
107 
108  std::this_thread::sleep_for(50ms);
109  }
110 }
111 
112 // ------------------------------------------------------
113 // MAIN
114 // ------------------------------------------------------
115 int main(int argc, char* argv[])
116 {
117  try
118  {
119  TestDisplay3D();
120  return 0;
121  }
122  catch (const std::exception& e)
123  {
124  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
125  return -1;
126  }
127  catch (...)
128  {
129  printf("Untyped exception!!");
130  return -1;
131  }
132 }
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
static Ptr Create(Args &&... args)
Definition: CAxis.h:31
GLenum GLsizei width
Definition: glext.h:3535
void setZoomDistance(float z)
Definition: CCamera.h:69
const GLubyte * c
Definition: glext.h:6406
This class creates a window as a graphical user interface (GUI) for displaying images to the user...
mrpt::gui::CDisplayWindow3D::Ptr win
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CRenderizable::Ptr getByName(const std::string &str, const std::string &viewportName=std::string("main"))
Returns the first object with a given name, or nullptr (an empty smart pointer) if not found...
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
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives...
Definition: COpenGLScene.h:58
static Ptr Create(Args &&... args)
Definition: CSphere.h:30
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
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
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
void insert(const CRenderizable::Ptr &newObject, const std::string &viewportName=std::string("main"))
Insert a new object into the scene, in the given viewport (by default, into the "main" viewport)...
static Ptr Create(Args &&... args)
Definition: CGridPlaneXY.h:31
void TestDisplay3D()
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:147
A class for rendering 3D scenes off-screen directly into an image using OpenGL extensions (glext)...
Definition: CFBORender.h:27



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