目录
opengl安装
pip install -U pyopengl
opengl显示bvh
https://github.com/chenzhike110/BVH-visualizer
opengl pyqt 显示2d骨骼
from PyQt5.QtWidgets import QApplication, QMainWindow, QOpenGLWidget
from PyQt5.QtCore import Qt
import OpenGL.GL as gl
import OpenGL.GLU as glu
import sys
class OpenGLWidget(QOpenGLWidget):
def initializeGL(self):
gl.glClearColor(0.0, 0.0, 0.0, 1.0)
gl.glEnable(gl.GL_DEPTH_TEST)
self.modelPosition = [0.0, 0.0, -10.0] # 初始位置
self.moveStep = 0.5 # 每次移动的步长
self.setFocusPolicy(Qt.StrongFocus)
def keyPressEvent(self, event):
key = event.key()
if key == Qt.Key_Right:
self.modelPosition[0] += self.moveStep # 向右移动
elif key == Qt.Key_Left:
self.modelPosition[0] -= self.moveStep # 向左移动
elif key == Qt.Key_Up:
self.modelPosition[1] += self.moveStep # 向上移动
elif key == Qt.Key_Down:
self.modelPosition[1] -= self.moveStep # 向下移动
self.update() # 更新OpenGL小部件以重新渲染模型
def mousePressEvent(self, event):
if event.button() == Qt.RightButton: # 检测右键点击
self.modelPosition[0] += self.moveStep # 向右移动模型
self.update() # 更新OpenGL小部件以重新渲染模型
def resizeGL(self, width, height):
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
glu.gluPerspective(45, width / height, 0.1, 50.0)
gl.glMatrixMode(gl.GL_MODELVIEW)
def paintGL(self):
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glLoadIdentity()
gl.glTranslatef(*self.modelPosition) # 使用模型位置变量
self.drawSkeleton()
# def paintGL(self):
# gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
# gl.glLoadIdentity()
# gl.glTranslatef(0.0, 0.0, -10.0)
#
# self.drawSkeleton()
def drawSkeleton(self):
# 定义骨骼关键点
joints = [
(0, 0, 0), # 身体中心
(-1, 2, 0), # 左手
(1, 2, 0), # 右手
(0, -2, 0), # 腿部中心
(-1, -4, 0), # 左脚
(1, -4, 0) # 右脚
]
# 定义骨骼连接
bones = [
(0, 1),
(0, 2),
(0, 3),
(3, 4),
(3, 5)
]
# 绘制关键点
gl.glPointSize(10)
gl.glBegin(gl.GL_POINTS)
for joint in joints:
gl.glVertex3fv(joint)
gl.glEnd()
# 绘制骨骼
gl.glBegin(gl.GL_LINES)
for bone in bones:
for vertex in bone:
gl.glVertex3fv(joints[vertex])
gl.glEnd()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQt OpenGL 3D Skeleton Example')
self.setGeometry(300, 300, 800, 600)
self.initUI()
def initUI(self):
self.openglWidget = OpenGLWidget(self)
self.setCentralWidget(self.openglWidget)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
显示文字:
2024.02.23测试成功,虽然变量引用报错 GLUT_BITMAP_8_BY_13
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
def draw_text(x, y, text):
glRasterPos2f(x, y)
for character in text:
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, ord(character))
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
draw_text(-0.9, 0.9, "Hello, OpenGL!")
glutSwapBuffers()
glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
glutInitWindowSize(800, 600)
glutInitWindowPosition(0, 0)
glutCreateWindow(b"OpenGL Text Example")
glutDisplayFunc(display)
glutMainLoop()
显示3d效果:
from PyQt5.QtWidgets import QApplication, QMainWindow, QOpenGLWidget
from PyQt5.QtCore import Qt
import OpenGL.GL as gl
import OpenGL.GLU as glu
import sys
class OpenGLWidget(QOpenGLWidget):
def initializeGL(self):
gl.glClearColor(0.0, 0.0, 0.0, 1.0)
gl.glEnable(gl.GL_DEPTH_TEST)
def resizeGL(self, width, height):
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
glu.gluPerspective(45, width / height, 0.1, 50.0)
gl.glMatrixMode(gl.GL_MODELVIEW)
def paintGL(self):
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glLoadIdentity()
gl.glTranslatef(0.0, 0.0, -5.0)
self.drawCube()
def drawCube(self):
# 绘制一个简单的3D立方体
vertices = [
[-1, -1, -1],
[ 1, -1, -1],
[ 1, 1, -1],
[-1, 1, -1],
[-1, -1, 1],
[ 1, -1, 1],
[ 1, 1, 1],
[-1, 1, 1]
]
edges = [
(0, 1), (1, 2), (2, 3), (3, 0),
(4, 5), (5, 6), (6, 7), (7, 4),
(0, 4), (1, 5), (2, 6), (3, 7)
]
gl.glColor3f(1.0, 1.0, 1.0)
gl.glBegin(gl.GL_LINES)
for edge in edges:
for vertex in edge:
gl.glVertex3fv(vertices[vertex])
gl.glEnd()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQt OpenGL 3D Cube Example')
self.setGeometry(300, 300, 800, 600)
self.initUI()
def initUI(self):
self.openglWidget = OpenGLWidget(self)
self.setCentralWidget(self.openglWidget)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())