大三05计算机图形学-3637working with callbacks_第1页
大三05计算机图形学-3637working with callbacks_第2页
大三05计算机图形学-3637working with callbacks_第3页
大三05计算机图形学-3637working with callbacks_第4页
大三05计算机图形学-3637working with callbacks_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Working with Callbacks回调的应用原著Ed AngelProfessor of Computer Science, Electrical and Computer Engineering, and Media ArtsUniversity of New Mexico编辑 武汉大学计算机学院图形学课程组ObjectivesLearn to build interactive programs using GLUT callbacks利用GLUT回调函数设计交互程序 MouseKeyboardReshape改变窗口形状Introduce menus in GLUT在GLUT中定

2、义菜单Outline3.6 Programming event input事件驱动输入编程3.6.1 Using the Pointing Device使用定位设备3.6.2 Windows Events窗口事件3.6.3 keyboard Events键盘事件3.7 Menu菜单3.6 Programming event input (The mouse callback鼠标回调函数)glutMouseFunc(mymouse)void mymouse(GLint button, GLint state, GLint x, GLint y)Returns返回值which button (GL

3、UT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON) caused event导致了事件发生state of that button (GLUT_UP, GLUT_DOWN)按钮的状态Position in window在窗口中的位置3.6.1 Using the Pointing Device (Positioning定位-1)The position in the screen window is usually measured in pixels with the origin at the top-left corner屏幕上的

4、位置通常是以像素为单位,原点在左上角Consequence of refresh done from top to bottom显示器自顶向下刷新显示内容OpenGL uses a world coordinate system with origin at the bottom left在OpenGL中应用一个世界坐标系,其原点在左下角(0,0)hw3.6.1 Using the Pointing Device (Positioning定位-2)Must invert y coordinate returned by callback by height of window在这个坐标系中的y

5、坐标需要从窗口高度中减去回调函数返回的y值y = h y;(0,0)hwObtaining the window size获取窗口尺寸To invert the y position we need the window height为了完成y坐标的转换,需要知道窗口的尺寸Height can change during program execution高度可能发生改变Track with a global variable用一个全局变量跟踪其变化New height returned to reshape callback新高度值返回给形状改变回调函数(that we will look

6、at in detail soon详细见后论述)Can also use query functions也可以用查询函数glGetIntvglGetFloatvto obtain any value that is part of the state可以得到任何状态值Terminating a program结束程序In our original programs, there was no way to terminate them through OpenGL在以前的程序中没有办法通过OpenGL结束当前程序We can use the simple mouse callback可以简单地

7、利用鼠标回调函数做到这一点void mouse(int btn, int state, int x, int y) if(btn=GLUT_RIGHT_BUTTON & state=GLUT_DOWN) exit(0);Using the mouse position鼠标位置的应用In the next example, we draw a small square at the location of the mouse each time the left mouse button is clicked每单击一次鼠标左按钮,就会在当前鼠标位置处画一个小方框This example does

8、 not use the display callback but one is required by GLUT在这个例子中并没有用到显示回调函数 We can use the empty display callback function但是由于GLUT要求必须有一个显示回调函数,因此要定义一个空函数 mydisplay()Drawing squares at cursor location在指针处画方框-1void mymouse(int btn, int state, int x, int y) if(btn=GLUT_RIGHT_BUTTON & state=GLUT_DOWN) e

