开发者

GLUT mouse position not updating

I'm rotating my camera depending on mouse position. But I want this active only when either left or right mouse button is down. The problem with this code is that I have to release and press again for the program to notice that I've moved the mouse.

When using the keyboard keys and moving the mouse it worked.

Trying to glutPostRedisplay but I'm not sure if it is what I need or how to use it.

void processMouse(int button, int state, int x, int y) {


开发者_如何学运维 if (state == GLUT_DOWN)  {

  if (button == GLUT_LEFT_BUTTON) {mouseM=true;}   if (button == GLUT_RIGHT_BUTTON) {mouseN=true;}
    }  if (state == GLUT_UP){   if (button == GLUT_LEFT_BUTTON){mouseM=false;}   if (button == GLUT_RIGHT_BUTTON) {mouseN=false;}  }

}


void mouseMove(int x, int y){
    if (x < 0)    angleX = 0.0;   else if (x > w)    angleX = 180.0;   else   //angleX = 5.0 * ((float) x)/w;    angleX = (x-320)/50;    angleZ = angleX;    angleY= (y-240)/50;  
}


You can combine glutMouseFunc, glutMotionFunc and glutPassiveMotionFunc to achieve it.

(1)glutMotionFunc tells you (x, y) of your cursor at any moment only when button(s) of your mouse is(are) pressed. On the other hand, glutPassiveMotionFunc tells you (x, y) when no button(s) is(are) pressed. (Check the glut specification for more details).

(2) To combine these functions

First, prepare onLeftButton(int x, int y) and onRightButton(int x, int y) to handle left-button-pressed and right-button-pressed events respectively, like this:

void onLeftButton(int x, int y){
   //change variables for your glRotatef function for example
   //(x, y) is the current coordinate and 
   //(preMouseX, preMouseY) is the previous coordinate of your cursor.
   //and axisX is the degree for rotation along x axis. Similar as axisY.
   axisX += (y - preMouseY);
   axisY += (x - preMouseX);
   ...
}

void onRightButton(int x, int y){
   //do something you want...
}

Second, prepare a function for glutMouseFunc, say onMouse, for example:

glutMouseFunc(onMouse);

And within the onMouse function, it'll be like this:

void onMouse(int button, int state, int x, int y)
{
   if(state == GLUT_DOWN){
      if(button == GLUT_RIGHT_BUTTON)
         glutMotionFunc(onRightButton);
      else if(button == GLUT_LEFT_BUTTON)
         glutMotionFunc(onLeftButton);
   }
}

After doing those things, you can get (x, y) of your cursor at any moment only when the left/right button is pressed and held.

For more information about how to combine those functions, you may check the 3.030 part at this site


I think you need to combine your glutMouseFunc with a glutMotionFunc. Set your mouse button state in the former and update your rotation in glutMotionFunc depending on the button state.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