![事件處理函式_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/13/b18504bf-6efd-42f6-8a8c-c89ddce857fe/b18504bf-6efd-42f6-8a8c-c89ddce857fe1.gif)
![事件處理函式_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/13/b18504bf-6efd-42f6-8a8c-c89ddce857fe/b18504bf-6efd-42f6-8a8c-c89ddce857fe2.gif)
![事件處理函式_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/13/b18504bf-6efd-42f6-8a8c-c89ddce857fe/b18504bf-6efd-42f6-8a8c-c89ddce857fe3.gif)
![事件處理函式_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/13/b18504bf-6efd-42f6-8a8c-c89ddce857fe/b18504bf-6efd-42f6-8a8c-c89ddce857fe4.gif)
![事件處理函式_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-1/13/b18504bf-6efd-42f6-8a8c-c89ddce857fe/b18504bf-6efd-42f6-8a8c-c89ddce857fe5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、GLUT 事件處理函式靜宜大學(xué)資管系蔡奇?zhèn)?副教授2003-2006大綱p註冊處理事件的 callback 函式p註冊鍵盤輸入的 callbacknglutKeyboardFuncnglutSpecialFuncp註冊滑鼠操作的 callbacknglutMouseFuncnglutMotionFunc & glutPassiveMotionFuncpglutGetModifiersglutKeyboardFuncvoid glutKeyboardFunc(void (*func)(unsigned char key, int x, int y);此函式註冊處理鍵盤輸入字元鍵的 cal
2、lback 函式。當(dāng)使用者在目前視窗中敲入能產(chǎn)生 ASCII 碼的按鍵時,GLUT 就會自動執(zhí)行你所指定的 callback 函式,並傳入下列三項資料: key: 輸入鍵的 ASCII 碼。 x: 按鍵時滑鼠在視窗位置的 x 座標(biāo)。 y: 按鍵時滑鼠在視窗位置的 y 座標(biāo)。範(fàn)例void (*draw_figure)(void);/ draw_triangle(), draw_square(), and draw_circle() are/ functions to darw a triangle, a square, and a circle,/ respectively.void choos
3、e_figure (unsigned char key, int x, int y)switch (key) case 1: draw_figure = draw_triangle; break;case 2: draw_figure = draw_square; break;case 3: draw_figure = draw_circle; break;glutPostRedisplay(); / update the window/ in main() functionglutKeyboardFunc(choose_figure);glutSpecialFuncvoid glutSpec
4、ialFunc(void (*func)(int key, int x, int y);此函式註冊處理鍵盤輸入功能鍵或方向鍵的 callback 函式。當(dāng)使用者在目前視窗中敲入功能鍵或方向鍵時,GLUT 就會自動執(zhí)行你所指定的 callback 函式,並傳入下列三項資料: key: 功能鍵或方向鍵的 GLUT 鍵名(見下一頁)。 x: 按鍵時滑鼠在視窗位置的 x 座標(biāo)。 y: 按鍵時滑鼠在視窗位置的 y 座標(biāo)。 GLUT 鍵名 按鍵GLUT_KEY_F1F1 功能鍵GLUT_KEY_F2F2 功能鍵GLUT_KEY_F3F3 功能鍵GLUT_KEY_F4F4 功能鍵GLUT_KEY_F5F5
5、功能鍵GLUT_KEY_F6F6 功能鍵GLUT_KEY_F7F7 功能鍵GLUT_KEY_F8F8 功能鍵GLUT_KEY_F9F9 功能鍵GLUT_KEY_F10F10 功能鍵GLUT_KEY_F11F11 功能鍵GLUT_KEY_F12F12 功能鍵GLUT_KEY_LEFT 方向鍵GLUT_KEY_UP 方向鍵GLUT_KEY_RIGHT 方向鍵GLUT_KEY_DOWN 方向鍵GLUT_KEY_PAGE_UPPage up 方向鍵GLUT_KEY_PAGE_DOWNPage down 方向鍵GLUT_KEY_HOMEHome 方向鍵GLUT_KEY_ENDEnd 方向鍵GLUT_KE
6、Y_INSERTInsert 鍵#include int window_width = 250, window_height = 250;float center_x = 0.5, center_y = 0.5, half_width = 0.25;const float inc_amount = 0.01;void display(void) glClear (GL_COLOR_BUFFER_BIT); /* clear all pixels */ glColor3f (1.0, 1.0, 1.0); glBegin(GL_POLYGON); glVertex3f (center_x - h
7、alf_width, center_y - half_width, 0.0); glVertex3f (center_x + half_width, center_y - half_width, 0.0); glVertex3f (center_x + half_width, center_y + half_width, 0.0); glVertex3f (center_x - half_width, center_y + half_width, 0.0); glEnd(); glFlush ();範(fàn)例void handle_special_key (int key, int x, int y
8、)switch (key) case GLUT_KEY_LEFT:if (center_x = half_width)center_x -= inc_amount;break;case GLUT_KEY_RIGHT:if (1.0 - center_x = half_width)center_x += inc_amount;break;case GLUT_KEY_UP:if (1.0 - center_y = half_width)center_y += inc_amount;break;case GLUT_KEY_DOWN:if (center_y = half_width)center_y
9、 -= inc_amount;break;glutPostRedisplay(); / update the windowvoid handle_key (unsigned char key, int x, int y)switch (key) case s:if (half_width = 0.1)half_width -= inc_amount;break;case l:if (half_width 0.5)half_width += inc_amount;break;glutPostRedisplay(); / update the windowint main(int argc, ch
10、ar* argv) glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (window_width, window_height); glutInitWindowPosition (100, 100); glutCreateWindow (hello); init (); glutDisplayFunc(display); glutKeyboardFunc(handle_key); glutSpecialFunc(handle_special_key); glut
11、MainLoop(); return 0; /* ANSI C requires main to return int. */glutMouseFuncvoid glutMouseFunc(void (*func)(int button, int state, int x, int y);此函式註冊處理滑鼠按鈕的 callback。當(dāng)滑鼠按鈕在目前視窗中按下或放開時, GLUT 就會自動執(zhí)行你所指定的 callback 函式。button: 下列三種按鈕值之一 GLUT_LEFT_BUTTON(左按鈕) GLUT_MIDDLE_BUTTON(中按鈕) GLUT_RIGHT_BUTTON(右按鈕
12、)state: 按鈕的狀態(tài) GLUT_UP(放開按鈕) GLUT_DOWN(壓下按鈕)x, y: 按鈕操作發(fā)生時滑鼠的視窗座標(biāo)。範(fàn)例void handle_mouse_buttons (int button, int state, int x, int y)if (state = GLUT_DOWN) / 只處理壓下滑鼠按鈕switch (button) case GLUT_LEFT_BUTTON:/ 處理滑鼠的左按鈕break;case GLUT_MIDDLE_BUTTON:/ 處理滑鼠的中按鈕break;case GLUT_RIGHT_BUTTON:/ 處理滑鼠的右按鈕break;/ in
13、 main() functionglutMouseFunc(handle_mouse_buttons);glutMotionFunc & glutPassiveMotionFunc void glutMotionFunc(void (*func)(int x, int y);此函式註冊處理滑鼠移動的 callback。按住任何一個滑鼠鈕在目前視窗中移動滑鼠時, GLUT 就會自動執(zhí)行你所指定的 callback 函式。x, y: 移動時滑鼠在視窗位置的座標(biāo)。void glutPassiveMotionFunc(void (*func)(int x, int y);此函式註冊處理滑鼠移動
14、的 callback。當(dāng)不按任何鈕地在目前視窗中移動滑鼠時, GLUT 就會自動執(zhí)行你所指定的 callback 函式。glutGetModifiersint glutGetModifiers(void);GLUT_ACTIVE_SHIFT 若按下 Shift 鍵或 Caps Lock 鍵GLUT_ACTIVE_CTRL若按下 Ctrl 鍵GLUT_ACTIVE_ALT 若按下 Alt 鍵此函式傳回鍵盤輸入或滑鼠操作時的修飾鍵狀態(tài)。在鍵盤 callback 或滑鼠 callback 中,你可以把函式的傳回值和上面的三個常數(shù)做 bit AND 運(yùn)算來測試那一個修飾鍵被按下。譬如: int mod
15、ifier_key = glutGetModifiers();if (modifier_key & GLUT_ACTIVE_CTRL) / 按下 Ctrl 鍵/* * mouseclick.c * This is a simple, introductory OpenGL program. */#include #include int window_width = 250, window_height = 250;const int max_point = 100;GLint x_coordmax_point, y_coordmax_point;int nPoints = 0;inl
16、ine GLfloat x_convert (int x)return x/(window_width-1.0);inline GLfloat y_convert (int y)return 1.0 - y /(window_height-1.0);範(fàn)例void display(void) glClear (GL_COLOR_BUFFER_BIT); /* clear all pixels */ glColor3f (1.0, 1.0, 1.0); glBegin(GL_POINTS); for (int i = 0; i nPoints; i+) glVertex3f (x_convert(
17、x_coordi), y_convert(y_coordi), 0.0); glEnd(); glFlush ();void handle_mouseclick (int button, int state, int x, int y)if (button = GLUT_LEFT_BUTTON & state = GLUT_DOWN) if (nPoints = max_point) nPoints = 0;x_coordnPoints = x;y_coordnPoints = y;nPoints+;glutPostRedisplay();int main(int argc, char
18、* argv) glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (window_width, window_height); glutInitWindowPosition (100, 100); glutCreateWindow (mouseclick); init (); glutDisplayFunc(display); glutMouseFunc(handle_mouseclick); glutMainLoop(); return 0; /* ANSI
19、C requires main to return int. */* * hermite.c * This is a simple, introductory OpenGL program. */#include #include int window_width = 250, window_height = 250;GLfloat x_coord3, y_coord3;int nPoints = 0;inline GLfloat x_convert (int x)return x/(float)(window_width-1);inline GLfloat y_convert (int y)
20、return 1.0 - y /(float)(window_height-1);範(fàn)例void HermiteCurve(int n) float t, dt, t2, t3, f1, f2, f3, f4; dt = 1.0/n; / t runs from 0 to 1. GLfloat PT0_x = x_coord1 - x_coord0; GLfloat PT0_y = y_coord1 - y_coord0; GLfloat PT1_x = x_coord2 - x_coord1; GLfloat PT1_y = y_coord2 - y_coord1; glBegin(GL_LINE_STRIP);for (t = 0.0; t = 1.0; t += dt) t2 = t * t; t3 = t2 * t; / t3 = t * t * t f1 = 2.0*t3 - 3.0*t2 + 1.0; f2 = -2
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 粉刷墻壁安全責(zé)任書合同
- 二零二五年航空運(yùn)輸預(yù)付款合同范本3篇
- 2025年國際貿(mào)易服務(wù)協(xié)議(SPA)合同模板下載
- 2025年度文化藝術(shù)院團(tuán)簡易聘用演員聘用合同樣本
- 2025年度科技園區(qū)土地出讓居間合同服務(wù)合同
- 2025年度公共安全視頻監(jiān)控設(shè)備租賃合同
- 2025年度家具出口退稅申請合同范本
- 2025年度城市更新項目整體改造合同
- 2025年度智慧醫(yī)院病房監(jiān)控系統(tǒng)建設(shè)合同
- 2025年度苗木種植與生態(tài)環(huán)境保護(hù)合同范本
- 2025年度化妝品電商平臺流量互換銷售合作合同
- 學(xué)習(xí)解讀2025年印發(fā)《教育強(qiáng)國建設(shè)規(guī)劃綱要(2024-2035年)》課件
- 全過程造價咨詢服務(wù)的質(zhì)量、進(jìn)度、保密等保證措施
- 縣城屠宰場建設(shè)可行性研究報告
- 25學(xué)年六年級數(shù)學(xué)寒假作業(yè)《每日一練》
- 2025高考數(shù)學(xué)一輪復(fù)習(xí)-第8章-第3節(jié) 圓的方程【課件】
- 環(huán)保行業(yè)深度研究報告
- 保障性住房補(bǔ)貼委托書范本
- 公益捐助活動影響力評估方法
- 國家電網(wǎng)安全培訓(xùn)
- 2025年中國陪診服務(wù)行業(yè)現(xiàn)狀、發(fā)展環(huán)境及投資前景分析報告
評論
0/150
提交評論