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 
12 #include <mrpt/opengl/CAxis.h>
13 #include <mrpt/opengl/CSphere.h>
14 #include <mrpt/opengl/CFBORender.h>
16 #include <mrpt/img/CImage.h>
17 
18 #include <thread>
19 #include <chrono>
20 #include <iostream>
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  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
41  -20, 20, -20, 20, 0, 1);
42  obj->setColor(0.4, 0.4, 0.4);
43  scene.insert(obj);
44  }
45  {
46  opengl::CAxis::Ptr obj = mrpt::make_aligned_shared<opengl::CAxis>();
47  obj->setFrequency(5);
48  obj->enableTickMarks();
49  obj->setAxisLimits(-10, -10, -10, 10, 10, 10);
50  scene.insert(obj);
51  }
52  {
53  opengl::CSphere::Ptr obj = mrpt::make_aligned_shared<opengl::CSphere>();
54  obj->setColor(0, 0, 1);
55  obj->setRadius(0.3);
56  obj->setLocation(0, 0, 1);
57  obj->setName("ball_1");
58  scene.insert(obj);
59  }
60  {
61  opengl::CSphere::Ptr obj = mrpt::make_aligned_shared<opengl::CSphere>();
62  obj->setColor(1, 0, 0);
63  obj->setRadius(0.3);
64  obj->setLocation(-1, -1, 1);
65  obj->setName("ball_2");
66  scene.insert(obj);
67  }
68 
69  CDisplayWindow win("output");
70 
71  int c = 0, width = 640, height = 480;
72 
73  CFBORender render(width, height);
74  CImage frame(width, height, 3, false);
75 
76  {
77  CCamera& camera = render.getCamera(scene);
78  camera.setZoomDistance(50);
79  }
80 
81  while (!mrpt::system::os::kbhit())
82  {
83  CRenderizable::Ptr obj = scene.getByName("ball_1");
84  obj->setLocation(
85  obj->getPoseX() + cos(obj->getPoseY() / 2) * 0.05,
86  obj->getPoseY() - sin(obj->getPoseX() / 2) * 0.09,
87  obj->getPoseZ() - sin(obj->getPoseX() / 2) * 0.08);
88 
89  obj = scene.getByName("ball_2");
90  obj->setLocation(
91  obj->getPoseX() + cos(obj->getPoseY() / 2) * 0.05,
92  obj->getPoseY() - sin(obj->getPoseX() / 2) * 0.09,
93  obj->getPoseZ() - sin(obj->getPoseX() / 2) * 0.08);
94 
95  // change the size
96  if (++c > 100)
97  {
98  width = 800, height = 600;
99  frame.resize(width, height, 3, false);
100  render.resize(width, height);
101  }
102 
103  // render the scene
104  render.getFrame2(scene, frame);
105 
106  // show the redered image
107  win.showImage(frame);
108 
109  std::this_thread::sleep_for(50ms);
110  }
111 }
112 
113 // ------------------------------------------------------
114 // MAIN
115 // ------------------------------------------------------
116 int main(int argc, char* argv[])
117 {
118  try
119  {
120  TestDisplay3D();
121  return 0;
122  }
123  catch (std::exception& e)
124  {
125  std::cout << "MRPT exception caught: " << e.what() << std::endl;
126  return -1;
127  }
128  catch (...)
129  {
130  printf("Untyped exception!!");
131  return -1;
132  }
133 }
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
mrpt::opengl::CGridPlaneXY::Ptr
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34
mrpt::opengl::COpenGLScene::insert
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).
Definition: COpenGLScene.cpp:178
CSphere.h
mrpt::opengl::CSphere::Ptr
std::shared_ptr< CSphere > Ptr
Definition: CSphere.h:33
mrpt::opengl::COpenGLScene
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives.
Definition: COpenGLScene.h:59
c
const GLubyte * c
Definition: glext.h:6313
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
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
CFBORender.h
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::img
Definition: CCanvas.h:17
COpenGLScene.h
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::opengl::CCamera
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:30
mrpt::opengl::CCamera::setZoomDistance
void setZoomDistance(float z)
Definition: CCamera.h:72
mrpt::gui::CDisplayWindow
This class creates a window as a graphical user interface (GUI) for displaying images to the user.
Definition: CDisplayWindow.h:30
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
height
GLenum GLsizei GLsizei height
Definition: glext.h:3554
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
CAxis.h
width
GLenum GLsizei width
Definition: glext.h:3531
CImage.h
mrpt::opengl::CFBORender
A class for rendering 3D scenes off-screen directly into an image using OpenGL extensions (glext).
Definition: CFBORender.h:30
TestDisplay3D
void TestDisplay3D()
Definition: vision_stereo_rectify/test.cpp:26
CGridPlaneXY.h
mrpt::opengl::COpenGLScene::getByName
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.
Definition: COpenGLScene.cpp:199
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
CDisplayWindow.h



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