转自:http://blog.csdn.net/xie_zi/article/details/1925778
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();
}
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;
}
}
}
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;
}
}
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();
}
现在我们的有个函数处理passive motion事件。函数将改变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;
}
}
最后鼠标离开窗口将使动画停止,为了做到这,我们也需要改变函数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是最后一个函数。注意,这个在微软操作系统下可能工作的不是很好。
void processMouseEntry(int state) {
if (state == GLUT_LEFT)
deltaAngle = 0.0;
else
deltaAngle = 1.0;
}
VC6.0工程可以在这里下载(glut8.zip)。
(到这里位置,键盘,鼠标方面的控制讲完了,下面就是菜单了。)
(原文地址:http://www.lighthouse3d.com/opengl/glut/index.php?9)