-
Notifications
You must be signed in to change notification settings - Fork 5
/
glwidget.py
239 lines (207 loc) · 7.15 KB
/
glwidget.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
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
"""
Copyright (c) 2012-2014, Emanuele Olivetti and Eleftherios Garyfallidis
Distributed under the BSD 3-clause license. See COPYING.txt.
"""
from PySide import QtCore, QtGui, QtOpenGL
from fos.world import *
try:
from pyglet.gl import *
except ImportError:
print("Need pyglet for OpenGL rendering")
empty_messages={ 'key_pressed':None,
'mouse_pressed':None,
'mouse_position':None,
'mouse_moved':None,
'mod_key_pressed':None}
class GLWidget(QtOpenGL.QGLWidget):
"""
"""
def __init__(self, parent=None,
width = None,
height = None,
bgcolor = None,
enable_light = False,
ortho = False):
"""
"""
QtOpenGL.QGLWidget.__init__(self, parent)
self.lastPos = QtCore.QPoint()
self.bgcolor = QtGui.QColor.fromRgbF(bgcolor[0], bgcolor[1], bgcolor[2], 1.0)
self.width = width
self.height = height
self.enable_light = enable_light
self.world = World()
self.ortho = ortho
self.messages = empty_messages
self.setMouseTracking(True)
# camera rotation speed
self.ang_step = 0.02
# necessary to grab key events
self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.parent = parent
def minimumSizeHint(self):
"""
"""
return QtCore.QSize(50, 50)
def sizeHint(self):
"""
"""
return QtCore.QSize(self.width, self.height)
def initializeGL(self):
"""
"""
self.qglClearColor(self.bgcolor)
glShadeModel(GL_SMOOTH)
# glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
glEnable(GL_BLEND)
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
if self.enable_light:
self.world.setup_light()
def paintGL(self):
"""
"""
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
self.world.draw_all()
def resizeGL(self, width, height):
"""
"""
# side = min(width, height)
# glViewport((width - side) / 2, (height - side) / 2, side, side)
if height == 0:
height = 1
vsml.setSize( width, height )
self.width = width
self.height = height
glViewport(0, 0, width, height)
if self.ortho:
self.width_ortho = self.width
self.height_ortho = self.height
self.update_projection()
def update_projection(self, factor = 1.0):
"""
"""
vsml.loadIdentity(vsml.MatrixTypes.PROJECTION)
ratio = abs(self.width * 1.0 / self.height)
if self.ortho:
self.width_ortho += -factor * ratio
self.height_ortho += -factor
vsml.ortho(-(self.width_ortho)/2.,(self.width_ortho)/2.,
(self.height_ortho)/2.,(self.height_ortho)/-2.,-500,8000)
else:
vsml.perspective(60., ratio, .1, 8000)
glMatrixMode(GL_PROJECTION)
glLoadMatrixf(vsml.get_projection())
glMatrixMode(GL_MODELVIEW)
def ortho_zoom(self, zoom_level = 1.0):
"""
"""
if not self.ortho:
print('Not on orthogonal projection mode')
return
vsml.loadIdentity(vsml.MatrixTypes.PROJECTION)
ratio = abs(self.width * 1.0 / self.height)
self.width_ortho = self.width * zoom_level * ratio
self.height_ortho = self.width * zoom_level
vsml.ortho(-(self.width_ortho)/2.,(self.width_ortho)/2.,
(self.height_ortho)/2.,(self.height_ortho)/-2.,-500,8000)
glMatrixMode(GL_PROJECTION)
glLoadMatrixf(vsml.get_projection())
glMatrixMode(GL_MODELVIEW)
def mousePressEvent(self, event):
"""
"""
self.lastPos = QtCore.QPoint(event.pos())
if (event.modifiers() & QtCore.Qt.ControlModifier):
x, y = event.x(), event.y()
self.world.pick_all(x, self.height - y)
def mouseMoveEvent(self, event):
"""
"""
self.messages=empty_messages.copy()
self.messages['mouse_position']=(event.x(),self.height - event.y())
self.world.send_all_messages(self.messages)
self.messages=empty_messages
dx = event.x() - self.lastPos.x()
dy = event.y() - self.lastPos.y()
if (event.modifiers() & QtCore.Qt.ShiftModifier):
shift = True
else:
shift = False
if (event.modifiers() & QtCore.Qt.ControlModifier):
ctrl = True
else:
ctrl = False
if event.buttons() & QtCore.Qt.LeftButton:
if not ctrl:
# should rotate
if dx != 0:
# rotate around yup
if dx > 0: angle = -self.ang_step #0.01
else: angle = self.ang_step #0.01
if shift: angle *= 2
self.world.camera.rotate_around_focal(angle, "yup")
if dy != 0:
# rotate around right
if dy > 0: angle = -self.ang_step #0.01
else: angle = self.ang_step #0.01
if shift: angle *= 2
self.world.camera.rotate_around_focal(angle, "right")
self.updateGL()
else:
# with control, do many selects!
x, y = event.x(), event.y()
self.world.pick_all( x, self.height - y)
elif event.buttons() & QtCore.Qt.RightButton:
# should pan
if dx > 0: pandx = -1.0
elif dx < 0: pandx = 1.0
else: pandx = 0.0
if dy > 0: pandy = 0.5
elif dy < 0: pandy = -0.5
else: pandy = 0.0
if shift:
pandx *= 4
pandy *= 4
self.world.camera.pan( pandx, pandy )
self.updateGL()
self.lastPos = QtCore.QPoint(event.pos())
def wheelEvent(self, e):
"""
"""
numSteps = e.delta()
if (e.modifiers() & QtCore.Qt.ControlModifier):
ctrl = True
else:
ctrl = False
if (e.modifiers() & QtCore.Qt.ShiftModifier):
shift = True
else:
shift = False
if self.ortho:
if shift:
numSteps *= 10
self.update_projection( 10.*numSteps )
self.updateGL()
return
if ctrl:
if shift:
self.world.camera.move_forward_all( numSteps * 10 )
else:
self.world.camera.move_forward_all( numSteps )
else:
if shift:
self.world.camera.move_forward( numSteps * 10 )
else:
self.world.camera.move_forward( numSteps )
self.updateGL()
def keyPressEvent(self, event):
""" Handle all key press events
"""
key = event.key()
self.messages=empty_messages.copy()
self.messages['key_pressed']=key
self.world.send_all_messages(self.messages)
self.updateGL()
self.parent.keyPressEvent(event)