Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

showbuffer.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. glDisable(GL_STENCIL_TEST);
  60. glDisable(GL_DEPTH_TEST);
  61. glRasterPos2f(0, 0);
  62. glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_FLOAT, depthValues);
  63. glPopMatrix();
  64. glMatrixMode(GL_PROJECTION);
  65. glPopMatrix();
  66. free(depthValues);
  67. glPopAttrib();
  68. }
  69. /*
  70. * Copy the alpha channel values into the current color buffer as a
  71. * grayscale image.
  72. * Input: winWidth, winHeight - size of the window
  73. */
  74. void
  75. ShowAlphaBuffer( GLsizei winWidth, GLsizei winHeight )
  76. {
  77. GLubyte *alphaValues;
  78. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  79. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  80. /* Read alpha values */
  81. alphaValues = (GLubyte *) malloc(winWidth * winHeight * sizeof(GLubyte));
  82. assert(alphaValues);
  83. glReadPixels(0, 0, winWidth, winHeight, GL_ALPHA, GL_UNSIGNED_BYTE, alphaValues);
  84. /* save GL state */
  85. glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL |
  86. GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
  87. /* setup raster pos for glDrawPixels */
  88. glMatrixMode(GL_PROJECTION);
  89. glPushMatrix();
  90. glLoadIdentity();
  91. glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
  92. glMatrixMode(GL_MODELVIEW);
  93. glPushMatrix();
  94. glLoadIdentity();
  95. glDisable(GL_STENCIL_TEST);
  96. glDisable(GL_DEPTH_TEST);
  97. glRasterPos2f(0, 0);
  98. glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, alphaValues);
  99. glPopMatrix();
  100. glMatrixMode(GL_PROJECTION);
  101. glPopMatrix();
  102. free(alphaValues);
  103. glPopAttrib();
  104. }
  105. /*
  106. * Copy the stencil buffer values into the current color buffer as a
  107. * grayscale image.
  108. * Input: winWidth, winHeight - size of the window
  109. * scale, bias - scale and bias to apply to stencil values for display
  110. */
  111. void
  112. ShowStencilBuffer( GLsizei winWidth, GLsizei winHeight,
  113. GLfloat scale, GLfloat bias )
  114. {
  115. GLubyte *stencilValues;
  116. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  117. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  118. /* Read stencil values */
  119. stencilValues = (GLubyte *) malloc(winWidth * winHeight * sizeof(GLubyte));
  120. assert(stencilValues);
  121. glReadPixels(0, 0, winWidth, winHeight, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencilValues);
  122. /* save GL state */
  123. glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT |
  124. GL_PIXEL_MODE_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
  125. /* setup raster pos for glDrawPixels */
  126. glMatrixMode(GL_PROJECTION);
  127. glPushMatrix();
  128. glLoadIdentity();
  129. glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
  130. glMatrixMode(GL_MODELVIEW);
  131. glPushMatrix();
  132. glLoadIdentity();
  133. glDisable(GL_STENCIL_TEST);
  134. glDisable(GL_DEPTH_TEST);
  135. glRasterPos2f(0, 0);
  136. glPixelTransferf(GL_RED_SCALE, scale);
  137. glPixelTransferf(GL_RED_BIAS, bias);
  138. glPixelTransferf(GL_GREEN_SCALE, scale);
  139. glPixelTransferf(GL_GREEN_BIAS, bias);
  140. glPixelTransferf(GL_BLUE_SCALE, scale);
  141. glPixelTransferf(GL_BLUE_BIAS, bias);
  142. glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, stencilValues);
  143. glPopMatrix();
  144. glMatrixMode(GL_PROJECTION);
  145. glPopMatrix();
  146. free(stencilValues);
  147. glPopAttrib();
  148. }