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 CDisplayWindowGUI
15from mrpt.viz import Scene, 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 = CDisplayWindowGUI('MRPT GUI demo', 800, 600)
27
28# The 3D scene shown in the window:
29scene = Scene()
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
58win.background_scene = scene
59
60# Move camera:
61win.camera().setAzimuthDegrees(-40.0)
62win.camera().setElevationDegrees(30.0)
63win.camera().setZoomDistance(15.0)
64
65print('Close the window to quit the program')
66timer = CTicTac()
67
68# spinOnce() processes the window events and repaints it. It returns False
69# once the window is closed.
70while win.spinOnce() and (args.duration <= 0 or timer.Tac() < args.duration):
71    y = 5.0 * math.sin(1.0 * timer.Tac())
72    glBox.setLocation(4.0, y, 0.0)
73    time.sleep(20e-3)