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
glDisable(GL_STENCIL_TEST);
75
glDisable(GL_DEPTH_TEST);
78
glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_FLOAT, depthValues);
81
glMatrixMode(GL_PROJECTION);
92
* Copy the alpha channel values into the current color buffer as a
94
* Input: winWidth, winHeight - size of the window
97
ShowAlphaBuffer( GLsizei winWidth, GLsizei winHeight )
101
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
102
glPixelStorei(GL_PACK_ALIGNMENT, 1);
104
/* Read alpha values */
105
alphaValues = (GLubyte *) malloc(winWidth * winHeight * sizeof(GLubyte));
107
glReadPixels(0, 0, winWidth, winHeight, GL_ALPHA, GL_UNSIGNED_BYTE, alphaValues);
110
glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL |
111
GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
113
/* setup raster pos for glDrawPixels */
114
glMatrixMode(GL_PROJECTION);
118
glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
119
glMatrixMode(GL_MODELVIEW);
123
glDisable(GL_STENCIL_TEST);
124
glDisable(GL_DEPTH_TEST);
127
glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, alphaValues);
130
glMatrixMode(GL_PROJECTION);
140
* Copy the stencil buffer values into the current color buffer as a
142
* Input: winWidth, winHeight - size of the window
143
* scale, bias - scale and bias to apply to stencil values for display
146
ShowStencilBuffer( GLsizei winWidth, GLsizei winHeight,
147
GLfloat scale, GLfloat bias )
149
GLubyte *stencilValues;
151
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
152
glPixelStorei(GL_PACK_ALIGNMENT, 1);
154
/* Read stencil values */
155
stencilValues = (GLubyte *) malloc(winWidth * winHeight * sizeof(GLubyte));
156
assert(stencilValues);
157
glReadPixels(0, 0, winWidth, winHeight, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencilValues);
160
glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT |
161
GL_PIXEL_MODE_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
163
/* setup raster pos for glDrawPixels */
164
glMatrixMode(GL_PROJECTION);
168
glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
169
glMatrixMode(GL_MODELVIEW);
173
glDisable(GL_STENCIL_TEST);
174
glDisable(GL_DEPTH_TEST);
177
glPixelTransferf(GL_RED_SCALE, scale);
178
glPixelTransferf(GL_RED_BIAS, bias);
179
glPixelTransferf(GL_GREEN_SCALE, scale);
180
glPixelTransferf(GL_GREEN_BIAS, bias);
181
glPixelTransferf(GL_BLUE_SCALE, scale);
182
glPixelTransferf(GL_BLUE_BIAS, bias);
184
glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, stencilValues);
187
glMatrixMode(GL_PROJECTION);