1. 程式人生 >實用技巧 >其它 >pyopengl全解析-4

pyopengl全解析-4


技術標籤:pythonopengl3d渲染

前言

還是,番外篇連結附上。
跟新速度會稍慢一些,因為我有專案要做,請各位看官不要著急哈~

開始

今天學習一個很重要的東西——事件
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()

pyopengl全解析-4

glutSpecialFunc

函式原型 ↓ \downarrow ↓

void glutSpecialFunc(void (*func)(int key,int x,int y));

其與glutKeyboardFunc相似,但只處理特殊鍵 ↓ \downarrow ↓

  • GLUT_KEY_BEGIN
  • GLUT_KEY_DELETE
  • GLUT_KEY_DOWN
  • GLUT_KEY_END
  • GLUT_KEY_F1
  • GLUT_KEY_F2
  • GLUT_KEY_F3
  • GLUT_KEY_F4
  • GLUT_KEY_F5
  • GLUT_KEY_F6
  • GLUT_KEY_F7
  • GLUT_KEY_F8
  • GLUT_KEY_F9
  • GLUT_KEY_F10
  • GLUT_KEY_F11
  • GLUT_KEY_F12
  • GLUT_KEY_HOME
  • GLUT_KEY_INSERT
  • GLUT_KEY_LEFT
  • GLUT_KEY_NUM_LOCK
  • GLUT_KEY_PAGE_DOWN
  • GLUT_KEY_PAGE_UP
  • GLUT_KEY_REPEAT_DEFAULT
  • GLUT_KEY_REPEAT_OFF
  • GLUT_KEY_REPEAT_ON
  • GLUT_KEY_RIGHT
  • GLUT_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()

pyopengl全解析-4

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()

pyopengl全解析-4

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()

pyopengl全解析-4

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()

pyopengl全解析-4




作者

hit-road

拜拜,下課!
回到頂部