ScrapExplorer - gears.c

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