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>
15 #include <mrpt/opengl/CSphere.h>
16 #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  opengl::CGridPlaneXY::Create(-20, 20, -20, 20, 0, 1);
44  obj->setColor(0.4, 0.4, 0.4);
45  scene->insert(obj);
46  }
47  {
48  opengl::CAxis::Ptr obj = opengl::CAxis::Create();
49  obj->setFrequency(5);
50  obj->enableTickMarks();
51  obj->setAxisLimits(-10, -10, -10, 10, 10, 10);
52  scene->insert(obj);
53  }
54  {
55  opengl::CSphere::Ptr obj = opengl::CSphere::Create();
56  obj->setColor(0, 0, 1);
57  obj->setRadius(0.3f);
58  obj->setLocation(0, 0, 1);
59  obj->setName("ball_1");
60  scene->insert(obj);
61  }
62  {
63  opengl::CSphere::Ptr obj = opengl::CSphere::Create();
64  obj->setColor(1, 0, 0);
65  obj->setRadius(0.3f);
66  obj->setLocation(-1, -1, 1);
67  obj->setName("ball_2");
68  scene->insert(obj);
69  }
70  {
72 
73  // image.loadFromFile(myDataDir + string("texture.png"), 0); //
74  // grayscale
75  image.loadFromFile(myDataDir + string("texture.png"), 1); // color
76  alpha.loadFromFile(myDataDir + string("mask.png"), 0); // transparency
77 
79  opengl::CSetOfTexturedTriangles::Create();
81 
84  -2.0, -2.0, 0, 0, 256), // 3D coord (x,y,z) Pixel coord (u,v)
85  opengl::CSetOfTexturedTriangles::TVertex(2.0, -2.0, 0, 256, 256),
86  opengl::CSetOfTexturedTriangles::TVertex(2.0, 2.0, 0, 256, 0));
87  obj->insertTriangle(tri);
88 
90  opengl::CSetOfTexturedTriangles::TVertex(-2.0, 2.0, 1, 0, 0),
91  opengl::CSetOfTexturedTriangles::TVertex(2.0, 2.0, 0, 256, 0),
92  opengl::CSetOfTexturedTriangles::TVertex(-2.0, -2.0, 0, 0, 256));
93  obj->insertTriangle(tri);
94 
95  obj->setName("set");
96  // obj->assignImage(image);
97  obj->assignImage(image, alpha); // transparency
98  scene->insert(obj);
99  }
100 
101  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
102  win.unlockAccess3DScene();
103 
104  win.setCameraElevationDeg(25.0f);
105 
106  while (!mrpt::system::os::kbhit() && win.isOpen())
107  {
108  // Move the scene:
109  COpenGLScene::Ptr& scene = win.get3DSceneAndLock();
110 
111  opengl::CRenderizable::Ptr obj = scene->getByName("ball_1");
112  obj->setLocation(
113  obj->getPoseX() + cos(obj->getPoseY() / 2) * 0.05,
114  obj->getPoseY() - sin(obj->getPoseX() / 2) * 0.09,
115  obj->getPoseZ() - sin(obj->getPoseX() / 2) * 0.08);
116 
117  obj = scene->getByName("ball_2");
118  obj->setLocation(
119  obj->getPoseX() + cos(obj->getPoseY() / 2) * 0.05,
120  obj->getPoseY() - sin(obj->getPoseX() / 2) * 0.09,
121  obj->getPoseZ() - sin(obj->getPoseX() / 2) * 0.08);
122 
123  // IMPORTANT!!! IF NOT UNLOCKED, THE WINDOW WILL NOT BE UPDATED!
124  win.unlockAccess3DScene();
125 
126  // Update window:
127  win.forceRepaint();
128 
129  std::this_thread::sleep_for(20ms);
130  }
131 }
132 
133 // ------------------------------------------------------
134 // MAIN
135 // ------------------------------------------------------
136 int main(int argc, char* argv[])
137 {
138  try
139  {
140  TestDisplay3D();
141  return 0;
142  }
143  catch (const std::exception& e)
144  {
145  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
146  return -1;
147  }
148  catch (...)
149  {
150  printf("Untyped exception!!");
151  return -1;
152  }
153 }
GLclampf GLclampf GLclampf alpha
Definition: glext.h:3529
std::string myDataDir
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glext.h:3555
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4085
mrpt::gui::CDisplayWindow3D::Ptr win
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void TestDisplay3D()
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:147
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