目录
opengl安装:
pip install -U pyopengl
有的报错:
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutIn
问题:使用 pip 安装了 PyOpenGL 包,然后运行程序,结果提示:OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit
原因:使用 pip 安装的 OpenGL 包是 32 位,与 64 位电脑不匹配,故出现此错误。
解决办法:pip 不能在线安装 64 位的 OpenGL,只能手动下载后安装。
下载地址:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl
opengl pyqt渲染缩放
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *
class OpenGLWidget(QGLWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setMinimumSize(200, 200)
self.scale_factor = 1.0
def initializeGL(self):
glClearColor(0.0, 0.0, 0.0, 1.0)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glScalef(self.scale_factor, self.scale_factor, 1.0)
# 绘制一个简单的三角形
glBegin(GL_TRIANGLES)
glColor3f(1.0, 0.0, 0.0)
glVertex2f(0.0, 1.0)
glColor3f(0.0, 1.0, 0.0)
glVertex2f(-1.0, -1.0)
glColor3f(0.0, 0.0, 1.0)
glVertex2f(1.0, -1.0)
glEnd()
def resizeGL(self, w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0)
glMatrixMode(GL_MODELVIEW)
def wheelEvent(self, event):
# 获取滚轮滚动的距离
num_degrees = event.angleDelta().y() / 8
num_steps = num_degrees / 15.0 # 每次缩放15度
self.scale_factor *= pow(1.1, num_steps) # 乘以一个缩放因子
self.update()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("OpenGL Widget Example")
self.setGeometry(100, 100, 400, 400)
self.gl_widget = OpenGLWidget(self)
self.setCentralWidget(self.gl_widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
opengl 和控件分屏
import sys
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QSplitter, QLabel
from PyQt5.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *
class OpenGLWidget(QGLWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setMinimumSize(200, 200)
self.scale_factor = 1.0
def initializeGL(self):
glClearColor(0.0, 0.0, 0.0, 1.0)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glScalef(self.scale_factor, self.scale_factor, 1.0)
# 绘制一个简单的三角形
glBegin(GL_TRIANGLES)
glColor3f(1.0, 0.0, 0.0)
glVertex2f(0.0, 1.0)
glColor3f(0.0, 1.0, 0.0)
glVertex2f(-1.0, -1.0)
glColor3f(0.0, 0.0, 1.0)
glVertex2f(1.0, -1.0)
glEnd()
def resizeGL(self, w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0)
glMatrixMode(GL_MODELVIEW)
def wheelEvent(self, event):
# 获取滚轮滚动的距离
num_degrees = event.angleDelta().y() / 8
num_steps = num_degrees / 15.0 # 每次缩放15度
self.scale_factor *= pow(1.1, num_steps) # 乘以一个缩放因子
self.update()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("OpenGL Widget Example")
self.setGeometry(100, 100, 400, 400)
self.gl_widget = OpenGLWidget(self)
splitter = QSplitter()
self.setCentralWidget(splitter)
splitter.addWidget(self.gl_widget)
self.image_label = QLabel()
pixmap = QPixmap(r"C:\Users\Administrator\Pictures\mm\pics\005953_4.jpg") # 替换为你的图片路径
self.image_label.setPixmap(pixmap)
splitter.addWidget(self.image_label)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
opengl旋转
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QMouseEvent
from PyQt5.QtWidgets import QApplication, QMainWindow, QSplitter, QLabel
from PyQt5.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *
class OpenGLWidget(QGLWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setMinimumSize(200, 200)
self.scale_factor = 1.0
self.x_rotation = 0.0
self.y_rotation = 0.0
self.z_rotation = 0.0
self.last_pos = None
def initializeGL(self):
glClearColor(0.0, 0.0, 0.0, 1.0)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT)
glLoadIdentity()
glScalef(self.scale_factor, self.scale_factor, 1.0)
# glTranslatef(0.0, 0.0, -5.0)
glRotatef(self.x_rotation, 1.0, 0.0, 0.0)
glRotatef(self.y_rotation, 0.0, 1.0, 0.0)
glRotatef(self.z_rotation, 0.0, 0.0, 1.0)
glBegin(GL_TRIANGLES)
glColor3f(1.0, 0.0, 0.0)
glVertex2f(0.0, 1.0)
glColor3f(0.0, 1.0, 0.0)
glVertex2f(-1.0, -1.0)
glColor3f(0.0, 0.0, 1.0)
glVertex2f(1.0, -1.0)
glEnd()
def resizeGL(self, w, h):
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0)
glMatrixMode(GL_MODELVIEW)
def wheelEvent(self, event):
num_degrees = event.angleDelta().y() / 8
num_steps = num_degrees / 15.0 # 每次缩放15度
self.scale_factor *= pow(1.1, num_steps) # 乘以一个缩放因子
self.update()
def mousePressEvent(self, event: QMouseEvent):
self.last_pos = event.pos()
def mouseMoveEvent(self, event: QMouseEvent):
if self.last_pos is None:
return
dx = event.x() - self.last_pos.x()
dy = event.y() - self.last_pos.y()
if event.buttons() & Qt.LeftButton:
self.x_rotation += dy * 0.5
self.y_rotation += dx * 0.5
elif event.buttons() & Qt.RightButton:
self.x_rotation += dy * 0.5
self.z_rotation += dx * 0.5
self.last_pos = event.pos()
self.update()
def mouseReleaseEvent(self, event: QMouseEvent):
self.last_pos = None
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("OpenGL Widget Example")
self.setGeometry(100, 100, 800, 400)
self.gl_widget = OpenGLWidget(self)
splitter = QSplitter()
self.setCentralWidget(splitter)
splitter.addWidget(self.gl_widget)
self.image_label = QLabel()
self.image_label.setFixedSize(400, 400)
pixmap = QPixmap(r"C:\Users\Administrator\Pictures\mm\pics\005953_4.jpg") # 替换为你的图片路径
self.image_label.setPixmap(pixmap)
splitter.addWidget(self.image_label)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())