GLUT
Tutorial
MouseIn the previous sections, we saw how to use the keyboard function of GLUT to increase the interaction of an OpenGL program. Now it's time to study the mouse. The glut mouse interface provides some column options to increase the interaction of the mouse. That is, to detect mouse clicks and move the mouse.
Detect mouse
ClicksLike keyboard processing, Glut provides a method for registering a function (that is, a function for processing the mouse clicks event. This function is called during program initialization. The function is prototype: void glumousefunc (void (* func) (INT button, int state, int X, int y); parameter: FUNC: the name of the function that processes the mouse click event. From the above we can see that the function for processing the mouse click event must have four parameters. The first parameter indicates which mouse key is pressed or released. This variable can be one of the following three values, that is, it is pressed or released. The possible value is as follows, even move the mouse outside the window. However, if the program calls glumousefunc to pass null as the parameter, the glut will not change the mouse state. The remaining two parameters (x, y) provide the current mouse window coordinates (starting from the upper left corner ).
Detection action (
Motion
)GLUT supports mouse motion detection. There are two types of GLUT processing motion: active motion and passive motion. Active motion refers to moving the mouse and a mouse key is pressed. Passive motion means that when the mouse moves, there is no mouse button to press. If a program is tracking the mouse, none of the frames will produce a result during the mouse movement. As before, you must register a function (defined function) that will handle mouse events ). GLUT allows us to specify two different functions, one tracing passive motion and the other tracing active motion their function prototype, as shown below: void glumotionfunc (void (* func) (int x, int y); void glupassivemotionfunc (void (* func) (int x, int y); parameter: FUNC: name of the function that processes the motion of each type. The parameter (x, y) of the Parameter Function for processing motion is the coordinate of the mouse in the window. Starting from the upper left corner.
Detect mouse access or exit windowGLUT can also detect the mouse and move to the window area. A callback function can be defined to process these two events. In glut, the function is called with the following prototype: void gluentryfunc (void (* func) (INT State); parameter: FUNC: name of the function that processes these events. In the above function parameters, the State has two values: Maid indicates whether to exit or enter the window.
Put them togetherFirst, we need to define in glut which functions will be responsible for processing mouse events. Therefore, we will rewrite our main function to include all required callback registration functions. We will describe other things that are not clearly stated in the program.
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. Now it's interesting. We will define the callback functions that will do some incredible events. When both the mouse key and the Alt key are pressed, we change the triangle color. The left mouse button turns the triangle red, the middle triangle turns green, and the right mouse button turns the triangle into blue. The function is as follows:
void processMouse(int button, int state, int x, int y) {
specialKey = glutGetModifiers();
//
When both the mouse key and the Alt key are pressed
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;
}
}
}
Next we will have a fine color pick-up method. When a mouse key is pressed, but the Alt key is pressed. We set blue to 0.0, and let the values of the Red and Green components depend on the position of the mouse in the window .. The function is as follows:
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;
}
}
Next we will add some actions to passive motion. When the Shift key is pressed, the mouse rotates on the X axis. We have to modify the renderscene function. The function is as follows:
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();
}
Now we have a function to process passive motion events. The function will change the value of 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;
}
}
The mouse leaves the window to stop the animation. To do this, we also need to change the renderscene function.
// 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 is the last function. Note that this may not work very well in the Microsoft operating system.
void processMouseEntry(int state) {
if (state == GLUT_LEFT)
deltaAngle = 0.0;
else
deltaAngle = 1.0;
}
The vc6.0project can be downloaded here (glu8.zip ).
(Now we have finished controlling the position, keyboard, and mouse, and the menu is displayed below .)
Http://www.lighthouse3d.com/opengl/glut/index.php? 9)