summaryrefslogtreecommitdiff
path: root/progs/util/showbuffer.c
blob: b0cedea217c3dba90cde374c241023cbc1016951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* showbuffer.c */


/*
 * Copy the depth buffer to the color buffer as a grayscale image.
 * Useful for inspecting the depth buffer values.
 *
 * This program is in the public domain.
 *
 * Brian Paul   November 4, 1998
 */


#include <assert.h>
#include <stdlib.h>
#include <GL/gl.h>
#include "showbuffer.h"



/*
 * Copy the depth buffer values into the current color buffer as a
 * grayscale image.
 * Input:  winWidth, winHeight - size of the window
 *         zBlack - the Z value which should map to black (usually 1)
 *         zWhite - the Z value which should map to white (usually 0)
 */
void
ShowDepthBuffer( GLsizei winWidth, GLsizei winHeight,
                 GLfloat zBlack, GLfloat zWhite )
{
   GLfloat *depthValues;

   assert(zBlack >= 0.0);
   assert(zBlack <= 1.0);
   assert(zWhite >= 0.0);
   assert(zWhite <= 1.0);
   assert(zBlack != zWhite);

   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glPixelStorei(GL_PACK_ALIGNMENT, 1);

   /* Read depth values */
   depthValues = (GLfloat *) malloc(winWidth * winHeight * sizeof(GLfloat));
   assert(depthValues);
   glReadPixels(0, 0, winWidth, winHeight, GL_DEPTH_COMPONENT,
                GL_FLOAT, depthValues);

   /* Map Z values from [zBlack, zWhite] to gray levels in [0, 1] */
   /* Not using glPixelTransfer() because it's broke on some systems! */
   if (zBlack != 0.0 || zWhite != 1.0) {
      GLfloat scale = 1.0 / (zWhite - zBlack);
      GLfloat bias = -zBlack * scale;
      int n = winWidth * winHeight;
      int i;
      for (i = 0; i < n; i++)
         depthValues[i] = depthValues[i] * scale + bias;
   }

   /* save GL state */
   glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT |
                GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);

   /* setup raster pos for glDrawPixels */
   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();

   glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glLoadIdentity();

   glViewport(0, 0, winWidth, winHeight);

   glDisable(GL_STENCIL_TEST);
   glDisable(GL_DEPTH_TEST);
   glRasterPos2f(0, 0);

   glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_FLOAT, depthValues);

   glPopMatrix();
   glMatrixMode(GL_PROJECTION);
   glPopMatrix();
   free(depthValues);

   glPopAttrib();
}




/*
 * Copy the alpha channel values into the current color buffer as a
 * grayscale image.
 * Input:  winWidth, winHeight - size of the window
 */
void
ShowAlphaBuffer( GLsizei winWidth, GLsizei winHeight )
{
   GLubyte *alphaValues;

   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glPixelStorei(GL_PACK_ALIGNMENT, 1);

   /* Read alpha values */
   alphaValues = (GLubyte *) malloc(winWidth * winHeight * sizeof(GLubyte));
   assert(alphaValues);
   glReadPixels(0, 0, winWidth, winHeight, GL_ALPHA, GL_UNSIGNED_BYTE, alphaValues);

   /* save GL state */
   glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL |
                GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);

   /* setup raster pos for glDrawPixels */
   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();

   glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glLoadIdentity();

   glViewport(0, 0, winWidth, winHeight);

   glDisable(GL_STENCIL_TEST);
   glDisable(GL_DEPTH_TEST);
   glRasterPos2f(0, 0);

   glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, alphaValues);

   glPopMatrix();
   glMatrixMode(GL_PROJECTION);
   glPopMatrix();
   free(alphaValues);

   glPopAttrib();
}



/*
 * Copy the stencil buffer values into the current color buffer as a
 * grayscale image.
 * Input:  winWidth, winHeight - size of the window
 *         scale, bias - scale and bias to apply to stencil values for display
 */
void
ShowStencilBuffer( GLsizei winWidth, GLsizei winHeight,
                   GLfloat scale, GLfloat bias )
{
   GLubyte *stencilValues;

   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glPixelStorei(GL_PACK_ALIGNMENT, 1);

   /* Read stencil values */
   stencilValues = (GLubyte *) malloc(winWidth * winHeight * sizeof(GLubyte));
   assert(stencilValues);
   glReadPixels(0, 0, winWidth, winHeight, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencilValues);

   /* save GL state */
   glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT |
                GL_PIXEL_MODE_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);

   /* setup raster pos for glDrawPixels */
   glMatrixMode(GL_PROJECTION);
   glPushMatrix();
   glLoadIdentity();

   glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
   glPushMatrix();
   glLoadIdentity();

   glViewport(0, 0, winWidth, winHeight);

   glDisable(GL_STENCIL_TEST);
   glDisable(GL_DEPTH_TEST);
   glRasterPos2f(0, 0);

   glPixelTransferf(GL_RED_SCALE, scale);
   glPixelTransferf(GL_RED_BIAS, bias);
   glPixelTransferf(GL_GREEN_SCALE, scale);
   glPixelTransferf(GL_GREEN_BIAS, bias);
   glPixelTransferf(GL_BLUE_SCALE, scale);
   glPixelTransferf(GL_BLUE_BIAS, bias);

   glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, stencilValues);

   glPopMatrix();
   glMatrixMode(GL_PROJECTION);
   glPopMatrix();
   free(stencilValues);

   glPopAttrib();
}