Merge commit 'origin/gallium-master-merge'
[mesa.git] / progs / tests / zdrawpix.c
1 /**
2 * Test glDrawPixels(GL_DEPTH_COMPONENT)
3 *
4 * We load a window-sized buffer of Z values so that Z=1 at the top and
5 * Z=0 at the bottom (and interpolate between).
6 * We draw that image into the Z buffer, then draw an ordinary cube.
7 * The bottom part of the cube should be "clipped" where the cube fails
8 * the Z test.
9 *
10 * Press 'd' to view the Z buffer as a grayscale image.
11 */
12
13 #define GL_GLEXT_PROTOTYPES
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <math.h>
17 #include <GL/glut.h>
18 #include "../util/showbuffer.c"
19
20
21 static int Win;
22 static GLfloat Xrot = 50, Yrot = 40, Zpos = 6;
23 static GLboolean Anim = GL_FALSE;
24
25 static int Width = 200, Height = 200;
26 static GLfloat *z;
27 static GLboolean showZ = 0;
28
29
30 static void
31 Idle(void)
32 {
33 Xrot += 3.0;
34 Yrot += 4.0;
35 glutPostRedisplay();
36 }
37
38
39 static void
40 Draw(void)
41 {
42 glClearColor(0, 0, 0.5, 0);
43 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
44
45 #if 1
46 glColor3f(1, 0, 0);
47 glWindowPos2i(0,0);
48 glColorMask(0,0,0,0);
49 glDrawPixels(Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, z);
50 #elif 0
51 glPushMatrix();
52 glTranslatef(-0.75, 0, Zpos);
53 glutSolidSphere(1.0, 20, 10);
54 glPopMatrix();
55 #endif
56 glColorMask(1,1,1,1);
57
58 /* draw cube */
59 glPushMatrix();
60 glTranslatef(0, 0, Zpos);
61 glRotatef(Xrot, 1, 0, 0);
62 glRotatef(Yrot, 0, 1, 0);
63 glutSolidCube(2.0);
64 glPopMatrix();
65
66 #if 0
67 /* drawpixels after cube */
68 glColor3f(1, 0, 0);
69 glWindowPos2i(0,0);
70 //glColorMask(0,0,0,0);
71 glDrawPixels(Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, z);
72 #endif
73
74 if (showZ) {
75 ShowDepthBuffer(Width, Height, 0.0, 1.0);
76 }
77
78 glutSwapBuffers();
79 }
80
81
82 static void
83 Reshape(int width, int height)
84 {
85 glViewport(0, 0, width, height);
86 glMatrixMode(GL_PROJECTION);
87 glLoadIdentity();
88 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 30.0);
89 glMatrixMode(GL_MODELVIEW);
90 glLoadIdentity();
91 glTranslatef(0.0, 0.0, -15.0);
92
93 Width = width;
94 Height = height;
95
96 z = (float *) malloc(width * height * 4);
97 {
98 int i, j, k = 0;
99 for (i = 0; i < height; i++) {
100 float zval = (float) i / (height - 1);
101 for (j = 0; j < width; j++) {
102 z[k++] = zval;
103 }
104 }
105 }
106 }
107
108
109 static void
110 Key(unsigned char key, int x, int y)
111 {
112 const GLfloat step = 1.0;
113 (void) x;
114 (void) y;
115 switch (key) {
116 case 'a':
117 Anim = !Anim;
118 if (Anim)
119 glutIdleFunc(Idle);
120 else
121 glutIdleFunc(NULL);
122 break;
123 case 'd':
124 showZ = !showZ;
125 break;
126 case 'z':
127 Zpos -= step;
128 break;
129 case 'Z':
130 Zpos += step;
131 break;
132 case 27:
133 glutDestroyWindow(Win);
134 exit(0);
135 break;
136 }
137 glutPostRedisplay();
138 }
139
140
141 static void
142 SpecialKey(int key, int x, int y)
143 {
144 const GLfloat step = 3.0;
145 (void) x;
146 (void) y;
147 switch (key) {
148 case GLUT_KEY_UP:
149 Xrot -= step;
150 break;
151 case GLUT_KEY_DOWN:
152 Xrot += step;
153 break;
154 case GLUT_KEY_LEFT:
155 Yrot -= step;
156 break;
157 case GLUT_KEY_RIGHT:
158 Yrot += step;
159 break;
160 }
161 glutPostRedisplay();
162 }
163
164
165 static void
166 Init(void)
167 {
168 /* setup lighting, etc */
169 glEnable(GL_DEPTH_TEST);
170 glEnable(GL_LIGHTING);
171 glEnable(GL_LIGHT0);
172 }
173
174
175 int
176 main(int argc, char *argv[])
177 {
178 glutInit(&argc, argv);
179 glutInitWindowPosition(0, 0);
180 glutInitWindowSize(400, 400);
181 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
182 Win = glutCreateWindow(argv[0]);
183 glutReshapeFunc(Reshape);
184 glutKeyboardFunc(Key);
185 glutSpecialFunc(SpecialKey);
186 glutDisplayFunc(Draw);
187 if (Anim)
188 glutIdleFunc(Idle);
189 Init();
190 glutMainLoop();
191 return 0;
192 }