5 * Copy the depth buffer to the color buffer as a grayscale image.
6 * Useful for inspecting the depth buffer values.
8 * This program is in the public domain.
10 * Brian Paul November 4, 1998
17 #include "showbuffer.h"
22 * Copy the depth buffer values into the current color buffer as a
24 * Input: winWidth, winHeight - size of the window
25 * zBlack - the Z value which should map to black (usually 1)
26 * zWhite - the Z value which should map to white (usually 0)
29 ShowDepthBuffer( GLsizei winWidth
, GLsizei winHeight
,
30 GLfloat zBlack
, GLfloat zWhite
)
34 assert(zBlack
>= 0.0);
35 assert(zBlack
<= 1.0);
36 assert(zWhite
>= 0.0);
37 assert(zWhite
<= 1.0);
38 assert(zBlack
!= zWhite
);
40 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
41 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
43 /* Read depth values */
44 depthValues
= (GLfloat
*) malloc(winWidth
* winHeight
* sizeof(GLfloat
));
46 glReadPixels(0, 0, winWidth
, winHeight
, GL_DEPTH_COMPONENT
,
47 GL_FLOAT
, depthValues
);
49 /* Map Z values from [zBlack, zWhite] to gray levels in [0, 1] */
50 /* Not using glPixelTransfer() because it's broke on some systems! */
51 if (zBlack
!= 0.0 || zWhite
!= 1.0) {
52 GLfloat scale
= 1.0 / (zWhite
- zBlack
);
53 GLfloat bias
= -zBlack
* scale
;
54 int n
= winWidth
* winHeight
;
56 for (i
= 0; i
< n
; i
++)
57 depthValues
[i
] = depthValues
[i
] * scale
+ bias
;
61 glPushAttrib(GL_CURRENT_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
|
62 GL_TRANSFORM_BIT
| GL_VIEWPORT_BIT
);
64 /* setup raster pos for glDrawPixels */
65 glMatrixMode(GL_PROJECTION
);
69 glOrtho(0.0, (GLdouble
) winWidth
, 0.0, (GLdouble
) winHeight
, -1.0, 1.0);
70 glMatrixMode(GL_MODELVIEW
);
74 glViewport(0, 0, winWidth
, winHeight
);
76 glDisable(GL_STENCIL_TEST
);
77 glDisable(GL_DEPTH_TEST
);
80 glDrawPixels(winWidth
, winHeight
, GL_LUMINANCE
, GL_FLOAT
, depthValues
);
83 glMatrixMode(GL_PROJECTION
);
94 * Copy the alpha channel values into the current color buffer as a
96 * Input: winWidth, winHeight - size of the window
99 ShowAlphaBuffer( GLsizei winWidth
, GLsizei winHeight
)
101 GLubyte
*alphaValues
;
103 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
104 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
106 /* Read alpha values */
107 alphaValues
= (GLubyte
*) malloc(winWidth
* winHeight
* sizeof(GLubyte
));
109 glReadPixels(0, 0, winWidth
, winHeight
, GL_ALPHA
, GL_UNSIGNED_BYTE
, alphaValues
);
112 glPushAttrib(GL_CURRENT_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL
|
113 GL_TRANSFORM_BIT
| GL_VIEWPORT_BIT
);
115 /* setup raster pos for glDrawPixels */
116 glMatrixMode(GL_PROJECTION
);
120 glOrtho(0.0, (GLdouble
) winWidth
, 0.0, (GLdouble
) winHeight
, -1.0, 1.0);
121 glMatrixMode(GL_MODELVIEW
);
125 glViewport(0, 0, winWidth
, winHeight
);
127 glDisable(GL_STENCIL_TEST
);
128 glDisable(GL_DEPTH_TEST
);
131 glDrawPixels(winWidth
, winHeight
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, alphaValues
);
134 glMatrixMode(GL_PROJECTION
);
144 * Copy the stencil buffer values into the current color buffer as a
146 * Input: winWidth, winHeight - size of the window
147 * scale, bias - scale and bias to apply to stencil values for display
150 ShowStencilBuffer( GLsizei winWidth
, GLsizei winHeight
,
151 GLfloat scale
, GLfloat bias
)
153 GLubyte
*stencilValues
;
155 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
156 glPixelStorei(GL_PACK_ALIGNMENT
, 1);
158 /* Read stencil values */
159 stencilValues
= (GLubyte
*) malloc(winWidth
* winHeight
* sizeof(GLubyte
));
160 assert(stencilValues
);
161 glReadPixels(0, 0, winWidth
, winHeight
, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, stencilValues
);
164 glPushAttrib(GL_CURRENT_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
|
165 GL_PIXEL_MODE_BIT
| GL_TRANSFORM_BIT
| GL_VIEWPORT_BIT
);
167 /* setup raster pos for glDrawPixels */
168 glMatrixMode(GL_PROJECTION
);
172 glOrtho(0.0, (GLdouble
) winWidth
, 0.0, (GLdouble
) winHeight
, -1.0, 1.0);
173 glMatrixMode(GL_MODELVIEW
);
177 glViewport(0, 0, winWidth
, winHeight
);
179 glDisable(GL_STENCIL_TEST
);
180 glDisable(GL_DEPTH_TEST
);
183 glPixelTransferf(GL_RED_SCALE
, scale
);
184 glPixelTransferf(GL_RED_BIAS
, bias
);
185 glPixelTransferf(GL_GREEN_SCALE
, scale
);
186 glPixelTransferf(GL_GREEN_BIAS
, bias
);
187 glPixelTransferf(GL_BLUE_SCALE
, scale
);
188 glPixelTransferf(GL_BLUE_BIAS
, bias
);
190 glDrawPixels(winWidth
, winHeight
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, stencilValues
);
193 glMatrixMode(GL_PROJECTION
);