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