zdrawpix.c revision 32001f49
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#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"
19
20
21static int Win;
22static GLfloat Xrot = 50, Yrot = 40, Zpos = 6;
23static GLboolean Anim = GL_FALSE;
24
25static int Width = 200, Height = 200;
26static GLfloat *z;
27static GLboolean showZ = 0;
28
29
30static void
31Idle(void)
32{
33   Xrot += 3.0;
34   Yrot += 4.0;
35   glutPostRedisplay();
36}
37
38
39static void
40Draw(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
82static void
83Reshape(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
109static void
110Key(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
141static void
142SpecialKey(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
165static void
166Init(void)
167{
168   /* setup lighting, etc */
169   glEnable(GL_DEPTH_TEST);
170   glEnable(GL_LIGHTING);
171   glEnable(GL_LIGHT0);
172}
173
174
175int
176main(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   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;
193}
194