如何在 pygame 和 pyopengl 中独立旋转 2 个对象
How to rotate 2 objects independently in pygame and pyopengl
我正在尝试独立旋转 2 个对象,但是当我 运行 代码时,两个对象都以相同的方向旋转,请在此处输入代码
here i save the matrix and rotate a square under the cube
def rotate_square():
glColor3f(1,1,1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glRotatef(45,0,1,0)
glBegin(GL_LINES)
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v1);
glVertex3fv(v3);
glVertex3fv(v3);
glVertex3fv(v4);
glVertex3fv(v2);
glVertex3fv(v4);
glEnd()
glPopMatrix()
main function
def main():
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE, DOUBLEBUF|OPENGL)
resize(*SCREEN_SIZE)
print(glGetString(GL_VERSION))
gluLookAt(0, 0, -6, 0, 0, 0, 0, 1, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Rotating the cube
glRotatef(1,0,1,0)
glutWireCube(2)
rotate_square()
pygame.display.flip()
pygame.time.wait(10)
请注意,通过 glBegin
/glEnd
序列绘制、固定功能管线矩阵堆栈和每个顶点光模型的固定功能管线已被弃用数十年。
阅读 Fixed Function Pipeline and see Vertex Specification and Shader 了解最先进的渲染方式。
总之,一般情况下,一个物体是由模型矩阵变换,然后场景由视图矩阵和投影矩阵变换。
可悲的是,在已弃用的 OpenGL 固定功能管道中,模型矩阵和视图矩阵是连接在一起的,不能那么容易地单独处理。
您必须通过每个对象自己的模型矩阵对其进行变换,才能独立旋转它们。创建 4*4 矩阵并通过单位矩阵对其进行初始化:
model1 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
model2 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
要操作模型矩阵,请按 glLoadMatrix
. Then manipulate them and finally read the result back by glGetFloatv(GL_MODELVIEW_MATRIX)
:
将它们加载到矩阵堆栈
glPushMatrix()
glLoadMatrixf(model1)
glRotatef(1,0,1,0)
model1 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
如果必须应用模型矩阵,可以乘glMultMatrix
:
到矩阵栈
glPushMatrix()
glMultMatrixf(model1)
glutWireCube(2)
glPopMatrix()
最终代码可能如下所示:
model1 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
model2 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
def rotate_square():
global model2
glPushMatrix()
glLoadMatrixf(model2)
glRotatef(5,0,1,0)
model2 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
glColor3f(1,1,1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glMultMatrixf(model2)
glBegin(GL_LINES)
glVertex3fv(v1)
glVertex3fv(v2)
glVertex3fv(v1)
glVertex3fv(v3)
glVertex3fv(v3)
glVertex3fv(v4)
glVertex3fv(v2)
glVertex3fv(v4)
glEnd()
glPopMatrix()
def main():
global model1
# [...]
while True:
# [...]
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadMatrixf(model1)
glRotatef(1,0,1,0)
model1 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
glPushMatrix()
glMultMatrixf(model1)
glutWireCube(2)
glPopMatrix()
rotate_square()
pygame.display.flip()
pygame.time.wait(10)
我正在尝试独立旋转 2 个对象,但是当我 运行 代码时,两个对象都以相同的方向旋转,请在此处输入代码
here i save the matrix and rotate a square under the cube
def rotate_square():
glColor3f(1,1,1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glRotatef(45,0,1,0)
glBegin(GL_LINES)
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v1);
glVertex3fv(v3);
glVertex3fv(v3);
glVertex3fv(v4);
glVertex3fv(v2);
glVertex3fv(v4);
glEnd()
glPopMatrix()
main function
def main():
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE, DOUBLEBUF|OPENGL)
resize(*SCREEN_SIZE)
print(glGetString(GL_VERSION))
gluLookAt(0, 0, -6, 0, 0, 0, 0, 1, 0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
Rotating the cube
glRotatef(1,0,1,0)
glutWireCube(2)
rotate_square()
pygame.display.flip()
pygame.time.wait(10)
请注意,通过 glBegin
/glEnd
序列绘制、固定功能管线矩阵堆栈和每个顶点光模型的固定功能管线已被弃用数十年。
阅读 Fixed Function Pipeline and see Vertex Specification and Shader 了解最先进的渲染方式。
总之,一般情况下,一个物体是由模型矩阵变换,然后场景由视图矩阵和投影矩阵变换。
可悲的是,在已弃用的 OpenGL 固定功能管道中,模型矩阵和视图矩阵是连接在一起的,不能那么容易地单独处理。
您必须通过每个对象自己的模型矩阵对其进行变换,才能独立旋转它们。创建 4*4 矩阵并通过单位矩阵对其进行初始化:
model1 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
model2 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
要操作模型矩阵,请按 glLoadMatrix
. Then manipulate them and finally read the result back by glGetFloatv(GL_MODELVIEW_MATRIX)
:
glPushMatrix()
glLoadMatrixf(model1)
glRotatef(1,0,1,0)
model1 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
如果必须应用模型矩阵,可以乘glMultMatrix
:
glPushMatrix()
glMultMatrixf(model1)
glutWireCube(2)
glPopMatrix()
最终代码可能如下所示:
model1 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
model2 = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
def rotate_square():
global model2
glPushMatrix()
glLoadMatrixf(model2)
glRotatef(5,0,1,0)
model2 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
glColor3f(1,1,1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glMultMatrixf(model2)
glBegin(GL_LINES)
glVertex3fv(v1)
glVertex3fv(v2)
glVertex3fv(v1)
glVertex3fv(v3)
glVertex3fv(v3)
glVertex3fv(v4)
glVertex3fv(v2)
glVertex3fv(v4)
glEnd()
glPopMatrix()
def main():
global model1
# [...]
while True:
# [...]
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadMatrixf(model1)
glRotatef(1,0,1,0)
model1 = glGetFloatv(GL_MODELVIEW_MATRIX)
glPopMatrix()
glPushMatrix()
glMultMatrixf(model1)
glutWireCube(2)
glPopMatrix()
rotate_square()
pygame.display.flip()
pygame.time.wait(10)