大三05計算機(jī)圖形學(xué)-3637working with callbacks_第1頁
大三05計算機(jī)圖形學(xué)-3637working with callbacks_第2頁
大三05計算機(jī)圖形學(xué)-3637working with callbacks_第3頁
大三05計算機(jī)圖形學(xué)-3637working with callbacks_第4頁
大三05計算機(jī)圖形學(xué)-3637working with callbacks_第5頁
已閱讀5頁,還剩21頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、Working with Callbacks回調(diào)的應(yīng)用原著Ed AngelProfessor of Computer Science, Electrical and Computer Engineering, and Media ArtsUniversity of New Mexico編輯 武漢大學(xué)計算機(jī)學(xué)院圖形學(xué)課程組ObjectivesLearn to build interactive programs using GLUT callbacks利用GLUT回調(diào)函數(shù)設(shè)計交互程序 MouseKeyboardReshape改變窗口形狀I(lǐng)ntroduce menus in GLUT在GLUT中定

2、義菜單Outline3.6 Programming event input事件驅(qū)動輸入編程3.6.1 Using the Pointing Device使用定位設(shè)備3.6.2 Windows Events窗口事件3.6.3 keyboard Events鍵盤事件3.7 Menu菜單3.6 Programming event input (The mouse callback鼠標(biāo)回調(diào)函數(shù))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導(dǎo)致了事件發(fā)生state of that button (GLUT_UP, GLUT_DOWN)按鈕的狀態(tài)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顯示器自頂向下刷新顯示內(nèi)容OpenGL uses a world coordinate system with origin at the bottom left在OpenGL中應(yīng)用一個世界坐標(biāo)系,其原點在左下角(0,0)hw3.6.1 Using the Pointing Device (Positioning定位-2)Must invert y coordinate returned by callback by height of window在這個坐標(biāo)系中的y

5、坐標(biāo)需要從窗口高度中減去回調(diào)函數(shù)返回的y值y = h y;(0,0)hwObtaining the window size獲取窗口尺寸To invert the y position we need the window height為了完成y坐標(biāo)的轉(zhuǎn)換,需要知道窗口的尺寸Height can change during program execution高度可能發(fā)生改變Track with a global variable用一個全局變量跟蹤其變化New height returned to reshape callback新高度值返回給形狀改變回調(diào)函數(shù)(that we will look

6、at in detail soon詳細(xì)見后論述)Can also use query functions也可以用查詢函數(shù)glGetIntvglGetFloatvto obtain any value that is part of the state可以得到任何狀態(tài)值Terminating a program結(jié)束程序In our original programs, there was no way to terminate them through OpenGL在以前的程序中沒有辦法通過OpenGL結(jié)束當(dāng)前程序We can use the simple mouse callback可以簡單地

7、利用鼠標(biāo)回調(diào)函數(shù)做到這一點void mouse(int btn, int state, int x, int y) if(btn=GLUT_RIGHT_BUTTON & state=GLUT_DOWN) exit(0);Using the mouse position鼠標(biāo)位置的應(yīng)用In the next example, we draw a small square at the location of the mouse each time the left mouse button is clicked每單擊一次鼠標(biāo)左按鈕,就會在當(dāng)前鼠標(biāo)位置處畫一個小方框This example does

8、 not use the display callback but one is required by GLUT在這個例子中并沒有用到顯示回調(diào)函數(shù) We can use the empty display callback function但是由于GLUT要求必須有一個顯示回調(diào)函數(shù),因此要定義一個空函數(shù) 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鼠標(biāo)移動回調(diào)函數(shù)的應(yīng)用-1We can draw squares (or anything else) continuously as long as a mouse button is depressed by using the motion callback通過利用移動回調(diào)函數(shù)可

11、以在不釋放鼠標(biāo)按鈕的情況下,連續(xù)畫一系列方框glutMotionFunc(drawSquare)Using the motion callback鼠標(biāo)移動回調(diào)函數(shù)的應(yīng)用-2We can draw squares without depressing a button using the passive motion callback應(yīng)用被動移動回調(diào)函數(shù),可以不用按鼠標(biāo)按鈕就可以連續(xù)畫方框glutPassiveMotionFunc(drawSquare)3.6.3 Keyboard Event(Using the keyboard鍵盤的應(yīng)用)glutKeyboardFunc(mykey)void

12、 mykey(unsigned char key, int x, int y)Returns ASCII code of key depressed and mouse location返回鍵盤上被按下鍵的ASCII碼和鼠標(biāo)位置, 注意在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由此可以利用單鈕或雙鈕鼠標(biāo)模擬三鈕鼠標(biāo)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?那么其中的顯示內(nèi)容該如何處理Must redraw from application重新繪制Two possibilities兩種可能性Display part of world顯示原來內(nèi)容

15、的一部分Display whole world but force to fit in new window通過強(qiáng)迫適應(yīng)新窗口來顯示所有內(nèi)容Can alter aspect ratio改變了顯示長寬比率3.6.2 Window Event (Reshape possiblities形狀改變的可能性)original初始reshaped改變3.6.2 Windows Events(The Reshape callback形狀改變的回調(diào)函數(shù))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在回調(diào)函數(shù)執(zhí)行后自動發(fā)送重新顯示的事件GLUT has a default reshape callback but you probably want to define your own GLUT有一個缺省的形狀改變的回調(diào)函數(shù),但用戶可能需要定義自己的行為The reshape callback is good place to put viewing functi

17、ons because it is invoked when the window is first opened 這個回調(diào)函數(shù)是放置照相機(jī)函數(shù)的恰當(dāng)?shù)胤剑驗楫?dāng)窗口第一次被打開時就會調(diào)用這個函數(shù)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許多窗口系統(tǒng)提供了一個工具包或者一組庫函數(shù)用來建立用戶界面,界面中用到了一些特殊類型的窗口,稱為資源構(gòu)件(widget)Widget sets include too

20、ls such as構(gòu)件集合中包含Menus菜單Slidebars滑動條Dials對話框Input boxes輸入文本框But toolkits tend to be platform dependent上述構(gòu)件通常都是與平臺相關(guān)的GLUT provides a few widgets including menus提供了包含菜單在內(nèi)的很少幾個構(gòu)件3.7 Menus菜單GLUT supports pop-up menus支持彈出式菜單A menu can have submenus子菜單Three steps創(chuàng)建彈出式菜單的三個步驟Define entries for the menu定義菜單內(nèi)各條目Define action for each menu item定義每個菜單項的行為Action carried out if entry selected如果條目被選擇所要執(zhí)行的操作Attach menu to a mouse button把菜單連接到鼠標(biāo)按鈕上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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論