Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get x position at start of motion and end using glutmotionfunc

Tags:

c++

opengl

glut

I want to use glutmotionfunc() to get the x position at the start of the dragging motion and the end i.e. so I can get the change in x. Is there a simple way of doing this?

like image 866
user1356791 Avatar asked Nov 30 '25 16:11

user1356791


1 Answers

I think glutmotionfunc() doesn't work the way you expect it to do.

The registered callback is not only called once, when the drag has finished. Instead it gets called every time the mouse gets moved, while the button is pushed.

The idea here is to be able to update the scene continiously for every little dragging motion.

To get the movement made between calls you have to store the values you got before. Oh, and don't forget to mark the stored values as invalid, if meanwhile the button was released. Otherwise you will get some funny results.

So here the general idea

int old_x=0;
int old_y=0;
int valid=0;

void mouse_func (int button, int state, int x, int y) {
  old_x=x; 
  old_y=y;
  valid = state == GLUT_DOWN;
}

void motion_func (int x, int y) {
  if (valid) {
    int dx = old_x - x;
    int dy = old_y - y;
    /* do something with dx and dy */
  }
  old_x = x;
  old_y = y;
}

Don't forget to connect both callbacks with glutMotionFunc and glutMouseFunc

like image 188
mikyra Avatar answered Dec 02 '25 05:12

mikyra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!