事件處理函式_第1頁
事件處理函式_第2頁
事件處理函式_第3頁
事件處理函式_第4頁
事件處理函式_第5頁
已閱讀5頁,還剩16頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論