9、xit(0); if(btn=GLUT_LEFT_BUTTON & state=GLUT_DOWN)drawSquare(x, y);void drawSquare(int x, int y)Drawing squares at cursor location在指针处画方框-2void drawSquare(int x, int y) y=w-y; /* invert y position */ glColor3ub( (char) rand()%256, (char) rand )%256, (char) rand()%256); /* a random color */ glBegin(G

10、L_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd();Using the motion callback鼠标移动回调函数的应用-1We can draw squares (or anything else) continuously as long as a mouse button is depressed by using the motion callback通过利用移动回调函数可

11、以在不释放鼠标按钮的情况下,连续画一系列方框glutMotionFunc(drawSquare)Using the motion callback鼠标移动回调函数的应用-2We can draw squares without depressing a button using the passive motion callback应用被动移动回调函数,可以不用按鼠标按钮就可以连续画方框glutPassiveMotionFunc(drawSquare)3.6.3 Keyboard Event(Using the keyboard键盘的应用)glutKeyboardFunc(mykey)void

12、 mykey(unsigned char key, int x, int y)Returns ASCII code of key depressed and mouse location返回键盘上被按下键的ASCII码和鼠标位置, 注意在GLUT中并不把释放键做为一个事件void mykey()if(key = Q | key = q) exit(0);3.6.3 Keyboard Event Special and Modifier Keys特殊按键GLUT defines the special keys in glut.h定义了特殊按键Function key功能键1: GLUT_KEY

13、_F1Up arrow key向上方向键: GLUT_KEY_UPif(key = GLUT_KEY_F1 3.6.3 Keyboard Event Special and Modifier Keys特殊按键其它键Can also check of one of the modifiers is depressed by glutGetModifiers()探测是否按下了GLUT_ACTIVE_SHIFTGLUT_ACTIVE_CTRLGLUT_ACTIVE_ALTAllows emulation of three-button mouse with one- or two-button mi

14、ce由此可以利用单钮或双钮鼠标模拟三钮鼠标3.6.2 window Event (Reshaping the window窗口形状的改变)We can reshape and resize the OpenGL display window by pulling the corner of the window通过拖动窗口的角点可以改变窗口的形状和尺寸What happens to the display?那么其中的显示内容该如何处理Must redraw from application重新绘制Two possibilities两种可能性Display part of world显示原来内容

15、的一部分Display whole world but force to fit in new window通过强迫适应新窗口来显示所有内容Can alter aspect ratio改变了显示长宽比率3.6.2 Window Event (Reshape possiblities形状改变的可能性)original初始reshaped改变3.6.2 Windows Events(The Reshape callback形状改变的回调函数)glutReshapeFunc(myreshape)void myreshape( int w, int h)Returns width and height

16、 of new window (in pixels)返回新窗口以像素为单位的宽度与高度A redisplay is posted automatically at end of execution of the callback在回调函数执行后自动发送重新显示的事件GLUT has a default reshape callback but you probably want to define your own GLUT有一个缺省的形状改变的回调函数,但用户可能需要定义自己的行为The reshape callback is good place to put viewing functi

17、ons because it is invoked when the window is first opened 这个回调函数是放置照相机函数的恰当地方,因为当窗口第一次被打开时就会调用这个函数3.6.2 Windows Events(Example Reshape示例)This reshape preserves shapes by making the viewport and world window have the same aspect ratio在下面的例子中通过改变 视口和世界窗口使得长宽比率不变void myReshape(int w, int h) glViewport(

18、0, 0, w, h); glMatrixMode(GL_PROJECTION); /* switch matrix mode */ glLoadIdentity(); if (w = h) gluOrtho2D(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w); else gluOrtho2D(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0); glMatrixMode(GL_MOD

19、ELVIEW); /* return to modelview mode */3.7 Menu菜单(Toolkits and Widgets工具包窗口资源)Most window systems provide a toolkit or library of functions for building user interfaces that use special types of windows called widgets许多窗口系统提供了一个工具包或者一组库函数用来建立用户界面,界面中用到了一些特殊类型的窗口,称为资源构件(widget)Widget sets include too

20、ls such as构件集合中包含Menus菜单Slidebars滑动条Dials对话框Input boxes输入文本框But toolkits tend to be platform dependent上述构件通常都是与平台相关的GLUT provides a few widgets including menus提供了包含菜单在内的很少几个构件3.7 Menus菜单GLUT supports pop-up menus支持弹出式菜单A menu can have submenus子菜单Three steps创建弹出式菜单的三个步骤Define entries for the menu定义菜单内各条目Define action for each menu item定义每个菜单项的行为Action carried out if entry selected如果条目被选择所要执行的操作Attach menu to a mouse button把菜单连接到鼠标按钮上3.7 Menus菜单(Defining a simple menu定义一个简单菜单)In main.cmenu_id = glutCreateMenu(mymenu);glutAddmenuEntry(“cl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论