Chapter3Drawing Basic Graphics Primitives基本图元绘制概要1.ppt

Chapter3Drawing Basic Graphics Primitives基本图元绘制概要1.ppt

Chapter3Drawing Basic Graphics Primitives基本图元绘制概要1

3.5. Simple Interaction with the mouse and keyboard 鼠标键盘交互 Interactive graphics applications let the user control the flow of a program by natural human motions: pointing and clicking the mouse, and pressing various keyboard keys.交互式图形应用就是让用户通过点击鼠标和键盘来控制程序 Using the OpenGL Utility Toolkit (GLUT) the programmer can register a callback function with each of these events by using the following commands: glutMouseFunc(myMouse) which registers myMouse() with the event that occurs when the mouse button is pressed or released; glutMotionFunc(myMovedMouse) which registers myMovedMouse() with the event that occurs when the mouse is moved while one of the buttons is pressed; glutKeyboardFunc(myKeyboard) which registers myKeyBoard() with the event that occurs when a keyboard key is pressed. 3.4.1. Mouse interaction鼠标交互 (1)callback function myMouse() must take four parameters, the prototype(必须4参数,其原型): void myMouse(int button, int state, int x, int y); value of button will be one of: GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON, value of state will be one of: GLUT_UP or GLUT_DOWN. values x and y are the position of the mouse at the time of the event (2)callback function myMovedMouse() must take two parameters,the prototype (必须2参数,其原型): myMovedMouse(int x, int y); values of x and y are the position of the mouse when the event occurred; Application Framework void main() { // initialize things5 // create a screen window glutDisplayFunc(myDisplay); // register the redraw function glutMouseFunc(myMouse); // register the mouse function glutKeyboardFunc(myKeyboard); // register the keyboard action function ………… glutMainLoop(); // enter the unending main loop } 例:鼠标坐标获取与绘图方法 #include GL/glut.h #include math.h #include stdlib.h GLfloat x = 0.0;GLfloat y = 0.0; GLfloat size = 50.0; GLsizei wh = 500, ww = 500; void drawSquare(GLint x, GLint y)//绘制矩形 { y = wh-y; glBegin(GL_POLYGON); glVertex3f(x + size, y + size, 0); glVertex3f(x - size, y + size, 0); glVertex3f(x

文档评论(0)

1亿VIP精品文档

相关文档