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.

zcomp.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * Test Z compositing with glDrawPixels(GL_DEPTH_COMPONENT) and stencil test.
  3. */
  4. #define GL_GLEXT_PROTOTYPES
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <GL/glut.h>
  9. #include "../util/showbuffer.c"
  10. static int Win;
  11. static GLfloat Xrot = 0, Yrot = 0, Zpos = 6;
  12. static GLboolean Anim = GL_FALSE;
  13. static int Width = 400, Height = 200;
  14. static GLfloat *Zimg;
  15. static GLubyte *Cimg;
  16. static GLboolean showZ = 0;
  17. static void
  18. Idle(void)
  19. {
  20. Xrot += 3.0;
  21. Yrot += 4.0;
  22. glutPostRedisplay();
  23. }
  24. /**
  25. * Draw first object, save color+Z images
  26. */
  27. static void
  28. DrawFirst(void)
  29. {
  30. static const GLfloat red[4] = { 1.0, 0.0, 0.0, 0.0 };
  31. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
  32. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  33. glPushMatrix();
  34. glTranslatef(-1, 0, 0);
  35. glRotatef(45 + Xrot, 1, 0, 0);
  36. glutSolidTorus(0.75, 2.0, 10, 20);
  37. glPopMatrix();
  38. glReadPixels(0, 0, Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, Zimg);
  39. glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, Cimg);
  40. }
  41. /**
  42. * Draw second object.
  43. */
  44. static void
  45. DrawSecond(void)
  46. {
  47. static const GLfloat blue[4] = { 0.0, 0.0, 1.0, 0.0 };
  48. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
  49. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  50. glPushMatrix();
  51. glTranslatef(+1, 0, 0);
  52. glRotatef(-45 + Xrot, 1, 0, 0);
  53. glutSolidTorus(0.75, 2.0, 10, 20);
  54. glPopMatrix();
  55. }
  56. /**
  57. * Composite first/saved image over second rendering.
  58. */
  59. static void
  60. Composite(void)
  61. {
  62. glWindowPos2i(0, 0);
  63. /* Draw Z values, set stencil where Z test passes */
  64. glEnable(GL_STENCIL_TEST);
  65. glStencilFunc(GL_ALWAYS, 1, ~0);
  66. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  67. glColorMask(0,0,0,0);
  68. glDrawPixels(Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, Zimg);
  69. glColorMask(1,1,1,1);
  70. /* Draw color where stencil==1 */
  71. glStencilFunc(GL_EQUAL, 1, ~0);
  72. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  73. glDisable(GL_DEPTH_TEST);
  74. glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, Cimg);
  75. glEnable(GL_DEPTH_TEST);
  76. glDisable(GL_STENCIL_TEST);
  77. }
  78. static void
  79. Draw(void)
  80. {
  81. DrawFirst();
  82. DrawSecond();
  83. Composite();
  84. glutSwapBuffers();
  85. }
  86. static void
  87. Reshape(int width, int height)
  88. {
  89. GLfloat ar = (float) width / height;
  90. glViewport(0, 0, width, height);
  91. glMatrixMode(GL_PROJECTION);
  92. glLoadIdentity();
  93. glFrustum(-ar, ar, -1.0, 1.0, 5.0, 30.0);
  94. glMatrixMode(GL_MODELVIEW);
  95. glLoadIdentity();
  96. glTranslatef(0.0, 0.0, -15.0);
  97. Width = width;
  98. Height = height;
  99. if (Zimg)
  100. free(Zimg);
  101. if (Cimg)
  102. free(Cimg);
  103. Zimg = (float *) malloc(width * height * 4);
  104. Cimg = (GLubyte *) malloc(width * height * 4);
  105. }
  106. static void
  107. Key(unsigned char key, int x, int y)
  108. {
  109. const GLfloat step = 1.0;
  110. (void) x;
  111. (void) y;
  112. switch (key) {
  113. case 'a':
  114. Anim = !Anim;
  115. if (Anim)
  116. glutIdleFunc(Idle);
  117. else
  118. glutIdleFunc(NULL);
  119. break;
  120. case 'd':
  121. showZ = !showZ;
  122. break;
  123. case 'z':
  124. Zpos -= step;
  125. break;
  126. case 'Z':
  127. Zpos += step;
  128. break;
  129. case 27:
  130. glutDestroyWindow(Win);
  131. exit(0);
  132. break;
  133. }
  134. glutPostRedisplay();
  135. }
  136. static void
  137. SpecialKey(int key, int x, int y)
  138. {
  139. const GLfloat step = 3.0;
  140. (void) x;
  141. (void) y;
  142. switch (key) {
  143. case GLUT_KEY_UP:
  144. Xrot -= step;
  145. break;
  146. case GLUT_KEY_DOWN:
  147. Xrot += step;
  148. break;
  149. case GLUT_KEY_LEFT:
  150. Yrot -= step;
  151. break;
  152. case GLUT_KEY_RIGHT:
  153. Yrot += step;
  154. break;
  155. }
  156. glutPostRedisplay();
  157. }
  158. static void
  159. Init(void)
  160. {
  161. /* setup lighting, etc */
  162. glEnable(GL_DEPTH_TEST);
  163. glEnable(GL_LIGHTING);
  164. glEnable(GL_LIGHT0);
  165. }
  166. int
  167. main(int argc, char *argv[])
  168. {
  169. glutInit(&argc, argv);
  170. glutInitWindowPosition(0, 0);
  171. glutInitWindowSize(Width, Height);
  172. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
  173. Win = glutCreateWindow(argv[0]);
  174. glutReshapeFunc(Reshape);
  175. glutKeyboardFunc(Key);
  176. glutSpecialFunc(SpecialKey);
  177. glutDisplayFunc(Draw);
  178. if (Anim)
  179. glutIdleFunc(Idle);
  180. Init();
  181. glutMainLoop();
  182. return 0;
  183. }