Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

quads.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * Draw colored quads.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <GL/glew.h>
  8. #include <GL/glut.h>
  9. #define NUM_QUADS 20
  10. static int Win;
  11. static GLfloat Xrot = 40, Yrot = 0, Zrot = 0;
  12. static GLboolean Anim = GL_TRUE;
  13. static GLuint Vbuffer = 0;
  14. #if 1
  15. #else
  16. static GLfloat buf[NUM_QUADS * 6 * 4];
  17. #endif
  18. static GLboolean doSwapBuffers = GL_TRUE;
  19. static GLint Frames = 0, T0 = 0;
  20. static void
  21. Idle(void)
  22. {
  23. Xrot += 3.0;
  24. Yrot += 4.0;
  25. Zrot += 2.0;
  26. glutPostRedisplay();
  27. }
  28. static void
  29. Draw(void)
  30. {
  31. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  32. glPushMatrix();
  33. glRotatef(Xrot, 1, 0, 0);
  34. glRotatef(Yrot, 0, 1, 0);
  35. glRotatef(Zrot, 0, 0, 1);
  36. glDrawArrays(GL_QUADS, 0, NUM_QUADS*4);
  37. glPopMatrix();
  38. if (doSwapBuffers)
  39. glutSwapBuffers();
  40. /*
  41. else
  42. glFinish();
  43. */
  44. {
  45. GLint t = glutGet(GLUT_ELAPSED_TIME);
  46. Frames++;
  47. if (t - T0 >= 5000) {
  48. GLfloat seconds = (t - T0) / 1000.0;
  49. GLfloat fps = Frames / seconds;
  50. printf("%d frames in %6.3f seconds = %6.3f FPS\n",
  51. Frames, seconds, fps);
  52. T0 = t;
  53. Frames = 0;
  54. }
  55. }
  56. }
  57. static void
  58. Reshape(int width, int height)
  59. {
  60. glViewport(0, 0, width, height);
  61. glMatrixMode(GL_PROJECTION);
  62. glLoadIdentity();
  63. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  64. glMatrixMode(GL_MODELVIEW);
  65. glLoadIdentity();
  66. glTranslatef(0.0, 0.0, -8.0);
  67. }
  68. static void
  69. Key(unsigned char key, int x, int y)
  70. {
  71. const GLfloat step = 3.0;
  72. (void) x;
  73. (void) y;
  74. switch (key) {
  75. case 's':
  76. doSwapBuffers = !doSwapBuffers;
  77. break;
  78. case 'a':
  79. Anim = !Anim;
  80. if (Anim)
  81. glutIdleFunc(Idle);
  82. else
  83. glutIdleFunc(NULL);
  84. break;
  85. case 'z':
  86. Zrot -= step;
  87. break;
  88. case 'Z':
  89. Zrot += step;
  90. break;
  91. case 27:
  92. glutDestroyWindow(Win);
  93. exit(0);
  94. break;
  95. }
  96. glutPostRedisplay();
  97. }
  98. static void
  99. SpecialKey(int key, int x, int y)
  100. {
  101. const GLfloat step = 3.0;
  102. (void) x;
  103. (void) y;
  104. switch (key) {
  105. case GLUT_KEY_UP:
  106. Xrot -= step;
  107. break;
  108. case GLUT_KEY_DOWN:
  109. Xrot += step;
  110. break;
  111. case GLUT_KEY_LEFT:
  112. Yrot -= step;
  113. break;
  114. case GLUT_KEY_RIGHT:
  115. Yrot += step;
  116. break;
  117. }
  118. glutPostRedisplay();
  119. }
  120. static void
  121. quad(float x, float y, float z, float *v)
  122. {
  123. int k = 0;
  124. /* color */
  125. v[k++] = x * 0.5 + 0.5;
  126. v[k++] = y * 0.5 + 0.5;
  127. v[k++] = z * 0.5 + 0.5;
  128. /* vert */
  129. v[k++] = x;
  130. v[k++] = y;
  131. v[k++] = z;
  132. /* color */
  133. v[k++] = -x * 0.5 + 0.5;
  134. v[k++] = -y * 0.5 + 0.5;
  135. v[k++] = z * 0.5 + 0.5;
  136. /* vert */
  137. v[k++] = -x;
  138. v[k++] = -y;
  139. v[k++] = z;
  140. /* color */
  141. v[k++] = -x * 0.5 + 0.5;
  142. v[k++] = -y * 0.5 + 0.5;
  143. v[k++] = -z * 0.5 + 0.5;
  144. /* vert */
  145. v[k++] = -x;
  146. v[k++] = -y;
  147. v[k++] = -z;
  148. /* color */
  149. v[k++] = x * 0.5 + 0.5;
  150. v[k++] = y * 0.5 + 0.5;
  151. v[k++] = -z * 0.5 + 0.5;
  152. /* vert */
  153. v[k++] = x;
  154. v[k++] = y;
  155. v[k++] = -z;
  156. }
  157. static void
  158. gen_quads(GLfloat *buf)
  159. {
  160. float *v = buf;
  161. float r = 1.0;
  162. int i;
  163. for (i = 0; i < NUM_QUADS; i++) {
  164. float angle = i / (float) NUM_QUADS * M_PI;
  165. float x = r * cos(angle);
  166. float y = r * sin(angle);
  167. float z = 1.10;
  168. quad(x, y, z, v);
  169. v += 24;
  170. }
  171. if (0) {
  172. float *p = buf;
  173. for (i = 0; i < NUM_QUADS * 4 * 2; i++) {
  174. printf("%d: %f %f %f\n", i, p[0], p[1], p[2]);
  175. p += 3;
  176. }
  177. }
  178. }
  179. static void
  180. Init(void)
  181. {
  182. int bytes = NUM_QUADS * 4 * 2 * 3 * sizeof(float);
  183. GLfloat *f;
  184. #if 1
  185. glGenBuffers(1, &Vbuffer);
  186. glBindBuffer(GL_ARRAY_BUFFER, Vbuffer);
  187. glBufferData(GL_ARRAY_BUFFER_ARB, bytes, NULL, GL_STATIC_DRAW_ARB);
  188. f = (float *) glMapBuffer(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
  189. gen_quads(f);
  190. glUnmapBuffer(GL_ARRAY_BUFFER_ARB);
  191. glColorPointer(3, GL_FLOAT, 6*sizeof(float), (void *) 0);
  192. glVertexPointer(3, GL_FLOAT, 6*sizeof(float), (void *) 12);
  193. #else
  194. f = buf;
  195. gen_quads(f);
  196. glColorPointer(3, GL_FLOAT, 6*sizeof(float), buf);
  197. glVertexPointer(3, GL_FLOAT, 6*sizeof(float), buf + 3);
  198. #endif
  199. glEnableClientState(GL_COLOR_ARRAY);
  200. glEnableClientState(GL_VERTEX_ARRAY);
  201. glEnable(GL_DEPTH_TEST);
  202. glClearColor(0.5, 0.5, 0.5, 0.0);
  203. }
  204. int
  205. main(int argc, char *argv[])
  206. {
  207. glutInit(&argc, argv);
  208. glutInitWindowPosition(0, 0);
  209. glutInitWindowSize(600, 600);
  210. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  211. Win = glutCreateWindow(argv[0]);
  212. glewInit();
  213. glutReshapeFunc(Reshape);
  214. glutKeyboardFunc(Key);
  215. glutSpecialFunc(SpecialKey);
  216. glutDisplayFunc(Draw);
  217. if (Anim)
  218. glutIdleFunc(Idle);
  219. Init();
  220. glutMainLoop();
  221. return 0;
  222. }