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 */
23 #define M_PI 3.14159265
27 static GLint Frames
= 0;
28 static GLint autoexit
= 0;
34 Draw a gear wheel. You'll probably want to call this function when
35 building a display list since we do a lot of trig here.
37 Input: inner_radius - radius of hole at center
38 outer_radius - radius at center of teeth
40 teeth - number of teeth
41 tooth_depth - depth of tooth
46 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
47 GLint teeth
, GLfloat tooth_depth
)
55 r1
= outer_radius
- tooth_depth
/ 2.0;
56 r2
= outer_radius
+ tooth_depth
/ 2.0;
58 da
= 2.0 * M_PI
/ teeth
/ 4.0;
60 glShadeModel(GL_FLAT
);
62 glNormal3f(0.0, 0.0, 1.0);
65 glBegin(GL_QUAD_STRIP
);
66 for (i
= 0; i
<= teeth
; i
++) {
67 angle
= i
* 2.0 * M_PI
/ teeth
;
68 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
69 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
71 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
72 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
77 /* draw front sides of teeth */
79 da
= 2.0 * M_PI
/ teeth
/ 4.0;
80 for (i
= 0; i
< teeth
; i
++) {
81 angle
= i
* 2.0 * M_PI
/ teeth
;
83 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
84 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
85 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), width
* 0.5);
86 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
90 glNormal3f(0.0, 0.0, -1.0);
93 glBegin(GL_QUAD_STRIP
);
94 for (i
= 0; i
<= teeth
; i
++) {
95 angle
= i
* 2.0 * M_PI
/ teeth
;
96 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
97 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
99 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
100 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
105 /* draw back sides of teeth */
107 da
= 2.0 * M_PI
/ teeth
/ 4.0;
108 for (i
= 0; i
< teeth
; i
++) {
109 angle
= i
* 2.0 * M_PI
/ teeth
;
111 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
112 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), -width
* 0.5);
113 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
114 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
118 /* draw outward faces of teeth */
119 glBegin(GL_QUAD_STRIP
);
120 for (i
= 0; i
< teeth
; i
++) {
121 angle
= i
* 2.0 * M_PI
/ teeth
;
123 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
124 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
125 u
= r2
* cos(angle
+ da
) - r1
* cos(angle
);
126 v
= r2
* sin(angle
+ da
) - r1
* sin(angle
);
127 len
= sqrt(u
* u
+ v
* v
);
130 glNormal3f(v
, -u
, 0.0);
131 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
132 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
133 glNormal3f(cos(angle
), sin(angle
), 0.0);
134 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), width
* 0.5);
135 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), -width
* 0.5);
136 u
= r1
* cos(angle
+ 3 * da
) - r2
* cos(angle
+ 2 * da
);
137 v
= r1
* sin(angle
+ 3 * da
) - r2
* sin(angle
+ 2 * da
);
138 glNormal3f(v
, -u
, 0.0);
139 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
140 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
141 glNormal3f(cos(angle
), sin(angle
), 0.0);
144 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
145 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
149 glShadeModel(GL_SMOOTH
);
151 /* draw inside radius cylinder */
152 glBegin(GL_QUAD_STRIP
);
153 for (i
= 0; i
<= teeth
; i
++) {
154 angle
= i
* 2.0 * M_PI
/ teeth
;
155 glNormal3f(-cos(angle
), -sin(angle
), 0.0);
156 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
157 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
163 static GLfloat view_rotx
= 20.0, view_roty
= 30.0, view_rotz
= 0.0;
164 static GLint gear1
, gear2
, gear3
;
165 static GLfloat angle
= 0.0;
170 glDeleteLists(gear1
, 1);
171 glDeleteLists(gear2
, 1);
172 glDeleteLists(gear3
, 1);
173 glutDestroyWindow(win
);
179 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
182 glRotatef(view_rotx
, 1.0, 0.0, 0.0);
183 glRotatef(view_roty
, 0.0, 1.0, 0.0);
184 glRotatef(view_rotz
, 0.0, 0.0, 1.0);
187 glTranslatef(-3.0, -2.0, 0.0);
188 glRotatef(angle
, 0.0, 0.0, 1.0);
193 glTranslatef(3.1, -2.0, 0.0);
194 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
199 glTranslatef(-3.1, 4.2, 0.0);
200 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
211 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
212 if (t
- T0
>= 5000) {
213 GLfloat seconds
= (t
- T0
) / 1000.0;
214 GLfloat fps
= Frames
/ seconds
;
215 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
218 if ((t
>= 999.0 * autoexit
) && (autoexit
)) {
230 static double t0
= -1.;
231 double dt
, t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
237 angle
+= 70.0 * dt
; /* 70 degrees per second */
238 angle
= fmod(angle
, 360.0); /* prevents eventual overflow */
243 /* change view angle, exit upon ESC */
246 key(unsigned char k
, int x
, int y
)
255 case 27: /* Escape */
265 /* change view angle */
268 special(int k
, int x
, int y
)
289 /* new window size or exposure */
291 reshape(int width
, int height
)
293 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
295 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
296 glMatrixMode(GL_PROJECTION
);
298 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
299 glMatrixMode(GL_MODELVIEW
);
301 glTranslatef(0.0, 0.0, -40.0);
305 init(int argc
, char *argv
[])
307 static GLfloat pos
[4] = {5.0, 5.0, 10.0, 0.0};
308 static GLfloat red
[4] = {0.8, 0.1, 0.0, 1.0};
309 static GLfloat green
[4] = {0.0, 0.8, 0.2, 1.0};
310 static GLfloat blue
[4] = {0.2, 0.2, 1.0, 1.0};
313 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
314 glEnable(GL_CULL_FACE
);
315 glEnable(GL_LIGHTING
);
317 glEnable(GL_DEPTH_TEST
);
320 gear1
= glGenLists(1);
321 glNewList(gear1
, GL_COMPILE
);
322 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
323 gear(1.0, 4.0, 1.0, 20, 0.7);
326 gear2
= glGenLists(1);
327 glNewList(gear2
, GL_COMPILE
);
328 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
329 gear(0.5, 2.0, 2.0, 10, 0.7);
332 gear3
= glGenLists(1);
333 glNewList(gear3
, GL_COMPILE
);
334 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
335 gear(1.3, 2.0, 0.5, 10, 0.7);
338 glEnable(GL_NORMALIZE
);
340 for ( i
=1; i
<argc
; i
++ ) {
341 if (strcmp(argv
[i
], "-info")==0) {
342 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
343 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
344 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
345 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
347 else if ( strcmp(argv
[i
], "-exit")==0) {
349 printf("Auto Exit after %i seconds.\n", autoexit
);
357 if (vis
== GLUT_VISIBLE
)
363 int main(int argc
, char *argv
[])
365 glutInit(&argc
, argv
);
366 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
368 glutInitWindowPosition(0, 0);
369 glutInitWindowSize(300, 300);
370 win
= glutCreateWindow("Gears");
373 glutDisplayFunc(draw
);
374 glutReshapeFunc(reshape
);
375 glutKeyboardFunc(key
);
376 glutSpecialFunc(special
);
377 glutVisibilityFunc(visible
);
380 return 0; /* ANSI C requires main to return int. */