-
Notifications
You must be signed in to change notification settings - Fork 30
/
viewer.py
111 lines (92 loc) · 3.21 KB
/
viewer.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
import sys
import moderngl as mgl
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from util import *
from camera import Camera
from scene import Scene
class ViewerWidget(QModernGLWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.scene = None
self.last_x = None
self.last_y = None
self.mouse_middle_pressed = False
self.shift_pressed = False
self.ctrl_pressed = False
def init(self):
self.ctx.viewport = self.viewport
self.scene = Scene(self.ctx)
self.ctx_init()
def render(self):
self.screen.use()
self.scene.draw()
def ctx_init(self):
self.ctx.enable(mgl.DEPTH_TEST)
self.ctx.enable(mgl.CULL_FACE)
def mousePressEvent(self, event):
self.last_x = event.x()
self.last_y = event.y()
if event.button() == 4:
self.mouse_middle_pressed = True
self.update()
def mouseReleaseEvent(self, event):
self.last_x = None
self.last_y = None
if event.button() == 4:
self.mouse_middle_pressed = False
self.update()
def mouseMoveEvent(self, event):
dx = event.x() - self.last_x
dy = event.y() - self.last_y
self.last_x = event.x()
self.last_y = event.y()
if self.mouse_middle_pressed == True:
if self.shift_pressed == False:
self.scene.camera.orbit(dx, dy)
else:
self.scene.camera.pan(dx, dy)
self.update()
def wheelEvent(self, event):
offset = event.angleDelta().y() / 120
self.scene.camera.dolly(offset)
self.update()
def resizeEvent(self, event):
width = event.size().width()
height = event.size().height()
if width > height:
self.viewport = (int((width - height) / 2), 0, height, height)
else:
self.viewport = (0, int((height - width) / 2), width, width)
if hasattr(self, 'ctx') and hasattr(self.ctx, 'viewport'):
self.ctx.viewport = self.viewport
def keyPressEvent(self, event):
if event.key() == 16777248:
self.shift_pressed = True
elif event.key() == 16777249:
self.ctrl_pressed = True
elif event.key() == ord('1'):
self.scene.camera.orthogonal(1, self.ctrl_pressed)
elif event.key() == ord('3'):
self.scene.camera.orthogonal(3, self.ctrl_pressed)
elif event.key() == ord('7'):
self.scene.camera.orthogonal(7, self.ctrl_pressed)
self.update()
def keyReleaseEvent(self, event):
if event.key() == 16777248:
self.shift_pressed = False
elif event.key() == 16777249:
self.ctrl_pressed = False
def load_mesh(self, mesh):
self.scene.load_mesh(mesh)
self.update()
def release_mesh(self):
self.scene.release_mesh()
self.update()
def main():
app = QtWidgets.QApplication(sys.argv)
widget = ViewerWidget()
widget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()