7dfed20cfb9d2b2236c2b8b68aac37c4026cc3d9
[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 if (TestPacking) {
62 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
63 glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
64 glPixelStorei(GL_UNPACK_ROW_LENGTH, 120);
65 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 5);
66 }
67
68 /* draw depth image with scaling (into z buffer) */
69 glPixelZoom(4.0, 4.0);
70 glColor4f(1, 0, 0, 0);
71 glWindowPos2i(100, 0);
72 if (Invert) {
73 glPixelTransferf(GL_DEPTH_SCALE, -1.0);
74 glPixelTransferf(GL_DEPTH_BIAS, 1.0);
75 }
76 if (TestList) {
77 list = glGenLists(1);
78 glNewList(list, GL_COMPILE);
79 glDrawPixels(100, 100, GL_DEPTH_COMPONENT, depthType, depth);
80 glEndList();
81 glCallList(list);
82 glDeleteLists(list, 1);
83 }
84 else {
85 glDrawPixels(100, 100, GL_DEPTH_COMPONENT, depthType, depth);
86 }
87 if (Invert) {
88 glPixelTransferf(GL_DEPTH_SCALE, 1.0);
89 glPixelTransferf(GL_DEPTH_BIAS, 0.0);
90 }
91
92 if (TestPacking) {
93 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
94 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
95 }
96
97 glDisable(GL_DEPTH_TEST);
98
99 /* read back scaled depth image */
100 glReadPixels(100, 0, 400, 400, GL_DEPTH_COMPONENT, GL_FLOAT, depth2);
101 /* draw as luminance */
102 glPixelZoom(1.0, 1.0);
103 glDrawPixels(400, 400, GL_LUMINANCE, GL_FLOAT, depth2);
104
105 glutSwapBuffers();
106 }
107
108
109 static void Reshape(int width, int height)
110 {
111 WinWidth = width;
112 WinHeight = height;
113 glViewport(0, 0, width, height);
114 }
115
116
117 static void Key(unsigned char key, int x, int y)
118 {
119 (void) x;
120 (void) y;
121 switch (key) {
122 case 'i':
123 Invert = !Invert;
124 break;
125 case 'p':
126 TestPacking = !TestPacking;
127 printf("Test pixel pack/unpack: %d\n", TestPacking);
128 break;
129 case 'l':
130 TestList = !TestList;
131 printf("Test dlist: %d\n", TestList);
132 break;
133 case 27:
134 exit(0);
135 break;
136 }
137 glutPostRedisplay();
138 }
139
140
141 static void Init(void)
142 {
143 const GLfloat blue[4] = {.1, .1, 1.0, 1.0};
144 const GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
145 const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
146 const GLfloat pos[4] = {0, 0, 10, 0};
147
148 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
149 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
150
151 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue);
152 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
153 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
154 glLightfv(GL_LIGHT0, GL_POSITION, pos);
155 glEnable(GL_LIGHTING);
156 glEnable(GL_LIGHT0);
157 }
158
159
160 int main(int argc, char *argv[])
161 {
162 glutInit(&argc, argv);
163 glutInitWindowPosition(0, 0);
164 glutInitWindowSize(WinWidth, WinHeight);
165 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
166 glutCreateWindow(argv[0]);
167 glewInit();
168 glutReshapeFunc(Reshape);
169 glutKeyboardFunc(Key);
170 glutDisplayFunc(Display);
171 Init();
172 glutMainLoop();
173 return 0;
174 }