progs/tests: added Z invert option
[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
17
18 static void Display(void)
19 {
20 GLfloat depth[100 * 100];
21 GLfloat depth2[400 * 400];
22 GLfloat min, max;
23 int i;
24
25 glClearColor(0.5, 0.5, 0.5, 1.0);
26 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
27
28 glEnable(GL_DEPTH_TEST);
29
30 /* draw a sphere */
31 glViewport(0, 0, 100, 100);
32 glMatrixMode(GL_PROJECTION);
33 glLoadIdentity();
34 glOrtho(-1, 1, -1, 1, -1, 0); /* clip away back half of sphere */
35 glMatrixMode(GL_MODELVIEW);
36 glLoadIdentity();
37 glutSolidSphere(1.0, 20, 10);
38
39 /* read the depth image */
40 glReadPixels(0, 0, 100, 100, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
41 min = max = depth[0];
42 for (i = 1; i < 100 * 100; i++) {
43 if (depth[i] < min)
44 min = depth[i];
45 if (depth[i] > max)
46 max = depth[i];
47 }
48 printf("Depth value range: [%f, %f]\n", min, max);
49
50 /* draw depth image with scaling (into z buffer) */
51 glPixelZoom(4.0, 4.0);
52 glColor4f(1, 0, 0, 0);
53 glWindowPos2i(100, 0);
54 if (Invert) {
55 glPixelTransferf(GL_DEPTH_SCALE, -1.0);
56 glPixelTransferf(GL_DEPTH_BIAS, 1.0);
57 }
58 glDrawPixels(100, 100, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
59 if (Invert) {
60 glPixelTransferf(GL_DEPTH_SCALE, 1.0);
61 glPixelTransferf(GL_DEPTH_BIAS, 0.0);
62 }
63
64 glDisable(GL_DEPTH_TEST);
65
66 /* read back scaled depth image */
67 glReadPixels(100, 0, 400, 400, GL_DEPTH_COMPONENT, GL_FLOAT, depth2);
68 /* draw as luminance */
69 glPixelZoom(1.0, 1.0);
70 glDrawPixels(400, 400, GL_LUMINANCE, GL_FLOAT, depth2);
71
72 glutSwapBuffers();
73 }
74
75
76 static void Reshape(int width, int height)
77 {
78 WinWidth = width;
79 WinHeight = height;
80 glViewport(0, 0, width, height);
81 }
82
83
84 static void Key(unsigned char key, int x, int y)
85 {
86 (void) x;
87 (void) y;
88 switch (key) {
89 case 'i':
90 Invert = !Invert;
91 break;
92 case 27:
93 exit(0);
94 break;
95 }
96 glutPostRedisplay();
97 }
98
99
100 static void Init(void)
101 {
102 const GLfloat blue[4] = {.1, .1, 1.0, 1.0};
103 const GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
104 const GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
105 const GLfloat pos[4] = {0, 0, 10, 0};
106
107 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
108 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
109
110 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blue);
111 glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
112 glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
113 glLightfv(GL_LIGHT0, GL_POSITION, pos);
114 glEnable(GL_LIGHTING);
115 glEnable(GL_LIGHT0);
116 }
117
118
119 int main(int argc, char *argv[])
120 {
121 glutInit(&argc, argv);
122 glutInitWindowPosition(0, 0);
123 glutInitWindowSize(WinWidth, WinHeight);
124 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
125 glutCreateWindow(argv[0]);
126 glewInit();
127 glutReshapeFunc(Reshape);
128 glutKeyboardFunc(Key);
129 glutDisplayFunc(Display);
130 Init();
131 glutMainLoop();
132 return 0;
133 }