132001f49Smrg/* 232001f49Smrg * Test glRead/DrawPixels for GL_DEPTH_COMPONENT, with pixelzoom. 332001f49Smrg * 432001f49Smrg * Brian Paul 532001f49Smrg * 23 August 2003 632001f49Smrg */ 732001f49Smrg 832001f49Smrg#include <stdio.h> 932001f49Smrg#include <stdlib.h> 1032001f49Smrg#include <math.h> 1132001f49Smrg#include <GL/glew.h> 1232001f49Smrg#include "glut_wrap.h" 1332001f49Smrg 1432001f49Smrg 1532001f49Smrg#define ZWIDTH 100 1632001f49Smrg#define ZHEIGHT 100 1732001f49Smrg 1832001f49Smrg#define ZOOM 4 1932001f49Smrg 2032001f49Smrg#define ZWIDTH2 (ZOOM * ZWIDTH) 2132001f49Smrg#define ZHEIGHT2 (ZOOM * ZHEIGHT) 2232001f49Smrg 2332001f49Smrgstatic GLint WinWidth = ZWIDTH + ZWIDTH2, WinHeight = ZHEIGHT + ZHEIGHT2; 2432001f49Smrgstatic GLboolean Invert = GL_FALSE; 2532001f49Smrgstatic GLboolean TestPacking = GL_FALSE; 2632001f49Smrgstatic GLboolean TestList = GL_FALSE; 2732001f49Smrg 2832001f49Smrg 2932001f49Smrgstatic void Display(void) 3032001f49Smrg{ 3132001f49Smrg GLfloat depth[ZWIDTH * ZHEIGHT * 2]; 3232001f49Smrg GLfloat depth2[ZWIDTH2 * ZHEIGHT2]; /* *2 to test pixelstore stuff */ 3332001f49Smrg GLuint list; 3432001f49Smrg GLenum depthType = GL_FLOAT; 3532001f49Smrg 3632001f49Smrg glClearColor(0.5, 0.5, 0.5, 1.0); 3732001f49Smrg glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 3832001f49Smrg 3932001f49Smrg glEnable(GL_DEPTH_TEST); 4032001f49Smrg 4132001f49Smrg /* draw a sphere */ 4232001f49Smrg glViewport(0, 0, ZWIDTH, ZHEIGHT); 4332001f49Smrg glMatrixMode(GL_PROJECTION); 4432001f49Smrg glLoadIdentity(); 4532001f49Smrg glOrtho(-1, 1, -1, 1, -1, 0); /* clip away back half of sphere */ 4632001f49Smrg glMatrixMode(GL_MODELVIEW); 4732001f49Smrg glLoadIdentity(); 4832001f49Smrg glutSolidSphere(1.0, 20, 10); 4932001f49Smrg 5032001f49Smrg if (TestPacking) { 5132001f49Smrg glPixelStorei(GL_PACK_ROW_LENGTH, 120); 5232001f49Smrg glPixelStorei(GL_PACK_SKIP_PIXELS, 5); 5332001f49Smrg } 5432001f49Smrg 5532001f49Smrg /* read the depth image */ 5632001f49Smrg glReadPixels(0, 0, ZWIDTH, ZHEIGHT, GL_DEPTH_COMPONENT, depthType, depth); 5732001f49Smrg if (depthType == GL_FLOAT) { 5832001f49Smrg GLfloat min, max; 5932001f49Smrg int i; 6032001f49Smrg min = max = depth[0]; 6132001f49Smrg for (i = 1; i < ZWIDTH * ZHEIGHT; i++) { 6232001f49Smrg if (depth[i] < min) 6332001f49Smrg min = depth[i]; 6432001f49Smrg if (depth[i] > max) 6532001f49Smrg max = depth[i]; 6632001f49Smrg } 6732001f49Smrg printf("Depth value range: [%f, %f]\n", min, max); 6832001f49Smrg } 6932001f49Smrg 7032001f49Smrg /* debug */ 7132001f49Smrg if (0) { 7232001f49Smrg int i; 7332001f49Smrg float *z = depth + ZWIDTH * 50; 7432001f49Smrg printf("z at y=50:\n"); 7532001f49Smrg for (i = 0; i < ZWIDTH; i++) { 7632001f49Smrg printf("%5.3f ", z[i]); 7732001f49Smrg if ((i + 1) % 12 == 0) 7832001f49Smrg printf("\n"); 7932001f49Smrg } 8032001f49Smrg printf("\n"); 8132001f49Smrg } 8232001f49Smrg 8332001f49Smrg /* Draw the Z image as luminance above original rendering */ 8432001f49Smrg glWindowPos2i(0, ZHEIGHT); 8532001f49Smrg glDrawPixels(ZWIDTH, ZHEIGHT, GL_LUMINANCE, depthType, depth); 8632001f49Smrg 8732001f49Smrg if (TestPacking) { 8832001f49Smrg glPixelStorei(GL_PACK_ROW_LENGTH, 0); 8932001f49Smrg glPixelStorei(GL_PACK_SKIP_PIXELS, 0); 9032001f49Smrg glPixelStorei(GL_UNPACK_ROW_LENGTH, 120); 9132001f49Smrg glPixelStorei(GL_UNPACK_SKIP_PIXELS, 5); 9232001f49Smrg } 9332001f49Smrg 9432001f49Smrg /* draw depth image with scaling (into z buffer) */ 9532001f49Smrg glPixelZoom(ZOOM, ZOOM); 9632001f49Smrg glColor4f(1, 0, 0, 0); 9732001f49Smrg glWindowPos2i(ZWIDTH, 0); 9832001f49Smrg if (Invert) { 9932001f49Smrg glPixelTransferf(GL_DEPTH_SCALE, -1.0); 10032001f49Smrg glPixelTransferf(GL_DEPTH_BIAS, 1.0); 10132001f49Smrg } 10232001f49Smrg if (TestList) { 10332001f49Smrg list = glGenLists(1); 10432001f49Smrg glNewList(list, GL_COMPILE); 10532001f49Smrg glDrawPixels(ZWIDTH, ZHEIGHT, GL_DEPTH_COMPONENT, depthType, depth); 10632001f49Smrg glEndList(); 10732001f49Smrg glCallList(list); 10832001f49Smrg glDeleteLists(list, 1); 10932001f49Smrg } 11032001f49Smrg else { 11132001f49Smrg glDrawPixels(ZWIDTH, ZHEIGHT, GL_DEPTH_COMPONENT, depthType, depth); 11232001f49Smrg } 11332001f49Smrg if (Invert) { 11432001f49Smrg glPixelTransferf(GL_DEPTH_SCALE, 1.0); 11532001f49Smrg glPixelTransferf(GL_DEPTH_BIAS, 0.0); 11632001f49Smrg } 11732001f49Smrg 11832001f49Smrg if (TestPacking) { 11932001f49Smrg glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); 12032001f49Smrg glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); 12132001f49Smrg } 12232001f49Smrg 12332001f49Smrg glDisable(GL_DEPTH_TEST); 12432001f49Smrg 12532001f49Smrg /* read back scaled depth image */ 12632001f49Smrg glReadPixels(ZWIDTH, 0, ZWIDTH2, ZHEIGHT2, GL_DEPTH_COMPONENT, GL_FLOAT, depth2); 12732001f49Smrg 12832001f49Smrg /* debug */ 12932001f49Smrg if (0) { 13032001f49Smrg int i; 13132001f49Smrg float *z = depth2 + ZWIDTH2 * 200; 13232001f49Smrg printf("z at y=200:\n"); 13332001f49Smrg for (i = 0; i < ZWIDTH2; i++) { 13432001f49Smrg printf("%5.3f ", z[i]); 13532001f49Smrg if ((i + 1) % 12 == 0) 13632001f49Smrg printf("\n"); 13732001f49Smrg } 13832001f49Smrg printf("\n"); 13932001f49Smrg } 14032001f49Smrg 14132001f49Smrg /* draw as luminance */ 14232001f49Smrg glPixelZoom(1.0, 1.0); 14332001f49Smrg glWindowPos2i(ZWIDTH, 0); 14432001f49Smrg glDrawPixels(ZWIDTH2, ZHEIGHT2, GL_LUMINANCE, GL_FLOAT, depth2); 14532001f49Smrg 14632001f49Smrg glutSwapBuffers(); 14732001f49Smrg} 14832001f49Smrg 14932001f49Smrg 15032001f49Smrgstatic void Reshape(int width, int height) 15132001f49Smrg{ 15232001f49Smrg WinWidth = width; 15332001f49Smrg WinHeight = height; 15432001f49Smrg glViewport(0, 0, width, height); 15532001f49Smrg} 15632001f49Smrg 15732001f49Smrg 15832001f49Smrgstatic void Key(unsigned char key, int x, int y) 15932001f49Smrg{ 16032001f49Smrg (void) x; 16132001f49Smrg (void) y; 16232001f49Smrg switch (key) { 16332001f49Smrg case 'i': 16432001f49Smrg Invert = !Invert; 16532001f49Smrg break; 16632001f49Smrg case 'p': 16732001f49Smrg TestPacking = !TestPacking; 16832001f49Smrg printf("Test pixel pack/unpack: %d\n", TestPacking); 16932001f49Smrg break; 17032001f49Smrg case 'l': 17132001f49Smrg TestList = !TestList; 17232001f49Smrg printf("Test dlist: %d\n", TestList); 17332001f49Smrg break; 17432001f49Smrg case 27: 17532001f49Smrg exit(0); 17632001f49Smrg break; 17732001f49Smrg } 17832001f49Smrg glutPostRedisplay(); 17932001f49Smrg} 18032001f49Smrg 18132001f49Smrg 18232001f49Smrgstatic void Init(void) 18332001f49Smrg{ 18432001f49Smrg const GLfloat blue[4] = {.1, .1, 1.0, 1.0}; 18532001f49Smrg const GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0}; 18632001f49Smrg const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0}; 18732001f49Smrg const GLfloat pos[4] = {0, 0, 10, 0}; 18832001f49Smrg GLint z; 18932001f49Smrg 19032001f49Smrg glGetIntegerv(GL_DEPTH_BITS, &z); 19132001f49Smrg 19232001f49Smrg printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); 19332001f49Smrg printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 19432001f49Smrg printf("GL_DEPTH_BITS = %d\n", z); 19532001f49Smrg 19632001f49Smrg glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue); 19732001f49Smrg glLightfv(GL_LIGHT0, GL_AMBIENT, gray); 19832001f49Smrg glLightfv(GL_LIGHT0, GL_DIFFUSE, white); 19932001f49Smrg glLightfv(GL_LIGHT0, GL_POSITION, pos); 20032001f49Smrg glEnable(GL_LIGHTING); 20132001f49Smrg glEnable(GL_LIGHT0); 20232001f49Smrg} 20332001f49Smrg 20432001f49Smrg 20532001f49Smrgint main(int argc, char *argv[]) 20632001f49Smrg{ 20732001f49Smrg glutInit(&argc, argv); 20832001f49Smrg glutInitWindowPosition(0, 0); 20932001f49Smrg glutInitWindowSize(WinWidth, WinHeight); 21032001f49Smrg glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); 21132001f49Smrg glutCreateWindow(argv[0]); 21232001f49Smrg glewInit(); 21332001f49Smrg glutReshapeFunc(Reshape); 21432001f49Smrg glutKeyboardFunc(Key); 21532001f49Smrg glutDisplayFunc(Display); 21632001f49Smrg Init(); 21732001f49Smrg glutMainLoop(); 21832001f49Smrg return 0; 21932001f49Smrg} 220