demos: add missing binaries to .gitignore
[mesa-demos.git] / src / tests / zdrawpix.c
blob4b0d7f9063257d97aa53c871ccf2e0232f42dada
1 /**
2 * Test glDrawPixels(GL_DEPTH_COMPONENT)
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.
10 * Press 'd' to view the Z buffer as a grayscale image.
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <math.h>
16 #include <GL/glew.h>
17 #include "glut_wrap.h"
18 #include "../util/showbuffer.c"
21 static int Win;
22 static GLfloat Xrot = 50, Yrot = 40, Zpos = 6;
23 static GLboolean Anim = GL_FALSE;
25 static int Width = 200, Height = 200;
26 static GLfloat *z;
27 static GLboolean showZ = 0;
30 static void
31 Idle(void)
33 Xrot += 3.0;
34 Yrot += 4.0;
35 glutPostRedisplay();
39 static void
40 Draw(void)
42 glClearColor(0, 0, 0.5, 0);
43 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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);
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();
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
74 if (showZ) {
75 ShowDepthBuffer(Width, Height, 0.0, 1.0);
78 glutSwapBuffers();
82 static void
83 Reshape(int width, int height)
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);
93 Width = width;
94 Height = height;
96 z = (float *) malloc(width * height * 4);
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;
109 static void
110 Key(unsigned char key, int x, int y)
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;
137 glutPostRedisplay();
141 static void
142 SpecialKey(int key, int x, int y)
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;
161 glutPostRedisplay();
165 static void
166 Init(void)
168 /* setup lighting, etc */
169 glEnable(GL_DEPTH_TEST);
170 glEnable(GL_LIGHTING);
171 glEnable(GL_LIGHT0);
176 main(int argc, char *argv[])
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 glewInit();
184 glutReshapeFunc(Reshape);
185 glutKeyboardFunc(Key);
186 glutSpecialFunc(SpecialKey);
187 glutDisplayFunc(Draw);
188 if (Anim)
189 glutIdleFunc(Idle);
190 Init();
191 glutMainLoop();
192 return 0;