gears.c
Go to the documentation of this file.
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 
49 static void
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 
65 
66  glNormal3f(0.f, 0.f, 1.f);
67 
68  /* draw front face */
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 */
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 */
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 */
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 
154 
155  /* draw inside radius cylinder */
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;
170 static GLfloat angle = 0.f;
171 
172 /* OpenGL draw function & timing */
173 static void draw(void)
174 {
175  glClearColor(0.0, 0.0, 0.0, 0.0);
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 */
206 static void animate(void)
207 {
208  angle = 100.f * (float) glfwGetTime();
209 }
210 
211 
212 /* change view angle, exit upon ESC */
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:
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 */
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 );
257  glLoadIdentity();
258  glFrustum( -xmax, xmax, -xmax*h, xmax*h, znear, zfar );
260  glLoadIdentity();
261  glTranslatef( 0.0, 0.0, -20.0 );
262 }
263 
264 
265 /* program & OpenGL initialization */
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 
278 
279  /* make the gears */
280  gear1 = glGenLists(1);
283  gear(1.f, 4.f, 1.f, 20, 0.7f);
284  glEndList();
285 
286  gear2 = glGenLists(1);
289  gear(0.5f, 2.f, 2.f, 10, 0.7f);
290  glEndList();
291 
292  gear3 = glGenLists(1);
295  gear(1.3f, 2.f, 0.5f, 10, 0.7f);
296  glEndList();
297 
299 }
300 
301 
302 /* program entry */
303 int main(int argc, char *argv[])
304 {
306  int width, height;
307 
308  if( !glfwInit() )
309  {
310  fprintf( stderr, "Failed to initialize GLFW\n" );
311  exit( EXIT_FAILURE );
312  }
313 
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
327  glfwSetKeyCallback(window, key);
328 
329  glfwMakeContextCurrent(window);
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 
#define glShadeModel
#define glLightfv
#define GLFW_KEY_LEFT
Definition: glfw3.h:419
khronos_float_t GLfloat
static void init(void)
Definition: gears.c:266
#define glRotatef
The header of the GLFW 3 API.
GLFWAPI GLFWglproc glfwGetProcAddress(const char *procname)
Returns the address of the specified function for the current context.
Definition: context.c:741
GLfloat green
#define GL_QUAD_STRIP
GLdouble s
#define glBegin
void *(* GLADloadproc)(const char *name)
static GLFWwindow * window
Definition: joysticks.c:55
#define GLFW_KEY_UP
Definition: glfw3.h:421
#define glPopMatrix
#define glMaterialfv
#define GL_PROJECTION
int main(int argc, char *argv[])
Definition: gears.c:303
GLfloat angle
Definition: glext.h:6819
GLFWAPI GLFWframebuffersizefun glfwSetFramebufferSizeCallback(GLFWwindow *window, GLFWframebuffersizefun cbfun)
Sets the framebuffer resize callback for the specified window.
Definition: window.c:1050
#define glLoadIdentity
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
#define GL_FLAT
#define glVertex3f
#define glEnable
#define GLFW_KEY_Z
Definition: glfw3.h:403
#define GLFW_TRANSPARENT_FRAMEBUFFER
Window framebuffer transparency hint and attribute.
Definition: glfw3.h:816
static GLfloat view_rotx
Definition: gears.c:168
GLenum GLsizei len
Definition: glext.h:3285
GLuint64 key
Definition: glext.h:8966
#define GL_LIGHTING
static GLint gear1
Definition: gears.c:169
#define GL_SMOOTH
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
Definition: init.c:198
GLdouble f
#define GL_AMBIENT_AND_DIFFUSE
#define glEndList
#define GL_COLOR_BUFFER_BIT
int GLint
GLFWAPI void glfwSwapInterval(int interval)
Sets the swap interval for the current context.
Definition: context.c:658
#define glPushMatrix
#define GLFW_MOD_SHIFT
If this bit is set one or more Shift keys were held down.
Definition: glfw3.h:499
#define GLFW_KEY_ESCAPE
Definition: glfw3.h:412
#define glClear
GLint GLsizei GLsizei height
static GLint gear3
Definition: gears.c:169
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:641
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:611
#define glTranslatef
#define GL_QUADS
void key(GLFWwindow *window, int k, int s, int action, int mods)
Definition: gears.c:213
#define glNormal3f
static void draw(void)
Definition: gears.c:173
#define glEnd
#define glGenLists
#define GL_LIGHT0
void reshape(GLFWwindow *window, int width, int height)
Definition: gears.c:246
#define GL_FRONT
action
Definition: enums.py:62
GLAPI int gladLoadGLLoader(GLADloadproc)
Definition: glad/glad.c:1697
#define glViewport
#define GL_POSITION
#define GL_DEPTH_BUFFER_BIT
#define GL_MODELVIEW
static const textual_icon exit
Definition: model-views.h:254
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow *window, int value)
Sets the close flag of the specified window.
Definition: window.c:486
#define glFrustum
GLfloat GLfloat blue
static GLfloat angle
Definition: gears.c:170
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:151
#define GLFW_TRUE
One.
Definition: glfw3.h:279
#define GL_CULL_FACE
#define GLFW_KEY_DOWN
Definition: glfw3.h:420
GLFWAPI double glfwGetTime(void)
Returns the value of the GLFW timer.
Definition: input.c:1275
static GLfloat view_rotz
Definition: gears.c:168
static void animate(void)
Definition: gears.c:206
static const ImVec4 red
Definition: model-views.h:64
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:647
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
Definition: init.c:243
#define glClearColor
static GLfloat view_roty
Definition: gears.c:168
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:791
float rs2_vector::* pos
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:1072
#define GL_COMPILE
#define glCallList
static GLint gear2
Definition: gears.c:169
#define NULL
Definition: tinycthread.c:47
#define glNewList
static void gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, GLint teeth, GLfloat tooth_depth)
Definition: gears.c:50
int i
#define GLFW_KEY_RIGHT
Definition: glfw3.h:418
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
GLdouble v
#define glMatrixMode
struct GLFWwindow GLFWwindow
GLFWAPI void glfwWindowHint(int hint, int value)
Sets the specified window hint to the desired value.
Definition: window.c:291
GLint GLsizei width
#define GL_DEPTH_TEST
#define GLFW_DEPTH_BITS
Framebuffer bit depth hint.
Definition: glfw3.h:853
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:477
#define GL_NORMALIZE


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:15