jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | /* showbuffer.c */ |
| 2 | |
| 3 | |
| 4 | /* |
| 5 | * Copy the depth buffer to the color buffer as a grayscale image. |
| 6 | * Useful for inspecting the depth buffer values. |
| 7 | * |
| 8 | * This program is in the public domain. |
| 9 | * |
| 10 | * Brian Paul November 4, 1998 |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <assert.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <GL/gl.h> |
| 17 | #include "showbuffer.h" |
| 18 | |
| 19 | |
| 20 | |
| 21 | /* |
| 22 | * Copy the depth buffer values into the current color buffer as a |
| 23 | * grayscale image. |
| 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) |
| 27 | */ |
| 28 | void |
| 29 | ShowDepthBuffer( GLsizei winWidth, GLsizei winHeight, |
| 30 | GLfloat zBlack, GLfloat zWhite ) |
| 31 | { |
| 32 | GLfloat *depthValues; |
| 33 | |
| 34 | assert(zBlack >= 0.0); |
| 35 | assert(zBlack <= 1.0); |
| 36 | assert(zWhite >= 0.0); |
| 37 | assert(zWhite <= 1.0); |
| 38 | assert(zBlack != zWhite); |
| 39 | |
| 40 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 41 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 42 | |
| 43 | /* Read depth values */ |
| 44 | depthValues = (GLfloat *) malloc(winWidth * winHeight * sizeof(GLfloat)); |
| 45 | assert(depthValues); |
| 46 | glReadPixels(0, 0, winWidth, winHeight, GL_DEPTH_COMPONENT, |
| 47 | GL_FLOAT, depthValues); |
| 48 | |
| 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; |
| 55 | int i; |
| 56 | for (i = 0; i < n; i++) |
| 57 | depthValues[i] = depthValues[i] * scale + bias; |
| 58 | } |
| 59 | |
| 60 | /* save GL state */ |
| 61 | glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | |
| 62 | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT); |
| 63 | |
| 64 | /* setup raster pos for glDrawPixels */ |
| 65 | glMatrixMode(GL_PROJECTION); |
| 66 | glPushMatrix(); |
| 67 | glLoadIdentity(); |
| 68 | |
| 69 | glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0); |
| 70 | glMatrixMode(GL_MODELVIEW); |
| 71 | glPushMatrix(); |
| 72 | glLoadIdentity(); |
| 73 | |
| 74 | glDisable(GL_STENCIL_TEST); |
| 75 | glDisable(GL_DEPTH_TEST); |
| 76 | glRasterPos2f(0, 0); |
| 77 | |
| 78 | glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_FLOAT, depthValues); |
| 79 | |
| 80 | glPopMatrix(); |
| 81 | glMatrixMode(GL_PROJECTION); |
| 82 | glPopMatrix(); |
| 83 | free(depthValues); |
| 84 | |
| 85 | glPopAttrib(); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | * Copy the alpha channel values into the current color buffer as a |
| 93 | * grayscale image. |
| 94 | * Input: winWidth, winHeight - size of the window |
| 95 | */ |
| 96 | void |
| 97 | ShowAlphaBuffer( GLsizei winWidth, GLsizei winHeight ) |
| 98 | { |
| 99 | GLubyte *alphaValues; |
| 100 | |
| 101 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 102 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 103 | |
| 104 | /* Read alpha values */ |
| 105 | alphaValues = (GLubyte *) malloc(winWidth * winHeight * sizeof(GLubyte)); |
| 106 | assert(alphaValues); |
| 107 | glReadPixels(0, 0, winWidth, winHeight, GL_ALPHA, GL_UNSIGNED_BYTE, alphaValues); |
| 108 | |
| 109 | /* save GL state */ |
| 110 | glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL | |
| 111 | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT); |
| 112 | |
| 113 | /* setup raster pos for glDrawPixels */ |
| 114 | glMatrixMode(GL_PROJECTION); |
| 115 | glPushMatrix(); |
| 116 | glLoadIdentity(); |
| 117 | |
| 118 | glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0); |
| 119 | glMatrixMode(GL_MODELVIEW); |
| 120 | glPushMatrix(); |
| 121 | glLoadIdentity(); |
| 122 | |
| 123 | glDisable(GL_STENCIL_TEST); |
| 124 | glDisable(GL_DEPTH_TEST); |
| 125 | glRasterPos2f(0, 0); |
| 126 | |
| 127 | glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, alphaValues); |
| 128 | |
| 129 | glPopMatrix(); |
| 130 | glMatrixMode(GL_PROJECTION); |
| 131 | glPopMatrix(); |
| 132 | free(alphaValues); |
| 133 | |
| 134 | glPopAttrib(); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | /* |
| 140 | * Copy the stencil buffer values into the current color buffer as a |
| 141 | * grayscale image. |
| 142 | * Input: winWidth, winHeight - size of the window |
| 143 | * scale, bias - scale and bias to apply to stencil values for display |
| 144 | */ |
| 145 | void |
| 146 | ShowStencilBuffer( GLsizei winWidth, GLsizei winHeight, |
| 147 | GLfloat scale, GLfloat bias ) |
| 148 | { |
| 149 | GLubyte *stencilValues; |
| 150 | |
| 151 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 152 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 153 | |
| 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); |
| 158 | |
| 159 | /* save GL state */ |
| 160 | glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | |
| 161 | GL_PIXEL_MODE_BIT | GL_TRANSFORM_BIT | GL_VIEWPORT_BIT); |
| 162 | |
| 163 | /* setup raster pos for glDrawPixels */ |
| 164 | glMatrixMode(GL_PROJECTION); |
| 165 | glPushMatrix(); |
| 166 | glLoadIdentity(); |
| 167 | |
| 168 | glOrtho(0.0, (GLdouble) winWidth, 0.0, (GLdouble) winHeight, -1.0, 1.0); |
| 169 | glMatrixMode(GL_MODELVIEW); |
| 170 | glPushMatrix(); |
| 171 | glLoadIdentity(); |
| 172 | |
| 173 | glDisable(GL_STENCIL_TEST); |
| 174 | glDisable(GL_DEPTH_TEST); |
| 175 | glRasterPos2f(0, 0); |
| 176 | |
| 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); |
| 183 | |
| 184 | glDrawPixels(winWidth, winHeight, GL_LUMINANCE, GL_UNSIGNED_BYTE, stencilValues); |
| 185 | |
| 186 | glPopMatrix(); |
| 187 | glMatrixMode(GL_PROJECTION); |
| 188 | glPopMatrix(); |
| 189 | free(stencilValues); |
| 190 | |
| 191 | glPopAttrib(); |
| 192 | } |