/opengles/redbook/torus.c

http://ftk.googlecode.com/ · C · 149 lines · 93 code · 12 blank · 44 comment · 5 complexity · 846a8dbf1f8d5fd61d16003f698b3850 MD5 · raw file

  1. /*
  2. * License Applicability. Except to the extent portions of this file are
  3. * made subject to an alternative license as permitted in the SGI Free
  4. * Software License B, Version 1.1 (the "License"), the contents of this
  5. * file are subject only to the provisions of the License. You may not use
  6. * this file except in compliance with the License. You may obtain a copy
  7. * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
  8. * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
  9. *
  10. * http://oss.sgi.com/projects/FreeB
  11. *
  12. * Note that, as provided in the License, the Software is distributed on an
  13. * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
  14. * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
  15. * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
  16. * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
  17. *
  18. * Original Code. The Original Code is: OpenGL Sample Implementation,
  19. * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
  20. * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
  21. * Copyright in any portions created by third parties is as indicated
  22. * elsewhere herein. All Rights Reserved.
  23. *
  24. * Additional Notice Provisions: The application programming interfaces
  25. * established by SGI in conjunction with the Original Code are The
  26. * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
  27. * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
  28. * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
  29. * Window System(R) (Version 1.3), released October 19, 1998. This software
  30. * was created using the OpenGL(R) version 1.2.1 Sample Implementation
  31. * published by SGI, but has not been independently verified as being
  32. * compliant with the OpenGL(R) version 1.2.1 Specification.
  33. *
  34. */
  35. /*
  36. * torus.c
  37. * This program demonstrates the drawing of a torus.
  38. */
  39. #include <stdio.h>
  40. #include <math.h>
  41. #include <stdlib.h>
  42. #include <math.h>
  43. #include "ug.h"
  44. #define PI_ 3.14159265358979323846
  45. /* Draw a torus */
  46. static void torus(int numc, int numt)
  47. {
  48. int i, j, k;
  49. double s, t, x, y, z, twopi;
  50. static GLfloat* v;
  51. GLfloat* p;
  52. if (!v) {
  53. p = v = malloc(numc*(numt+1)*2*3*sizeof *v);
  54. twopi = 2 * PI_;
  55. for (i = 0; i < numc; i++) {
  56. for (j = 0; j <= numt; j++) {
  57. for (k = 1; k >= 0; k--) {
  58. s = (i + k) % numc + 0.5;
  59. t = j % numt;
  60. x = (1+.1*cos(s*twopi/numc))*cos(t*twopi/numt);
  61. y = (1+.1*cos(s*twopi/numc))*sin(t*twopi/numt);
  62. z = .1 * sin(s * twopi / numc);
  63. *p++ = x;
  64. *p++ = y;
  65. *p++ = z;
  66. }
  67. }
  68. }
  69. glVertexPointer(3, GL_FLOAT, 0, v);
  70. glEnableClientState (GL_VERTEX_ARRAY);
  71. }
  72. glDrawArrays(GL_TRIANGLE_STRIP, 0, numc*(numt+1)*2);
  73. }
  74. /* Initialize state */
  75. static void init(void)
  76. {
  77. glShadeModel(GL_FLAT);
  78. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  79. }
  80. /* Clear window and draw torus */
  81. void display(UGWindow win)
  82. {
  83. glClear(GL_COLOR_BUFFER_BIT);
  84. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  85. torus(8, 25);
  86. glFlush();
  87. ugSwapBuffers(win);
  88. }
  89. /* Handle window resize */
  90. void reshape(UGWindow uwin, int w, int h)
  91. {
  92. printf("reshape\n");
  93. glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  94. glMatrixMode(GL_PROJECTION);
  95. glLoadIdentity();
  96. ugluPerspectivef(30, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
  97. glMatrixMode(GL_MODELVIEW);
  98. glLoadIdentity();
  99. ugluLookAtf(0, 0, 10, 0, 0, 0, 0, 1, 0);
  100. }
  101. /* Rotate about x-axis when "x" typed; rotate about y-axis
  102. when "y" typed; "i" returns torus to original view */
  103. void keyboard(UGWindow uwin, int key, int x, int y)
  104. {
  105. switch (key) {
  106. case 'x':
  107. case 'X':
  108. glRotatef(30.,1.0,0.0,0.0);
  109. ugPostRedisplay(uwin);
  110. break;
  111. case 'y':
  112. case 'Y':
  113. glRotatef(30.,0.0,1.0,0.0);
  114. ugPostRedisplay(uwin);
  115. break;
  116. case 'i':
  117. case 'I':
  118. glLoadIdentity();
  119. ugluLookAtf(0, 0, 10, 0, 0, 0, 0, 1, 0);
  120. ugPostRedisplay(uwin);
  121. break;
  122. case 27:
  123. exit(0);
  124. break;
  125. }
  126. }
  127. int main(int argc, char **argv)
  128. {
  129. UGCtx ug = ugInit();
  130. UGWindow win = ugCreateWindow(ug, "", argv[0], 200, 200, 0, 0);
  131. init();
  132. ugReshapeFunc(win, reshape);
  133. ugKeyboardFunc(win, keyboard);
  134. ugDisplayFunc(win, display);
  135. ugMainLoop(ug);
  136. return 0;
  137. }