Python example: opengl-demo-gui.py
Animated 3D scene in a GUI window, with mrpt.gui and mrpt.viz.
Modules: mrpt.gui, mrpt.viz, mrpt.math, mrpt.img, mrpt.system | Requires: gui
1#!/usr/bin/env python3
2"""
3Animated 3D scene in a GUI window, with mrpt.gui and mrpt.viz.
4
5Usage:
6
7 . install/setup.bash && python3 opengl-demo-gui.py [--duration SECONDS]
8"""
9
10import argparse
11import time
12import math
13
14from mrpt.gui import CDisplayWindow3D
15from mrpt.viz import CGridPlaneXY, CBox, CSetOfObjects, stock_objects
16from mrpt.math import TPoint3D
17from mrpt.img import TColor
18from mrpt.system import CTicTac
19
20parser = argparse.ArgumentParser()
21parser.add_argument("--duration", type=float, default=0,
22 help="Close the window after this many seconds (default: 0 = never)")
23args = parser.parse_args()
24
25# Create GUI:
26win = CDisplayWindow3D('MRPT GUI demo', 800, 600)
27
28# Lock 3D scene before modifying it (rendering runs on a separate thread):
29scene = win.get3DSceneAndLock()
30
31# A grid on the XY horizontal plane:
32glGrid = CGridPlaneXY(-3, 3, -3, 3, 0, 1)
33scene.insert(glGrid)
34
35# A couple of XYZ "corners":
36glCorner = stock_objects.CornerXYZ(2.0)
37scene.insert(glCorner)
38
39glCorner2: CSetOfObjects = stock_objects.CornerXYZ(1.0)
40glCorner2.setLocation(4.0, 0.0, 0.0)
41scene.insert(glCorner2)
42
43# A floor "block":
44glFloor = CBox()
45glFloor.setBoxCorners(TPoint3D(-15, -15, 0), TPoint3D(15, 15, 0.1))
46glFloor.setLocation(0, 0, -3.0)
47glFloor.setColor(0x70/255.0, 0x70/255.0, 0x70/255.0)
48scene.insert(glFloor)
49
50# A mobile box to illustrate animations:
51glBox = CBox()
52glBox.setBoxCorners(TPoint3D(0, 0, 0), TPoint3D(1, 1, 1))
53glBox.enableBoxBorder(True)
54glBox.setBoxBorderColor(TColor(0, 0, 0))
55glBox.castShadows = True
56scene.insert(glBox)
57
58# Move camera:
59win.setCameraAzimuthDeg(-40.0)
60win.setCameraElevationDeg(30.0)
61
62win.unlockAccess3DScene()
63
64print('Close the window to quit the program')
65timer = CTicTac()
66
67while win.isOpen() and (args.duration <= 0 or timer.Tac() < args.duration):
68 y = 5.0 * math.sin(1.0 * timer.Tac())
69 glBox.setLocation(4.0, y, 0.0)
70 win.repaint()
71 time.sleep(50e-3)