1 /* $Id: gears.c,v 1.3 1999/08/22 08:56:50 jtaylor Exp $ */
4 * 3-D gear wheels. This program is in the public domain.
9 /* Conversion to GLUT by Mark J. Kilgard */
17 #define M_PI 3.14159265
21 static GLint Frames
= 0;
26 Draw a gear wheel. You'll probably want to call this function when
27 building a display list since we do a lot of trig here.
29 Input: inner_radius - radius of hole at center
30 outer_radius - radius at center of teeth
32 teeth - number of teeth
33 tooth_depth - depth of tooth
38 gear(GLfloat inner_radius
, GLfloat outer_radius
, GLfloat width
,
39 GLint teeth
, GLfloat tooth_depth
)
47 r1
= outer_radius
- tooth_depth
/ 2.0;
48 r2
= outer_radius
+ tooth_depth
/ 2.0;
50 da
= 2.0 * M_PI
/ teeth
/ 4.0;
52 glShadeModel(GL_FLAT
);
54 glNormal3f(0.0, 0.0, 1.0);
57 glBegin(GL_QUAD_STRIP
);
58 for (i
= 0; i
<= teeth
; i
++) {
59 angle
= i
* 2.0 * M_PI
/ teeth
;
60 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
61 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
63 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
64 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
69 /* draw front sides of teeth */
71 da
= 2.0 * M_PI
/ teeth
/ 4.0;
72 for (i
= 0; i
< teeth
; i
++) {
73 angle
= i
* 2.0 * M_PI
/ teeth
;
75 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
76 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
77 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), width
* 0.5);
78 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
82 glNormal3f(0.0, 0.0, -1.0);
85 glBegin(GL_QUAD_STRIP
);
86 for (i
= 0; i
<= teeth
; i
++) {
87 angle
= i
* 2.0 * M_PI
/ teeth
;
88 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
89 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
91 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
92 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
97 /* draw back sides of teeth */
99 da
= 2.0 * M_PI
/ teeth
/ 4.0;
100 for (i
= 0; i
< teeth
; i
++) {
101 angle
= i
* 2.0 * M_PI
/ teeth
;
103 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
104 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), -width
* 0.5);
105 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
106 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
110 /* draw outward faces of teeth */
111 glBegin(GL_QUAD_STRIP
);
112 for (i
= 0; i
< teeth
; i
++) {
113 angle
= i
* 2.0 * M_PI
/ teeth
;
115 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), width
* 0.5);
116 glVertex3f(r1
* cos(angle
), r1
* sin(angle
), -width
* 0.5);
117 u
= r2
* cos(angle
+ da
) - r1
* cos(angle
);
118 v
= r2
* sin(angle
+ da
) - r1
* sin(angle
);
119 len
= sqrt(u
* u
+ v
* v
);
122 glNormal3f(v
, -u
, 0.0);
123 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), width
* 0.5);
124 glVertex3f(r2
* cos(angle
+ da
), r2
* sin(angle
+ da
), -width
* 0.5);
125 glNormal3f(cos(angle
), sin(angle
), 0.0);
126 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), width
* 0.5);
127 glVertex3f(r2
* cos(angle
+ 2 * da
), r2
* sin(angle
+ 2 * da
), -width
* 0.5);
128 u
= r1
* cos(angle
+ 3 * da
) - r2
* cos(angle
+ 2 * da
);
129 v
= r1
* sin(angle
+ 3 * da
) - r2
* sin(angle
+ 2 * da
);
130 glNormal3f(v
, -u
, 0.0);
131 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), width
* 0.5);
132 glVertex3f(r1
* cos(angle
+ 3 * da
), r1
* sin(angle
+ 3 * da
), -width
* 0.5);
133 glNormal3f(cos(angle
), sin(angle
), 0.0);
136 glVertex3f(r1
* cos(0), r1
* sin(0), width
* 0.5);
137 glVertex3f(r1
* cos(0), r1
* sin(0), -width
* 0.5);
141 glShadeModel(GL_SMOOTH
);
143 /* draw inside radius cylinder */
144 glBegin(GL_QUAD_STRIP
);
145 for (i
= 0; i
<= teeth
; i
++) {
146 angle
= i
* 2.0 * M_PI
/ teeth
;
147 glNormal3f(-cos(angle
), -sin(angle
), 0.0);
148 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), -width
* 0.5);
149 glVertex3f(r0
* cos(angle
), r0
* sin(angle
), width
* 0.5);
155 static GLfloat view_rotx
= 20.0, view_roty
= 30.0, view_rotz
= 0.0;
156 static GLint gear1
, gear2
, gear3
;
157 static GLfloat angle
= 0.0;
162 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
165 glRotatef(view_rotx
, 1.0, 0.0, 0.0);
166 glRotatef(view_roty
, 0.0, 1.0, 0.0);
167 glRotatef(view_rotz
, 0.0, 0.0, 1.0);
170 glTranslatef(-3.0, -2.0, 0.0);
171 glRotatef(angle
, 0.0, 0.0, 1.0);
176 glTranslatef(3.1, -2.0, 0.0);
177 glRotatef(-2.0 * angle
- 9.0, 0.0, 0.0, 1.0);
182 glTranslatef(-3.1, 4.2, 0.0);
183 glRotatef(-2.0 * angle
- 25.0, 0.0, 0.0, 1.0);
193 GLint t
= glutGet(GLUT_ELAPSED_TIME
);
194 if (t
- T0
>= 5000) {
195 GLfloat seconds
= (t
- T0
) / 1000.0;
196 GLfloat fps
= Frames
/ seconds
;
197 printf("%d frames in %g seconds = %g FPS\n", Frames
, seconds
, fps
);
212 /* change view angle, exit upon ESC */
215 key(unsigned char k
, int x
, int y
)
224 case 27: /* Escape */
225 exit(0); /* FIXME: Shutdown and free resources cleanly in ggiglut */
233 /* change view angle */
236 special(int k
, int x
, int y
)
257 /* new window size or exposure */
259 reshape(int width
, int height
)
261 GLfloat h
= (GLfloat
) height
/ (GLfloat
) width
;
263 glViewport(0, 0, (GLint
) width
, (GLint
) height
);
264 glMatrixMode(GL_PROJECTION
);
266 glFrustum(-1.0, 1.0, -h
, h
, 5.0, 60.0);
267 glMatrixMode(GL_MODELVIEW
);
269 glTranslatef(0.0, 0.0, -40.0);
275 static GLfloat pos
[4] =
276 {5.0, 5.0, 10.0, 0.0};
277 static GLfloat red
[4] =
278 {0.8, 0.1, 0.0, 1.0};
279 static GLfloat green
[4] =
280 {0.0, 0.8, 0.2, 1.0};
281 static GLfloat blue
[4] =
282 {0.2, 0.2, 1.0, 1.0};
284 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
285 glEnable(GL_CULL_FACE
);
286 glEnable(GL_LIGHTING
);
288 glEnable(GL_DEPTH_TEST
);
291 gear1
= glGenLists(1);
292 glNewList(gear1
, GL_COMPILE
);
293 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, red
);
294 gear(1.0, 4.0, 1.0, 20, 0.7);
297 gear2
= glGenLists(1);
298 glNewList(gear2
, GL_COMPILE
);
299 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, green
);
300 gear(0.5, 2.0, 2.0, 10, 0.7);
303 gear3
= glGenLists(1);
304 glNewList(gear3
, GL_COMPILE
);
305 glMaterialfv(GL_FRONT
, GL_AMBIENT_AND_DIFFUSE
, blue
);
306 gear(1.3, 2.0, 0.5, 10, 0.7);
309 glEnable(GL_NORMALIZE
);
315 if (vis
== GLUT_VISIBLE
)
321 int main(int argc
, char *argv
[])
323 glutInit(&argc
, argv
);
324 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
326 // glutInitWindowPosition(0, 0);
327 // glutInitWindowSize(300, 300);
328 glutCreateWindow("Gears");
331 glutDisplayFunc(draw
);
332 glutReshapeFunc(reshape
);
333 glutKeyboardFunc(key
);
334 glutSpecialFunc(special
);
335 glutVisibilityFunc(visible
);