1 /* $Id: gears.c,v 1.6 2000/04/06 02:22:59 brianp Exp $ */
4 * 3-D gear wheels. This program is in the public domain.
6 * Command line options:
7 * -info print GL implementation information
8 * -exit automatically exit after 30 seconds
14 /* Conversion to GLUT by Mark J. Kilgard */
25 #define M_PI 3.14159265
29 static GLint Frames
= 0;
30 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 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
173 glRotatef(view_rotx
, 1.0, 0.0, 0.0);
174 glRotatef(view_roty
, 0.0, 1.0, 0.0);
175 glRotatef(view_rotz
, 0.0, 0.0, 1.0);
178 glTranslatef(-3.0, -2.0, 0.0);
179 glRotatef(angle
, 0.0, 0.0, 1.0);
184 glTranslatef(3.1, -2.0, 0.0);
185 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
190 glTranslatef(-3.1, 4.2, 0.0);
191 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
202 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
203 if (t
- T0
>= 5000) {
204 GLfloat seconds
= (t
- T0
) / 1000.0;
205 GLfloat fps
= Frames
/ seconds
;
206 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
209 if ((t
>= 999.0 * autoexit
) && (autoexit
))
223 /* change view angle, exit upon ESC */
226 key(unsigned char k
, int x
, int y
)
235 case 27: /* Escape */
244 /* change view angle */
247 special(int k
, int x
, int y
)
268 /* new window size or exposure */
270 reshape(int width
, int height
)
272 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
274 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
275 glMatrixMode(GL_PROJECTION
);
277 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
278 glMatrixMode(GL_MODELVIEW
);
280 glTranslatef(0.0, 0.0, -40.0);
284 init(int argc
, char *argv
[])
286 static GLfloat pos
[4] = {5.0, 5.0, 10.0, 0.0};
287 static GLfloat red
[4] = {0.8, 0.1, 0.0, 1.0};
288 static GLfloat green
[4] = {0.0, 0.8, 0.2, 1.0};
289 static GLfloat blue
[4] = {0.2, 0.2, 1.0, 1.0};
292 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
293 glEnable(GL_CULL_FACE
);
294 glEnable(GL_LIGHTING
);
296 glEnable(GL_DEPTH_TEST
);
299 gear1
= glGenLists(1);
300 glNewList(gear1
, GL_COMPILE
);
301 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
302 gear(1.0, 4.0, 1.0, 20, 0.7);
305 gear2
= glGenLists(1);
306 glNewList(gear2
, GL_COMPILE
);
307 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
308 gear(0.5, 2.0, 2.0, 10, 0.7);
311 gear3
= glGenLists(1);
312 glNewList(gear3
, GL_COMPILE
);
313 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
314 gear(1.3, 2.0, 0.5, 10, 0.7);
317 glEnable(GL_NORMALIZE
);
319 for ( i
=1; i
<argc
; i
++ ) {
320 if (strcmp(argv
[i
], "-info")==0) {
321 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
322 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
323 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
324 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
326 else if ( strcmp(argv
[i
], "-exit")==0) {
328 printf("Auto Exit after %i seconds.\n", autoexit
);
336 if (vis
== GLUT_VISIBLE
)
342 int main(int argc
, char *argv
[])
344 glutInit(&argc
, argv
);
345 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
347 glutInitWindowPosition(0, 0);
348 glutInitWindowSize(300, 300);
349 glutCreateWindow("Gears");
352 glutDisplayFunc(draw
);
353 glutReshapeFunc(reshape
);
354 glutKeyboardFunc(key
);
355 glutSpecialFunc(special
);
356 glutVisibilityFunc(visible
);
359 return 0; /* ANSI C requires main to return int. */