132001f49Smrg/**
232001f49Smrg * Test glDrawPixels(GL_DEPTH_COMPONENT)
332001f49Smrg *
432001f49Smrg * We load a window-sized buffer of Z values so that Z=1 at the top and
532001f49Smrg * Z=0 at the bottom (and interpolate between).
632001f49Smrg * We draw that image into the Z buffer, then draw an ordinary cube.
732001f49Smrg * The bottom part of the cube should be "clipped" where the cube fails
832001f49Smrg * the Z test.
932001f49Smrg *
1032001f49Smrg * Press 'd' to view the Z buffer as a grayscale image.
1132001f49Smrg */
1232001f49Smrg
1332001f49Smrg#include <stdio.h>
1432001f49Smrg#include <stdlib.h>
1532001f49Smrg#include <math.h>
1632001f49Smrg#include <GL/glew.h>
1732001f49Smrg#include "glut_wrap.h"
1832001f49Smrg#include "../util/showbuffer.c"
1932001f49Smrg
2032001f49Smrg
2132001f49Smrgstatic int Win;
2232001f49Smrgstatic GLfloat Xrot = 50, Yrot = 40, Zpos = 6;
2332001f49Smrgstatic GLboolean Anim = GL_FALSE;
2432001f49Smrg
2532001f49Smrgstatic int Width = 200, Height = 200;
2632001f49Smrgstatic GLfloat *z;
2732001f49Smrgstatic GLboolean showZ = 0;
2832001f49Smrg
2932001f49Smrg
3032001f49Smrgstatic void
3132001f49SmrgIdle(void)
3232001f49Smrg{
3332001f49Smrg   Xrot += 3.0;
3432001f49Smrg   Yrot += 4.0;
3532001f49Smrg   glutPostRedisplay();
3632001f49Smrg}
3732001f49Smrg
3832001f49Smrg
3932001f49Smrgstatic void
4032001f49SmrgDraw(void)
4132001f49Smrg{
4232001f49Smrg   glClearColor(0, 0, 0.5, 0);
4332001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
4432001f49Smrg
4532001f49Smrg#if 1
4632001f49Smrg   glColor3f(1, 0, 0);
4732001f49Smrg   glWindowPos2i(0,0);
4832001f49Smrg   glColorMask(0,0,0,0);
4932001f49Smrg   glDrawPixels(Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, z);
5032001f49Smrg#elif 0
5132001f49Smrg   glPushMatrix();
5232001f49Smrg   glTranslatef(-0.75, 0, Zpos);
5332001f49Smrg   glutSolidSphere(1.0, 20, 10);
5432001f49Smrg   glPopMatrix();
5532001f49Smrg#endif
5632001f49Smrg   glColorMask(1,1,1,1);
5732001f49Smrg
5832001f49Smrg   /* draw cube */
5932001f49Smrg   glPushMatrix();
6032001f49Smrg   glTranslatef(0, 0, Zpos);
6132001f49Smrg   glRotatef(Xrot, 1, 0, 0);
6232001f49Smrg   glRotatef(Yrot, 0, 1, 0);
6332001f49Smrg   glutSolidCube(2.0);
6432001f49Smrg   glPopMatrix();
6532001f49Smrg
6632001f49Smrg#if 0
6732001f49Smrg   /* drawpixels after cube */
6832001f49Smrg   glColor3f(1, 0, 0);
6932001f49Smrg   glWindowPos2i(0,0);
7032001f49Smrg   //glColorMask(0,0,0,0);
7132001f49Smrg   glDrawPixels(Width, Height, GL_DEPTH_COMPONENT, GL_FLOAT, z);
7232001f49Smrg#endif
7332001f49Smrg
7432001f49Smrg   if (showZ) {
7532001f49Smrg      ShowDepthBuffer(Width, Height, 0.0, 1.0);
7632001f49Smrg   }
7732001f49Smrg
7832001f49Smrg   glutSwapBuffers();
7932001f49Smrg}
8032001f49Smrg
8132001f49Smrg
8232001f49Smrgstatic void
8332001f49SmrgReshape(int width, int height)
8432001f49Smrg{
8532001f49Smrg   glViewport(0, 0, width, height);
8632001f49Smrg   glMatrixMode(GL_PROJECTION);
8732001f49Smrg   glLoadIdentity();
8832001f49Smrg   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 30.0);
8932001f49Smrg   glMatrixMode(GL_MODELVIEW);
9032001f49Smrg   glLoadIdentity();
9132001f49Smrg   glTranslatef(0.0, 0.0, -15.0);
9232001f49Smrg
9332001f49Smrg   Width = width;
9432001f49Smrg   Height = height;
9532001f49Smrg
9632001f49Smrg   z = (float *) malloc(width * height * 4);
9732001f49Smrg   {
9832001f49Smrg      int i, j, k = 0;
9932001f49Smrg      for (i = 0; i < height; i++) {
10032001f49Smrg         float zval = (float) i / (height - 1);
10132001f49Smrg         for (j = 0; j < width; j++) {
10232001f49Smrg            z[k++] = zval;
10332001f49Smrg         }
10432001f49Smrg      }
10532001f49Smrg   }
10632001f49Smrg}
10732001f49Smrg
10832001f49Smrg
10932001f49Smrgstatic void
11032001f49SmrgKey(unsigned char key, int x, int y)
11132001f49Smrg{
11232001f49Smrg   const GLfloat step = 1.0;
11332001f49Smrg   (void) x;
11432001f49Smrg   (void) y;
11532001f49Smrg   switch (key) {
11632001f49Smrg      case 'a':
11732001f49Smrg         Anim = !Anim;
11832001f49Smrg         if (Anim)
11932001f49Smrg            glutIdleFunc(Idle);
12032001f49Smrg         else
12132001f49Smrg            glutIdleFunc(NULL);
12232001f49Smrg         break;
12332001f49Smrg      case 'd':
12432001f49Smrg         showZ = !showZ;
12532001f49Smrg         break;
12632001f49Smrg      case 'z':
12732001f49Smrg         Zpos -= step;
12832001f49Smrg         break;
12932001f49Smrg      case 'Z':
13032001f49Smrg         Zpos += step;
13132001f49Smrg         break;
13232001f49Smrg      case 27:
13332001f49Smrg         glutDestroyWindow(Win);
13432001f49Smrg         exit(0);
13532001f49Smrg         break;
13632001f49Smrg   }
13732001f49Smrg   glutPostRedisplay();
13832001f49Smrg}
13932001f49Smrg
14032001f49Smrg
14132001f49Smrgstatic void
14232001f49SmrgSpecialKey(int key, int x, int y)
14332001f49Smrg{
14432001f49Smrg   const GLfloat step = 3.0;
14532001f49Smrg   (void) x;
14632001f49Smrg   (void) y;
14732001f49Smrg   switch (key) {
14832001f49Smrg      case GLUT_KEY_UP:
14932001f49Smrg         Xrot -= step;
15032001f49Smrg         break;
15132001f49Smrg      case GLUT_KEY_DOWN:
15232001f49Smrg         Xrot += step;
15332001f49Smrg         break;
15432001f49Smrg      case GLUT_KEY_LEFT:
15532001f49Smrg         Yrot -= step;
15632001f49Smrg         break;
15732001f49Smrg      case GLUT_KEY_RIGHT:
15832001f49Smrg         Yrot += step;
15932001f49Smrg         break;
16032001f49Smrg   }
16132001f49Smrg   glutPostRedisplay();
16232001f49Smrg}
16332001f49Smrg
16432001f49Smrg
16532001f49Smrgstatic void
16632001f49SmrgInit(void)
16732001f49Smrg{
16832001f49Smrg   /* setup lighting, etc */
16932001f49Smrg   glEnable(GL_DEPTH_TEST);
17032001f49Smrg   glEnable(GL_LIGHTING);
17132001f49Smrg   glEnable(GL_LIGHT0);
17232001f49Smrg}
17332001f49Smrg
17432001f49Smrg
17532001f49Smrgint
17632001f49Smrgmain(int argc, char *argv[])
17732001f49Smrg{
17832001f49Smrg   glutInit(&argc, argv);
17932001f49Smrg   glutInitWindowPosition(0, 0);
18032001f49Smrg   glutInitWindowSize(400, 400);
18132001f49Smrg   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
18232001f49Smrg   Win = glutCreateWindow(argv[0]);
18332001f49Smrg   glewInit();
18432001f49Smrg   glutReshapeFunc(Reshape);
18532001f49Smrg   glutKeyboardFunc(Key);
18632001f49Smrg   glutSpecialFunc(SpecialKey);
18732001f49Smrg   glutDisplayFunc(Draw);
18832001f49Smrg   if (Anim)
18932001f49Smrg      glutIdleFunc(Idle);
19032001f49Smrg   Init();
19132001f49Smrg   glutMainLoop();
19232001f49Smrg   return 0;
19332001f49Smrg}
194