Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5 import QtCore
- from PyQt5 import QtGui
- from PyQt5 import QtOpenGL
- from PyQt5.QtWidgets import *
- import OpenGL.GL as gl
- from OpenGL import GLU
- import sys
- from OpenGL.arrays import vbo
- import numpy as np
- class GLWidget(QtOpenGL.QGLWidget):
- def __init__(self, parent=None):
- self.parent = parent
- QtOpenGL.QGLWidget.__init__(self, parent)
- def initializeGL(self):
- self.qglClearColor(QtGui.QColor(0, 0, 255))
- gl.glEnable(gl.GL_DEPTH_TEST)
- self.initGeometry()
- self.rotX = 0.0
- self.rotY = 0.0
- self.rotZ = 0.0
- def resizeGL(self, width, height):
- gl.glViewport(0, 0, width, height)
- gl.glMatrixMode(gl.GL_PROJECTION)
- gl.glLoadIdentity()
- aspect = width / float(height)
- GLU.gluPerspective(45.0, aspect, 1.0, 100.0)
- gl.glMatrixMode(gl.GL_MODELVIEW)
- def paintGL(self):
- gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
- gl.glPushMatrix()
- gl.glTranslate(0.0, 0.0, -50.0)
- gl.glScale(20.0, 20.0, 20.0)
- gl.glRotate(self.rotX, 1.0, 0.0, 0.0)
- gl.glRotate(self.rotY, 0.0, 1.0, 0.0)
- gl.glRotate(self.rotZ, 0.0, 0.0, 1.0)
- gl.glTranslate(-0.5, -0.5, -0.5)
- gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
- gl.glEnableClientState(gl.GL_COLOR_ARRAY)
- gl.glVertexPointer(3, gl.GL_FLOAT, 0, self.vertVBO)
- gl.glColorPointer(3, gl.GL_FLOAT, 0, self.colorVBO)
- gl.glDrawElements(gl.GL_QUADS, len(self.cubeIdxArray), gl.GL_UNSIGNED_INT, self.cubeIdxArray)
- gl.glDisableClientState(gl.GL_VERTEX_ARRAY)
- gl.glDisableClientState(gl.GL_COLOR_ARRAY)
- gl.glPopMatrix()
- def initGeometry(self):
- self.cubeVtxArray = np.array(
- [[0.0, 0.0, 0.0],
- [1.0, 0.0, 0.0],
- [1.0, 1.0, 0.0],
- [0.0, 1.0, 0.0],
- [0.0, 0.0, 1.0],
- [1.0, 0.0, 1.0],
- [1.0, 1.0, 1.0],
- [0.0, 1.0, 1.0]])
- self.vertVBO = vbo.VBO(np.reshape(self.cubeVtxArray,
- (1, -1)).astype(np.float32))
- self.vertVBO.bind()
- self.cubeClrArray = np.array(
- [[0.0, 0.0, 0.0],
- [1.0, 0.0, 0.0],
- [1.0, 1.0, 0.0],
- [0.0, 1.0, 0.0],
- [0.0, 0.0, 1.0],
- [1.0, 0.0, 1.0],
- [1.0, 1.0, 1.0],
- [0.0, 1.0, 1.0]])
- self.colorVBO = vbo.VBO(np.reshape(self.cubeClrArray,
- (1, -1)).astype(np.float32))
- self.colorVBO.bind()
- self.cubeIdxArray = np.array(
- [0, 1, 2, 3,
- 3, 2, 6, 7,
- 1, 0, 4, 5,
- 2, 1, 5, 6,
- 0, 3, 7, 4,
- 7, 6, 5, 4])
- def setRotX(self, val):
- self.rotX = np.pi * val
- def setRotY(self, val):
- self.rotY = np.pi * val
- def setRotZ(self, val):
- self.rotZ = np.pi * val
- class MainWindow(QMainWindow):
- def __init__(self):
- QMainWindow.__init__(self) # call the init for the parent class
- self.resize(300, 300)
- self.setWindowTitle('Hello OpenGL App')
- self.glWidget = GLWidget(self)
- self.initGUI()
- timer = QtCore.QTimer(self)
- timer.setInterval(2) # period, in milliseconds
- timer.timeout.connect(self.glWidget.updateGL)
- timer.start()
- def initGUI(self):
- central_widget = QWidget()
- gui_layout = QVBoxLayout()
- central_widget.setLayout(gui_layout)
- self.setCentralWidget(central_widget)
- gui_layout.addWidget(self.glWidget)
- sliderX = QSlider(QtCore.Qt.Horizontal)
- sliderX.valueChanged.connect(lambda val: self.glWidget.setRotX(val))
- sliderY = QSlider(QtCore.Qt.Horizontal)
- sliderY.valueChanged.connect(lambda val: self.glWidget.setRotY(val))
- sliderZ = QSlider(QtCore.Qt.Horizontal)
- sliderZ.valueChanged.connect(lambda val: self.glWidget.setRotZ(val))
- gui_layout.addWidget(sliderX)
- gui_layout.addWidget(sliderY)
- gui_layout.addWidget(sliderZ)
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- win = MainWindow()
- win.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement