Merge branch '7.8'
[mesa.git] / progs / tests / zreaddraw.c
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 <GL/glut.h>
13
14 static GLint WinWidth = 500, WinHeight = 500;
15 static GLboolean Invert = GL_FALSE;
16 static GLboolean TestPacking = GL_FALSE;
17 static GLboolean TestList = GL_FALSE;
18
19
20 static void Display(void)
21 {
22 GLfloat depth[100 * 100 * 2];
23 GLfloat depth2[400 * 400]; /* *2 to test pixelstore stuff */
24 GLuint list;
25 GLenum depthType = GL_FLOAT;
26
27 glClearColor(0.5, 0.5, 0.5, 1.0);
28 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
29
30 glEnable(GL_DEPTH_TEST);
31
32 /* draw a sphere */
33 glViewport(0, 0, 100, 100);
34 glMatrixMode(GL_PROJECTION);
35 glLoadIdentity();
36 glOrtho(-1, 1, -1, 1, -1, 0); /* clip away back half of sphere */
37 glMatrixMode(GL_MODELVIEW);
38 glLoadIdentity();
39 glutSolidSphere(1.0, 20, 10);
40
41 if (TestPacking) {
42 glPixelStorei(GL_PACK_ROW_LENGTH, 120);
43 glPixelStorei(GL_PACK_SKIP_PIXELS, 5);
44 }
45
46 /* read the depth image */
47 glReadPixels(0, 0, 100, 100, GL_DEPTH_COMPONENT, depthType, depth);
48 if (depthType == GL_FLOAT) {
49 GLfloat min, max;
50 int i;
51 min = max = depth[0];
52 for (i = 1; i < 100 * 100; i++) {
53 if (depth[i] < min)
54 min = depth[i];
55 if (depth[i] > max)
56 max = depth[i];
57 }
58 printf("Depth value range: [%f, %f]\n", min, max);
59 }
60
61 /* Draw the Z image as luminance above original rendering */
62 glWindowPos2i(0, 100);
63 glDrawPixels(100, 100, GL_LUMINANCE, depthType, depth);
64
65 if (TestPacking) {
66 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
67 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
68 glPixelStorei(GL_UNPACK_ROW_LENGTH, 120);
69 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 5);
70 }
71
72 /* draw depth image with scaling (into z buffer) */
73 glPixelZoom(4.0, 4.0);
74 glColor4f(1, 0, 0, 0);
75 glWindowPos2i(100, 0);
76 if (Invert) {
77 glPixelTransferf(GL_DEPTH_SCALE, -1.0);
78 glPixelTransferf(GL_DEPTH_BIAS, 1.0);
79 }
80 if (TestList) {
81 list = glGenLists(1);
82 glNewList(list, GL_COMPILE);
83 glDrawPixels(100, 100, GL_DEPTH_COMPONENT, depthType, depth);
84 glEndList();
85 glCallList(list);
86 glDeleteLists(list, 1);
87 }
88 else {
89 glDrawPixels(100, 100, GL_DEPTH_COMPONENT, depthType, depth);
90 }
91 if (Invert) {
92 glPixelTransferf(GL_DEPTH_SCALE, 1.0);
93 glPixelTransferf(GL_DEPTH_BIAS, 0.0);
94 }
95
96 if (TestPacking) {
97 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
98 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
99 }
100
101 glDisable(GL_DEPTH_TEST);
102
103 /* read back scaled depth image */
104 glReadPixels(100, 0, 400, 400, GL_DEPTH_COMPONENT, GL_FLOAT, depth2);
105
106 /* debug */
107 if (0) {
108 int i;
109 float *z = depth2 + 400 * 200;
110 printf("z at y=200:\n");
111 for (i = 0; i < 400; i++) {
112 printf("%5.3f ", z[i]);
113 if ((i + 1) % 12 == 0)
114 printf("\n");
115 }
116 }
117
118 /* draw as luminance */
119 glPixelZoom(1.0, 1.0);
120 glWindowPos2i(100, 0);
121 glDrawPixels(400, 400, GL_LUMINANCE, GL_FLOAT, depth2);
122
123 glutSwapBuffers();
124 }
125
126
127 static void Reshape(int width, int height)
128 {
129 WinWidth = width;
130 WinHeight = height;
131 glViewport(0, 0, width, height);
132 }
133
134
135 static void Key(unsigned char key, int x, int y)
136 {
137 (void) x;
138 (void) y;
139 switch (key) {
140 case 'i':
141 Invert = !Invert;
142 break;
143 case 'p':
144 TestPacking = !TestPacking;
145 printf("Test pixel pack/unpack: %d\n", TestPacking);
146 break;
147 case 'l':
148 TestList = !TestList;
149 printf("Test dlist: %d\n", TestList);
150 break;
151 case 27:
152 exit(0);
153 break;
154 }
155 glutPostRedisplay();
156 }
157
158
159 static void Init(void)
160 {
161 const GLfloat blue[4] = {.1, .1, 1.0, 1.0};
162 const GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
163 const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
164 const GLfloat pos[4] = {0, 0, 10, 0};
165
166 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
167 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
168
169 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue);
170 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
171 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
172 glLightfv(GL_LIGHT0, GL_POSITION, pos);
173 glEnable(GL_LIGHTING);
174 glEnable(GL_LIGHT0);
175 }
176
177
178 int main(int argc, char *argv[])
179 {
180 glutInit(&argc, argv);
181 glutInitWindowPosition(0, 0);
182 glutInitWindowSize(WinWidth, WinHeight);
183 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
184 glutCreateWindow(argv[0]);
185 glewInit();
186 glutReshapeFunc(Reshape);
187 glutKeyboardFunc(Key);
188 glutDisplayFunc(Display);
189 Init();
190 glutMainLoop();
191 return 0;
192 }