-
Notifications
You must be signed in to change notification settings - Fork 24
/
pt2-webcam3D.py
105 lines (88 loc) · 3.12 KB
/
pt2-webcam3D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
This serve as our base openGL class.
"""
import numpy as np
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtCore, QtGui
import sys
class Terrain(object):
def __init__(self):
"""
Initialize the graphics window and mesh surface
"""
# setup the view window
self.app = QtGui.QApplication(sys.argv)
self.window = gl.GLViewWidget()
self.window.setWindowTitle('Terrain')
self.window.setGeometry(0, 110, 1920, 1080)
self.window.setCameraPosition(distance=30, elevation=12)
self.window.show()
# constants and arrays
self.nsteps = 1
self.ypoints = np.arange(-20, 20 + self.nsteps, self.nsteps)
self.xpoints = np.arange(-20, 20 + self.nsteps, self.nsteps)
self.nfaces = len(self.ypoints)
# create the veritices array
verts, faces, colors = self.mesh()
self.mesh1 = gl.GLMeshItem(
faces=faces,
vertexes=verts,
faceColors=colors,
drawEdges=True,
smooth=False,
)
self.mesh1.setGLOptions('additive')
self.window.addItem(self.mesh1)
def mesh(self, height=2.5):
faces = []
colors = []
verts = np.array([
[
x, y, height * np.random.rand(1)
] for xid, x in enumerate(self.xpoints) for yid, y in enumerate(self.ypoints)
], dtype=np.float32)
for yid in range(self.nfaces - 1):
yoff = yid * self.nfaces
for xid in range(self.nfaces - 1):
faces.append([
xid + yoff,
xid + yoff + self.nfaces,
xid + yoff + self.nfaces + 1,
])
faces.append([
xid + yoff,
xid + yoff + 1,
xid + yoff + self.nfaces + 1,
])
colors.append([
xid / self.nfaces, 1 - xid / self.nfaces, yid / self.nfaces, 0.7
])
colors.append([
xid / self.nfaces, 1 - xid / self.nfaces, yid / self.nfaces, 0.8
])
faces = np.array(faces, dtype=np.uint32)
colors = np.array(colors, dtype=np.float32)
return verts, faces, colors
def update(self):
"""
update the mesh and shift the noise each time
"""
verts, faces, colors = self.mesh()
self.mesh1.setMeshData(vertexes=verts, faces=faces, faceColors=colors)
def start(self):
"""
get the graphics window open and setup
"""
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
def animation(self, frametime=10):
"""
calls the update method to run in a loop
"""
timer = QtCore.QTimer()
timer.timeout.connect(self.update)
timer.start(frametime)
self.start()
if __name__ == '__main__':
t = Terrain()
t.animation()