-
/*
* 3-D gear wheels. This program is in the public domain.
*
static GLint T0 = 0;
static GLint Frames = 0;
static GLint autoexit = 0;
+static GLint win = 0;
+static GLboolean Visible = GL_TRUE;
+static GLboolean Animate = GL_TRUE;
+static GLfloat viewDist = 40.0;
+
/**
static GLint gear1, gear2, gear3;
static GLfloat angle = 0.0;
+static void
+cleanup(void)
+{
+ glDeleteLists(gear1, 1);
+ glDeleteLists(gear2, 1);
+ glDeleteLists(gear3, 1);
+ glutDestroyWindow(win);
+}
+
static void
draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
+
+ glTranslatef(0.0, 0.0, -viewDist);
+
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
Frames++;
{
- GLint t = glutGet(GLUT_ELAPSED_TIME);
- if (t - T0 >= 5000) {
- GLfloat seconds = (t - T0) / 1000.0;
- GLfloat fps = Frames / seconds;
- printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
- T0 = t;
- Frames = 0;
- if ((t >= 999.0 * autoexit) && (autoexit))
- exit(0);
- }
+ GLint t = glutGet(GLUT_ELAPSED_TIME);
+ if (t - T0 >= 5000) {
+ GLfloat seconds = (t - T0) / 1000.0;
+ GLfloat fps = Frames / seconds;
+ printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
+ T0 = t;
+ Frames = 0;
+ if ((t >= 999.0 * autoexit) && (autoexit)) {
+ cleanup();
+ exit(0);
+ }
+ }
}
}
static void
idle(void)
{
- angle += 2.0;
+ static double t0 = -1.;
+ double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
+ if (t0 < 0.0)
+ t0 = t;
+ dt = t - t0;
+ t0 = t;
+
+ angle += 70.0 * dt; /* 70 degrees per second */
+ angle = fmod(angle, 360.0); /* prevents eventual overflow */
+
glutPostRedisplay();
}
+static void
+update_idle_func(void)
+{
+ if (Visible && Animate)
+ glutIdleFunc(idle);
+ else
+ glutIdleFunc(NULL);
+}
+
/* change view angle, exit upon ESC */
/* ARGSUSED1 */
static void
case 'Z':
view_rotz -= 5.0;
break;
+ case 'd':
+ viewDist += 1.0;
+ break;
+ case 'D':
+ viewDist -= 1.0;
+ break;
+ case 'a':
+ Animate = !Animate;
+ update_idle_func();
+ break;
case 27: /* Escape */
+ cleanup();
exit(0);
break;
default:
glLoadIdentity();
glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);
glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.0, 0.0, -40.0);
}
static void
}
}
+
static void
visible(int vis)
{
- if (vis == GLUT_VISIBLE)
- glutIdleFunc(idle);
- else
- glutIdleFunc(NULL);
+ Visible = vis;
+ update_idle_func();
}
int main(int argc, char *argv[])
glutInitWindowPosition(0, 0);
glutInitWindowSize(300, 300);
- glutCreateWindow("Gears");
+ win = glutCreateWindow("Gears");
init(argc, argv);
glutDisplayFunc(draw);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutVisibilityFunc(visible);
+ update_idle_func();
glutMainLoop();
return 0; /* ANSI C requires main to return int. */