-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqtgrcontext.py
More file actions
314 lines (256 loc) · 11.9 KB
/
Copy pathqtgrcontext.py
File metadata and controls
314 lines (256 loc) · 11.9 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#/usr/bin/python3
# -*- coding: utf-8 -*-
#
# (c) 2019 by Rob Knop
#
# This file is part of physvis
#
# physvis is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# physvis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with physvis. If not, see <https://www.gnu.org/licenses/>.
import sys
import math
import queue
import PyQt5.QtCore as qtcore
import PyQt5.QtWidgets as qt
import PyQt5.QtGui as qtgui
import OpenGL.GL as GL
import OpenGL.GLU as GLU
from grcontext import *
class QtGrContext(GrContext, qt.QOpenGLWidget):
"""A Qt wigdet that encapsulates a physvis display. Can be put in a Qt
GUI where you'd put any other widget, and can also be used with the context
argument of graphics objects.
"""
def __init__(self, width=500, height=400, title="PhysVis", *args, **kwargs):
"""Parameters:
width — width of the display (default: 500)
height — height of the display (default: 400)
title — a title used for fps printing and maybe other things (default: Physvis)
"""
self._width = width
self._height = height
self._title = title
super().__init__(*args, **kwargs)
fmt = qtgui.QSurfaceFormat()
fmt.setMajorVersion(3)
fmt.setMinorVersion(3)
fmt.setSwapInterval(0) # Let video card vsync setting decide
self.setFormat(fmt)
self.window_is_initialized = False
self.framecount = 0
self._mousex0 = 0.
self._mousey0 = 0.
self._leftmousemoving = False
self._middlemousemoving = False
self._rightmousemoving = False
self.things_to_run = queue.Queue()
self.qtimer = qtcore.QTimer()
self.qtimer.timeout.connect(lambda : self.idlefunc())
# I'm going to run my timer every 10 milliseconds; should I go full bore, or slower, or...?
self.qtimer.start(10)
if GrContext.print_fps:
self.fpstimer = qtcore.QTimer()
self.fpstimer.timeout.connect(lambda : self.printfps())
self.fpstimer.start(2000)
def initializeGL(self):
self.object_collections.append(object_collection.SimpleObjectCollection(self))
self.object_collections.append(object_collection.CurveCollection(self))
self.window_is_initialized = True
self.update()
def printfps(self):
sys.stderr.write("{} display fps: {}\n".format(self._title, self.framecount/2.))
self.framecount = 0
def idlefunc(self):
if not self.window_is_initialized:
return
try:
# Make sure that no rendering is happening while we do idle shit
GL.glFinish()
while not self.things_to_run.empty():
func = self.things_to_run.get()
func()
except (queue.Empty):
pass
def update(self):
qt.QOpenGLWidget.update(self)
def run_glcode(self, func):
self.things_to_run.put(func)
def paintGL(self):
if not self.window_is_initialized:
return
GL.glClearColor(self.background_color[0], self.background_color[1],
self.background_color[2], self.background_color[3])
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
GL.glEnable(GL.GL_DEPTH_TEST)
with Subject._threadlock:
# sys.stderr.write("About to draw collections\n")
for collection in self.object_collections:
collection.draw()
err = GL.glGetError()
if err != GL.GL_NO_ERROR:
sys.stderr.write("Error {} drawing: {}\n".format(err, gluErrorString(err)))
sys.exit(-1)
# Do I have to do anything to make QTGL double-buffer?
Rater.get().set()
self.framecount += 1
def resizeGL(self, width, height):
self.resize2d(width, height) # this is in the superclass
def minimumSizeHint(self):
return qtcore.QSize(100, 100)
def sizeHint(self):
return qtcore.QSize(640, 480)
#========================================
# Mouse handling
#
# A LOT of code is copied straight from GLUTContext. I should
# superclass it... or maybe move to only using Qt as my window
# manager.
# def mouseDoubleClickEvent(self, event):
# sys.stderr.write("Double click!\n")
# if self.isFullScreen():
# sys.stderr.write("Going normal\n")
# self.setParent(self.oldparent)
# self.resize(self.oldwindowsiae)
# self.overridwindowflags(self.oldflags)
# self.showNormal()
# else:
# sys.stderr.write("Going full screen\n")
# self.oldparent = self.parentWidget()
# self.oldwindowsize = self.size()
# self.oldflags = self.windowFlags()
# self.setParent(None)
# self.showFullScreen()
def mousePressEvent(self, event):
buts = event.buttons()
mods = event.modifiers()
x = event.pos().x()
y = event.pos().y()
if buts & qtcore.Qt.LeftButton:
self._leftmousemoving = True
if mods & qtcore.Qt.ShiftModifier:
self._mouseposx0 = x
self._mouseposy0 = y
self._origcenter = self._center
self._upinscreen = self._up - self._forward * ( numpy.sum(self._up*self._forward ) /
math.sqrt( self._up[0]**2 +
self._up[1]**2 +
self._up[2]**2 ) )
self._upinscreen /= math.sqrt( self._upinscreen[0]**2 + self._upinscreen[1]**2 +
self._upinscreen[2]**2 )
self._rightinscreen = numpy.array( [ self._forward[1]*self._upinscreen[2] -
self._forward[2]*self._upinscreen[1],
self._forward[2]*self._upinscreen[0] -
self._forward[0]*self._upinscreen[2],
self._forward[0]*self._upinscreen[1] -
self._forward[1]*self._upinscreen[0] ] )
self._rightinscreen /= math.sqrt( self._rightinscreen[0]**2 + self._rightinscreen[1]**2 +
self._rightinscreen[2]**2 )
if buts & qtcore.Qt.RightButton:
self._rightmousemoving = True
self._mousex0 = x
self._mousey0 = y
self._origtheta = math.acos(-self.forward[1] / math.sqrt( self._forward[0]**2 +
self._forward[1]**2 +
self._forward[2]**2 ) )
self._origphi = math.atan2(-self._forward[0], -self._forward[2])
# sys.stderr.write("Right Press: θ = {:.3f}, φ = {:.3f}\n".format(self._origtheta, self._origphi))
if buts & qtcore.Qt.MidButton:
self._middlemousemoving = True
self._mousex0 = x
self._mousey0 = y
self._origrange = self._range
def mouseReleaseEvent(self, event):
buts = event.buttons()
if not buts & qtcore.Qt.LeftButton:
self._leftmousemoving = False
if not buts & qtcore.Qt.RightButton:
self._rightmousemoving = False
if not buts & qtcore.Qt.MidButton:
self._middlemousemoving = False
def mouseMoveEvent(self, event):
mods = event.modifiers()
x = event.pos().x()
y = event.pos().y()
if self._rightmousemoving:
dx = x - self._mousex0
dy = y - self._mousey0
theta = self._origtheta - dy * math.pi/2. / self._height
if theta > math.pi:
theta = math.pi
if theta < 0.:
theta = 0.
phi = self._origphi - dx * math.pi / self._width
# if phi < -math.pi:
# phi += 2.*math.pi
# if phi > math.pi:
# phi -= 2.*math.pi
self._forward = numpy.array( [ -math.sin(theta) * math.sin(phi),
-math.cos(theta),
-math.sin(theta) * math.cos(phi) ] )
uptheta = theta - math.pi/2.
upphi = phi
if uptheta < 0.:
uptheta = math.fabs(uptheta)
upphi += math.pi
self._up = numpy.array( [ math.sin(uptheta) * math.sin(upphi),
math.cos(uptheta),
math.sin(uptheta) * math.cos(upphi) ] )
# self._up = numpy.array( [0., 1., 0.] )
# sys.stderr.write("Moved from (θ,φ) = ({:.2f},{:.2f}) to ({:.2f},{:.2f})\n".
# format(self._origtheta, self._origphi, theta, phi))
# sys.stderr.write("Forward is now: {}\n".format(self._forward))
self._theta = theta
self._phi = phi
self._uptheta = uptheta
self._upphi = upphi
self.update_cam_posrot_gl()
if self._leftmousemoving and (mods & qtcore.Qt.ShiftModifier) :
dx = x - self._mouseposx0
dy = y - self._mouseposy0
self._center = self._origcenter - self._rightinscreen * dx / self._width * self._range[0]
self._center += self._upinscreen * dy / self._height * self._range[1]
self.update_cam_posrot_gl()
if self._middlemousemoving:
dy = y - self._mousey0
self._range = self._origrange * 10.**(dy/self._width)
self.update_cam_posrot_gl()
def wheelEvent(self, event):
dang = event.angleDelta().y()
steps = abs( dang / 120. )
if dang > 0:
if self._range[0] > 1e-4:
self._range *= (0.9 ** steps)
elif dang < 0:
if self._range[0] < 1e4:
self._range *= (1.1 ** steps)
# sys.stderr.write("Updating self._range to {}\n".format(self._range))
self.update_cam_posrot_gl()
# ========================================
# These next two are 100% redundant with GLUTContext... maybe they should
# go into the superclass?
def add_object(self, obj):
self.run_glcode( lambda : self.do_add_object(obj) )
def remove_object(self, obj):
self.run_glcode( lambda : self.do_remove_object(obj) )
def add_object(self, obj):
# See if any current collection will take it:
for collection in self.object_collections:
if collection.canyoutake(obj):
collection.add_object(obj)
return
# If nobody took it, get a new collection
newcollection = object_collection.GLObjectCollection.get_new_collection(obj, self)
sys.stderr.write("CREATED a new collection for object type {}\n".format(newcollection.my_object_type))
newcollection.add_object(obj)
self.object_collections.append(newcollection)
def remove_object(self, obj):
for collection in self.object_collections:
collection.remove_object(obj)