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 
10 #include <mrpt/img/CImage.h>
12 #include <mrpt/opengl/CAxis.h>
14 #include <mrpt/opengl/CSphere.h>
15 #include <mrpt/system/os.h>
17 #include <iostream>
18 
19 using namespace std;
20 using namespace mrpt;
21 using namespace mrpt::gui;
22 using namespace mrpt::opengl;
23 using namespace mrpt::img;
24 
25 #include <mrpt/examples_config.h>
26 
27 string myDataDir(
28  MRPT_EXAMPLES_BASE_DIRECTORY +
29  string("opengl_textured_triangles_example/"));
30 
31 // ------------------------------------------------------
32 // TestDisplay3D
33 // ------------------------------------------------------
34 void TestDisplay3D()
35 {
36  CDisplayWindow3D win("Test of CSetOfTexturedTriangles");
37  COpenGLScene::Ptr& scene = win.get3DSceneAndLock();
38 
39  // Modify the scene:
40  // ------------------------------------------------------
41  {
43  mrpt::make_aligned_shared<opengl::CGridPlaneXY>(
44  -20, 20, -20, 20, 0, 1);
45  obj->setColor(0.4, 0.4, 0.4);
46  scene->insert(obj);
47  }
48  {
49  opengl::CAxis::Ptr obj = mrpt::make_aligned_shared<opengl::CAxis>();
50  obj->setFrequency(5);
51  obj->enableTickMarks();
52  obj->setAxisLimits(-10, -10, -10, 10, 10, 10);
53  scene->insert(obj);
54  }
55  {
56  opengl::CSphere::Ptr obj = mrpt::make_aligned_shared<opengl::CSphere>();
57  obj->setColor(0, 0, 1);
58  obj->setRadius(0.3f);
59  obj->setLocation(0, 0, 1);
60  obj->setName("ball_1");
61  scene->insert(obj);
62  }
63  {
64  opengl::CSphere::Ptr obj = mrpt::make_aligned_shared<opengl::CSphere>();
65  obj->setColor(1, 0, 0);
66  obj->setRadius(0.3f);
67  obj->setLocation(-1, -1, 1);
68  obj->setName("ball_2");
69  scene->insert(obj);
70  }
71  {
73 
74  // image.loadFromFile(myDataDir + string("texture.png"), 0); //
75  // grayscale
76  image.loadFromFile(myDataDir + string("texture.png"), 1); // color
77  alpha.loadFromFile(myDataDir + string("mask.png"), 0); // transparency
78 
80  mrpt::make_aligned_shared<opengl::CSetOfTexturedTriangles>();
82 
85  -2.0, -2.0, 0, 0, 256), // 3D coord (x,y,z) Pixel coord (u,v)
86  opengl::CSetOfTexturedTriangles::TVertex(2.0, -2.0, 0, 256, 256),
87  opengl::CSetOfTexturedTriangles::TVertex(2.0, 2.0, 0, 256, 0));
88  obj->insertTriangle(tri);
89 
91  opengl::CSetOfTexturedTriangles::TVertex(-2.0, 2.0, 1, 0, 0),
92  opengl::CSetOfTexturedTriangles::TVertex(2.0, 2.0, 0, 256, 0),
93  opengl::CSetOfTexturedTriangles::TVertex(-2.0, -2.0, 0, 0, 256));
94  obj->insertTriangle(tri);
95 
96  obj->setName("set");
97  // obj->assignImage(image);
98  obj->assignImage(image, alpha); // transparency
99  scene->insert(obj);
100  }
101 
102  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
103  win.unlockAccess3DScene();
104 
105  win.setCameraElevationDeg(25.0f);
106 
107  while (!mrpt::system::os::kbhit() && win.isOpen())
108  {
109  // Move the scene:
110  COpenGLScene::Ptr& scene = win.get3DSceneAndLock();
111 
112  opengl::CRenderizable::Ptr obj = scene->getByName("ball_1");
113  obj->setLocation(
114  obj->getPoseX() + cos(obj->getPoseY() / 2) * 0.05,
115  obj->getPoseY() - sin(obj->getPoseX() / 2) * 0.09,
116  obj->getPoseZ() - sin(obj->getPoseX() / 2) * 0.08);
117 
118  obj = scene->getByName("ball_2");
119  obj->setLocation(
120  obj->getPoseX() + cos(obj->getPoseY() / 2) * 0.05,
121  obj->getPoseY() - sin(obj->getPoseX() / 2) * 0.09,
122  obj->getPoseZ() - sin(obj->getPoseX() / 2) * 0.08);
123 
124  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
125  win.unlockAccess3DScene();
126 
127  // Update window:
128  win.forceRepaint();
129 
130  std::this_thread::sleep_for(20ms);
131  }
132 }
133 
134 // ------------------------------------------------------
135 // MAIN
136 // ------------------------------------------------------
137 int main(int argc, char* argv[])
138 {
139  try
140  {
141  TestDisplay3D();
142  return 0;
143  }
144  catch (std::exception& e)
145  {
146  std::cout << "MRPT exception caught: " << e.what() << std::endl;
147  return -1;
148  }
149  catch (...)
150  {
151  printf("Untyped exception!!");
152  return -1;
153  }
154 }
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
CSetOfTexturedTriangles.h
mrpt::opengl::CGridPlaneXY::Ptr
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34
CSphere.h
mrpt::opengl::CSphere::Ptr
std::shared_ptr< CSphere > Ptr
Definition: CSphere.h:33
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
mrpt::opengl::CSetOfTexturedTriangles::TTriangle
Triangle.
Definition: CSetOfTexturedTriangles.h:47
myDataDir
std::string myDataDir
Definition: vision_stereo_rectify/test.cpp:23
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
alpha
GLclampf GLclampf GLclampf alpha
Definition: glext.h:3525
CDisplayWindow3D.h
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
mrpt::img
Definition: CCanvas.h:17
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
mrpt::opengl::CSetOfTexturedTriangles::TVertex
Triangle vertex.
Definition: CSetOfTexturedTriangles.h:31
mrpt::opengl::CSetOfTexturedTriangles::Ptr
std::shared_ptr< CSetOfTexturedTriangles > Ptr
Definition: CSetOfTexturedTriangles.h:25
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
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
CAxis.h
image
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glext.h:3552
mrpt::opengl::COpenGLScene::Ptr
std::shared_ptr< COpenGLScene > Ptr
Definition: COpenGLScene.h:61
CImage.h
TestDisplay3D
void TestDisplay3D()
Definition: vision_stereo_rectify/test.cpp:26
CGridPlaneXY.h
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::gui::CDisplayWindow3D
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
Definition: CDisplayWindow3D.h:117



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