乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      OpenGL---GLUT教程(九) GLUT鼠標

       昵稱2265740 2011-01-06
      OpenGL---GLUT教程(九) GLUT鼠標

      在前幾節(jié),我們看了怎么使用GLUT的keyboard函數(shù),來增加一個OpenGL程序的交互性?,F(xiàn)在,是時候研究下鼠標了。GLUT的鼠標接口提供一些列的選項來增加鼠標的交互性。也就是檢測鼠標單擊,和鼠標移動。
       
      檢測鼠標Clicks
      和鍵盤處理一樣,GLUT為你的注冊函數(shù)(也就是處理鼠標clicks事件的函數(shù))提供了一個方法。函數(shù)glutMouseFunc,這個函數(shù)一般在程序初始化階段被調(diào)用。函數(shù)原型如下:
      void glutMouseFunc(void(*func)(int button,int state,int x,int y));
      參數(shù):
      func:處理鼠標click事件的函數(shù)的函數(shù)名。
      從上面可以看到到,處理鼠標click事件的函數(shù),一定有4個參數(shù)。

      第一個參數(shù)表明哪個鼠標鍵被按下或松開,這個變量可以是下面的三個值中的一個:
      GLUT_LEFT_BUTTON
      GLUT_MIDDLE_BUTTON
      GLUT_RIGHT_BUTTON

      第二個參數(shù)表明,函數(shù)被調(diào)用發(fā)生時,鼠標的狀態(tài),也就是是被按下,或松開,可能取值如下:
      GLUT_DOWN
      GLUT_UP

      當函數(shù)被調(diào)用時,state的值是GLUT_DOWN,那么程序可能會假定將會有個GLUT_UP事件,甚至鼠標移動到窗口外面,也如此。然而,如果程序調(diào)用glutMouseFunc傳遞NULL作為參數(shù),那么GLUT將不會改變鼠標的狀態(tài)。
       
      剩下的兩個參數(shù)(x,y)提供了鼠標當前的窗口坐標(以左上角為原點)。
       
      檢測動作(motion)
      GLUT提供鼠標motion檢測能力。有兩種GLUT處理的motion:active motion和passive motion。

      Active motion是指鼠標移動并且有一個鼠標鍵被按下。(拖動鼠標)

      Passive motion是指當鼠標移動時,并有沒鼠標鍵按下。(移動鼠標)

      如果一個程序正在追蹤鼠標,那么鼠標移動期間,沒一幀將產(chǎn)生一個結(jié)果。
       
      和以前一樣,你必須注冊將處理鼠標事件的函數(shù)(定義函數(shù))。GLUT讓我們可以指定兩個不同的函數(shù),一個追蹤passive motion,另一個追蹤active motion
       
      它們的函數(shù)原型,如下:
      void glutMotionFunc(void(*func)(int x,int y));
      void glutPassiveMotionFunc(void (*func)(int x,int y));
      參數(shù):
      Func:處理各自類型motion的函數(shù)名。
      處理motion的參數(shù)函數(shù)的參數(shù)(x,y)是鼠標在窗口的坐標。以左上角為原點。
       
      檢測鼠標進入或離開窗口
      GLUT還能檢測鼠標鼠標離開,進入窗口區(qū)域。一個回調(diào)函數(shù)可以被定義去處理這兩個事件。GLUT里,調(diào)用這個函數(shù)的是glutEntryFunc,函數(shù)原型如下:
      void glutEntryFunc(void(*func)(int state));
      參數(shù):
      Func:處理這些事件的函數(shù)名。
      上面函數(shù)的參數(shù)中,state有兩個值:
      GLUT_LEFT    鼠標離開窗口
      GLUT_ENTERED  鼠標進入窗口

      表明,是離開,還是進入窗口。
       




      把它們放一起
      首先我們要做的是在GLUT里定義哪些函數(shù)將負責處理鼠標事件。因此我們將重寫我們的main函數(shù),讓它包含所有必須的回調(diào)注冊函數(shù)。我們將在程序里描述其他一些教程里沒說清楚的地方。
      void main(int argc, char **argv) {         glutInit(&argc, argv);         glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);         glutInitWindowPosition(100,100);         glutInitWindowSize(320,320);         glutCreateWindow("SnowMen");         glutDisplayFunc(renderScene);         glutIdleFunc(renderScene);         glutReshapeFunc(changeSize);          //adding here the mouse processing callbacks         glutMouseFunc(processMouse);         glutMotionFunc(processMouseActiveMotion);         glutPassiveMotionFunc(processMousePassiveMotion);         glutEntryFunc(processMouseEntry);                  glutMainLoop();}OK,現(xiàn)在做點有趣的。我們將定義那些將做一些不可思議事件的回調(diào)函數(shù)。當一個鼠標鍵和alt鍵都被按下,我們將改變?nèi)切蔚念伾?。鼠標左鍵使三角形變成紅色,中間的將三角形變成綠色,鼠標右鍵將三角形變成藍色。函數(shù)如下:
      void processMouse(int button, int state, int x, int y) {           specialKey = glutGetModifiers();         // 當鼠標鍵和alt鍵都被按下         if ((state == GLUT_DOWN) &&                           (specialKey == GLUT_ACTIVE_ALT)) {                  // set the color to pure red for the left button                 if (button == GLUT_LEFT_BUTTON) {                          red = 1.0; green = 0.0; blue = 0.0;                 }                 // set the color to pure green for the middle button                 else if (button == GLUT_MIDDLE_BUTTON) {                          red = 0.0; green = 1.0; blue = 0.0;                 }                 // set the color to pure blue for the right button                 else {                          red = 0.0; green = 0.0; blue = 1.0;                 }         }}接下來有一個精細的顏色拾取方法。當一個鼠標鍵被按下,但alt鍵被被按下。我們把blue設(shè)為0.0,并且讓red和green分量的值取決于鼠標在窗口中的位置。。函數(shù)如下:
      void processMouseActiveMotion(int x, int y) {          // the ALT key was used in the previous function         if (specialKey != GLUT_ACTIVE_ALT) {                 // setting red to be relative to the mouse                  // position inside the window                 if (x < 0)                          red = 0.0;                 else if (x > width)                          red = 1.0;                 else                          red = ((float) x)/height;                 // setting green to be relative to the mouse                  // position inside the window                 if (y < 0)                          green = 0.0;                 else if (y > width)                          green = 1.0;                 else                          green = ((float) y)/height;                 // removing the blue component.                 blue = 0.0;         }}下面給passive motion添加一些動作。當shift鍵被按下,鼠標將在x軸上有一個旋轉(zhuǎn)。我們不得不修改renderScene函數(shù)。函數(shù)如下:
      float angleX = 0.0;...void renderScene(void) {         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         glPushMatrix();         glRotatef(angle,0.0,1.0,0.0);                  // This is the line we added for the         // rotation on the X axis;         glRotatef(angleX,1.0,0.0,0.0);                  glColor3f(red,green,blue);          glBegin(GL_TRIANGLES);                 glVertex3f(-0.5,-0.5,0.0);                 glVertex3f(0.5,0.0,0.0);                 glVertex3f(0.0,0.5,0.0);         glEnd();         glPopMatrix();         angle++;         glutSwapBuffers();}現(xiàn)在我們的有個函數(shù)處理passive motion事件。函數(shù)將改變angleX的值。void processMousePassiveMotion(int x, int y) {          // User must press the SHIFT key to change the          // rotation in the X axis         if (specialKey != GLUT_ACTIVE_SHIFT) {                  // setting the angle to be relative to the mouse                  // position inside the window                 if (x < 0)                          angleX = 0.0;                 else if (x > width)                          angleX = 180.0;                 else                          angleX = 180.0 * ((float) x)/height;         }}最后鼠標離開窗口將使動畫停止,為了做到這,我們也需要改變函數(shù)renderScene。// initially define the increase of the angle by 1.0;float deltaAngle = 1.0;...void renderScene(void) {         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         glPushMatrix();         glRotatef(angle,0.0,1.0,0.0);         glRotatef(angleX,1.0,0.0,0.0);         glColor3f(red,green,blue);          glBegin(GL_TRIANGLES);                 glVertex3f(-0.5,-0.5,0.0);                 glVertex3f(0.5,0.0,0.0);                 glVertex3f(0.0,0.5,0.0);         glEnd();         glPopMatrix();         // this is the new line         // previously it was: angle++;         angle+=deltaAngle;         glutSwapBuffers();}processMouseEntry是最后一個函數(shù)。注意,這個在微軟操作系統(tǒng)下可能工作的不是很好。void processMouseEntry(int state) {         if (state == GLUT_LEFT)                 deltaAngle = 0.0;         else                 deltaAngle = 1.0;}VC6.0工程可以在這里下載(glut8.zip)。 (到這里位置,鍵盤,鼠標方面的控制講完了,下面就是菜單了。)(原文地址:
      http://www./opengl/glut/index.php?9

       

      本文來自CSDN博客,轉(zhuǎn)載請標明出處:http://blog.csdn.net/xie_zi/archive/2007/12/09/1925778.aspx

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多