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.

zdrawpix.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Test glDrawPixels(GL_DEPTH_COMPONENT)
  3. *
  4. * We load a window-sized buffer of Z values so that Z=1 at the top and
  5. * Z=0 at the bottom (and interpolate between).
  6. * We draw that image into the Z buffer, then draw an ordinary cube.
  7. * The bottom part of the cube should be "clipped" where the cube fails
  8. * the Z test.
  9. *
  10. * Press 'd' to view the Z buffer as a grayscale image.
  11. */
  12. #define GL_GLEXT_PROTOTYPES
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <GL/glut.h>
  17. #include "../util/showbuffer.c"
  18. static int Win;
  19. static GLfloat Xrot = 50, Yrot = 40, Zpos = 6;
  20. static GLboolean Anim = GL_FALSE;
  21. static int Width = 200, Height = 200;
  22. static GLfloat *z;
  23. static GLboolean showZ = 0;
  24. static void
  25. Idle(void)
  26. {
  27. Xrot += 3.0;
  28. Yrot += 4.0;
  29. glutPostRedisplay();
  30. }
  31. static void
  32. Draw(void)
  33. {
  34. glClearColor(0, 0, 0.5, 0);
  35. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  36. #if 1
  37. glColor3f(1, 0, 0);
  38. glWindowPos2i(0,0);
  39. glColorMask(0,0,0,0);
  40. glDrawPixels(Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, z);
  41. #elif 0
  42. glPushMatrix();
  43. glTranslatef(-0.75, 0, Zpos);
  44. glutSolidSphere(1.0, 20, 10);
  45. glPopMatrix();
  46. #endif
  47. glColorMask(1,1,1,1);
  48. /* draw cube */
  49. glPushMatrix();
  50. glTranslatef(0, 0, Zpos);
  51. glRotatef(Xrot, 1, 0, 0);
  52. glRotatef(Yrot, 0, 1, 0);
  53. glutSolidCube(2.0);
  54. glPopMatrix();
  55. #if 0
  56. /* drawpixels after cube */
  57. glColor3f(1, 0, 0);
  58. glWindowPos2i(0,0);
  59. //glColorMask(0,0,0,0);
  60. glDrawPixels(Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, z);
  61. #endif
  62. if (showZ) {
  63. ShowDepthBuffer(Width, Height, 0.0, 1.0);
  64. }
  65. glutSwapBuffers();
  66. }
  67. static void
  68. Reshape(int width, int height)
  69. {
  70. glViewport(0, 0, width, height);
  71. glMatrixMode(GL_PROJECTION);
  72. glLoadIdentity();
  73. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 30.0);
  74. glMatrixMode(GL_MODELVIEW);
  75. glLoadIdentity();
  76. glTranslatef(0.0, 0.0, -15.0);
  77. Width = width;
  78. Height = height;
  79. z = (float *) malloc(width * height * 4);
  80. {
  81. int i, j, k = 0;
  82. for (i = 0; i < height; i++) {
  83. float zval = (float) i / (height - 1);
  84. for (j = 0; j < width; j++) {
  85. z[k++] = zval;
  86. }
  87. }
  88. }
  89. }
  90. static void
  91. Key(unsigned char key, int x, int y)
  92. {
  93. const GLfloat step = 1.0;
  94. (void) x;
  95. (void) y;
  96. switch (key) {
  97. case 'a':
  98. Anim = !Anim;
  99. if (Anim)
  100. glutIdleFunc(Idle);
  101. else
  102. glutIdleFunc(NULL);
  103. break;
  104. case 'd':
  105. showZ = !showZ;
  106. break;
  107. case 'z':
  108. Zpos -= step;
  109. break;
  110. case 'Z':
  111. Zpos += step;
  112. break;
  113. case 27:
  114. glutDestroyWindow(Win);
  115. exit(0);
  116. break;
  117. }
  118. glutPostRedisplay();
  119. }
  120. static void
  121. SpecialKey(int key, int x, int y)
  122. {
  123. const GLfloat step = 3.0;
  124. (void) x;
  125. (void) y;
  126. switch (key) {
  127. case GLUT_KEY_UP:
  128. Xrot -= step;
  129. break;
  130. case GLUT_KEY_DOWN:
  131. Xrot += step;
  132. break;
  133. case GLUT_KEY_LEFT:
  134. Yrot -= step;
  135. break;
  136. case GLUT_KEY_RIGHT:
  137. Yrot += step;
  138. break;
  139. }
  140. glutPostRedisplay();
  141. }
  142. static void
  143. Init(void)
  144. {
  145. /* setup lighting, etc */
  146. glEnable(GL_DEPTH_TEST);
  147. glEnable(GL_LIGHTING);
  148. glEnable(GL_LIGHT0);
  149. }
  150. int
  151. main(int argc, char *argv[])
  152. {
  153. glutInit(&argc, argv);
  154. glutInitWindowPosition(0, 0);
  155. glutInitWindowSize(400, 400);
  156. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  157. Win = glutCreateWindow(argv[0]);
  158. glutReshapeFunc(Reshape);
  159. glutKeyboardFunc(Key);
  160. glutSpecialFunc(SpecialKey);
  161. glutDisplayFunc(Draw);
  162. if (Anim)
  163. glutIdleFunc(Idle);
  164. Init();
  165. glutMainLoop();
  166. return 0;
  167. }