zreaddraw.c revision 32001f49
1/*
2 * Test glRead/DrawPixels for GL_DEPTH_COMPONENT, with pixelzoom.
3 *
4 * Brian Paul
5 * 23 August 2003
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <math.h>
11#include <GL/glew.h>
12#include "glut_wrap.h"
13
14
15#define ZWIDTH 100
16#define ZHEIGHT 100
17
18#define ZOOM 4
19
20#define ZWIDTH2 (ZOOM * ZWIDTH)
21#define ZHEIGHT2 (ZOOM * ZHEIGHT)
22
23static GLint WinWidth = ZWIDTH + ZWIDTH2, WinHeight = ZHEIGHT + ZHEIGHT2;
24static GLboolean Invert = GL_FALSE;
25static GLboolean TestPacking = GL_FALSE;
26static GLboolean TestList = GL_FALSE;
27
28
29static void Display(void)
30{
31   GLfloat depth[ZWIDTH * ZHEIGHT * 2];
32   GLfloat depth2[ZWIDTH2 * ZHEIGHT2]; /* *2 to test pixelstore stuff */
33   GLuint list;
34   GLenum depthType = GL_FLOAT;
35
36   glClearColor(0.5, 0.5, 0.5, 1.0);
37   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
38
39   glEnable(GL_DEPTH_TEST);
40
41   /* draw a sphere */
42   glViewport(0, 0, ZWIDTH, ZHEIGHT);
43   glMatrixMode(GL_PROJECTION);
44   glLoadIdentity();
45   glOrtho(-1, 1, -1, 1, -1, 0);  /* clip away back half of sphere */
46   glMatrixMode(GL_MODELVIEW);
47   glLoadIdentity();
48   glutSolidSphere(1.0, 20, 10);
49
50   if (TestPacking) {
51      glPixelStorei(GL_PACK_ROW_LENGTH, 120);
52      glPixelStorei(GL_PACK_SKIP_PIXELS, 5);
53   }
54
55   /* read the depth image */
56   glReadPixels(0, 0, ZWIDTH, ZHEIGHT, GL_DEPTH_COMPONENT, depthType, depth);
57   if (depthType == GL_FLOAT) {
58      GLfloat min, max;
59      int i;
60      min = max = depth[0];
61      for (i = 1; i < ZWIDTH * ZHEIGHT; i++) {
62         if (depth[i] < min)
63            min = depth[i];
64         if (depth[i] > max)
65            max = depth[i];
66      }
67      printf("Depth value range: [%f, %f]\n", min, max);
68   }
69
70   /* debug */
71   if (0) {
72      int i;
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)
78            printf("\n");
79      }
80      printf("\n");
81   }
82
83   /* Draw the Z image as luminance above original rendering */
84   glWindowPos2i(0, ZHEIGHT);
85   glDrawPixels(ZWIDTH, ZHEIGHT, GL_LUMINANCE, depthType, depth);
86
87   if (TestPacking) {
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);
92   }
93
94   /* draw depth image with scaling (into z buffer) */
95   glPixelZoom(ZOOM, ZOOM);
96   glColor4f(1, 0, 0, 0);
97   glWindowPos2i(ZWIDTH, 0);
98   if (Invert) {
99      glPixelTransferf(GL_DEPTH_SCALE, -1.0);
100      glPixelTransferf(GL_DEPTH_BIAS, 1.0);
101   }
102   if (TestList) {
103      list = glGenLists(1);
104      glNewList(list, GL_COMPILE);
105      glDrawPixels(ZWIDTH, ZHEIGHT, GL_DEPTH_COMPONENT, depthType, depth);
106      glEndList();
107      glCallList(list);
108      glDeleteLists(list, 1);
109   }
110   else {
111      glDrawPixels(ZWIDTH, ZHEIGHT, GL_DEPTH_COMPONENT, depthType, depth);
112   }
113   if (Invert) {
114      glPixelTransferf(GL_DEPTH_SCALE, 1.0);
115      glPixelTransferf(GL_DEPTH_BIAS, 0.0);
116   }
117
118   if (TestPacking) {
119      glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
120      glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
121   }
122
123   glDisable(GL_DEPTH_TEST);
124
125   /* read back scaled depth image */
126   glReadPixels(ZWIDTH, 0, ZWIDTH2, ZHEIGHT2, GL_DEPTH_COMPONENT, GL_FLOAT, depth2);
127
128   /* debug */
129   if (0) {
130      int i;
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)
136            printf("\n");
137      }
138      printf("\n");
139   }
140
141   /* draw as luminance */
142   glPixelZoom(1.0, 1.0);
143   glWindowPos2i(ZWIDTH, 0);
144   glDrawPixels(ZWIDTH2, ZHEIGHT2, GL_LUMINANCE, GL_FLOAT, depth2);
145
146   glutSwapBuffers();
147}
148
149
150static void Reshape(int width, int height)
151{
152   WinWidth = width;
153   WinHeight = height;
154   glViewport(0, 0, width, height);
155}
156
157
158static void Key(unsigned char key, int x, int y)
159{
160   (void) x;
161   (void) y;
162   switch (key) {
163      case 'i':
164         Invert = !Invert;
165         break;
166      case 'p':
167         TestPacking = !TestPacking;
168         printf("Test pixel pack/unpack: %d\n", TestPacking);
169         break;
170      case 'l':
171         TestList = !TestList;
172         printf("Test dlist: %d\n", TestList);
173         break;
174      case 27:
175         exit(0);
176         break;
177   }
178   glutPostRedisplay();
179}
180
181
182static void Init(void)
183{
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};
188   GLint z;
189
190   glGetIntegerv(GL_DEPTH_BITS, &z);
191
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);
195
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);
201   glEnable(GL_LIGHT0);
202}
203
204
205int main(int argc, char *argv[])
206{
207   glutInit(&argc, argv);
208   glutInitWindowPosition(0, 0);
209   glutInitWindowSize(WinWidth, WinHeight);
210   glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
211   glutCreateWindow(argv[0]);
212   glewInit();
213   glutReshapeFunc(Reshape);
214   glutKeyboardFunc(Key);
215   glutDisplayFunc(Display);
216   Init();
217   glutMainLoop();
218   return 0;
219}
220