我尝试独立旋转2个对象,但是当我运行代码时,两个对象都沿相同方向旋转,请在此处输入代码
在这里我保存矩阵并在立方体下旋转一个正方形
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()
主要功能
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)
旋转立方体
glRotatef(1,0,1,0)
glutWireCube(2)
rotate_square()
pygame.display.flip()
pygame.time.wait(10)
答案 0 :(得分:2)
请注意,自几十年来,不赞成使用glBegin
/ glEnd
序列绘制每个顶点光模型的固定功能管线矩阵堆栈和固定功能管线。
阅读有关Fixed Function Pipeline的信息,并参阅Vertex Specification和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
将它们放到矩阵堆栈中。然后对其进行操作,最后通过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()
最终的cde可能如下所示:
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)