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