如何在pyopengl中使用鼠标旋转立方体

问题描述 投票:1回答:1

我知道问题出在哪里,但我找不到解决办法。问题在于,旋转是在固定在立方体上的轴上进行的,因此,如果您旋转一个轴的Pi弧度,则另一轴看起来像是鼠标控件被反转了。我希望它能够做到这一点,因此,单击鼠标左键然后向右移动鼠标可以将多维数据集向右旋转,向左移动它可以将多维数据集向左旋转,依此类推。请提供帮助。

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

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), (0, 3), (0, 4), (1, 2), (1, 5), (2, 3), (2, 6), (3, 7), (4, 5), (4, 7), (5, 6), (6, 7))
faces = ((0, 1, 2 , 3),(4,  5, 6, 7),(0, 4, 7, 3),(1, 5, 6, 2),(2, 6, 7, 3),(1, 5, 4, 0))

# =============================================================================
# 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), (0, 3), (0, 4), (2, 1), (2, 3), (2, 7), (6, 3), (6, 4), (6, 7), (5, 1), (5, 4), (5, 7))
# =============================================================================

def Cube():
    glBegin(GL_QUADS)
    for face in faces:
        for vertex in face:
            glColor3fv((1, 0, 1))
            glVertex3fv(vertices[vertex])
    glEnd()

    glBegin(GL_LINES)
    glColor3fv((1, 1, 1))
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

def Main():
    pygame.init()
    screen = (1600, 1200)
    display = pygame.display.set_mode(screen, DOUBLEBUF|OPENGL)
    gluPerspective(45, (screen[0] / screen[1]), 0.1, 500)
    glTranslatef(0, 0, -5)
    button_down = False

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    glTranslatef(-1, 0, 0)
                if event.key == pygame.K_RIGHT:
                    glTranslatef(1, 0, 0)
                if event.key == pygame.K_UP:
                    glTranslatef(0, 1, 0)
                if event.key == pygame.K_DOWN:
                    glTranslatef(0, -1, 0)
            if event.type == pygame.MOUSEMOTION:
                if button_down == True:
                    glRotatef(event.rel[1], 1, 0, 0)
                    glRotatef(event.rel[0], 0, 1, 0)
                print(event.rel)

        for event in pygame.mouse.get_pressed():
            print(pygame.mouse.get_pressed())
            if pygame.mouse.get_pressed()[0] == 1:
                button_down = True
            elif pygame.mouse.get_pressed()[0] == 0:
                button_down = False

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)

Main()
python opengl pyopengl opengl-compat
1个回答
1
投票

glTranslatedglTranslated之类的操作定义了一个新矩阵,并将当前矩阵乘以新矩阵。

glRotate

如果要对当前模型转换应用操作,则不必按相反的顺序进行。

glRotate

finalmatrix = matrix1 * matrix2 * matrix3 中,存在不同的当前矩阵(请参见finalmatrix = matrix3 * matrix2 * matrix1 )。矩阵是按堆栈组织的。可以通过Legacy OpenGL将矩阵压入堆栈并从堆栈中弹出。这可用于保存和恢复矩阵。

将投影矩阵应用于投影矩阵堆栈(glMatrixMode:]

glMatrixMode

切换到模型视图矩阵并将当前矩阵存储在变量glPushMatrix / glPopMatrix的开头,这是glPushMatrix

glPopMatrix

在循环中:

  • 存储模型视图矩阵并填充单位矩阵:
GL_PROJECTION
  • 进行新的变换(glMatrixMode(GL_PROJECTION) gluPerspective(45, (screen[0] / screen[1]), 0.1, 500) / modelMatrix

  • 乘以存储的模型视图矩阵,然后得到新的最终模型视图矩阵

identity matrix
  • 加载单位矩阵,设置视图矩阵并乘以模型视图矩阵:
identity matrix
  • 绘制模型并恢复当前矩阵
glMatrixMode(GL_MODELVIEW)  
modelMatrix= glGetFloatv(GL_MODELVIEW_MATRIX)

请参见示例:

glPushMatrix() glLoadIdentity()

glTranslatef
© www.soinside.com 2019 - 2024. All rights reserved.