[ Previous | Next | Contents | Glossary | Home | Search ]
GL3.2 for AIX: Graphics Library (GL) Technical Reference

zbuffer2.c Example C Language Program

/* Example: zbuffer2.c */
/*
This program illustrates z buffering. It draws three parallel
pipeds rotating through each other.
This program requires a 24-bit adapter and the z buffer option.
*/
#include <gl/gl.h>
#include <gl/device.h>
float v0[3] = {-1.0, -1.0, -1.0} ;
float v1[3] = { -1.0, -1.0, 1.0};
float v2[3] = { -1.0, 1.0, 1.0};
float v3[3] = { -1.0, 1.0, -1.0};
float v4[3] = { 1.0, -1.0, -1.0};
float v5[3] = { 1.0, -1.0, 1.0};
float v6[3] = { 1.0, 1.0, 1.0};
float v7[3] = { 1.0, 1.0, -1.0};
Int32 delaycount;
main(argc, argv)
int     argc;
char    *argv[];
{
   Int32 xrot, yrot, zrot;
   xrot = yrot = zrot = 0;
   if (argc == 1)
      delaycount = 0;
   else
      delaycount = 1;
   keepaspect(1, 1);
   prefposition (0,800,0,800);
   winopen("zbuffer");
   RGBmode();
   if (delaycount == 0)
      doublebuffer();
   gconfig();
   ortho(-4.0, 4.0, -4.0, 4.0, -4.0, 4.0);
   while (1) {
      pushmatrix();
      rotate(xrot, 'x');
      rotate(yrot, 'y');
      rotate(zrot, 'z');
      xrot += 11;
      yrot += 15;
      if (xrot + yrot > 3500) zrot += 23;
      if (xrot > 3600) xrot -= 3600;
      if (yrot > 3600) yrot -= 3600;
      if (zrot > 3600) zrot -= 3600;
      cpack(0);
      clear();
      if (getbutton(LEFTMOUSE)) {
         zbuffer(TRUE);
         zclear();
      }
      else
         zbuffer(FALSE);
      pushmatrix();
      scale(1.2, 1.2, 1.2);
      translate(.3, .2, .2);
      if (getbutton(MIDDLEMOUSE)) {
         reshapeviewport();
      }
      drawcube();
      popmatrix();
      pushmatrix();
      rotate(450+zrot, 'x');
      rotate(300-xrot, 'y');
      scale(1.8, .8, .8);
      drawcube();
      popmatrix();
      pushmatrix();
      rotate(500+yrot, 'z');
      rotate(-zrot, 'x');
      translate(-.3, -.2, .6);
      scale(1.4, 1.2, .7);
      drawcube();
      popmatrix();
      if (delaycount == 0)
         swapbuffers();
      popmatrix();
   }
}
delay()
{
   sleep(delaycount);
}
drawcube()
{
   cpack(0xff);
   bgnpolygon();
   v3f(v0); 
   v3f(v1); 
   v3f(v2); 
   v3f(v3);
   endpolygon();
   delay();
   cpack(0xff00);
   bgnpolygon();
   v3f(v0); 
   v3f(v4); 
   v3f(v5); 
   v3f(v1);
   endpolygon();
   delay();
   cpack(0xff0000);
   bgnpolygon();
   v3f(v4); 
   v3f(v7); 
   v3f(v6); 
   v3f(v5);
   endpolygon();
   delay();
   cpack(0xffff);
   bgnpolygon();
   v3f(v3); 
   v3f(v7); 
   v3f(v6); 
   v3f(v2);
   endpolygon();
   delay();
   cpack(0xff00ff);
   bgnpolygon();
   v3f(v5); 
   v3f(v1); 
   v3f(v2); 
   v3f(v6);
   endpolygon();
   delay();
   cpack(0xffff00);
   bgnpolygon();
   v3f(v0); 
   v3f(v4); 
   v3f(v7); 
   v3f(v3);
   endpolygon();
}
/*
  Changes:
     - Adding reshapeviewport when middlemouse
     - Added a prefposition(0,800,0,800);
*/

Related Information

The zbuffer subroutine, zclear subroutine.

Zbuffering in GL3.2 Version 4 for AIX: Programming Concepts introduces basic concepts about displaying objects closest to the viewer.


[ Previous | Next | Contents | Glossary | Home | Search ]