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 Löwy:
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   glClearColor(0.0, 0.0, 0.0, 0.0);
176   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
177 
178   glPushMatrix();
179     glRotatef(view_rotx, 1.0, 0.0, 0.0);
180     glRotatef(view_roty, 0.0, 1.0, 0.0);
181     glRotatef(view_rotz, 0.0, 0.0, 1.0);
182 
183     glPushMatrix();
184       glTranslatef(-3.0, -2.0, 0.0);
185       glRotatef(angle, 0.0, 0.0, 1.0);
186       glCallList(gear1);
187     glPopMatrix();
188 
189     glPushMatrix();
190       glTranslatef(3.1f, -2.f, 0.f);
191       glRotatef(-2.f * angle - 9.f, 0.f, 0.f, 1.f);
192       glCallList(gear2);
193     glPopMatrix();
194 
195     glPushMatrix();
196       glTranslatef(-3.1f, 4.2f, 0.f);
197       glRotatef(-2.f * angle - 25.f, 0.f, 0.f, 1.f);
198       glCallList(gear3);
199     glPopMatrix();
200 
201   glPopMatrix();
202 }
203 
204 
205 /* update animation parameters */
animate(void)206 static void animate(void)
207 {
208   angle = 100.f * (float) glfwGetTime();
209 }
210 
211 
212 /* change view angle, exit upon ESC */
key(GLFWwindow * window,int k,int s,int action,int mods)213 void key( GLFWwindow* window, int k, int s, int action, int mods )
214 {
215   if( action != GLFW_PRESS ) return;
216 
217   switch (k) {
218   case GLFW_KEY_Z:
219     if( mods & GLFW_MOD_SHIFT )
220       view_rotz -= 5.0;
221     else
222       view_rotz += 5.0;
223     break;
224   case GLFW_KEY_ESCAPE:
225     glfwSetWindowShouldClose(window, GLFW_TRUE);
226     break;
227   case GLFW_KEY_UP:
228     view_rotx += 5.0;
229     break;
230   case GLFW_KEY_DOWN:
231     view_rotx -= 5.0;
232     break;
233   case GLFW_KEY_LEFT:
234     view_roty += 5.0;
235     break;
236   case GLFW_KEY_RIGHT:
237     view_roty -= 5.0;
238     break;
239   default:
240     return;
241   }
242 }
243 
244 
245 /* new window size */
reshape(GLFWwindow * window,int width,int height)246 void reshape( GLFWwindow* window, int width, int height )
247 {
248   GLfloat h = (GLfloat) height / (GLfloat) width;
249   GLfloat xmax, znear, zfar;
250 
251   znear = 5.0f;
252   zfar  = 30.0f;
253   xmax  = znear * 0.5f;
254 
255   glViewport( 0, 0, (GLint) width, (GLint) height );
256   glMatrixMode( GL_PROJECTION );
257   glLoadIdentity();
258   glFrustum( -xmax, xmax, -xmax*h, xmax*h, znear, zfar );
259   glMatrixMode( GL_MODELVIEW );
260   glLoadIdentity();
261   glTranslatef( 0.0, 0.0, -20.0 );
262 }
263 
264 
265 /* program & OpenGL initialization */
init(void)266 static void init(void)
267 {
268   static GLfloat pos[4] = {5.f, 5.f, 10.f, 0.f};
269   static GLfloat red[4] = {0.8f, 0.1f, 0.f, 1.f};
270   static GLfloat green[4] = {0.f, 0.8f, 0.2f, 1.f};
271   static GLfloat blue[4] = {0.2f, 0.2f, 1.f, 1.f};
272 
273   glLightfv(GL_LIGHT0, GL_POSITION, pos);
274   glEnable(GL_CULL_FACE);
275   glEnable(GL_LIGHTING);
276   glEnable(GL_LIGHT0);
277   glEnable(GL_DEPTH_TEST);
278 
279   /* make the gears */
280   gear1 = glGenLists(1);
281   glNewList(gear1, GL_COMPILE);
282   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
283   gear(1.f, 4.f, 1.f, 20, 0.7f);
284   glEndList();
285 
286   gear2 = glGenLists(1);
287   glNewList(gear2, GL_COMPILE);
288   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
289   gear(0.5f, 2.f, 2.f, 10, 0.7f);
290   glEndList();
291 
292   gear3 = glGenLists(1);
293   glNewList(gear3, GL_COMPILE);
294   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
295   gear(1.3f, 2.f, 0.5f, 10, 0.7f);
296   glEndList();
297 
298   glEnable(GL_NORMALIZE);
299 }
300 
301 
302 /* program entry */
main(int argc,char * argv[])303 int main(int argc, char *argv[])
304 {
305     GLFWwindow* window;
306     int width, height;
307 
308     if( !glfwInit() )
309     {
310         fprintf( stderr, "Failed to initialize GLFW\n" );
311         exit( EXIT_FAILURE );
312     }
313 
314     glfwWindowHint(GLFW_DEPTH_BITS, 16);
315     glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
316 
317     window = glfwCreateWindow( 300, 300, "Gears", NULL, NULL );
318     if (!window)
319     {
320         fprintf( stderr, "Failed to open GLFW window\n" );
321         glfwTerminate();
322         exit( EXIT_FAILURE );
323     }
324 
325     // Set callback functions
326     glfwSetFramebufferSizeCallback(window, reshape);
327     glfwSetKeyCallback(window, key);
328 
329     glfwMakeContextCurrent(window);
330     gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
331     glfwSwapInterval( 1 );
332 
333     glfwGetFramebufferSize(window, &width, &height);
334     reshape(window, width, height);
335 
336     // Parse command-line options
337     init();
338 
339     // Main loop
340     while( !glfwWindowShouldClose(window) )
341     {
342         // Draw gears
343         draw();
344 
345         // Update animation
346         animate();
347 
348         // Swap buffers
349         glfwSwapBuffers(window);
350         glfwPollEvents();
351     }
352 
353     // Terminate GLFW
354     glfwTerminate();
355 
356     // Exit program
357     exit( EXIT_SUCCESS );
358 }
359 
360