-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqt5_QOpenGLWidget.py
111 lines (87 loc) · 2.86 KB
/
qt5_QOpenGLWidget.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, signal
import numpy as np
import moderngl
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QOpenGLWidget
from PyQt5.QtGui import QSurfaceFormat
def make_qt_format():
fmt = QSurfaceFormat()
fmt.setVersion(3, 3)
fmt.setProfile(QSurfaceFormat.CoreProfile)
return fmt
class StimDisplay(QOpenGLWidget):
def __init__(self, app):
super().__init__()
self.setFormat(make_qt_format())
self.app = app
self.ctx = None
self.vao = None
self.subscreen_viewports = [(0, 0, 100, 100),
(101, 0, 100, 100),
(51, 101, 100, 100)]
self.frame_cnt = 0
def initializeGL(self):
# Create ModernGL context
self.ctx = moderngl.create_context()
# Triangle vertices (x, y)
vertices = np.array([
-0.6, -0.6,
0.6, -0.6,
0.0, 0.6
], dtype='f4')
# Vertex Buffer Object
vbo = self.ctx.buffer(vertices)
# Vertex Array Object
self.vao = self.ctx.simple_vertex_array(
self.ctx.program(
vertex_shader="""
#version 330
in vec2 in_vert;
void main() {
gl_Position = vec4(in_vert, 0.0, 1.0);
}
""",
fragment_shader="""
#version 330
out vec4 fragColor;
void main() {
fragColor = vec4(1.0, 0.0, 0.0, 1.0); // Red color
}
""",
),
vbo,
'in_vert'
)
def paintGL(self):
# Clear the buffer
# self.ctx.clear(0.5, 0.5, 0.5, 1.0)
print(f"Frame # {self.frame_cnt}")
if self.frame_cnt % 2 == 0:
print("Clearing context!")
else:
print("Not clearing context!")
for vp in self.subscreen_viewports:
self.ctx.viewport = vp
if self.frame_cnt % 2 == 0:
self.ctx.clear(0.5, 0.5, 0.5, 1.0, viewport=vp)
# Render the triangle
self.vao.render(moderngl.TRIANGLES)
self.frame_cnt += 1
def main():
# set default format with OpenGL context
QSurfaceFormat.setDefaultFormat(make_qt_format())
# launch application
app = QtWidgets.QApplication([])
# create the StimDisplay object
stim_display = StimDisplay(app=app)
stim_display.setGeometry(100, 100, 200, 200)
stim_display.show()
####################################
# Run QApplication
####################################
# Use Ctrl+C to exit.
# ref: https://stackoverflow.com/questions/2300401/qapplication-how-to-shutdown-gracefully-on-ctrl-c
signal.signal(signal.SIGINT, signal.SIG_DFL)
sys.exit(app.exec_())
if __name__ == '__main__':
main()