2 * 3-D gear wheels. This program is in the public domain.
4 * Command line options:
5 * -info print GL implementation information
6 * -exit automatically exit after 30 seconds
12 /* Conversion to GLUT by Mark J. Kilgard */
20 #include "glut_wrap.h"
23 #define M_PI 3.14159265
27 static GLint Frames
= 0;
28 static GLint autoexit
= 0;
30 static GLboolean Visible
= GL_TRUE
;
31 static GLboolean Animate
= GL_TRUE
;
32 static GLfloat viewDist
= 40.0;
37 Draw a gear wheel. You'll probably want to call this function when
38 building a display list since we do a lot of trig here.
40 Input: inner_radius - radius of hole at center
41 outer_radius - radius at center of teeth
43 teeth - number of teeth
44 tooth_depth - depth of tooth
49 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
50 GLint teeth
, GLfloat tooth_depth
)
58 r1
= outer_radius
- tooth_depth
/ 2.0;
59 r2
= outer_radius
+ tooth_depth
/ 2.0;
61 da
= 2.0 * M_PI
/ teeth
/ 4.0;
63 glShadeModel(GL_FLAT
);
65 glNormal3f(0.0, 0.0, 1.0);
68 glBegin(GL_QUAD_STRIP
);
69 for (i
= 0; i
<= teeth
; i
++) {
70 angle
= i
* 2.0 * M_PI
/ teeth
;
71 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
72 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
74 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
75 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
80 /* draw front sides of teeth */
82 da
= 2.0 * M_PI
/ teeth
/ 4.0;
83 for (i
= 0; i
< teeth
; i
++) {
84 angle
= i
* 2.0 * M_PI
/ teeth
;
86 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
87 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
88 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), width
* 0.5);
89 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
93 glNormal3f(0.0, 0.0, -1.0);
96 glBegin(GL_QUAD_STRIP
);
97 for (i
= 0; i
<= teeth
; i
++) {
98 angle
= i
* 2.0 * M_PI
/ teeth
;
99 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
100 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
102 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
103 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
108 /* draw back sides of teeth */
110 da
= 2.0 * M_PI
/ teeth
/ 4.0;
111 for (i
= 0; i
< teeth
; i
++) {
112 angle
= i
* 2.0 * M_PI
/ teeth
;
114 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
115 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), -width
* 0.5);
116 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
117 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
121 /* draw outward faces of teeth */
122 glBegin(GL_QUAD_STRIP
);
123 for (i
= 0; i
< teeth
; i
++) {
124 angle
= i
* 2.0 * M_PI
/ teeth
;
126 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
127 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
128 u
= r2
* cos(angle
+ da
) - r1
* cos(angle
);
129 v
= r2
* sin(angle
+ da
) - r1
* sin(angle
);
130 len
= sqrt(u
* u
+ v
* v
);
133 glNormal3f(v
, -u
, 0.0);
134 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
135 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
136 glNormal3f(cos(angle
), sin(angle
), 0.0);
137 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), width
* 0.5);
138 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), -width
* 0.5);
139 u
= r1
* cos(angle
+ 3 * da
) - r2
* cos(angle
+ 2 * da
);
140 v
= r1
* sin(angle
+ 3 * da
) - r2
* sin(angle
+ 2 * da
);
141 glNormal3f(v
, -u
, 0.0);
142 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
143 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
144 glNormal3f(cos(angle
), sin(angle
), 0.0);
147 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
148 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
152 glShadeModel(GL_SMOOTH
);
154 /* draw inside radius cylinder */
155 glBegin(GL_QUAD_STRIP
);
156 for (i
= 0; i
<= teeth
; i
++) {
157 angle
= i
* 2.0 * M_PI
/ teeth
;
158 glNormal3f(-cos(angle
), -sin(angle
), 0.0);
159 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
160 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
166 static GLfloat view_rotx
= 20.0, view_roty
= 30.0, view_rotz
= 0.0;
167 static GLint gear1
, gear2
, gear3
;
168 static GLfloat angle
= 0.0;
173 glDeleteLists(gear1
, 1);
174 glDeleteLists(gear2
, 1);
175 glDeleteLists(gear3
, 1);
176 glutDestroyWindow(win
);
182 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
186 glTranslatef(0.0, 0.0, -viewDist
);
188 glRotatef(view_rotx
, 1.0, 0.0, 0.0);
189 glRotatef(view_roty
, 0.0, 1.0, 0.0);
190 glRotatef(view_rotz
, 0.0, 0.0, 1.0);
193 glTranslatef(-3.0, -2.0, 0.0);
194 glRotatef(angle
, 0.0, 0.0, 1.0);
199 glTranslatef(3.1, -2.0, 0.0);
200 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
205 glTranslatef(-3.1, 4.2, 0.0);
206 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
217 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
218 if (t
- T0
>= 5000) {
219 GLfloat seconds
= (t
- T0
) / 1000.0;
220 GLfloat fps
= Frames
/ seconds
;
221 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
225 if ((t
>= 999.0 * autoexit
) && (autoexit
)) {
237 static double t0
= -1.;
238 double dt
, t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
244 angle
+= 70.0 * dt
; /* 70 degrees per second */
245 angle
= fmod(angle
, 360.0); /* prevents eventual overflow */
251 update_idle_func(void)
253 if (Visible
&& Animate
)
259 /* change view angle, exit upon ESC */
262 key(unsigned char k
, int x
, int y
)
281 case 27: /* Escape */
291 /* change view angle */
294 special(int k
, int x
, int y
)
315 /* new window size or exposure */
317 reshape(int width
, int height
)
319 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
321 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
322 glMatrixMode(GL_PROJECTION
);
324 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 200.0);
325 glMatrixMode(GL_MODELVIEW
);
329 init(int argc
, char *argv
[])
331 static GLfloat pos
[4] = {5.0, 5.0, 10.0, 0.0};
332 static GLfloat red
[4] = {0.8, 0.1, 0.0, 1.0};
333 static GLfloat green
[4] = {0.0, 0.8, 0.2, 1.0};
334 static GLfloat blue
[4] = {0.2, 0.2, 1.0, 1.0};
337 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
338 glEnable(GL_CULL_FACE
);
339 glEnable(GL_LIGHTING
);
341 glEnable(GL_DEPTH_TEST
);
344 gear1
= glGenLists(1);
345 glNewList(gear1
, GL_COMPILE
);
346 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
347 gear(1.0, 4.0, 1.0, 20, 0.7);
350 gear2
= glGenLists(1);
351 glNewList(gear2
, GL_COMPILE
);
352 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
353 gear(0.5, 2.0, 2.0, 10, 0.7);
356 gear3
= glGenLists(1);
357 glNewList(gear3
, GL_COMPILE
);
358 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
359 gear(1.3, 2.0, 0.5, 10, 0.7);
362 glEnable(GL_NORMALIZE
);
364 for ( i
=1; i
<argc
; i
++ ) {
365 if (strcmp(argv
[i
], "-info")==0) {
366 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
367 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
368 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
369 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
371 else if ( strcmp(argv
[i
], "-exit")==0) {
373 printf("Auto Exit after %i seconds.\n", autoexit
);
386 int main(int argc
, char *argv
[])
388 glutInitWindowSize(300, 300);
389 glutInit(&argc
, argv
);
390 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
391 win
= glutCreateWindow("Gears");
394 glutDisplayFunc(draw
);
395 glutReshapeFunc(reshape
);
396 glutKeyboardFunc(key
);
397 glutSpecialFunc(special
);
398 glutVisibilityFunc(visible
);
402 return 0; /* ANSI C requires main to return int. */