上一篇PyQt5 通过 OpenGLWidget 控件显示 3D 网格-CSDN博客
滑条使用参考PyQt5基本控件详解之QSlider(十一)_pyqt5的slider-CSDN博客
1.界面设计
2.撰写代码
我们设置的滑条的范围是0–89,要实现的目的是绕各轴 正反 旋转90度,但是因为函数的用法为 glRotatef(30.0, 0.0f, 0.0f, -1.0f);//glRotate前缀不变,后缀f代表参数是浮点型,第一个参数是旋转角度,后三个参数代表按照向量n=(0,0,-1)为轴进行旋转,
故设置了两个变量,一个self.diffNum(用来存储两次变换之间的差值), 一个self.lastPos(用来存放上一次的实际位置),核心思想就是通过本次滑条真实值 减去 上次模型滑条真实位置,得到本次差值,通过self.openGLWidget.rotX(旋转量,每次都会重置为0) 加上(减去) 本次差值,得到新的旋转量, 这样便可以使模型到指定的位置
(加载部分可以在上一篇博客获取)
为Mesh.py和LoadMesh.py
self.mesh = LoadMesh("C:/Users/admin/Desktop/tt/tt_openGL/cube.obj", GL_LINE_STRIP)
from PyQt5 import QtWidgets,QtCore
from Ui_test import Ui_MainWindow
import sys # we'll need this later to run our Qt application
from widgetest import GLWidget
class MainWindow(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self,*args,obj=None,**kwargs):
super(MainWindow,self).__init__(*args,**kwargs)
# self.showMaximized()
self.app = QtWidgets.QApplication([])
self.setupUi(self)
self.lastPosX = 0.0
self.diffNumX = 0.0
self.lastPosY = 0.0
self.diffNumY = 0.0
self.lastPosZ = 0.0
self.diffNumZ = 0.0
self.showBtn.clicked.connect(self.SHOW)
self.SliderX.valueChanged.connect(self.GetValueSliderX)
self.SliderY.valueChanged.connect(self.GetValueSliderY)
self.SliderZ.valueChanged.connect(self.GetValueSliderZ)
# self.openGLWidget.initGeometry()
# self.clearBtn.clicked.connect(self.CLEAR)
def SHOW(self):
self.timer = QtCore.QTimer(self)
self.timer.setInterval(1) # period, in milliseconds
self.timer.timeout.connect(self.openGLWidget.update)
self.timer.start()
print("显示")
# def CLEAR(self):
# print("清除")
def GetValueSliderX(self):
self.diffNumX = self.SliderX.value() - self.lastPosX
if self.diffNumX > 0 and self.diffNumX < 90:
self.openGLWidget.rotX += self.diffNumX
print('sliderX value= {0}, self.diffNumX = {1}, self.lastPosX = {2}'.format(self.SliderX.value(), self.diffNumX, self.lastPosX))
self.lastPosX = self.SliderX.value()
self.openGLWidget.dir = True
elif self.diffNumX < 0 and self.diffNumX > -90:
self.openGLWidget.rotX -= self.diffNumX
print('sliderX value= {0}, self.diffNumX = {1}, self.lastPosX = {2}'.format(self.SliderX.value(), self.diffNumX, self.lastPosX))
self.lastPosX = self.SliderX.value()
self.openGLWidget.dir = False
def GetValueSliderY(self):
self.diffNumY = self.SliderY.value() - self.lastPosY
if self.diffNumY > 0 and self.diffNumY < 90:
self.openGLWidget.rotY += self.diffNumY
print('sliderY value= {0}, self.diffNumY = {1}, self.lastPosY = {2}'.format(self.SliderY.value(), self.diffNumY, self.lastPosY))
self.lastPosY = self.SliderY.value()
self.openGLWidget.dir = True
elif self.diffNumY < 0 and self.diffNumY > -90:
self.openGLWidget.rotY -= self.diffNumY
print('sliderY value= {0}, self.diffNumY = {1}, self.lastPosY = {2}'.format(self.SliderY.value(), self.diffNumY, self.lastPosY))
self.lastPosY = self.SliderY.value()
self.openGLWidget.dir = False
def GetValueSliderZ(self):
self.diffNumZ = self.SliderZ.value() - self.lastPosZ
if self.diffNumZ > 0 and self.diffNumZ < 90:
self.openGLWidget.rotZ += self.diffNumZ
print('sliderZ value= {0}, self.diffNumZ = {1}, self.lastPosZ = {2}'.format(self.SliderZ.value(), self.diffNumZ, self.lastPosZ))
self.lastPosZ = self.SliderZ.value()
self.openGLWidget.dir = True
elif self.diffNumZ < 0 and self.diffNumZ > -90:
self.openGLWidget.rotZ -= self.diffNumZ
print('sliderZ value= {0}, self.diffNumZ = {1}, self.lastPosZ = {2}'.format(self.SliderZ.value(), self.diffNumZ, self.lastPosZ))
self.lastPosZ = self.SliderZ.value()
self.openGLWidget.dir = False
if __name__ == "__main__":
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) # 解决实际和预览大小不一样的问题
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from PyQt5 import QtCore, QtGui, QtWidgets, QtOpenGL
from LoadMesh import *
import numpy as np
class GLWidget(QtWidgets.QOpenGLWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.wdith_ = 321
self.height_ = 451
self.rotX = 0.0
self.rotY = 0.0
self.rotZ = 0.0
self.dir = True
self.num_ = 0.0
self.mesh = LoadMesh("C:/Users/admin/Desktop/tt/tt_openGL/cube.obj", GL_LINE_STRIP)
def initializeGL(self):
glClearColor(0, 0, 0, 1)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(90, (self.wdith_ / self.height_), 0.1, 1000.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glViewport(0, 0, self.wdith_, self.height_)
glEnable(GL_DEPTH_TEST)
glTranslate(0, 0, -5)
# glTranslate(0, 0, -100)
# 绘图函数
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
#glRotatef(1.0, 0.0, 0.0, 1.0)
if self.dir: # 正转
glRotatef(self.rotX, 1.0, 0.0, 0.0)
glRotatef(self.rotY, 0.0, 1.0, 0.0)
glRotatef(self.rotZ, 0.0, 0.0, 1.0)
elif not(self.dir):
glRotatef(self.rotX, -1.0, 0.0, 0.0)
glRotatef(self.rotY, 0.0, -1.0, 0.0)
glRotatef(self.rotZ, 0.0, 0.0, -1.0)
self.rotX = 0.0
self.rotY = 0.0
self.rotZ = 0.0
glPushMatrix()
self.mesh.draw()
glPopMatrix()
# self.num_ += 1
# print("rotx value = ", self.rotX)
# print("num = {0}".format(self.num_))
3.效果演示
滑条控制3D模型旋转