欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pyopengl全解析-4

程序员文章站 2022-03-26 08:47:31
...

前言

还是,番外篇链接附上。
跟新速度会稍慢一些,因为我有项目要做,请各位看官不要着急哈~

开始

今天学习一个很重要的东西——事件
OpenGL支持的事件很少,都是由GLUT提供的,有:

  • 键盘按下(glutKeyboardFuncglutSpecialFunc)
  • 鼠标移动(glutMotionFuncglutPassiveMotionFunc)
  • 鼠标按下、放开(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

拜拜,下课!
回到顶部