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.

showbuffer.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* showbuffer.c */
  2. /*
  3. * Copy the depth buffer to the color buffer as a grayscale image.
  4. * Useful for inspecting the depth buffer values.
  5. *
  6. * This program is in the public domain.
  7. *
  8. * Brian Paul November 4, 1998
  9. */
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <GL/gl.h>
  13. #include "showbuffer.h"
  14. /*
  15. * Copy the depth buffer values into the current color buffer as a
  16. * grayscale image.
  17. * Input: winWidth, winHeight - size of the window
  18. * zBlack - the Z value which should map to black (usually 1)
  19. * zWhite - the Z value which should map to white (usually 0)
  20. */
  21. void
  22. ShowDepthBuffer( GLsizei winWidth, GLsizei winHeight,
  23. GLfloat zBlack, GLfloat zWhite )
  24. {
  25. GLfloat *depthValues;
  26. assert(zBlack >= 0.0);
  27. assert(zBlack <= 1.0);
  28. assert(zWhite >= 0.0);
  29. assert(zWhite <= 1.0);
  30. assert(zBlack != zWhite);
  31. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  32. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  33. /* Read depth values */
  34. depthValues = (GLfloat *) malloc(winWidth * winHeight * sizeof(GLfloat));
  35. assert(depthValues);
  36. glReadPixels(0, 0, winWidth, winHeight, GL_DEPTH_COMPONENT,
  37. GL_FLOAT, depthValues);
  38. /* Map Z values from [zBlack, zWhite] to gray levels in [0, 1] */
  39. /* Not using glPixelTransfer() because it's broke on some systems! */
  40. if (zBlack != 0.0 || zWhite != 1.0) {
  41. GLfloat scale = 1.0 / (zWhite - zBlack);
  42. GLfloat bias = -zBlack * scale;
  43. int n = winWidth * winHeight;
  44. int i;
  45. for (i = 0; i < n; i++)
  46. depthValues[i] = depthValues[i] * scale + bias;
  47. }
  48. /* save GL state */
  49. glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT |
  50. GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
  51. /* setup raster pos for glDrawPixels */
  52. glMatrixMode(GL_PROJECTION);
  53. glPushMatrix();
  54. glLoadIdentity();
  55. glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
  56. glMatrixMode(GL_MODELVIEW);
  57. glPushMatrix();
  58. glLoadIdentity();
  59. glViewport(0, 0, winWidth, winHeight);
  60. glDisable(GL_STENCIL_TEST);
  61. glDisable(GL_DEPTH_TEST);
  62. glRasterPos2f(0, 0);
  63. glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_FLOAT, depthValues);
  64. glPopMatrix();
  65. glMatrixMode(GL_PROJECTION);
  66. glPopMatrix();
  67. free(depthValues);
  68. glPopAttrib();
  69. }
  70. /*
  71. * Copy the alpha channel values into the current color buffer as a
  72. * grayscale image.
  73. * Input: winWidth, winHeight - size of the window
  74. */
  75. void
  76. ShowAlphaBuffer( GLsizei winWidth, GLsizei winHeight )
  77. {
  78. GLubyte *alphaValues;
  79. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  80. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  81. /* Read alpha values */
  82. alphaValues = (GLubyte *) malloc(winWidth * winHeight * sizeof(GLubyte));
  83. assert(alphaValues);
  84. glReadPixels(0, 0, winWidth, winHeight, GL_ALPHA, GL_UNSIGNED_BYTE, alphaValues);
  85. /* save GL state */
  86. glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL |
  87. GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
  88. /* setup raster pos for glDrawPixels */
  89. glMatrixMode(GL_PROJECTION);
  90. glPushMatrix();
  91. glLoadIdentity();
  92. glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
  93. glMatrixMode(GL_MODELVIEW);
  94. glPushMatrix();
  95. glLoadIdentity();
  96. glViewport(0, 0, winWidth, winHeight);
  97. glDisable(GL_STENCIL_TEST);
  98. glDisable(GL_DEPTH_TEST);
  99. glRasterPos2f(0, 0);
  100. glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, alphaValues);
  101. glPopMatrix();
  102. glMatrixMode(GL_PROJECTION);
  103. glPopMatrix();
  104. free(alphaValues);
  105. glPopAttrib();
  106. }
  107. /*
  108. * Copy the stencil buffer values into the current color buffer as a
  109. * grayscale image.
  110. * Input: winWidth, winHeight - size of the window
  111. * scale, bias - scale and bias to apply to stencil values for display
  112. */
  113. void
  114. ShowStencilBuffer( GLsizei winWidth, GLsizei winHeight,
  115. GLfloat scale, GLfloat bias )
  116. {
  117. GLubyte *stencilValues;
  118. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  119. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  120. /* Read stencil values */
  121. stencilValues = (GLubyte *) malloc(winWidth * winHeight * sizeof(GLubyte));
  122. assert(stencilValues);
  123. glReadPixels(0, 0, winWidth, winHeight, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencilValues);
  124. /* save GL state */
  125. glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT |
  126. GL_PIXEL_MODE_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
  127. /* setup raster pos for glDrawPixels */
  128. glMatrixMode(GL_PROJECTION);
  129. glPushMatrix();
  130. glLoadIdentity();
  131. glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
  132. glMatrixMode(GL_MODELVIEW);
  133. glPushMatrix();
  134. glLoadIdentity();
  135. glViewport(0, 0, winWidth, winHeight);
  136. glDisable(GL_STENCIL_TEST);
  137. glDisable(GL_DEPTH_TEST);
  138. glRasterPos2f(0, 0);
  139. glPixelTransferf(GL_RED_SCALE, scale);
  140. glPixelTransferf(GL_RED_BIAS, bias);
  141. glPixelTransferf(GL_GREEN_SCALE, scale);
  142. glPixelTransferf(GL_GREEN_BIAS, bias);
  143. glPixelTransferf(GL_BLUE_SCALE, scale);
  144. glPixelTransferf(GL_BLUE_BIAS, bias);
  145. glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, stencilValues);
  146. glPopMatrix();
  147. glMatrixMode(GL_PROJECTION);
  148. glPopMatrix();
  149. free(stencilValues);
  150. glPopAttrib();
  151. }