from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
angle = 0
move = 0
size = 1
def perp():
glClearColor(0.0, 0.0, 0.0,0.0)
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(37,1,0.1,5)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(-2.0, -3.0, -2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
def OnMouse(button,state,x,y):
global size
if(button==GLUT_LEFT_BUTTON and state==GLUT_DOWN):
size = size+0.001
glMatrixMode(GL_MODELVIEW)
glScalef(size,size,size)
glutPostRedisplay()
if(button==GLUT_RIGHT_BUTTON and state==GLUT_DOWN):
size = size-0.001
glMatrixMode(GL_MODELVIEW)
glScalef(size,size,size)
glutPostRedisplay()
def KeyBoards(key,x,y):
global angle,move,size
if key == b"w":
move = move+0.001
glMatrixMode(GL_MODELVIEW)
glRotatef(angle,-1,0,0)
glutPostRedisplay()
elif key == b"s":
move = move-0.001
glMatrixMode(GL_MODELVIEW)
glRotatef(angle,-1,0,0)
glutPostRedisplay()
elif key == b"a":
angle = angle-0.1
glMatrixMode(GL_MODELVIEW)
glRotatef(angle,-1,0,0)
glutPostRedisplay()
elif key == b"d":
angle = angle+0.1
glMatrixMode(GL_MODELVIEW)
glRotatef(angle,-1,0,0)
glutPostRedisplay()
elif key == b"\r":
angle = 0
move = 0
size =1
def drawFunc():
glClearColor(0.1,0.1,0.1,0.1)
glClear(GL_COLOR_BUFFER_BIT)
glRotatef(angle,0,1,0);
glScalef(size,size,size)
glTranslatef(move,0,0)
#perp()
glutWireTeapot(0.05)
glFlush()
def main():
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowPosition(0,0)
glutInitWindowSize(400,400)
glutCreateWindow("object")
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMouseFunc(OnMouse)
glutKeyboardFunc(KeyBoards)
glutMainLoop()
if __name__=="__main__":
main()
说明:
a,向左旋转
d,向右旋转
w,向左移动
d,向右移动
鼠标左键,放大
鼠标右键,缩小
enter,停止