Python example: opengl-demo-gui.py
This example illustrates how to create a 3D GUI in MRPT using the Python API and populate it with different objects, including animating them across the scene.
1#!/usr/bin/env python3
2
3# ---------------------------------------------------------------------
4# Install python3-pymrpt, ros-$ROS_DISTRO-mrpt2, or test with a local build with:
5# export PYTHONPATH=$HOME/code/mrpt/build-Release/:$PYTHONPATH
6# ---------------------------------------------------------------------
7
8from mrpt.pymrpt import mrpt
9import time
10import math
11
12# Create GUI:
13win = mrpt.gui.CDisplayWindow3D('MRPT GUI demo', 800, 600)
14
15# Get and lock 3D scene:
16# Lock is required again each time the scene is modified, to prevent
17# data race between the main and the rendering threads.
18scene = win.get3DSceneAndLock()
19
20# A grid on the XY horizontal plane:
21# ctor args: xMin: float, xMax: float, yMin: float, yMax: float, z: float, frequency: float
22glGrid = mrpt.opengl.CGridPlaneXY.Create(-3, 3, -3, 3, 0, 1)
23scene.insert(glGrid)
24
25# A couple of XYZ "corners":
26glCorner = mrpt.opengl.stock_objects.CornerXYZ(2.0)
27scene.insert(glCorner)
28
29glCorner2: mrpt.opengl.CSetOfObjects = mrpt.opengl.stock_objects.CornerXYZ(1.0)
30glCorner2.setLocation(4.0, 0.0, 0.0)
31scene.insert(glCorner2)
32
33# A 3D inverse-depth ellipsoid:
34glEllip = mrpt.opengl.CEllipsoidInverseDepth3D()
35cov = mrpt.math.CMatrixFixed_double_3UL_3UL_t.Zero()
36cov[0, 0] = 0.01
37cov[1, 1] = 0.001
38cov[2, 2] = 0.002
39mean = mrpt.math.CMatrixFixed_double_3UL_1UL_t()
40mean[0, 0] = 0.2 # inv_range
41mean[1, 0] = 0.5 # yaw
42mean[2, 0] = -0.6 # pitch
43glEllip.setCovMatrixAndMean(cov, mean)
44scene.insert(glEllip)
45
46# A floor "block":
47glFloor = mrpt.opengl.CBox()
48glFloor.setBoxCorners(mrpt.math.TPoint3D_double_t(-15, -15, 0),
49 mrpt.math.TPoint3D_double_t(15, 15, 0.1))
50glFloor.setLocation(0, 0, -3.0)
51glFloor.setColor_u8(mrpt.img.TColor(0x70, 0x70, 0x70))
52scene.insert(glFloor)
53
54# A mobile box to illustrate animations:
55glBox = mrpt.opengl.CBox()
56glBox.setBoxCorners(mrpt.math.TPoint3D_double_t(0, 0, 0),
57 mrpt.math.TPoint3D_double_t(1, 1, 1))
58glBox.setBoxBorderColor(mrpt.img.TColor(0, 0, 0))
59glBox.castShadows(True)
60scene.insert(glBox)
61
62# Shadows are disabled by default, enable them:
63scene.getViewport().enableShadowCasting(True)
64print('Shadow casting: ' + str(scene.getViewport().isShadowCastingEnabled()))
65
66# Move camera:
67win.setCameraAzimuthDeg(-40.0)
68win.setCameraElevationDeg(30.0)
69
70# end of scene lock:
71win.unlockAccess3DScene()
72
73
74print('Close the window to quit the program')
75timer = mrpt.system.CTicTac()
76
77while win.isOpen():
78 y = 5.0*math.sin(1.0*timer.Tac())
79 glBox.setLocation(4.0, y, 0.0)
80
81 win.repaint()
82 time.sleep(50e-3)