EVIL STICK MAN ENTERPRISES

Heya everyone. In keeping with my promises of the prior month, I thought I’d post a quick article in my summer series. These will essentially be short blurbs on different functions in Glut and OpenGL. This article will cover using the mouse functions built into the Glut system.

Of Mice and Men

Using the mouse with an OpenGL program, as you’ve probably guessed by now, is essentially just a matter of registering another callback with the system. Here’s the call to register the function with Glut:

 glutMouseFunc(myMouse);

and the general prototype for the mouse function is:

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

Every time a mouse button is used, this function is called. It is passed the button pressed (GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON, GLUT_MIDDLE_BUTTON - note that for systems with only two buttons, the GLUT_MIDDLE_BUTTON cannot be generated, and I don’t know the effects of the one button (MAC) systems, as I am a computer bigot :-P), the type of action (GLUT_DOWN, GLUT_UP), and the (x,y) position in screen coordinates. For every GLUT_DOWN call, one can assume a GLUT_UP call, provided that the callback is still registered. If a menu is registered to a button (this will be covered in a future article), then that button will not call the callback function. To remove all callbacks, make a call to glutMouseFunc(NULL);

To exemplify the use of this function ,I’ll just include a generic mouse function from the first graphics assignment I ever did, and let you work it for yourself. I’ll integrate it with a program come the return of techtrax. Below is the sample function:

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

 int ry=480-y;

 if (button!=GLUT_LEFT_BUTTON && button!=GLUT_RIGHT_BUTTON)

 {

  return;

 }

 if(state==GLUT_DOWN && button==GLUT_LEFT_BUTTON){

  glBegin(GL_POINTS);

   glColor3f(1.0, 0.0, 0.0); //change color, then draw a point

   glVertex2i(x,ry);

  glEnd();

 }

 if(state==GLUT_DOWN && button==GLUT_RIGHT_BUTTON){

  glBegin(GL_POINTS);

   glColor3f(0.0, 0.0, 1.0); //change color, then draw a point

   glVertex2i(x,ry);

  glEnd();

 }

 glFlush();

}

Note how in order to convert the screen coordinates I subtract the passed balue from 480 (the window height for that program). This function only acts when the left or right buttons are pressed, and only on the down press. Each mouse click creates a red or blue point on the screen at the location of the click, based on the mouse button pressed. The middle button is ignored.

One more quick note - the command glFlush() is used instead of glPostRedisplay() because this program was written using only a single buffer. In a single-buffered system, the screen is updated in one fell swoop with a glFlush() call, no glPostRedisplay()/glSwapBuffers() involved.

And that’s all for now. As always, if you have any questions, drop me a line at evilstickman@evilstickman.com

Copyright (c) 2004 by Matthew Raymond Billock. All images and content on this website are original creations by Matthew Raymond Billock. Use of logos, images, or the Evil Stick Man Enterprises name without express written permission is prohibited