pyopengl全解析-4
阿新 • 來源:網路 • 發佈:2021-02-10
pyopengl全解析-4
前言
還是,番外篇連結附上。
跟新速度會稍慢一些,因為我有專案要做,請各位看官不要著急哈~
開始
今天學習一個很重要的東西——事件
OpenGL支援的事件很少,都是由GLUT提供的,有:
- 鍵盤按下(
glutKeyboardFunc、glutSpecialFunc) - 滑鼠移動(
glutMotionFunc、glutPassiveMotionFunc) - 滑鼠按下、放開(
glutMouseFunc)
註冊方式都是register(func),例如 glutKeyboardFunc(keyboard)
glutKeyboardFunc
glutKeyboardFunc在c++中的原型是:
void glutKeyboardFunc(void (*func) (unsigned char key, int x, int y));
學過c++能看明白,其引數是一個以(按鈕,滑鼠x軸位置,滑鼠y軸位置)為引數的函式。另glutKeyboardFunc不處理特殊鍵。
例子:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
glRotatef(1, 0, 1, 0)
glutWireTeapot(0.5)
glFlush()
def procKeyboard(key,x,y):
print('KEYBOARD RECALL:')
print('MOUSE:%s,%s'%(x,y))
print('KEY:%s'%key.decode())
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow(b"First")
glutKeyboardFunc(procKeyboard)
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMainLoop()

glutSpecialFunc
函式原型 ↓ \downarrow ↓
void glutSpecialFunc(void (*func)(int key,int x,int y));
其與glutKeyboardFunc相似,但只處理特殊鍵
↓
\downarrow
↓
GLUT_KEY_BEGINGLUT_KEY_DELETEGLUT_KEY_DOWNGLUT_KEY_ENDGLUT_KEY_F1GLUT_KEY_F2GLUT_KEY_F3GLUT_KEY_F4GLUT_KEY_F5GLUT_KEY_F6GLUT_KEY_F7GLUT_KEY_F8GLUT_KEY_F9GLUT_KEY_F10GLUT_KEY_F11GLUT_KEY_F12GLUT_KEY_HOMEGLUT_KEY_INSERTGLUT_KEY_LEFTGLUT_KEY_NUM_LOCKGLUT_KEY_PAGE_DOWNGLUT_KEY_PAGE_UPGLUT_KEY_REPEAT_DEFAULTGLUT_KEY_REPEAT_OFFGLUT_KEY_REPEAT_ONGLUT_KEY_RIGHTGLUT_KEY_UP
例子:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
glRotatef(1, 0, 1, 0)
glutWireTeapot(0.5)
glFlush()
def procKeyboard(key,x,y):
print('KEYBOARD RECALL:')
print('MOUSE:%s,%s'%(x,y))
print('KEY:%s'%key)
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow(b"First")
glutSpecialFunc(procKeyboard)
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMainLoop()

glutMotionFunc
拖拽事件
函式原型 ↓ \downarrow ↓
void glutMotionFunc(void(*func)(int x,int y));
例子:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
glRotatef(1, 0, 1, 0)
glutWireTeapot(0.5)
glFlush()
def procMotion(x,y):
print('MOTION RECALL:')
print('MOUSE:%s,%s'%(x,y))
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow(b"First")
glutMotionFunc(procMotion)
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMainLoop()

glutPassiveMotionFunc
滑鼠移動事件
函式原型 ↓ \downarrow ↓
void glutPassivMotionFunc(void(*func)(int x,int y));
例子:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
glRotatef(1, 0, 1, 0)
glutWireTeapot(0.5)
glFlush()
def procMotion(x,y):
print('MOTION RECALL:')
print('MOUSE:%s,%s'%(x,y))
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow(b"First")
glutPassiveMotionFunc(procMotion)
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMainLoop()

glutMouseFunc
函式原型 ↓ \downarrow ↓
void glutMouseFunc(void(*func)(int x,int y));
例子:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
glRotatef(1, 0, 1, 0)
glutWireTeapot(0.5)
glFlush()
def procMouse(btn,state,x,y):
st = 'pressed' if(state == GLUT_DOWN) else 'released'
print('MOTION RECALL:')
print('button %s %s'%(btn,st))
print('MOUSE:%s,%s'%(x,y))
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow(b"First")
glutMouseFunc(procMouse)
glutDisplayFunc(drawFunc)
glutIdleFunc(drawFunc)
glutMainLoop()

作者
hit-road
拜拜,下課!
回到頂部
