es2gears.c revision 32001f49
1/* 2 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included 12 * in all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 */ 21 22/* 23 * Ported to GLES2. 24 * Kristian Høgsberg <krh@bitplanet.net> 25 * May 3, 2010 26 * 27 * Improve GLES2 port: 28 * * Refactor gear drawing. 29 * * Use correct normals for surfaces. 30 * * Improve shader. 31 * * Use perspective projection transformation. 32 * * Add FPS count. 33 * * Add comments. 34 * Alexandros Frantzis <alexandros.frantzis@linaro.org> 35 * Jul 13, 2010 36 */ 37 38#define GL_GLEXT_PROTOTYPES 39#define EGL_EGLEXT_PROTOTYPES 40 41#define _GNU_SOURCE 42 43#include <math.h> 44#include <stdlib.h> 45#include <stdio.h> 46#include <string.h> 47#include <sys/time.h> 48#include <unistd.h> 49#include <GLES2/gl2.h> 50#include <EGL/egl.h> 51#include <EGL/eglext.h> 52#include "eglut.h" 53 54#define STRIPS_PER_TOOTH 7 55#define VERTICES_PER_TOOTH 34 56#define GEAR_VERTEX_STRIDE 6 57 58/** 59 * Struct describing the vertices in triangle strip 60 */ 61struct vertex_strip { 62 /** The first vertex in the strip */ 63 GLint first; 64 /** The number of consecutive vertices in the strip after the first */ 65 GLint count; 66}; 67 68/* Each vertex consist of GEAR_VERTEX_STRIDE GLfloat attributes */ 69typedef GLfloat GearVertex[GEAR_VERTEX_STRIDE]; 70 71/** 72 * Struct representing a gear. 73 */ 74struct gear { 75 /** The array of vertices comprising the gear */ 76 GearVertex *vertices; 77 /** The number of vertices comprising the gear */ 78 int nvertices; 79 /** The array of triangle strips comprising the gear */ 80 struct vertex_strip *strips; 81 /** The number of triangle strips comprising the gear */ 82 int nstrips; 83 /** The Vertex Buffer Object holding the vertices in the graphics card */ 84 GLuint vbo; 85}; 86 87/** The view rotation [x, y, z] */ 88static GLfloat view_rot[3] = { 20.0, 30.0, 0.0 }; 89/** The gears */ 90static struct gear *gear1, *gear2, *gear3; 91/** The current gear rotation angle */ 92static GLfloat angle = 0.0; 93/** The location of the shader uniforms */ 94static GLuint ModelViewProjectionMatrix_location, 95 NormalMatrix_location, 96 LightSourcePosition_location, 97 MaterialColor_location; 98/** The projection matrix */ 99static GLfloat ProjectionMatrix[16]; 100/** The direction of the directional light for the scene */ 101static const GLfloat LightSourcePosition[4] = { 5.0, 5.0, 10.0, 1.0}; 102 103/** 104 * Fills a gear vertex. 105 * 106 * @param v the vertex to fill 107 * @param x the x coordinate 108 * @param y the y coordinate 109 * @param z the z coortinate 110 * @param n pointer to the normal table 111 * 112 * @return the operation error code 113 */ 114static GearVertex * 115vert(GearVertex *v, GLfloat x, GLfloat y, GLfloat z, GLfloat n[3]) 116{ 117 v[0][0] = x; 118 v[0][1] = y; 119 v[0][2] = z; 120 v[0][3] = n[0]; 121 v[0][4] = n[1]; 122 v[0][5] = n[2]; 123 124 return v + 1; 125} 126 127/** 128 * Create a gear wheel. 129 * 130 * @param inner_radius radius of hole at center 131 * @param outer_radius radius at center of teeth 132 * @param width width of gear 133 * @param teeth number of teeth 134 * @param tooth_depth depth of tooth 135 * 136 * @return pointer to the constructed struct gear 137 */ 138static struct gear * 139create_gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, 140 GLint teeth, GLfloat tooth_depth) 141{ 142 GLfloat r0, r1, r2; 143 GLfloat da; 144 GearVertex *v; 145 struct gear *gear; 146 double s[5], c[5]; 147 GLfloat normal[3]; 148 int cur_strip = 0; 149 int i; 150 151 /* Allocate memory for the gear */ 152 gear = malloc(sizeof *gear); 153 if (gear == NULL) 154 return NULL; 155 156 /* Calculate the radii used in the gear */ 157 r0 = inner_radius; 158 r1 = outer_radius - tooth_depth / 2.0; 159 r2 = outer_radius + tooth_depth / 2.0; 160 161 da = 2.0 * M_PI / teeth / 4.0; 162 163 /* Allocate memory for the triangle strip information */ 164 gear->nstrips = STRIPS_PER_TOOTH * teeth; 165 gear->strips = calloc(gear->nstrips, sizeof (*gear->strips)); 166 167 /* Allocate memory for the vertices */ 168 gear->vertices = calloc(VERTICES_PER_TOOTH * teeth, sizeof(*gear->vertices)); 169 v = gear->vertices; 170 171 for (i = 0; i < teeth; i++) { 172 /* Calculate needed sin/cos for varius angles */ 173 sincos(i * 2.0 * M_PI / teeth, &s[0], &c[0]); 174 sincos(i * 2.0 * M_PI / teeth + da, &s[1], &c[1]); 175 sincos(i * 2.0 * M_PI / teeth + da * 2, &s[2], &c[2]); 176 sincos(i * 2.0 * M_PI / teeth + da * 3, &s[3], &c[3]); 177 sincos(i * 2.0 * M_PI / teeth + da * 4, &s[4], &c[4]); 178 179 /* A set of macros for making the creation of the gears easier */ 180#define GEAR_POINT(r, da) { (r) * c[(da)], (r) * s[(da)] } 181#define SET_NORMAL(x, y, z) do { \ 182 normal[0] = (x); normal[1] = (y); normal[2] = (z); \ 183} while(0) 184 185#define GEAR_VERT(v, point, sign) vert((v), p[(point)].x, p[(point)].y, (sign) * width * 0.5, normal) 186 187#define START_STRIP do { \ 188 gear->strips[cur_strip].first = v - gear->vertices; \ 189} while(0); 190 191#define END_STRIP do { \ 192 int _tmp = (v - gear->vertices); \ 193 gear->strips[cur_strip].count = _tmp - gear->strips[cur_strip].first; \ 194 cur_strip++; \ 195} while (0) 196 197#define QUAD_WITH_NORMAL(p1, p2) do { \ 198 SET_NORMAL((p[(p1)].y - p[(p2)].y), -(p[(p1)].x - p[(p2)].x), 0); \ 199 v = GEAR_VERT(v, (p1), -1); \ 200 v = GEAR_VERT(v, (p1), 1); \ 201 v = GEAR_VERT(v, (p2), -1); \ 202 v = GEAR_VERT(v, (p2), 1); \ 203} while(0) 204 205 struct point { 206 GLfloat x; 207 GLfloat y; 208 }; 209 210 /* Create the 7 points (only x,y coords) used to draw a tooth */ 211 struct point p[7] = { 212 GEAR_POINT(r2, 1), // 0 213 GEAR_POINT(r2, 2), // 1 214 GEAR_POINT(r1, 0), // 2 215 GEAR_POINT(r1, 3), // 3 216 GEAR_POINT(r0, 0), // 4 217 GEAR_POINT(r1, 4), // 5 218 GEAR_POINT(r0, 4), // 6 219 }; 220 221 /* Front face */ 222 START_STRIP; 223 SET_NORMAL(0, 0, 1.0); 224 v = GEAR_VERT(v, 0, +1); 225 v = GEAR_VERT(v, 1, +1); 226 v = GEAR_VERT(v, 2, +1); 227 v = GEAR_VERT(v, 3, +1); 228 v = GEAR_VERT(v, 4, +1); 229 v = GEAR_VERT(v, 5, +1); 230 v = GEAR_VERT(v, 6, +1); 231 END_STRIP; 232 233 /* Inner face */ 234 START_STRIP; 235 QUAD_WITH_NORMAL(4, 6); 236 END_STRIP; 237 238 /* Back face */ 239 START_STRIP; 240 SET_NORMAL(0, 0, -1.0); 241 v = GEAR_VERT(v, 6, -1); 242 v = GEAR_VERT(v, 5, -1); 243 v = GEAR_VERT(v, 4, -1); 244 v = GEAR_VERT(v, 3, -1); 245 v = GEAR_VERT(v, 2, -1); 246 v = GEAR_VERT(v, 1, -1); 247 v = GEAR_VERT(v, 0, -1); 248 END_STRIP; 249 250 /* Outer face */ 251 START_STRIP; 252 QUAD_WITH_NORMAL(0, 2); 253 END_STRIP; 254 255 START_STRIP; 256 QUAD_WITH_NORMAL(1, 0); 257 END_STRIP; 258 259 START_STRIP; 260 QUAD_WITH_NORMAL(3, 1); 261 END_STRIP; 262 263 START_STRIP; 264 QUAD_WITH_NORMAL(5, 3); 265 END_STRIP; 266 } 267 268 gear->nvertices = (v - gear->vertices); 269 270 /* Store the vertices in a vertex buffer object (VBO) */ 271 glGenBuffers(1, &gear->vbo); 272 glBindBuffer(GL_ARRAY_BUFFER, gear->vbo); 273 glBufferData(GL_ARRAY_BUFFER, gear->nvertices * sizeof(GearVertex), 274 gear->vertices, GL_STATIC_DRAW); 275 276 return gear; 277} 278 279/** 280 * Multiplies two 4x4 matrices. 281 * 282 * The result is stored in matrix m. 283 * 284 * @param m the first matrix to multiply 285 * @param n the second matrix to multiply 286 */ 287static void 288multiply(GLfloat *m, const GLfloat *n) 289{ 290 GLfloat tmp[16]; 291 const GLfloat *row, *column; 292 div_t d; 293 int i, j; 294 295 for (i = 0; i < 16; i++) { 296 tmp[i] = 0; 297 d = div(i, 4); 298 row = n + d.quot * 4; 299 column = m + d.rem; 300 for (j = 0; j < 4; j++) 301 tmp[i] += row[j] * column[j * 4]; 302 } 303 memcpy(m, &tmp, sizeof tmp); 304} 305 306/** 307 * Rotates a 4x4 matrix. 308 * 309 * @param[in,out] m the matrix to rotate 310 * @param angle the angle to rotate 311 * @param x the x component of the direction to rotate to 312 * @param y the y component of the direction to rotate to 313 * @param z the z component of the direction to rotate to 314 */ 315static void 316rotate(GLfloat *m, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) 317{ 318 double s, c; 319 320 sincos(angle, &s, &c); 321 GLfloat r[16] = { 322 x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0, 323 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0, 324 x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0, 325 0, 0, 0, 1 326 }; 327 328 multiply(m, r); 329} 330 331 332/** 333 * Translates a 4x4 matrix. 334 * 335 * @param[in,out] m the matrix to translate 336 * @param x the x component of the direction to translate to 337 * @param y the y component of the direction to translate to 338 * @param z the z component of the direction to translate to 339 */ 340static void 341translate(GLfloat *m, GLfloat x, GLfloat y, GLfloat z) 342{ 343 GLfloat t[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }; 344 345 multiply(m, t); 346} 347 348/** 349 * Creates an identity 4x4 matrix. 350 * 351 * @param m the matrix make an identity matrix 352 */ 353static void 354identity(GLfloat *m) 355{ 356 GLfloat t[16] = { 357 1.0, 0.0, 0.0, 0.0, 358 0.0, 1.0, 0.0, 0.0, 359 0.0, 0.0, 1.0, 0.0, 360 0.0, 0.0, 0.0, 1.0, 361 }; 362 363 memcpy(m, t, sizeof(t)); 364} 365 366/** 367 * Transposes a 4x4 matrix. 368 * 369 * @param m the matrix to transpose 370 */ 371static void 372transpose(GLfloat *m) 373{ 374 GLfloat t[16] = { 375 m[0], m[4], m[8], m[12], 376 m[1], m[5], m[9], m[13], 377 m[2], m[6], m[10], m[14], 378 m[3], m[7], m[11], m[15]}; 379 380 memcpy(m, t, sizeof(t)); 381} 382 383/** 384 * Inverts a 4x4 matrix. 385 * 386 * This function can currently handle only pure translation-rotation matrices. 387 * Read http://www.gamedev.net/community/forums/topic.asp?topic_id=425118 388 * for an explanation. 389 */ 390static void 391invert(GLfloat *m) 392{ 393 GLfloat t[16]; 394 identity(t); 395 396 // Extract and invert the translation part 't'. The inverse of a 397 // translation matrix can be calculated by negating the translation 398 // coordinates. 399 t[12] = -m[12]; t[13] = -m[13]; t[14] = -m[14]; 400 401 // Invert the rotation part 'r'. The inverse of a rotation matrix is 402 // equal to its transpose. 403 m[12] = m[13] = m[14] = 0; 404 transpose(m); 405 406 // inv(m) = inv(r) * inv(t) 407 multiply(m, t); 408} 409 410/** 411 * Calculate a perspective projection transformation. 412 * 413 * @param m the matrix to save the transformation in 414 * @param fovy the field of view in the y direction 415 * @param aspect the view aspect ratio 416 * @param zNear the near clipping plane 417 * @param zFar the far clipping plane 418 */ 419void perspective(GLfloat *m, GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar) 420{ 421 GLfloat tmp[16]; 422 identity(tmp); 423 424 double sine, cosine, cotangent, deltaZ; 425 GLfloat radians = fovy / 2 * M_PI / 180; 426 427 deltaZ = zFar - zNear; 428 sincos(radians, &sine, &cosine); 429 430 if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) 431 return; 432 433 cotangent = cosine / sine; 434 435 tmp[0] = cotangent / aspect; 436 tmp[5] = cotangent; 437 tmp[10] = -(zFar + zNear) / deltaZ; 438 tmp[11] = -1; 439 tmp[14] = -2 * zNear * zFar / deltaZ; 440 tmp[15] = 0; 441 442 memcpy(m, tmp, sizeof(tmp)); 443} 444 445/** 446 * Draws a gear. 447 * 448 * @param gear the gear to draw 449 * @param transform the current transformation matrix 450 * @param x the x position to draw the gear at 451 * @param y the y position to draw the gear at 452 * @param angle the rotation angle of the gear 453 * @param color the color of the gear 454 */ 455static void 456draw_gear(struct gear *gear, GLfloat *transform, 457 GLfloat x, GLfloat y, GLfloat angle, const GLfloat color[4]) 458{ 459 GLfloat model_view[16]; 460 GLfloat normal_matrix[16]; 461 GLfloat model_view_projection[16]; 462 463 /* Translate and rotate the gear */ 464 memcpy(model_view, transform, sizeof (model_view)); 465 translate(model_view, x, y, 0); 466 rotate(model_view, 2 * M_PI * angle / 360.0, 0, 0, 1); 467 468 /* Create and set the ModelViewProjectionMatrix */ 469 memcpy(model_view_projection, ProjectionMatrix, sizeof(model_view_projection)); 470 multiply(model_view_projection, model_view); 471 472 glUniformMatrix4fv(ModelViewProjectionMatrix_location, 1, GL_FALSE, 473 model_view_projection); 474 475 /* 476 * Create and set the NormalMatrix. It's the inverse transpose of the 477 * ModelView matrix. 478 */ 479 memcpy(normal_matrix, model_view, sizeof (normal_matrix)); 480 invert(normal_matrix); 481 transpose(normal_matrix); 482 glUniformMatrix4fv(NormalMatrix_location, 1, GL_FALSE, normal_matrix); 483 484 /* Set the gear color */ 485 glUniform4fv(MaterialColor_location, 1, color); 486 487 /* Set the vertex buffer object to use */ 488 glBindBuffer(GL_ARRAY_BUFFER, gear->vbo); 489 490 /* Set up the position of the attributes in the vertex buffer object */ 491 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 492 6 * sizeof(GLfloat), NULL); 493 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 494 6 * sizeof(GLfloat), (GLfloat *) 0 + 3); 495 496 /* Enable the attributes */ 497 glEnableVertexAttribArray(0); 498 glEnableVertexAttribArray(1); 499 500 /* Draw the triangle strips that comprise the gear */ 501 int n; 502 for (n = 0; n < gear->nstrips; n++) 503 glDrawArrays(GL_TRIANGLE_STRIP, gear->strips[n].first, gear->strips[n].count); 504 505 /* Disable the attributes */ 506 glDisableVertexAttribArray(1); 507 glDisableVertexAttribArray(0); 508} 509 510/** 511 * Draws the gears. 512 */ 513static void 514gears_draw(void) 515{ 516 const static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 }; 517 const static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 }; 518 const static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 }; 519 GLfloat transform[16]; 520 identity(transform); 521 522 glClearColor(0.0, 0.0, 0.0, 0.0); 523 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 524 525 /* Translate and rotate the view */ 526 translate(transform, 0, 0, -20); 527 rotate(transform, 2 * M_PI * view_rot[0] / 360.0, 1, 0, 0); 528 rotate(transform, 2 * M_PI * view_rot[1] / 360.0, 0, 1, 0); 529 rotate(transform, 2 * M_PI * view_rot[2] / 360.0, 0, 0, 1); 530 531 /* Draw the gears */ 532 draw_gear(gear1, transform, -3.0, -2.0, angle, red); 533 draw_gear(gear2, transform, 3.1, -2.0, -2 * angle - 9.0, green); 534 draw_gear(gear3, transform, -3.1, 4.2, -2 * angle - 25.0, blue); 535} 536 537/** 538 * Handles a new window size or exposure. 539 * 540 * @param width the window width 541 * @param height the window height 542 */ 543static void 544gears_reshape(int width, int height) 545{ 546 /* Update the projection matrix */ 547 perspective(ProjectionMatrix, 60.0, width / (float)height, 1.0, 1024.0); 548 549 /* Set the viewport */ 550 glViewport(0, 0, (GLint) width, (GLint) height); 551} 552 553/** 554 * Handles special eglut events. 555 * 556 * @param special the event to handle. 557 */ 558static void 559gears_special(int special) 560{ 561 switch (special) { 562 case EGLUT_KEY_LEFT: 563 view_rot[1] += 5.0; 564 break; 565 case EGLUT_KEY_RIGHT: 566 view_rot[1] -= 5.0; 567 break; 568 case EGLUT_KEY_UP: 569 view_rot[0] += 5.0; 570 break; 571 case EGLUT_KEY_DOWN: 572 view_rot[0] -= 5.0; 573 break; 574 } 575} 576 577static void 578gears_idle(void) 579{ 580 static int frames = 0; 581 static double tRot0 = -1.0, tRate0 = -1.0; 582 double dt, t = eglutGet(EGLUT_ELAPSED_TIME) / 1000.0; 583 584 if (tRot0 < 0.0) 585 tRot0 = t; 586 dt = t - tRot0; 587 tRot0 = t; 588 589 /* advance rotation for next frame */ 590 angle += 70.0 * dt; /* 70 degrees per second */ 591 if (angle > 3600.0) 592 angle -= 3600.0; 593 594 eglutPostRedisplay(); 595 frames++; 596 597 if (tRate0 < 0.0) 598 tRate0 = t; 599 if (t - tRate0 >= 5.0) { 600 GLfloat seconds = t - tRate0; 601 GLfloat fps = frames / seconds; 602 printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, 603 fps); 604 tRate0 = t; 605 frames = 0; 606 } 607} 608 609static const char vertex_shader[] = 610"attribute vec3 position;\n" 611"attribute vec3 normal;\n" 612"\n" 613"uniform mat4 ModelViewProjectionMatrix;\n" 614"uniform mat4 NormalMatrix;\n" 615"uniform vec4 LightSourcePosition;\n" 616"uniform vec4 MaterialColor;\n" 617"\n" 618"varying vec4 Color;\n" 619"\n" 620"void main(void)\n" 621"{\n" 622" // Transform the normal to eye coordinates\n" 623" vec3 N = normalize(vec3(NormalMatrix * vec4(normal, 1.0)));\n" 624"\n" 625" // The LightSourcePosition is actually its direction for directional light\n" 626" vec3 L = normalize(LightSourcePosition.xyz);\n" 627"\n" 628" // Multiply the diffuse value by the vertex color (which is fixed in this case)\n" 629" // to get the actual color that we will use to draw this vertex with\n" 630" float diffuse = max(dot(N, L), 0.0);\n" 631" Color = diffuse * MaterialColor;\n" 632"\n" 633" // Transform the position to clip coordinates\n" 634" gl_Position = ModelViewProjectionMatrix * vec4(position, 1.0);\n" 635"}"; 636 637static const char fragment_shader[] = 638"precision mediump float;\n" 639"varying vec4 Color;\n" 640"\n" 641"void main(void)\n" 642"{\n" 643" gl_FragColor = Color;\n" 644"}"; 645 646static void 647gears_init(void) 648{ 649 GLuint v, f, program; 650 const char *p; 651 char msg[512]; 652 653 glEnable(GL_CULL_FACE); 654 glEnable(GL_DEPTH_TEST); 655 656 /* Compile the vertex shader */ 657 p = vertex_shader; 658 v = glCreateShader(GL_VERTEX_SHADER); 659 glShaderSource(v, 1, &p, NULL); 660 glCompileShader(v); 661 glGetShaderInfoLog(v, sizeof msg, NULL, msg); 662 printf("vertex shader info: %s\n", msg); 663 664 /* Compile the fragment shader */ 665 p = fragment_shader; 666 f = glCreateShader(GL_FRAGMENT_SHADER); 667 glShaderSource(f, 1, &p, NULL); 668 glCompileShader(f); 669 glGetShaderInfoLog(f, sizeof msg, NULL, msg); 670 printf("fragment shader info: %s\n", msg); 671 672 /* Create and link the shader program */ 673 program = glCreateProgram(); 674 glAttachShader(program, v); 675 glAttachShader(program, f); 676 glBindAttribLocation(program, 0, "position"); 677 glBindAttribLocation(program, 1, "normal"); 678 679 glLinkProgram(program); 680 glGetProgramInfoLog(program, sizeof msg, NULL, msg); 681 printf("info: %s\n", msg); 682 683 /* Enable the shaders */ 684 glUseProgram(program); 685 686 /* Get the locations of the uniforms so we can access them */ 687 ModelViewProjectionMatrix_location = glGetUniformLocation(program, "ModelViewProjectionMatrix"); 688 NormalMatrix_location = glGetUniformLocation(program, "NormalMatrix"); 689 LightSourcePosition_location = glGetUniformLocation(program, "LightSourcePosition"); 690 MaterialColor_location = glGetUniformLocation(program, "MaterialColor"); 691 692 /* Set the LightSourcePosition uniform which is constant throught the program */ 693 glUniform4fv(LightSourcePosition_location, 1, LightSourcePosition); 694 695 /* make the gears */ 696 gear1 = create_gear(1.0, 4.0, 1.0, 20, 0.7); 697 gear2 = create_gear(0.5, 2.0, 2.0, 10, 0.7); 698 gear3 = create_gear(1.3, 2.0, 0.5, 10, 0.7); 699} 700 701int 702main(int argc, char *argv[]) 703{ 704 /* Initialize the window */ 705 eglutInitWindowSize(300, 300); 706 eglutInitAPIMask(EGLUT_OPENGL_ES2_BIT); 707 eglutInit(argc, argv); 708 709 eglutCreateWindow("es2gears"); 710 711 /* Set up eglut callback functions */ 712 eglutIdleFunc(gears_idle); 713 eglutReshapeFunc(gears_reshape); 714 eglutDisplayFunc(gears_draw); 715 eglutSpecialFunc(gears_special); 716 717 /* Initialize the gears */ 718 gears_init(); 719 720 eglutMainLoop(); 721 722 return 0; 723} 724