2 * Test glRead/DrawPixels for GL_DEPTH_COMPONENT, with pixelzoom.
20 #define ZWIDTH2 (ZOOM * ZWIDTH)
21 #define ZHEIGHT2 (ZOOM * ZHEIGHT)
23 static GLint WinWidth
= ZWIDTH
+ ZWIDTH2
, WinHeight
= ZHEIGHT
+ ZHEIGHT2
;
24 static GLboolean Invert
= GL_FALSE
;
25 static GLboolean TestPacking
= GL_FALSE
;
26 static GLboolean TestList
= GL_FALSE
;
29 static void Display(void)
31 GLfloat depth
[ZWIDTH
* ZHEIGHT
* 2];
32 GLfloat depth2
[ZWIDTH2
* ZHEIGHT2
]; /* *2 to test pixelstore stuff */
34 GLenum depthType
= GL_FLOAT
;
36 glClearColor(0.5, 0.5, 0.5, 1.0);
37 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
39 glEnable(GL_DEPTH_TEST
);
42 glViewport(0, 0, ZWIDTH
, ZHEIGHT
);
43 glMatrixMode(GL_PROJECTION
);
45 glOrtho(-1, 1, -1, 1, -1, 0); /* clip away back half of sphere */
46 glMatrixMode(GL_MODELVIEW
);
48 glutSolidSphere(1.0, 20, 10);
51 glPixelStorei(GL_PACK_ROW_LENGTH
, 120);
52 glPixelStorei(GL_PACK_SKIP_PIXELS
, 5);
55 /* read the depth image */
56 glReadPixels(0, 0, ZWIDTH
, ZHEIGHT
, GL_DEPTH_COMPONENT
, depthType
, depth
);
57 if (depthType
== GL_FLOAT
) {
61 for (i
= 1; i
< ZWIDTH
* ZHEIGHT
; i
++) {
67 printf("Depth value range: [%f, %f]\n", min
, max
);
73 float *z
= depth
+ ZWIDTH
* 50;
74 printf("z at y=50:\n");
75 for (i
= 0; i
< ZWIDTH
; i
++) {
76 printf("%5.3f ", z
[i
]);
77 if ((i
+ 1) % 12 == 0)
83 /* Draw the Z image as luminance above original rendering */
84 glWindowPos2i(0, ZHEIGHT
);
85 glDrawPixels(ZWIDTH
, ZHEIGHT
, GL_LUMINANCE
, depthType
, depth
);
88 glPixelStorei(GL_PACK_ROW_LENGTH
, 0);
89 glPixelStorei(GL_PACK_SKIP_PIXELS
, 0);
90 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 120);
91 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 5);
94 /* draw depth image with scaling (into z buffer) */
95 glPixelZoom(ZOOM
, ZOOM
);
96 glColor4f(1, 0, 0, 0);
97 glWindowPos2i(ZWIDTH
, 0);
99 glPixelTransferf(GL_DEPTH_SCALE
, -1.0);
100 glPixelTransferf(GL_DEPTH_BIAS
, 1.0);
103 list
= glGenLists(1);
104 glNewList(list
, GL_COMPILE
);
105 glDrawPixels(ZWIDTH
, ZHEIGHT
, GL_DEPTH_COMPONENT
, depthType
, depth
);
108 glDeleteLists(list
, 1);
111 glDrawPixels(ZWIDTH
, ZHEIGHT
, GL_DEPTH_COMPONENT
, depthType
, depth
);
114 glPixelTransferf(GL_DEPTH_SCALE
, 1.0);
115 glPixelTransferf(GL_DEPTH_BIAS
, 0.0);
119 glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
120 glPixelStorei(GL_UNPACK_SKIP_PIXELS
, 0);
123 glDisable(GL_DEPTH_TEST
);
125 /* read back scaled depth image */
126 glReadPixels(ZWIDTH
, 0, ZWIDTH2
, ZHEIGHT2
, GL_DEPTH_COMPONENT
, GL_FLOAT
, depth2
);
131 float *z
= depth2
+ ZWIDTH2
* 200;
132 printf("z at y=200:\n");
133 for (i
= 0; i
< ZWIDTH2
; i
++) {
134 printf("%5.3f ", z
[i
]);
135 if ((i
+ 1) % 12 == 0)
141 /* draw as luminance */
142 glPixelZoom(1.0, 1.0);
143 glWindowPos2i(ZWIDTH
, 0);
144 glDrawPixels(ZWIDTH2
, ZHEIGHT2
, GL_LUMINANCE
, GL_FLOAT
, depth2
);
150 static void Reshape(int width
, int height
)
154 glViewport(0, 0, width
, height
);
158 static void Key(unsigned char key
, int x
, int y
)
167 TestPacking
= !TestPacking
;
168 printf("Test pixel pack/unpack: %d\n", TestPacking
);
171 TestList
= !TestList
;
172 printf("Test dlist: %d\n", TestList
);
182 static void Init(void)
184 const GLfloat blue
[4] = {.1, .1, 1.0, 1.0};
185 const GLfloat gray
[4] = {0.2, 0.2, 0.2, 1.0};
186 const GLfloat white
[4] = {1.0, 1.0, 1.0, 1.0};
187 const GLfloat pos
[4] = {0, 0, 10, 0};
190 glGetIntegerv(GL_DEPTH_BITS
, &z
);
192 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
193 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
194 printf("GL_DEPTH_BITS = %d\n", z
);
196 glMaterialfv(GL_FRONT_AND_BACK
, GL_DIFFUSE
, blue
);
197 glLightfv(GL_LIGHT0
, GL_AMBIENT
, gray
);
198 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, white
);
199 glLightfv(GL_LIGHT0
, GL_POSITION
, pos
);
200 glEnable(GL_LIGHTING
);
205 int main(int argc
, char *argv
[])
207 glutInit(&argc
, argv
);
208 glutInitWindowPosition(0, 0);
209 glutInitWindowSize(WinWidth
, WinHeight
);
210 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
211 glutCreateWindow(argv
[0]);
213 glutReshapeFunc(Reshape
);
214 glutKeyboardFunc(Key
);
215 glutDisplayFunc(Display
);