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
|
# Copyright (C) 2013 Riverbank Computing Limited.
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from __future__ import annotations
"""PySide6 port of the opengl/textures example from Qt v6.x showing the use
of legacy OpenGL functions with QOpenGLVersionFunctionsFactory."""
import sys
from PySide6.QtCore import QPoint, QSize, Qt, QTimer, Signal
from PySide6.QtGui import QColor, QImage, QSurfaceFormat
from PySide6.QtWidgets import QApplication, QGridLayout, QMessageBox, QWidget
from PySide6.QtOpenGL import (QOpenGLTexture, QOpenGLVersionFunctionsFactory,
QOpenGLVersionProfile)
from PySide6.QtOpenGLWidgets import QOpenGLWidget
try:
from OpenGL import GL
except ImportError:
app = QApplication(sys.argv)
messageBox = QMessageBox(QMessageBox.Critical, "OpenGL textures",
"PyOpenGL must be installed to run this example.",
QMessageBox.Close)
messageBox.setDetailedText("Run:\npip install PyOpenGL PyOpenGL_accelerate")
messageBox.exec()
sys.exit(1)
import textures_rc # noqa: F401
class GLWidget(QOpenGLWidget):
sharedObject = 0
refCount = 0
coords = (
((+1, -1, -1), (-1, -1, -1), (-1, +1, -1), (+1, +1, -1)),
((+1, +1, -1), (-1, +1, -1), (-1, +1, +1), (+1, +1, +1)),
((+1, -1, +1), (+1, -1, -1), (+1, +1, -1), (+1, +1, +1)),
((-1, -1, -1), (-1, -1, +1), (-1, +1, +1), (-1, +1, -1)),
((+1, -1, +1), (-1, -1, +1), (-1, -1, -1), (+1, -1, -1)),
((-1, -1, +1), (+1, -1, +1), (+1, +1, +1), (-1, +1, +1))
)
clicked = Signal()
def __init__(self, parent):
super().__init__(parent)
self.clearColor = Qt.GlobalColor.black
self.xRot = 0
self.yRot = 0
self.zRot = 0
self.clearColor = QColor()
self.lastPos = QPoint()
self.funcs = None
def freeGLResources(self):
GLWidget.refCount -= 1
if GLWidget.refCount == 0:
self.makeCurrent()
self.funcs.glDeleteLists(self.__class__.sharedObject, 1)
def minimumSizeHint(self):
return QSize(50, 50)
def sizeHint(self):
return QSize(200, 200)
def rotateBy(self, xAngle, yAngle, zAngle):
self.xRot = (self.xRot + xAngle) % 5760
self.yRot = (self.yRot + yAngle) % 5760
self.zRot = (self.zRot + zAngle) % 5760
self.update()
def setClearColor(self, color):
self.clearColor = color
self.update()
def initializeGL(self):
profile = QOpenGLVersionProfile()
profile.setVersion(3, 2)
profile.setProfile(QSurfaceFormat.OpenGLContextProfile.CompatibilityProfile)
self.funcs = QOpenGLVersionFunctionsFactory.get(profile)
self.funcs.initializeOpenGLFunctions()
if not GLWidget.sharedObject:
self.textures = []
for i in range(6):
image = QImage(f":/images/side{i + 1}.png")
self.textures.append(QOpenGLTexture(image))
GLWidget.sharedObject = self.makeObject()
GLWidget.refCount += 1
self.funcs.glEnable(GL.GL_DEPTH_TEST)
self.funcs.glEnable(GL.GL_CULL_FACE)
self.funcs.glEnable(GL.GL_TEXTURE_2D)
def paintGL(self):
self.funcs.glClearColor(self.clearColor.red(), self.clearColor.green(),
self.clearColor.blue(), 1)
self.funcs.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
self.funcs.glLoadIdentity()
self.funcs.glTranslated(0.0, 0.0, -10.0)
self.funcs.glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0)
self.funcs.glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0)
self.funcs.glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0)
self.funcs.glCallList(GLWidget.sharedObject)
def resizeGL(self, width, height):
side = min(width, height)
x = int((width - side) / 2)
y = int((height - side) / 2)
self.funcs.glViewport(x, y, side, side)
self.funcs.glMatrixMode(GL.GL_PROJECTION)
self.funcs.glLoadIdentity()
self.funcs.glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0)
self.funcs.glMatrixMode(GL.GL_MODELVIEW)
def mousePressEvent(self, event):
self.lastPos = event.position().toPoint()
def mouseMoveEvent(self, event):
pos = event.position().toPoint()
dx = pos.x() - self.lastPos.x()
dy = pos.y() - self.lastPos.y()
if event.buttons() & Qt.MouseButton.LeftButton:
self.rotateBy(8 * dy, 8 * dx, 0)
elif event.buttons() & Qt.RightButton:
self.rotateBy(8 * dy, 0, 8 * dx)
self.lastPos = pos
def mouseReleaseEvent(self, event):
self.clicked.emit()
def makeObject(self):
dlist = self.funcs.glGenLists(1)
self.funcs.glNewList(dlist, GL.GL_COMPILE)
for i in range(6):
self.textures[i].bind()
self.funcs.glBegin(GL.GL_QUADS)
for j in range(4):
tx = {False: 0, True: 1}[j == 0 or j == 3]
ty = {False: 0, True: 1}[j == 0 or j == 1]
self.funcs.glTexCoord2d(tx, ty)
x = 0.2 * GLWidget.coords[i][j][0]
y = 0.2 * GLWidget.coords[i][j][1]
z = 0.2 * GLWidget.coords[i][j][2]
self.funcs.glVertex3d(x, y, z)
self.funcs.glEnd()
self.funcs.glEndList()
return dlist
class Window(QWidget):
NumRows = 2
NumColumns = 3
def __init__(self, parent=None):
QWidget.__init__(self, parent)
mainLayout = QGridLayout(self)
self.glWidgets = []
for i in range(Window.NumRows):
self.glWidgets.append([])
for j in range(Window.NumColumns):
self.glWidgets[i].append(None)
hue_div = (Window.NumRows * Window.NumColumns - 1)
for i in range(Window.NumRows):
for j in range(Window.NumColumns):
clearColor = QColor()
hue = ((i * Window.NumColumns) + j) * 255 / hue_div
clearColor.setHsv(hue, 255, 63)
glw = GLWidget(self)
self.glWidgets[i][j] = glw
glw.setClearColor(clearColor)
glw.rotateBy(+42 * 16, +42 * 16, -21 * 16)
mainLayout.addWidget(glw, i, j)
glw.clicked.connect(self.setCurrentGlWidget)
qApp.lastWindowClosed.connect(glw.freeGLResources) # noqa: F821
self.currentGlWidget = self.glWidgets[0][0]
timer = QTimer(self)
timer.timeout.connect(self.rotateOneStep)
timer.start(20)
self.setWindowTitle(self.tr("Textures"))
def setCurrentGlWidget(self):
self.currentGlWidget = self.sender()
def rotateOneStep(self):
if self.currentGlWidget:
self.currentGlWidget.rotateBy(+2 * 16, +2 * 16, -1 * 16)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
|