diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-14 10:33:47 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-07-14 10:08:14 +0000 |
commit | d65f3ab8ce9abc8ed99b699efca9bda4f0a18006 (patch) | |
tree | 581005caafce85a60740ee35162af4930f639d78 /examples/opengl | |
parent | 31d2303a836177cc3e50a85e5190096be1232447 (diff) |
Add QtOpenGLWidgets
QOpenGLWidget has been moved from QtWidgets into
a separate library.
Fix the examples and remove obsolete examples using deceased
QGLWidget.
Task-number: PYSIDE-1339
Task-number: PYSIDE-904
Change-Id: Ib291d49c22ee6a32d7c03b6ff4980e87c8a09ecb
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples/opengl')
-rw-r--r-- | examples/opengl/2dpainting.py | 172 | ||||
-rw-r--r-- | examples/opengl/contextinfo.py | 5 | ||||
-rw-r--r-- | examples/opengl/grabber.py | 437 | ||||
-rw-r--r-- | examples/opengl/hellogl.py | 287 | ||||
-rw-r--r-- | examples/opengl/hellogl2.py | 12 | ||||
-rw-r--r-- | examples/opengl/opengl.pyproject | 4 | ||||
-rw-r--r-- | examples/opengl/overpainting.py | 385 | ||||
-rw-r--r-- | examples/opengl/samplebuffers.py | 192 |
8 files changed, 12 insertions, 1482 deletions
diff --git a/examples/opengl/2dpainting.py b/examples/opengl/2dpainting.py deleted file mode 100644 index 5b3ba6183..000000000 --- a/examples/opengl/2dpainting.py +++ /dev/null @@ -1,172 +0,0 @@ - -############################################################################ -## -## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: http://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################ - -"""PySide2 port of the opengl/legacy/2dpainting example from Qt v5.x""" - -import sys -from PySide2.QtCore import * -from PySide2.QtGui import * -from PySide2.QtWidgets import * -from PySide2.QtOpenGL import * - -try: - from OpenGL import GL -except ImportError: - app = QApplication(sys.argv) - messageBox = QMessageBox(QMessageBox.Critical, "OpenGL 2dpainting", - "PyOpenGL must be installed to run this example.", - QMessageBox.Close) - messageBox.setDetailedText("Run:\npip install PyOpenGL PyOpenGL_accelerate") - messageBox.exec_() - sys.exit(1) - - -class Helper: - def __init__(self): - gradient = QLinearGradient(QPointF(50, -20), QPointF(80, 20)) - gradient.setColorAt(0.0, Qt.white) - gradient.setColorAt(1.0, QColor(0xa6, 0xce, 0x39)) - - self.background = QBrush(QColor(64, 32, 64)) - self.circleBrush = QBrush(gradient) - self.circlePen = QPen(Qt.black) - self.circlePen.setWidth(1) - self.textPen = QPen(Qt.white) - self.textFont = QFont() - self.textFont.setPixelSize(50) - - def paint(self, painter, event, elapsed): - painter.fillRect(event.rect(), self.background) - painter.translate(100, 100) - - painter.save() - painter.setBrush(self.circleBrush) - painter.setPen(self.circlePen) - painter.rotate(elapsed * 0.030) - - r = elapsed/1000.0 - n = 30 - for i in range(n): - painter.rotate(30) - radius = 0 + 120.0*((i+r)/n) - circleRadius = 1 + ((i+r)/n)*20 - painter.drawEllipse(QRectF(radius, -circleRadius, - circleRadius*2, circleRadius*2)) - - painter.restore() - - painter.setPen(self.textPen) - painter.setFont(self.textFont) - painter.drawText(QRect(-50, -50, 100, 100), Qt.AlignCenter, "Qt") - - -class Widget(QWidget): - def __init__(self, helper, parent = None): - QWidget.__init__(self, parent) - - self.helper = helper - self.elapsed = 0 - self.setFixedSize(200, 200) - - def animate(self): - self.elapsed = (self.elapsed + self.sender().interval()) % 1000 - self.repaint() - - def paintEvent(self, event): - painter = QPainter() - painter.begin(self) - painter.setRenderHint(QPainter.Antialiasing) - self.helper.paint(painter, event, self.elapsed) - painter.end() - - -class GLWidget(QGLWidget): - def __init__(self, helper, parent = None): - QGLWidget.__init__(self, QGLFormat(QGL.SampleBuffers), parent) - - self.helper = helper - self.elapsed = 0 - self.setFixedSize(200, 200) - - def animate(self): - self.elapsed = (self.elapsed + self.sender().interval()) % 1000 - self.repaint() - - def paintEvent(self, event): - painter = QPainter() - painter.begin(self) - self.helper.paint(painter, event, self.elapsed) - painter.end() - - -class Window(QWidget): - def __init__(self, parent = None): - QWidget.__init__(self, parent) - - helper = Helper() - native = Widget(helper, self) - openGL = GLWidget(helper, self) - nativeLabel = QLabel(self.tr("Native")) - nativeLabel.setAlignment(Qt.AlignHCenter) - openGLLabel = QLabel(self.tr("OpenGL")) - openGLLabel.setAlignment(Qt.AlignHCenter) - - layout = QGridLayout() - layout.addWidget(native, 0, 0) - layout.addWidget(openGL, 0, 1) - layout.addWidget(nativeLabel, 1, 0) - layout.addWidget(openGLLabel, 1, 1) - self.setLayout(layout) - - timer = QTimer(self) - self.connect(timer, SIGNAL("timeout()"), native.animate) - self.connect(timer, SIGNAL("timeout()"), openGL.animate) - timer.start(50) - - self.setWindowTitle(self.tr("2D Painting on Native and OpenGL Widgets")) - - -if __name__ == '__main__': - app = QApplication(sys.argv) - window = Window() - window.show() - sys.exit(app.exec_()) diff --git a/examples/opengl/contextinfo.py b/examples/opengl/contextinfo.py index a963b8952..fce400e70 100644 --- a/examples/opengl/contextinfo.py +++ b/examples/opengl/contextinfo.py @@ -48,8 +48,9 @@ from textwrap import dedent from PySide2.QtCore import QCoreApplication, QLibraryInfo, QSize, QTimer, Qt -from PySide2.QtGui import (QMatrix4x4, QOpenGLBuffer, QOpenGLContext, QOpenGLShader, - QOpenGLShaderProgram, QOpenGLVertexArrayObject, QSurfaceFormat, QWindow) +from PySide2.QtGui import (QMatrix4x4, QOpenGLContext, QSurfaceFormat, QWindow) +from PySide2.QtOpenGL import (QOpenGLBuffer, QOpenGLShader, + QOpenGLShaderProgram, QOpenGLVertexArrayObject) from PySide2.QtWidgets import (QApplication, QHBoxLayout, QMessageBox, QPlainTextEdit, QWidget) from PySide2.support import VoidPtr diff --git a/examples/opengl/grabber.py b/examples/opengl/grabber.py deleted file mode 100644 index 04a16fcbc..000000000 --- a/examples/opengl/grabber.py +++ /dev/null @@ -1,437 +0,0 @@ - -############################################################################ -## -## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: http://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################ - -"""PySide2 port of the opengl/legacy/grabber example from Qt v5.x""" - -import sys -import math - -from PySide2 import QtCore, QtGui, QtWidgets, QtOpenGL - -try: - from OpenGL.GL import * -except ImportError: - app = QtWidgets.QApplication(sys.argv) - messageBox = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Critical, "OpenGL grabber", - "PyOpenGL must be installed to run this example.", - QtWidgets.QMessageBox.Close) - messageBox.setDetailedText("Run:\npip install PyOpenGL PyOpenGL_accelerate") - messageBox.exec_() - sys.exit(1) - - -class GLWidget(QtOpenGL.QGLWidget): - xRotationChanged = QtCore.Signal(int) - yRotationChanged = QtCore.Signal(int) - zRotationChanged = QtCore.Signal(int) - - def __init__(self, parent=None): - super(GLWidget, self).__init__(parent) - - self.gear1 = 0 - self.gear2 = 0 - self.gear3 = 0 - self.xRot = 0 - self.yRot = 0 - self.zRot = 0 - self.gear1Rot = 0 - - timer = QtCore.QTimer(self) - timer.timeout.connect(self.advanceGears) - timer.start(20) - - def freeResources(self): - self.makeCurrent() - glDeleteLists(self.gear1, 1) - glDeleteLists(self.gear2, 1) - glDeleteLists(self.gear3, 1) - - def setXRotation(self, angle): - self.normalizeAngle(angle) - - if angle != self.xRot: - self.xRot = angle - self.xRotationChanged.emit(angle) - self.updateGL() - - def setYRotation(self, angle): - self.normalizeAngle(angle) - - if angle != self.yRot: - self.yRot = angle - self.yRotationChanged.emit(angle) - self.updateGL() - - def setZRotation(self, angle): - self.normalizeAngle(angle) - - if angle != self.zRot: - self.zRot = angle - self.zRotationChanged.emit(angle) - self.updateGL() - - def initializeGL(self): - lightPos = (5.0, 5.0, 10.0, 1.0) - reflectance1 = (0.8, 0.1, 0.0, 1.0) - reflectance2 = (0.0, 0.8, 0.2, 1.0) - reflectance3 = (0.2, 0.2, 1.0, 1.0) - - glLightfv(GL_LIGHT0, GL_POSITION, lightPos) - glEnable(GL_LIGHTING) - glEnable(GL_LIGHT0) - glEnable(GL_DEPTH_TEST) - - self.gear1 = self.makeGear(reflectance1, 1.0, 4.0, 1.0, 0.7, 20) - self.gear2 = self.makeGear(reflectance2, 0.5, 2.0, 2.0, 0.7, 10) - self.gear3 = self.makeGear(reflectance3, 1.3, 2.0, 0.5, 0.7, 10) - - glEnable(GL_NORMALIZE) - glClearColor(0.0, 0.0, 0.0, 1.0) - - def paintGL(self): - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) - - glPushMatrix() - glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0) - glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0) - glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0) - - self.drawGear(self.gear1, -3.0, -2.0, 0.0, self.gear1Rot / 16.0) - self.drawGear(self.gear2, +3.1, -2.0, 0.0, - -2.0 * (self.gear1Rot / 16.0) - 9.0) - - glRotated(+90.0, 1.0, 0.0, 0.0) - self.drawGear(self.gear3, -3.1, -1.8, -2.2, - +2.0 * (self.gear1Rot / 16.0) - 2.0) - - glPopMatrix() - - def resizeGL(self, width, height): - side = min(width, height) - if side < 0: - return - - glViewport(int((width - side) / 2), int((height - side) / 2), side, side) - - glMatrixMode(GL_PROJECTION) - glLoadIdentity() - glFrustum(-1.0, +1.0, -1.0, 1.0, 5.0, 60.0) - glMatrixMode(GL_MODELVIEW) - glLoadIdentity() - glTranslated(0.0, 0.0, -40.0) - - def mousePressEvent(self, event): - self.lastPos = event.pos() - - def mouseMoveEvent(self, event): - dx = event.x() - self.lastPos.x() - dy = event.y() - self.lastPos.y() - - if event.buttons() & QtCore.Qt.LeftButton: - self.setXRotation(self.xRot + 8 * dy) - self.setYRotation(self.yRot + 8 * dx) - elif event.buttons() & QtCore.Qt.RightButton: - self.setXRotation(self.xRot + 8 * dy) - self.setZRotation(self.zRot + 8 * dx) - - self.lastPos = event.pos() - - def advanceGears(self): - self.gear1Rot += 2 * 16 - self.updateGL() - - def xRotation(self): - return self.xRot - - def yRotation(self): - return self.yRot - - def zRotation(self): - return self.zRot - - def makeGear(self, reflectance, innerRadius, outerRadius, thickness, toothSize, toothCount): - list = glGenLists(1) - glNewList(list, GL_COMPILE) - glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, reflectance) - - r0 = innerRadius - r1 = outerRadius - toothSize / 2.0 - r2 = outerRadius + toothSize / 2.0 - delta = (2.0 * math.pi / toothCount) / 4.0 - z = thickness / 2.0 - - glShadeModel(GL_FLAT) - - for i in range(2): - if i == 0: - sign = +1.0 - else: - sign = -1.0 - - glNormal3d(0.0, 0.0, sign) - - glBegin(GL_QUAD_STRIP) - - for j in range(toothCount+1): - angle = 2.0 * math.pi * j / toothCount - glVertex3d(r0 * math.cos(angle), r0 * math.sin(angle), sign * z) - glVertex3d(r1 * math.cos(angle), r1 * math.sin(angle), sign * z) - glVertex3d(r0 * math.cos(angle), r0 * math.sin(angle), sign * z) - glVertex3d(r1 * math.cos(angle + 3 * delta), r1 * math.sin(angle + 3 * delta), sign * z) - - glEnd() - - glBegin(GL_QUADS) - - for j in range(toothCount): - angle = 2.0 * math.pi * j / toothCount - glVertex3d(r1 * math.cos(angle), r1 * math.sin(angle), sign * z) - glVertex3d(r2 * math.cos(angle + delta), r2 * math.sin(angle + delta), sign * z) - glVertex3d(r2 * math.cos(angle + 2 * delta), r2 * math.sin(angle + 2 * delta), sign * z) - glVertex3d(r1 * math.cos(angle + 3 * delta), r1 * math.sin(angle + 3 * delta), sign * z) - - glEnd() - - glBegin(GL_QUAD_STRIP) - - for i in range(toothCount): - for j in range(2): - angle = 2.0 * math.pi * (i + (j / 2.0)) / toothCount - s1 = r1 - s2 = r2 - - if j == 1: - s1, s2 = s2, s1 - - glNormal3d(math.cos(angle), math.sin(angle), 0.0) - glVertex3d(s1 * math.cos(angle), s1 * math.sin(angle), +z) - glVertex3d(s1 * math.cos(angle), s1 * math.sin(angle), -z) - - glNormal3d(s2 * math.sin(angle + delta) - s1 * math.sin(angle), s1 * math.cos(angle) - s2 * math.cos(angle + delta), 0.0) - glVertex3d(s2 * math.cos(angle + delta), s2 * math.sin(angle + delta), +z) - glVertex3d(s2 * math.cos(angle + delta), s2 * math.sin(angle + delta), -z) - - glVertex3d(r1, 0.0, +z) - glVertex3d(r1, 0.0, -z) - glEnd() - - glShadeModel(GL_SMOOTH) - - glBegin(GL_QUAD_STRIP) - - for i in range(toothCount+1): - angle = i * 2.0 * math.pi / toothCount - glNormal3d(-math.cos(angle), -math.sin(angle), 0.0) - glVertex3d(r0 * math.cos(angle), r0 * math.sin(angle), +z) - glVertex3d(r0 * math.cos(angle), r0 * math.sin(angle), -z) - - glEnd() - - glEndList() - - return list - - def drawGear(self, gear, dx, dy, dz, angle): - glPushMatrix() - glTranslated(dx, dy, dz) - glRotated(angle, 0.0, 0.0, 1.0) - glCallList(gear) - glPopMatrix() - - def normalizeAngle(self, angle): - while (angle < 0): - angle += 360 * 16 - - while (angle > 360 * 16): - angle -= 360 * 16 - - -class MainWindow(QtWidgets.QMainWindow): - def __init__(self): - super(MainWindow, self).__init__() - - centralWidget = QtWidgets.QWidget() - self.setCentralWidget(centralWidget) - - self.glWidget = GLWidget() - self.pixmapLabel = QtWidgets.QLabel() - - self.glWidgetArea = QtWidgets.QScrollArea() - self.glWidgetArea.setWidget(self.glWidget) - self.glWidgetArea.setWidgetResizable(True) - self.glWidgetArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) - self.glWidgetArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) - self.glWidgetArea.setSizePolicy(QtWidgets.QSizePolicy.Ignored, - QtWidgets.QSizePolicy.Ignored) - self.glWidgetArea.setMinimumSize(50, 50) - - self.pixmapLabelArea = QtWidgets.QScrollArea() - self.pixmapLabelArea.setWidget(self.pixmapLabel) - self.pixmapLabelArea.setSizePolicy(QtWidgets.QSizePolicy.Ignored, - QtWidgets.QSizePolicy.Ignored) - self.pixmapLabelArea.setMinimumSize(50, 50) - - xSlider = self.createSlider(self.glWidget.xRotationChanged, - self.glWidget.setXRotation) - ySlider = self.createSlider(self.glWidget.yRotationChanged, - self.glWidget.setYRotation) - zSlider = self.createSlider(self.glWidget.zRotationChanged, - self.glWidget.setZRotation) - - self.createActions() - self.createMenus() - - centralLayout = QtWidgets.QGridLayout() - centralLayout.addWidget(self.glWidgetArea, 0, 0) - centralLayout.addWidget(self.pixmapLabelArea, 0, 1) - centralLayout.addWidget(xSlider, 1, 0, 1, 2) - centralLayout.addWidget(ySlider, 2, 0, 1, 2) - centralLayout.addWidget(zSlider, 3, 0, 1, 2) - centralWidget.setLayout(centralLayout) - - xSlider.setValue(15 * 16) - ySlider.setValue(345 * 16) - zSlider.setValue(0 * 16) - - self.setWindowTitle("Grabber") - self.resize(400, 300) - - def renderIntoPixmap(self): - size = self.getSize() - - if size.isValid(): - pixmap = self.glWidget.renderPixmap(size.width(), size.height()) - self.setPixmap(pixmap) - - def grabFrameBuffer(self): - image = self.glWidget.grabFrameBuffer() - self.setPixmap(QtGui.QPixmap.fromImage(image)) - - def clearPixmap(self): - self.setPixmap(QtGui.QPixmap()) - - def about(self): - QtWidgets.QMessageBox.about(self, "About Grabber", - "The <b>Grabber</b> example demonstrates two approaches for " - "rendering OpenGL into a Qt pixmap.") - - def createActions(self): - self.renderIntoPixmapAct = QtGui.QAction("&Render into Pixmap...", - self, shortcut="Ctrl+R", triggered=self.renderIntoPixmap) - - self.grabFrameBufferAct = QtGui.QAction("&Grab Frame Buffer", self, - shortcut="Ctrl+G", triggered=self.grabFrameBuffer) - - self.clearPixmapAct = QtGui.QAction("&Clear Pixmap", self, - shortcut="Ctrl+L", triggered=self.clearPixmap) - - self.exitAct = QtGui.QAction("E&xit", self, shortcut="Ctrl+Q", - triggered=self.close) - - self.aboutAct = QtGui.QAction("&About", self, triggered=self.about) - - self.aboutQtAct = QtGui.QAction("About &Qt", self, - triggered=qApp.aboutQt) - - def createMenus(self): - self.fileMenu = self.menuBar().addMenu("&File") - self.fileMenu.addAction(self.renderIntoPixmapAct) - self.fileMenu.addAction(self.grabFrameBufferAct) - self.fileMenu.addAction(self.clearPixmapAct) - self.fileMenu.addSeparator() - self.fileMenu.addAction(self.exitAct) - - self.helpMenu = self.menuBar().addMenu("&Help") - self.helpMenu.addAction(self.aboutAct) - self.helpMenu.addAction(self.aboutQtAct) - - def createSlider(self, changedSignal, setterSlot): - slider = QtWidgets.QSlider(QtCore.Qt.Horizontal) - slider.setRange(0, 360 * 16) - slider.setSingleStep(16) - slider.setPageStep(15 * 16) - slider.setTickInterval(15 * 16) - slider.setTickPosition(QtWidgets.QSlider.TicksRight) - - slider.valueChanged.connect(setterSlot) - changedSignal.connect(slider.setValue) - - return slider - - def setPixmap(self, pixmap): - self.pixmapLabel.setPixmap(pixmap) - size = pixmap.size() - - if size - QtCore.QSize(1, 0) == self.pixmapLabelArea.maximumViewportSize(): - size -= QtCore.QSize(1, 0) - - self.pixmapLabel.resize(size) - - def getSize(self): - text, ok = QtWidgets.QInputDialog.getText(self, "Grabber", - "Enter pixmap size:", QtWidgets.QLineEdit.Normal, - "%d x %d" % (self.glWidget.width(), self.glWidget.height())) - - if not ok: - return QtCore.QSize() - - regExp = QtCore.QRegularExpression("([0-9]+) *x *([0-9]+)") - assert regExp.isValid() - - match = regExp.match(text) - if match.hasMatch(): - width = int(match.captured(1)) - height = int(match.captured(2)) - if width > 0 and width < 2048 and height > 0 and height < 2048: - return QtCore.QSize(width, height) - - return self.glWidget.size() - - -if __name__ == '__main__': - - app = QtWidgets.QApplication(sys.argv) - mainWin = MainWindow() - mainWin.show() - res = app.exec_() - mainWin.glWidget.freeResources() - sys.exit(res) diff --git a/examples/opengl/hellogl.py b/examples/opengl/hellogl.py deleted file mode 100644 index f3aa73ad5..000000000 --- a/examples/opengl/hellogl.py +++ /dev/null @@ -1,287 +0,0 @@ - -############################################################################ -## -## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: http://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################ - -"""PySide2 port of the opengl/legacy/hellogl example from Qt v5.x""" - -import sys -import math -from PySide2 import QtCore, QtGui, QtWidgets, QtOpenGL - -try: - from OpenGL import GL -except ImportError: - app = QtWidgets.QApplication(sys.argv) - messageBox = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Critical, "OpenGL hellogl", - "PyOpenGL must be installed to run this example.", - QtWidgets.QMessageBox.Close) - messageBox.setDetailedText("Run:\npip install PyOpenGL PyOpenGL_accelerate") - messageBox.exec_() - sys.exit(1) - - -class Window(QtWidgets.QWidget): - def __init__(self, parent=None): - QtWidgets.QWidget.__init__(self, parent) - - self.glWidget = GLWidget() - - self.xSlider = self.createSlider(QtCore.SIGNAL("xRotationChanged(int)"), - self.glWidget.setXRotation) - self.ySlider = self.createSlider(QtCore.SIGNAL("yRotationChanged(int)"), - self.glWidget.setYRotation) - self.zSlider = self.createSlider(QtCore.SIGNAL("zRotationChanged(int)"), - self.glWidget.setZRotation) - - mainLayout = QtWidgets.QHBoxLayout() - mainLayout.addWidget(self.glWidget) - mainLayout.addWidget(self.xSlider) - mainLayout.addWidget(self.ySlider) - mainLayout.addWidget(self.zSlider) - self.setLayout(mainLayout) - - self.xSlider.setValue(170 * 16) - self.ySlider.setValue(160 * 16) - self.zSlider.setValue(90 * 16) - - self.setWindowTitle(self.tr("Hello GL")) - - def createSlider(self, changedSignal, setterSlot): - slider = QtWidgets.QSlider(QtCore.Qt.Vertical) - - slider.setRange(0, 360 * 16) - slider.setSingleStep(16) - slider.setPageStep(15 * 16) - slider.setTickInterval(15 * 16) - slider.setTickPosition(QtWidgets.QSlider.TicksRight) - - self.glWidget.connect(slider, QtCore.SIGNAL("valueChanged(int)"), setterSlot) - self.connect(self.glWidget, changedSignal, slider, QtCore.SLOT("setValue(int)")) - - return slider - - -class GLWidget(QtOpenGL.QGLWidget): - xRotationChanged = QtCore.Signal(int) - yRotationChanged = QtCore.Signal(int) - zRotationChanged = QtCore.Signal(int) - - def __init__(self, parent=None): - QtOpenGL.QGLWidget.__init__(self, parent) - - self.object = 0 - self.xRot = 0 - self.yRot = 0 - self.zRot = 0 - - self.lastPos = QtCore.QPoint() - - self.trolltechGreen = QtGui.QColor.fromCmykF(0.40, 0.0, 1.0, 0.0) - self.trolltechPurple = QtGui.QColor.fromCmykF(0.39, 0.39, 0.0, 0.0) - - def xRotation(self): - return self.xRot - - def yRotation(self): - return self.yRot - - def zRotation(self): - return self.zRot - - def minimumSizeHint(self): - return QtCore.QSize(50, 50) - - def sizeHint(self): - return QtCore.QSize(400, 400) - - def setXRotation(self, angle): - angle = self.normalizeAngle(angle) - if angle != self.xRot: - self.xRot = angle - self.emit(QtCore.SIGNAL("xRotationChanged(int)"), angle) - self.updateGL() - - def setYRotation(self, angle): - angle = self.normalizeAngle(angle) - if angle != self.yRot: - self.yRot = angle - self.emit(QtCore.SIGNAL("yRotationChanged(int)"), angle) - self.updateGL() - - def setZRotation(self, angle): - angle = self.normalizeAngle(angle) - if angle != self.zRot: - self.zRot = angle - self.emit(QtCore.SIGNAL("zRotationChanged(int)"), angle) - self.updateGL() - - def initializeGL(self): - self.qglClearColor(self.trolltechPurple.darker()) - self.object = self.makeObject() - GL.glShadeModel(GL.GL_FLAT) - GL.glEnable(GL.GL_DEPTH_TEST) - GL.glEnable(GL.GL_CULL_FACE) - - def paintGL(self): - GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) - GL.glLoadIdentity() - GL.glTranslated(0.0, 0.0, -10.0) - GL.glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0) - GL.glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0) - GL.glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0) - GL.glCallList(self.object) - - def resizeGL(self, width, height): - side = min(width, height) - GL.glViewport(int((width - side) / 2),int((height - side) / 2), side, side) - - GL.glMatrixMode(GL.GL_PROJECTION) - GL.glLoadIdentity() - GL.glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0) - GL.glMatrixMode(GL.GL_MODELVIEW) - - def mousePressEvent(self, event): - self.lastPos = QtCore.QPoint(event.pos()) - - def mouseMoveEvent(self, event): - dx = event.x() - self.lastPos.x() - dy = event.y() - self.lastPos.y() - - if event.buttons() & QtCore.Qt.LeftButton: - self.setXRotation(self.xRot + 8 * dy) - self.setYRotation(self.yRot + 8 * dx) - elif event.buttons() & QtCore.Qt.RightButton: - self.setXRotation(self.xRot + 8 * dy) - self.setZRotation(self.zRot + 8 * dx) - - self.lastPos = QtCore.QPoint(event.pos()) - - def makeObject(self): - genList = GL.glGenLists(1) - GL.glNewList(genList, GL.GL_COMPILE) - - GL.glBegin(GL.GL_QUADS) - - x1 = +0.06 - y1 = -0.14 - x2 = +0.14 - y2 = -0.06 - x3 = +0.08 - y3 = +0.00 - x4 = +0.30 - y4 = +0.22 - - self.quad(x1, y1, x2, y2, y2, x2, y1, x1) - self.quad(x3, y3, x4, y4, y4, x4, y3, x3) - - self.extrude(x1, y1, x2, y2) - self.extrude(x2, y2, y2, x2) - self.extrude(y2, x2, y1, x1) - self.extrude(y1, x1, x1, y1) - self.extrude(x3, y3, x4, y4) - self.extrude(x4, y4, y4, x4) - self.extrude(y4, x4, y3, x3) - - Pi = 3.14159265358979323846 - NumSectors = 200 - - for i in range(NumSectors): - angle1 = (i * 2 * Pi) / NumSectors - x5 = 0.30 * math.sin(angle1) - y5 = 0.30 * math.cos(angle1) - x6 = 0.20 * math.sin(angle1) - y6 = 0.20 * math.cos(angle1) - - angle2 = ((i + 1) * 2 * Pi) / NumSectors - x7 = 0.20 * math.sin(angle2) - y7 = 0.20 * math.cos(angle2) - x8 = 0.30 * math.sin(angle2) - y8 = 0.30 * math.cos(angle2) - - self.quad(x5, y5, x6, y6, x7, y7, x8, y8) - - self.extrude(x6, y6, x7, y7) - self.extrude(x8, y8, x5, y5) - - GL.glEnd() - GL.glEndList() - - return genList - - def quad(self, x1, y1, x2, y2, x3, y3, x4, y4): - self.qglColor(self.trolltechGreen) - - GL.glVertex3d(x1, y1, +0.05) - GL.glVertex3d(x2, y2, +0.05) - GL.glVertex3d(x3, y3, +0.05) - GL.glVertex3d(x4, y4, +0.05) - - GL.glVertex3d(x4, y4, -0.05) - GL.glVertex3d(x3, y3, -0.05) - GL.glVertex3d(x2, y2, -0.05) - GL.glVertex3d(x1, y1, -0.05) - - def extrude(self, x1, y1, x2, y2): - self.qglColor(self.trolltechGreen.darker(250 + int(100 * x1))) - - GL.glVertex3d(x1, y1, -0.05) - GL.glVertex3d(x2, y2, -0.05) - GL.glVertex3d(x2, y2, +0.05) - GL.glVertex3d(x1, y1, +0.05) - - def normalizeAngle(self, angle): - while angle < 0: - angle += 360 * 16 - while angle > 360 * 16: - angle -= 360 * 16 - return angle - - def freeResources(self): - self.makeCurrent() - GL.glDeleteLists(self.object, 1) - -if __name__ == '__main__': - app = QtWidgets.QApplication(sys.argv) - window = Window() - window.show() - res = app.exec_() - window.glWidget.freeResources() - sys.exit(res) diff --git a/examples/opengl/hellogl2.py b/examples/opengl/hellogl2.py index ad3562f0b..b12a7b275 100644 --- a/examples/opengl/hellogl2.py +++ b/examples/opengl/hellogl2.py @@ -47,10 +47,14 @@ import math import numpy import ctypes from PySide2.QtCore import QCoreApplication, Signal, SIGNAL, SLOT, Qt, QSize, QPoint -from PySide2.QtGui import (QVector3D, QOpenGLFunctions, QOpenGLVertexArrayObject, QOpenGLBuffer, - QOpenGLShaderProgram, QMatrix4x4, QOpenGLShader, QOpenGLContext, QSurfaceFormat) -from PySide2.QtWidgets import (QApplication, QWidget, QMessageBox, QHBoxLayout, QSlider, - QOpenGLWidget) +from PySide2.QtGui import (QVector3D, QOpenGLFunctions, + QMatrix4x4, QOpenGLContext, QSurfaceFormat) +from PySide2.QtOpenGL import (QOpenGLVertexArrayObject, QOpenGLBuffer, + QOpenGLShaderProgram, QOpenGLShader) +from PySide2.QtWidgets import (QApplication, QWidget, QMessageBox, QHBoxLayout, + QSlider) +from PySide2.QtOpenGLWidgets import QOpenGLWidget + from shiboken2 import VoidPtr try: diff --git a/examples/opengl/opengl.pyproject b/examples/opengl/opengl.pyproject index 12f435daf..20b289d7b 100644 --- a/examples/opengl/opengl.pyproject +++ b/examples/opengl/opengl.pyproject @@ -1,5 +1,3 @@ { - "files": ["grabber.py", "samplebuffers.py", "hellogl.py", - "hellogl2.py", "contextinfo.py", "2dpainting.py", - "overpainting.py"] + "files": ["hellogl2.py", "contextinfo.py"] } diff --git a/examples/opengl/overpainting.py b/examples/opengl/overpainting.py deleted file mode 100644 index 786337f88..000000000 --- a/examples/opengl/overpainting.py +++ /dev/null @@ -1,385 +0,0 @@ - -############################################################################ -## -## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: http://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################ - -"""PySide2 port of the opengl/legacy/overpainting example from Qt v5.x""" - -import sys -import math, random -from PySide2.QtCore import * -from PySide2.QtGui import * -from PySide2.QtWidgets import * -from PySide2.QtOpenGL import * - -try: - from OpenGL.GL import * -except ImportError: - app = QApplication(sys.argv) - messageBox = QMessageBox(QMessageBox.Critical, "OpenGL overpainting", - "PyOpenGL must be installed to run this example.", - QMessageBox.Close) - messageBox.setDetailedText("Run:\npip install PyOpenGL PyOpenGL_accelerate") - messageBox.exec_() - sys.exit(1) - - -class Bubble: - def __init__(self, position, radius, velocity): - self.position = position - self.vel = velocity - self.radius = radius - self.innerColor = self.randomColor() - self.outerColor = self.randomColor() - self.updateBrush() - - def updateBrush(self): - gradient = QRadialGradient(QPointF(self.radius, self.radius), self.radius, - QPointF(self.radius*0.5, self.radius*0.5)) - - gradient.setColorAt(0, QColor(255, 255, 255, 255)) - gradient.setColorAt(0.25, self.innerColor) - gradient.setColorAt(1, self.outerColor) - self.brush = QBrush(gradient) - - def drawBubble(self, painter): - painter.save() - painter.translate(self.position.x() - self.radius, - self.position.y() - self.radius) - painter.setBrush(self.brush) - painter.drawEllipse(0, 0, int(2*self.radius), int(2*self.radius)) - painter.restore() - - def randomColor(self): - red = random.randrange(205, 256) - green = random.randrange(205, 256) - blue = random.randrange(205, 256) - alpha = random.randrange(91, 192) - - return QColor(red, green, blue, alpha) - - def move(self, bbox): - self.position += self.vel - leftOverflow = self.position.x() - self.radius - bbox.left() - rightOverflow = self.position.x() + self.radius - bbox.right() - topOverflow = self.position.y() - self.radius - bbox.top() - bottomOverflow = self.position.y() + self.radius - bbox.bottom() - - if leftOverflow < 0.0: - self.position.setX(self.position.x() - 2 * leftOverflow) - self.vel.setX(-self.vel.x()) - elif rightOverflow > 0.0: - self.position.setX(self.position.x() - 2 * rightOverflow) - self.vel.setX(-self.vel.x()) - - if topOverflow < 0.0: - self.position.setY(self.position.y() - 2 * topOverflow) - self.vel.setY(-self.vel.y()) - elif bottomOverflow > 0.0: - self.position.setY(self.position.y() - 2 * bottomOverflow) - self.vel.setY(-self.vel.y()) - - def rect(self): - return QRectF(self.position.x() - self.radius, - self.position.y() - self.radius, - 2 * self.radius, 2 * self.radius) - - -class GLWidget(QGLWidget): - def __init__(self, parent = None): - QGLWidget.__init__(self, QGLFormat(QGL.SampleBuffers), parent) - - midnight = QTime(0, 0, 0) - random.seed(midnight.secsTo(QTime.currentTime())) - - self.object = 0 - self.xRot = 0 - self.yRot = 0 - self.zRot = 0 - self.image = QImage() - self.bubbles = [] - self.lastPos = QPoint() - - self.trolltechGreen = QColor.fromCmykF(0.40, 0.0, 1.0, 0.0) - self.trolltechPurple = QColor.fromCmykF(0.39, 0.39, 0.0, 0.0) - - self.animationTimer = QTimer() - self.animationTimer.setSingleShot(False) - self.connect(self.animationTimer, SIGNAL("timeout()"), self.animate) - self.animationTimer.start(25) - - self.setAttribute(Qt.WA_NoSystemBackground) - self.setMinimumSize(200, 200) - self.setWindowTitle(self.tr("Overpainting a Scene")) - - def freeResources(self): - self.makeCurrent() - glDeleteLists(self.object, 1) - - def setXRotation(self, angle): - angle = self.normalizeAngle(angle) - if angle != self.xRot: - self.xRot = angle - self.emit(SIGNAL("xRotationChanged(int)"), angle) - - def setYRotation(self, angle): - angle = self.normalizeAngle(angle) - if angle != self.yRot: - self.yRot = angle - self.emit(SIGNAL("yRotationChanged(int)"), angle) - - def setZRotation(self, angle): - angle = self.normalizeAngle(angle) - if angle != self.zRot: - self.zRot = angle - self.emit(SIGNAL("zRotationChanged(int)"), angle) - - def initializeGL(self): - self.object = self.makeObject() - - def mousePressEvent(self, event): - self.lastPos = QPoint(event.pos()) - - def mouseMoveEvent(self, event): - dx = event.x() - self.lastPos.x() - dy = event.y() - self.lastPos.y() - - if event.buttons() & Qt.LeftButton: - self.setXRotation(self.xRot + 8 * dy) - self.setYRotation(self.yRot + 8 * dx) - elif event.buttons() & Qt.RightButton: - self.setXRotation(self.xRot + 8 * dy) - self.setZRotation(self.zRot + 8 * dx) - - self.lastPos = QPoint(event.pos()) - - def paintEvent(self, event): - painter = QPainter() - painter.begin(self) - painter.setRenderHint(QPainter.Antialiasing) - - glPushAttrib(GL_ALL_ATTRIB_BITS) - glMatrixMode(GL_PROJECTION) - glPushMatrix() - glMatrixMode(GL_MODELVIEW) - glPushMatrix() - - self.qglClearColor(self.trolltechPurple.darker()) - glShadeModel(GL_SMOOTH) - glEnable(GL_DEPTH_TEST) - glEnable(GL_CULL_FACE) - glEnable(GL_LIGHTING) - glEnable(GL_LIGHT0) - lightPosition = ( 0.5, 5.0, 7.0, 1.0 ) - glLightfv(GL_LIGHT0, GL_POSITION, lightPosition) - - self.resizeGL(self.width(), self.height()) - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) - glLoadIdentity() - glTranslated(0.0, 0.0, -10.0) - glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0) - glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0) - glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0) - glCallList(self.object) - - glPopAttrib() - glMatrixMode(GL_MODELVIEW) - glPopMatrix() - glMatrixMode(GL_PROJECTION) - glPopMatrix() - - glDisable(GL_CULL_FACE) ### not required if begin() also does it - - for bubble in self.bubbles: - if bubble.rect().intersects(QRectF(event.rect())): - bubble.drawBubble(painter) - - painter.drawImage((self.width() - self.image.width())/2, 0, self.image) - painter.end() - - def resizeGL(self, width, height): - side = min(width, height) - glViewport(int((width - side) / 2), int((height - side) / 2), side, side) - - glMatrixMode(GL_PROJECTION) - glLoadIdentity() - glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0) - glMatrixMode(GL_MODELVIEW) - - self.formatInstructions(width, height) - - def showEvent(self, event): - self.createBubbles(20 - len(self.bubbles)) - - def sizeHint(self): - return QSize(400, 400) - - def makeObject(self): - list = glGenLists(1) - glNewList(list, GL_COMPILE) - - glEnable(GL_NORMALIZE) - glBegin(GL_QUADS) - - logoDiffuseColor = (self.trolltechGreen.red()/255.0, - self.trolltechGreen.green()/255.0, - self.trolltechGreen.blue()/255.0, 1.0) - glMaterialfv(GL_FRONT, GL_DIFFUSE, logoDiffuseColor) - - x1 = +0.06 - y1 = -0.14 - x2 = +0.14 - y2 = -0.06 - x3 = +0.08 - y3 = +0.00 - x4 = +0.30 - y4 = +0.22 - - self.quad(x1, y1, x2, y2, y2, x2, y1, x1) - self.quad(x3, y3, x4, y4, y4, x4, y3, x3) - - self.extrude(x1, y1, x2, y2) - self.extrude(x2, y2, y2, x2) - self.extrude(y2, x2, y1, x1) - self.extrude(y1, x1, x1, y1) - self.extrude(x3, y3, x4, y4) - self.extrude(x4, y4, y4, x4) - self.extrude(y4, x4, y3, x3) - - NumSectors = 200 - - for i in range(NumSectors): - angle1 = (i * 2 * math.pi) / NumSectors - x5 = 0.30 * math.sin(angle1) - y5 = 0.30 * math.cos(angle1) - x6 = 0.20 * math.sin(angle1) - y6 = 0.20 * math.cos(angle1) - - angle2 = ((i + 1) * 2 * math.pi) / NumSectors - x7 = 0.20 * math.sin(angle2) - y7 = 0.20 * math.cos(angle2) - x8 = 0.30 * math.sin(angle2) - y8 = 0.30 * math.cos(angle2) - - self.quad(x5, y5, x6, y6, x7, y7, x8, y8) - - self.extrude(x6, y6, x7, y7) - self.extrude(x8, y8, x5, y5) - - glEnd() - - glEndList() - return list - - def quad(self, x1, y1, x2, y2, x3, y3, x4, y4): - glNormal3d(0.0, 0.0, -1.0) - glVertex3d(x1, y1, -0.05) - glVertex3d(x2, y2, -0.05) - glVertex3d(x3, y3, -0.05) - glVertex3d(x4, y4, -0.05) - - glNormal3d(0.0, 0.0, 1.0) - glVertex3d(x4, y4, +0.05) - glVertex3d(x3, y3, +0.05) - glVertex3d(x2, y2, +0.05) - glVertex3d(x1, y1, +0.05) - - def extrude(self, x1, y1, x2, y2): - self.qglColor(self.trolltechGreen.darker(250 + int(100 * x1))) - - glNormal3d((x1 + x2)/2.0, (y1 + y2)/2.0, 0.0) - glVertex3d(x1, y1, +0.05) - glVertex3d(x2, y2, +0.05) - glVertex3d(x2, y2, -0.05) - glVertex3d(x1, y1, -0.05) - - def normalizeAngle(self, angle): - while angle < 0: - angle += 360 * 16 - while angle > 360 * 16: - angle -= 360 * 16 - return angle - - def createBubbles(self, number): - for i in range(number): - position = QPointF(self.width()*(0.1 + 0.8*random.random()), - self.height()*(0.1 + 0.8*random.random())) - radius = min(self.width(), self.height())*(0.0125 + 0.0875*random.random()) - velocity = QPointF(self.width()*0.0125*(-0.5 + random.random()), - self.height()*0.0125*(-0.5 + random.random())) - - self.bubbles.append(Bubble(position, radius, velocity)) - - def animate(self): - for bubble in self.bubbles: - self.update(bubble.rect().toRect()) - bubble.move(self.rect()) - self.update(bubble.rect().toRect()) - - def formatInstructions(self, width, height): - text = self.tr("Click and drag with the left mouse button " - "to rotate the Qt logo.") - metrics = QFontMetrics(self.font()) - border = max(4, metrics.leading()) - - rect = metrics.boundingRect(0, 0, width - 2*border, int(height*0.125), - Qt.AlignCenter | Qt.TextWordWrap, text) - self.image = QImage(width, rect.height() + 2*border, - QImage.Format_ARGB32_Premultiplied) - self.image.fill(qRgba(0, 0, 0, 127)) - - painter = QPainter() - painter.begin(self.image) - painter.setRenderHint(QPainter.TextAntialiasing) - painter.setPen(Qt.white) - painter.drawText((width - rect.width())/2, border, - rect.width(), rect.height(), - Qt.AlignCenter | Qt.TextWordWrap, text) - painter.end() - - -if __name__ == '__main__': - app = QApplication(sys.argv) - window = GLWidget() - window.show() - res = app.exec_() - window.freeResources() - sys.exit(res) diff --git a/examples/opengl/samplebuffers.py b/examples/opengl/samplebuffers.py deleted file mode 100644 index ad5b79466..000000000 --- a/examples/opengl/samplebuffers.py +++ /dev/null @@ -1,192 +0,0 @@ - -############################################################################ -## -## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: http://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################ - -"""PySide2 port of the opengl/legacy/samplebuffers example from Qt v5.x""" - -import sys -import math -from PySide2 import QtCore, QtGui, QtWidgets, QtOpenGL - -try: - from OpenGL import GL -except ImportError: - app = QtWidgets.QApplication(sys.argv) - messageBox = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Critical, "OpenGL samplebuffers", - "PyOpenGL must be installed to run this example.", - QtWidgets.QMessageBox.Close) - messageBox.setDetailedText("Run:\npip install PyOpenGL PyOpenGL_accelerate") - messageBox.exec_() - sys.exit(1) - - -class GLWidget(QtOpenGL.QGLWidget): - GL_MULTISAMPLE = 0x809D - rot = 0.0 - - def __init__(self, parent=None): - QtOpenGL.QGLWidget.__init__(self, QtOpenGL.QGLFormat(QtOpenGL.QGL.SampleBuffers), parent) - - self.list_ = [] - - self.startTimer(40) - self.setWindowTitle(self.tr("Sample Buffers")) - - def initializeGL(self): - GL.glMatrixMode(GL.GL_PROJECTION) - GL.glLoadIdentity() - GL.glOrtho( -.5, .5, .5, -.5, -1000, 1000) - GL.glMatrixMode(GL.GL_MODELVIEW) - GL.glLoadIdentity() - GL.glClearColor(1.0, 1.0, 1.0, 1.0) - - self.makeObject() - - def resizeGL(self, w, h): - GL.glViewport(0, 0, w, h) - - def paintGL(self): - GL.glClear(GL.GL_COLOR_BUFFER_BIT) - - GL.glMatrixMode(GL.GL_MODELVIEW) - GL.glPushMatrix() - GL.glEnable(GLWidget.GL_MULTISAMPLE) - GL.glTranslatef( -0.25, -0.10, 0.0) - GL.glScalef(0.75, 1.15, 0.0) - GL.glRotatef(GLWidget.rot, 0.0, 0.0, 1.0) - GL.glCallList(self.list_) - GL.glPopMatrix() - - GL.glPushMatrix() - GL.glDisable(GLWidget.GL_MULTISAMPLE) - GL.glTranslatef(0.25, -0.10, 0.0) - GL.glScalef(0.75, 1.15, 0.0) - GL.glRotatef(GLWidget.rot, 0.0, 0.0, 1.0) - GL.glCallList(self.list_) - GL.glPopMatrix() - - GLWidget.rot += 0.2 - - self.qglColor(QtCore.Qt.black) - self.renderText(-0.35, 0.4, 0.0, "Multisampling enabled") - self.renderText(0.15, 0.4, 0.0, "Multisampling disabled") - - def timerEvent(self, event): - self.update() - - def makeObject(self): - trolltechGreen = QtGui.QColor.fromCmykF(0.40, 0.0, 1.0, 0.0) - Pi = 3.14159265358979323846 - NumSectors = 15 - x1 = +0.06 - y1 = -0.14 - x2 = +0.14 - y2 = -0.06 - x3 = +0.08 - y3 = +0.00 - x4 = +0.30 - y4 = +0.22 - - self.list_ = GL.glGenLists(1) - GL.glNewList(self.list_, GL.GL_COMPILE) - - for i in range(NumSectors): - angle1 = float((i * 2 * Pi) / NumSectors) - x5 = 0.30 * math.sin(angle1) - y5 = 0.30 * math.cos(angle1) - x6 = 0.20 * math.sin(angle1) - y6 = 0.20 * math.cos(angle1) - - angle2 = float(((i + 1) * 2 * Pi) / NumSectors) - x7 = 0.20 * math.sin(angle2) - y7 = 0.20 * math.cos(angle2) - x8 = 0.30 * math.sin(angle2) - y8 = 0.30 * math.cos(angle2) - - self.qglColor(trolltechGreen) - self.quad(GL.GL_QUADS, x5, y5, x6, y6, x7, y7, x8, y8) - self.qglColor(QtCore.Qt.black) - self.quad(GL.GL_LINE_LOOP, x5, y5, x6, y6, x7, y7, x8, y8) - - self.qglColor(trolltechGreen) - self.quad(GL.GL_QUADS, x1, y1, x2, y2, y2, x2, y1, x1) - self.quad(GL.GL_QUADS, x3, y3, x4, y4, y4, x4, y3, x3) - - self.qglColor(QtCore.Qt.black) - self.quad(GL.GL_LINE_LOOP, x1, y1, x2, y2, y2, x2, y1, x1) - self.quad(GL.GL_LINE_LOOP, x3, y3, x4, y4, y4, x4, y3, x3) - - GL.glEndList() - - def quad(self, primitive, x1, y1, x2, y2, x3, y3, x4, y4): - GL.glBegin(primitive) - - GL.glVertex2d(x1, y1) - GL.glVertex2d(x2, y2) - GL.glVertex2d(x3, y3) - GL.glVertex2d(x4, y4) - - GL.glEnd() - - def freeResources(self): - self.makeCurrent() - GL.glDeleteLists(self.list_, 1) - - -if __name__ == '__main__': - app = QtWidgets.QApplication(sys.argv) - - if not QtOpenGL.QGLFormat.hasOpenGL(): - QMessageBox.information(0, "OpenGL pbuffers", - "This system does not support OpenGL.", - QMessageBox.Ok) - sys.exit(1) - - f = QtOpenGL.QGLFormat.defaultFormat() - f.setSampleBuffers(True) - QtOpenGL.QGLFormat.setDefaultFormat(f) - - widget = GLWidget() - widget.resize(640, 480) - widget.show() - res = app.exec_() - widget.freeResources() - sys.exit(res) |