opengl实现鼠标键盘事件

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,停止
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值