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

3
我知道问题出在哪里,但我无法找到解决方案。问题在于旋转是以立方体为轴进行的,因此如果您将一个轴旋转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()

我认为在这里仅仅设置旋转是不够的,因为你希望它根据立方体与相机的变换而旋转。要做到这种事情,你需要开始研究如何将变换矩阵相乘。 - dsvp9xyjsqmfvi8p
1个回答

2

glTranslatedglRotate这样的操作会定义一个新矩阵,并将当前矩阵乘以新矩阵。

finalmatrix = matrix1 * matrix2 * matrix3

如果您希望将操作应用于当前模型转换,则必须按相反的顺序进行。

finalmatrix = matrix3 * matrix2 * matrix1

Legacy OpenGL 中,有不同的当前矩阵(参见 glMatrixMode)。这些矩阵被组织在一个栈中。可以通过 glPushMatrix / glPopMatrix 将矩阵压入和弹出栈中。这可用于保存和恢复矩阵。
将投影矩阵应用于投影矩阵堆栈(GL_PROJECTION):
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (screen[0] / screen[1]), 0.1, 500)

切换到模型视图矩阵并将当前矩阵存储到变量modelMatrix中,开始时这是单位矩阵

glMatrixMode(GL_MODELVIEW)  
modelMatrix= glGetFloatv(GL_MODELVIEW_MATRIX)

在循环中:

  • 存储模型视图矩阵并加载单位矩阵:
glPushMatrix()
glLoadIdentity()
  • 进行新的变换(glTranslatef / glRotatef

  • 将存储的模型视图矩阵相乘,并得到新的最终模型视图矩阵

glMultMatrixf(modelMatrix)
modelMatrix= glGetFloatv(GL_MODELVIEW_MATRIX)
  • 加载单位矩阵,设置视图矩阵并将模型视图矩阵相乘:
glLoadIdentity()
glTranslatef(0, 0, -5)
glMultMatrixf(modelMatrix)
  • 绘制模型并还原当前矩阵
Cube()
glPopMatrix()

看这个例子:

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)
    glMatrixMode(GL_PROJECTION)
    gluPerspective(45, (screen[0] / screen[1]), 0.1, 500)
    button_down = False

    glMatrixMode(GL_MODELVIEW)  
    modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)

    while True:
        glPushMatrix()
        glLoadIdentity()

        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)

        glMultMatrixf(modelMatrix)
        modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)

        glLoadIdentity()
        glTranslatef(0, 0, -5)
        glMultMatrixf(modelMatrix)

        Cube()

        glPopMatrix()
        pygame.display.flip()
        pygame.time.wait(10)

Main()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接