当用户按下或释放鼠标按钮、按下按钮时移动鼠标或按下和松开键盘按键时,就会产生一个相关事件。程序员可以用每类事件注册一个回调函数,例如使用如下函数:
- glutMouseFunc(myMouse):利用按下或释放鼠标按钮时发生的事件来注册myMouse
- glutMotionFunc(myMovedMouse):利用按下按钮同时移动鼠标的事件来注册myMovedMouse
- glutKeyboardFunc(myKeyboard):利用按下和松开键盘按键的事件来注册myKeyboard
1.用鼠标交互
回调函数的名字可以随便起,但是一定要带4个参数,其原型如下:
void myMouse(int button,int state,int x,int y); 当鼠标事件发生时,系统会调用注册的函数,并向其提供这些参数值。
button的值可能是下面明显含义中的一个:GLUT_LEFT_BUTTON、GLUT_MIDDLE_BUTTON、GLUT_RIGHT_BUTTON
state 的值可能是:GLUT_UP、GLUT_DOWN
x和y的值指明事件发生时鼠标的位置(注意:x是距离窗口左边的像素数、y是距离窗口顶端的像素数)
时间处理器本身并不引起屏幕的重新绘制事件。因此,为了看到鼠标的效果,鼠标处理器应该调用glutPostRedisplay()
#include "stdafx.h" #include <cstdlib> #include <gl\glut.h> const int screenWidth = 600; const int screenHeight = 480; class GLintPoint { public: GLint x; GLint y; GLintPoint(){ x = 0; y = 0; } GLintPoint(GLint a, GLint b){ x = a; y = b; } }; void myInit(){ glClearColor(1.0, 1.0, 1.0, 0.0); glColor3f(0.0f, 0.0f, 0.0f); glPointSize(2.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight); } void drawDot(GLint x, GLint y){ glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); } void myDisplay(GLintPoint corner[3]){ //绘制Sierrapinski垫片的函数,参数为三个坐标点 glClear(GL_COLOR_BUFFER_BIT); int index = rand() % 3; GLintPoint point = corner[index]; drawDot(point.x, point.y); for (int i = 0; i < 55000; i++){ index = rand() % 3; point.x = (point.x + corner[index].x) / 2; point.y = (point.y + corner[index].y) / 2; //printf("%d %d\n", point.x, point.y); drawDot(point.x, point.y); } glFlush(); } //Simple One 用鼠标放置点,用户每次按下左键时,就会在屏幕窗口鼠标所在位置绘制出一个点;如果用户按下右键,就改变窗口背景颜色 void myMouse(int button, int state, int x, int y){ if (state == GLUT_DOWN){ if (button == GLUT_LEFT_BUTTON){ drawDot(x, screenHeight - y); glFlush(); } else if (button == GLUT_RIGHT_BUTTON){ glClearColor(1.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } } return; } //Sierpinski 用鼠标控制Sierpinski垫片 void myMouseForSierpinski(int button, int state, int x, int y) { static GLintPoint corner[3]; static int numCorners = 0; if (state == GLUT_DOWN){ if (button == GLUT_LEFT_BUTTON){ corner[numCorners].x = x; corner[numCorners].y = screenHeight - y; if (++numCorners == 3){ myDisplay(corner); numCorners = 0; } } } else if (button == GLUT_RIGHT_BUTTON){ glClear(GL_COLOR_BUFFER_BIT); glFlush(); } } void myDis(){} int main(int argc, char **argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(100, 100); glutInitWindowSize(screenWidth, screenHeight); glutCreateWindow("Mouse Sierpinski"); glutMouseFunc(myMouseForSierpinski); //glutMouseFunc(myMouse); glutDisplayFunc(myDis); myInit(); glutMainLoop(); return 0; }
时间: 2024-10-16 04:33:30