pyopengl全解析-4
- 前言
- 开始
-
-
-
- glutKeyboardFunc
- glutSpecialFunc
- glutMotionFunc
- glutPassiveMotionFunc
- glutMouseFunc
-
-
-
- 作者
-
-
-
-
前言 还是,番外篇链接附上。
跟新速度会稍慢一些,因为我有项目要做,请各位看官不要着急哈~
开始 今天学习一个很重要的东西——事件
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_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()
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));
【python|pyopengl全解析-4】例子:
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
拜拜,下课!
回到顶部
推荐阅读
- python|Pandas实例|药品发放汇总与excel表数据回填
- python|python 测试用例 自动生成_pythonpytest自动测试框架生成测试报告,PythonPytest,自动化...
- python|sqlmap使用
- Python量化|行业轮动(股票)——Python量化
- Python量化|LSTM、GRU 时间序列股票数据预测(文末完整代码)
- 程序员|「1024 程序员节」各大公司和程序员们都是怎么过的(你都做了哪些计划或安排?)
- java|卧槽!迅雷的代码竟然被扒了精光!
- 资讯|频繁被吐槽的Java依然很强大!
- Python|蓝桥杯FJ的字符串Python