如何在pyopengl中使用鼠标旋转立方体
How to rotate a cube using mouse in pyopengl
我知道问题出在哪里,但我想不出解决办法。问题是旋转是在固定在立方体上的轴上应用的,因此如果您将一个轴旋转 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()
像glTranslated
and glRotate
这样的操作定义了一个新矩阵并将当前矩阵乘以新矩阵。
finalmatrix = matrix1 * matrix2 * matrix3
如果您想将操作应用于当前模型转换,则必须以相反的顺序进行。
finalmatrix = matrix3 * matrix2 * matrix1
在Legacy OpenGL, there are different current matrices (see glMatrixMode
). The matrices are organized on a stack. The matrices can be pushe on and popped from the stack by glPushMatrix
/ glPopMatrix
。这可用于保存和恢复矩阵。
将投影矩阵应用于投影矩阵堆栈(GL_PROJECTION
):
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (screen[0] / screen[1]), 0.1, 500)
切换到模型视图矩阵并将当前矩阵存储到变量 modelMatrix
开头这是 identity matrix
:
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()
我知道问题出在哪里,但我想不出解决办法。问题是旋转是在固定在立方体上的轴上应用的,因此如果您将一个轴旋转 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()
像glTranslated
and glRotate
这样的操作定义了一个新矩阵并将当前矩阵乘以新矩阵。
finalmatrix = matrix1 * matrix2 * matrix3
如果您想将操作应用于当前模型转换,则必须以相反的顺序进行。
finalmatrix = matrix3 * matrix2 * matrix1
在Legacy OpenGL, there are different current matrices (see glMatrixMode
). The matrices are organized on a stack. The matrices can be pushe on and popped from the stack by glPushMatrix
/ glPopMatrix
。这可用于保存和恢复矩阵。
将投影矩阵应用于投影矩阵堆栈(GL_PROJECTION
):
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (screen[0] / screen[1]), 0.1, 500)
切换到模型视图矩阵并将当前矩阵存储到变量 modelMatrix
开头这是 identity matrix
:
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()