• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 3-D gear wheels.  This program is in the public domain.
3  *
4  * Command line options:
5  *    -info      print GL implementation information
6  *    -exit      automatically exit after 30 seconds
7  *
8  *
9  * Brian Paul
10  *
11  *
12  * Marcus Geelnard:
13  *   - Conversion to GLFW
14  *   - Time based rendering (frame rate independent)
15  *   - Slightly modified camera that should work better for stereo viewing
16  *
17  *
18  * Camilla Berglund:
19  *   - Removed FPS counter (this is not a benchmark)
20  *   - Added a few comments
21  *   - Enabled vsync
22  */
23 
24 #if defined(_MSC_VER)
25  // Make MS math.h define M_PI
26  #define _USE_MATH_DEFINES
27 #endif
28 
29 #include <math.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include <glad/glad.h>
35 #include <GLFW/glfw3.h>
36 
37 /**
38 
39   Draw a gear wheel.  You'll probably want to call this function when
40   building a display list since we do a lot of trig here.
41 
42   Input:  inner_radius - radius of hole at center
43           outer_radius - radius at center of teeth
44           width - width of gear teeth - number of teeth
45           tooth_depth - depth of tooth
46 
47  **/
48 
49 static void
gear(GLfloat inner_radius,GLfloat outer_radius,GLfloat width,GLint teeth,GLfloat tooth_depth)50 gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
51   GLint teeth, GLfloat tooth_depth)
52 {
53   GLint i;
54   GLfloat r0, r1, r2;
55   GLfloat angle, da;
56   GLfloat u, v, len;
57 
58   r0 = inner_radius;
59   r1 = outer_radius - tooth_depth / 2.f;
60   r2 = outer_radius + tooth_depth / 2.f;
61 
62   da = 2.f * (float) M_PI / teeth / 4.f;
63 
64   glShadeModel(GL_FLAT);
65 
66   glNormal3f(0.f, 0.f, 1.f);
67 
68   /* draw front face */
69   glBegin(GL_QUAD_STRIP);
70   for (i = 0; i <= teeth; i++) {
71     angle = i * 2.f * (float) M_PI / teeth;
72     glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), width * 0.5f);
73     glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), width * 0.5f);
74     if (i < teeth) {
75       glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), width * 0.5f);
76       glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), width * 0.5f);
77     }
78   }
79   glEnd();
80 
81   /* draw front sides of teeth */
82   glBegin(GL_QUADS);
83   da = 2.f * (float) M_PI / teeth / 4.f;
84   for (i = 0; i < teeth; i++) {
85     angle = i * 2.f * (float) M_PI / teeth;
86 
87     glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), width * 0.5f);
88     glVertex3f(r2 * (float) cos(angle + da), r2 * (float) sin(angle + da), width * 0.5f);
89     glVertex3f(r2 * (float) cos(angle + 2 * da), r2 * (float) sin(angle + 2 * da), width * 0.5f);
90     glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), width * 0.5f);
91   }
92   glEnd();
93 
94   glNormal3f(0.0, 0.0, -1.0);
95 
96   /* draw back face */
97   glBegin(GL_QUAD_STRIP);
98   for (i = 0; i <= teeth; i++) {
99     angle = i * 2.f * (float) M_PI / teeth;
100     glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), -width * 0.5f);
101     glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), -width * 0.5f);
102     if (i < teeth) {
103       glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), -width * 0.5f);
104       glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), -width * 0.5f);
105     }
106   }
107   glEnd();
108 
109   /* draw back sides of teeth */
110   glBegin(GL_QUADS);
111   da = 2.f * (float) M_PI / teeth / 4.f;
112   for (i = 0; i < teeth; i++) {
113     angle = i * 2.f * (float) M_PI / teeth;
114 
115     glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), -width * 0.5f);
116     glVertex3f(r2 * (float) cos(angle + 2 * da), r2 * (float) sin(angle + 2 * da), -width * 0.5f);
117     glVertex3f(r2 * (float) cos(angle + da), r2 * (float) sin(angle + da), -width * 0.5f);
118     glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), -width * 0.5f);
119   }
120   glEnd();
121 
122   /* draw outward faces of teeth */
123   glBegin(GL_QUAD_STRIP);
124   for (i = 0; i < teeth; i++) {
125     angle = i * 2.f * (float) M_PI / teeth;
126 
127     glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), width * 0.5f);
128     glVertex3f(r1 * (float) cos(angle), r1 * (float) sin(angle), -width * 0.5f);
129     u = r2 * (float) cos(angle + da) - r1 * (float) cos(angle);
130     v = r2 * (float) sin(angle + da) - r1 * (float) sin(angle);
131     len = (float) sqrt(u * u + v * v);
132     u /= len;
133     v /= len;
134     glNormal3f(v, -u, 0.0);
135     glVertex3f(r2 * (float) cos(angle + da), r2 * (float) sin(angle + da), width * 0.5f);
136     glVertex3f(r2 * (float) cos(angle + da), r2 * (float) sin(angle + da), -width * 0.5f);
137     glNormal3f((float) cos(angle), (float) sin(angle), 0.f);
138     glVertex3f(r2 * (float) cos(angle + 2 * da), r2 * (float) sin(angle + 2 * da), width * 0.5f);
139     glVertex3f(r2 * (float) cos(angle + 2 * da), r2 * (float) sin(angle + 2 * da), -width * 0.5f);
140     u = r1 * (float) cos(angle + 3 * da) - r2 * (float) cos(angle + 2 * da);
141     v = r1 * (float) sin(angle + 3 * da) - r2 * (float) sin(angle + 2 * da);
142     glNormal3f(v, -u, 0.f);
143     glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), width * 0.5f);
144     glVertex3f(r1 * (float) cos(angle + 3 * da), r1 * (float) sin(angle + 3 * da), -width * 0.5f);
145     glNormal3f((float) cos(angle), (float) sin(angle), 0.f);
146   }
147 
148   glVertex3f(r1 * (float) cos(0), r1 * (float) sin(0), width * 0.5f);
149   glVertex3f(r1 * (float) cos(0), r1 * (float) sin(0), -width * 0.5f);
150 
151   glEnd();
152 
153   glShadeModel(GL_SMOOTH);
154 
155   /* draw inside radius cylinder */
156   glBegin(GL_QUAD_STRIP);
157   for (i = 0; i <= teeth; i++) {
158     angle = i * 2.f * (float) M_PI / teeth;
159     glNormal3f(-(float) cos(angle), -(float) sin(angle), 0.f);
160     glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), -width * 0.5f);
161     glVertex3f(r0 * (float) cos(angle), r0 * (float) sin(angle), width * 0.5f);
162   }
163   glEnd();
164 
165 }
166 
167 
168 static GLfloat view_rotx = 20.f, view_roty = 30.f, view_rotz = 0.f;
169 static GLint gear1, gear2, gear3;
170 static GLfloat angle = 0.f;
171 
172 /* OpenGL draw function & timing */
draw(void)173 static void draw(void)
174 {
175   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
176 
177   glPushMatrix();
178     glRotatef(view_rotx, 1.0, 0.0, 0.0);
179     glRotatef(view_roty, 0.0, 1.0, 0.0);
180     glRotatef(view_rotz, 0.0, 0.0, 1.0);
181 
182     glPushMatrix();
183       glTranslatef(-3.0, -2.0, 0.0);
184       glRotatef(angle, 0.0, 0.0, 1.0);
185       glCallList(gear1);
186     glPopMatrix();
187 
188     glPushMatrix();
189       glTranslatef(3.1f, -2.f, 0.f);
190       glRotatef(-2.f * angle - 9.f, 0.f, 0.f, 1.f);
191       glCallList(gear2);
192     glPopMatrix();
193 
194     glPushMatrix();
195       glTranslatef(-3.1f, 4.2f, 0.f);
196       glRotatef(-2.f * angle - 25.f, 0.f, 0.f, 1.f);
197       glCallList(gear3);
198     glPopMatrix();
199 
200   glPopMatrix();
201 }
202 
203 
204 /* update animation parameters */
animate(void)205 static void animate(void)
206 {
207   angle = 100.f * (float) glfwGetTime();
208 }
209 
210 
211 /* change view angle, exit upon ESC */
key(GLFWwindow * window,int k,int s,int action,int mods)212 void key( GLFWwindow* window, int k, int s, int action, int mods )
213 {
214   if( action != GLFW_PRESS ) return;
215 
216   switch (k) {
217   case GLFW_KEY_Z:
218     if( mods & GLFW_MOD_SHIFT )
219       view_rotz -= 5.0;
220     else
221       view_rotz += 5.0;
222     break;
223   case GLFW_KEY_ESCAPE:
224     glfwSetWindowShouldClose(window, GLFW_TRUE);
225     break;
226   case GLFW_KEY_UP:
227     view_rotx += 5.0;
228     break;
229   case GLFW_KEY_DOWN:
230     view_rotx -= 5.0;
231     break;
232   case GLFW_KEY_LEFT:
233     view_roty += 5.0;
234     break;
235   case GLFW_KEY_RIGHT:
236     view_roty -= 5.0;
237     break;
238   default:
239     return;
240   }
241 }
242 
243 
244 /* new window size */
reshape(GLFWwindow * window,int width,int height)245 void reshape( GLFWwindow* window, int width, int height )
246 {
247   GLfloat h = (GLfloat) height / (GLfloat) width;
248   GLfloat xmax, znear, zfar;
249 
250   znear = 5.0f;
251   zfar  = 30.0f;
252   xmax  = znear * 0.5f;
253 
254   glViewport( 0, 0, (GLint) width, (GLint) height );
255   glMatrixMode( GL_PROJECTION );
256   glLoadIdentity();
257   glFrustum( -xmax, xmax, -xmax*h, xmax*h, znear, zfar );
258   glMatrixMode( GL_MODELVIEW );
259   glLoadIdentity();
260   glTranslatef( 0.0, 0.0, -20.0 );
261 }
262 
263 
264 /* program & OpenGL initialization */
init(void)265 static void init(void)
266 {
267   static GLfloat pos[4] = {5.f, 5.f, 10.f, 0.f};
268   static GLfloat red[4] = {0.8f, 0.1f, 0.f, 1.f};
269   static GLfloat green[4] = {0.f, 0.8f, 0.2f, 1.f};
270   static GLfloat blue[4] = {0.2f, 0.2f, 1.f, 1.f};
271 
272   glLightfv(GL_LIGHT0, GL_POSITION, pos);
273   glEnable(GL_CULL_FACE);
274   glEnable(GL_LIGHTING);
275   glEnable(GL_LIGHT0);
276   glEnable(GL_DEPTH_TEST);
277 
278   /* make the gears */
279   gear1 = glGenLists(1);
280   glNewList(gear1, GL_COMPILE);
281   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
282   gear(1.f, 4.f, 1.f, 20, 0.7f);
283   glEndList();
284 
285   gear2 = glGenLists(1);
286   glNewList(gear2, GL_COMPILE);
287   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
288   gear(0.5f, 2.f, 2.f, 10, 0.7f);
289   glEndList();
290 
291   gear3 = glGenLists(1);
292   glNewList(gear3, GL_COMPILE);
293   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
294   gear(1.3f, 2.f, 0.5f, 10, 0.7f);
295   glEndList();
296 
297   glEnable(GL_NORMALIZE);
298 }
299 
300 
301 /* program entry */
main(int argc,char * argv[])302 int main(int argc, char *argv[])
303 {
304     GLFWwindow* window;
305     int width, height;
306 
307     if( !glfwInit() )
308     {
309         fprintf( stderr, "Failed to initialize GLFW\n" );
310         exit( EXIT_FAILURE );
311     }
312 
313     glfwWindowHint(GLFW_DEPTH_BITS, 16);
314 
315     window = glfwCreateWindow( 300, 300, "Gears", NULL, NULL );
316     if (!window)
317     {
318         fprintf( stderr, "Failed to open GLFW window\n" );
319         glfwTerminate();
320         exit( EXIT_FAILURE );
321     }
322 
323     // Set callback functions
324     glfwSetFramebufferSizeCallback(window, reshape);
325     glfwSetKeyCallback(window, key);
326 
327     glfwMakeContextCurrent(window);
328     gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
329     glfwSwapInterval( 1 );
330 
331     glfwGetFramebufferSize(window, &width, &height);
332     reshape(window, width, height);
333 
334     // Parse command-line options
335     init();
336 
337     // Main loop
338     while( !glfwWindowShouldClose(window) )
339     {
340         // Draw gears
341         draw();
342 
343         // Update animation
344         animate();
345 
346         // Swap buffers
347         glfwSwapBuffers(window);
348         glfwPollEvents();
349     }
350 
351     // Terminate GLFW
352     glfwTerminate();
353 
354     // Exit program
355     exit( EXIT_SUCCESS );
356 }
357 
358