[ Next Article |
Previous Article |
Book Contents |
Library Home |
Legal |
Search ]
GL3.2 for AIX: Graphics Library (GL) Technical Reference
depthcue.c Example C Language Program
/* depthcue.c Example C Language Program
*
* Draws a depthcue'd 3-d wireframe cube with lots of
* little points inside. Moving the mouse rotates the
* cube.
* NEAR and FAR (Z) clipplanes map to z values of
* getgdesc(GD_ZMIN) and getgdesc(GD_ZMAX) respectively.
* Points near NEAR are the brightest, those near FAR are
* the dimmest. Give two hexadecimal values on the command
* line to specify which zvalues are mapped to bright
* and dark.
*
* Press the middle mouse to quit.
*/
#include <gl/gl.h>
#include <gl/device.h>
#include <math.h>
#define CUBE 9531
float hrand();
/*Hold the colormap values*/
static short *CarrayR, *CarrayG, *CarrayB;
main (argc,argv)
int argc;
char **argv;
{
int val, i;
long near, far, /*Z buffer and depth cueing*/
nearest, farthest; /*information*/
float inc, c;
short lo_end=128, /*Low/High index of the colormap*/
hi_end=255; /*Used for the depth cueing*/
prefposition(200, 600, 200, 600);
winopen("depthcue");
winconstraints();
doublebuffer();
gconfig();
/*Find out the nearest and farthest values of the z mapping*/
nearest = getgdesc(GD_ZMIN);
farthest = getgdesc(GD_ZMAX);
if (argc == 5) {
near = strtol(argv[1], (char **) NULL, 0);
far = strtol(argv[2], (char **) NULL, 0);
lo_end = strtol(argv[3], (char **) NULL, 0);
hi_end = strtol(argv[4], (char **) NULL, 0);
}
else if (argc == 3) {
near = strtol(argv[1], (char **) NULL, 0);
far = strtol(argv[2], (char **) NULL, 0);
}
else {
near = nearest;
far = farthest;
}
if (near < nearest) near=nearest; /*Clamp to adapters constraints*/
if (far > farthest) far=farthest;
reshapeviewport();
perspective(600, 1.0, 350.0, 1400.0);
lookat(0.0, 0.0, 700.0, 0.0, 0.0, 0.0, 0);
qdevice(KEYBD);
makeobj(CUBE); /* generate a bunch of random points */
for (i = 0; i < 100; i++)
pnt(hrand(-200.0,200.0), hrand(-200.0,200.0),
hrand(-200.0,200.0)); /* and a cube */
movei(-200, -200, -200);
drawi( 200, -200, -200);
drawi( 200, 200, -200);
drawi(-200, 200, -200);
drawi(-200, -200, -200);
drawi(-200, -200, 200);
drawi(-200, 200, 200);
drawi(-200, 200, -200);
movei(-200, 200, 200);
drawi( 200, 200, 200);
drawi( 200, -200, 200);
drawi(-200, -200, 200);
movei( 200, 200, 200);
drawi( 200, 200, -200);
movei( 200, -200, -200);
drawi( 200, -200, 200);
closeobj();
/*Save the current colormap so it can be reset when the program ends*/
save_cmap(lo_end,hi_end);
/* load the color map with a cyan ramp */
/* starting at lo_end and going to hi_end inclusively */
inc = 255.0/((float)(hi_end-lo_end));
c = 0.0;
for (i = lo_end; i <= hi_end; i++)
{
c += inc;
mapcolor(i, 0,(Int16) c, (Int16) c);
}
/*Print out the information about the color and z buffer*/
printf (" near = %ld, far = %ld \n", near, far);
printf (" nearest = %ld, farthest = %ld \n", nearest, farthest);
printf (" low colormap = %d, high colormap = %d\n", lo_end, hi_end);
printf (" number of colors in ramp = %d \n", 1+hi_end-lo_end);
/* Set the range of z values that will be stored in the */
/* bitplanes. The percentage takes care of the error */
/* accumulation that occurs when the farthest limits are */
/* used. */
lsetdepth((long)(near*0.98), (long)(far*0.98));
/* set up the mapping of z values to color map */
/* indexes: z value nearest is mapped to index lo_end */
/* and z value farthest is mapped to index hi_end */
lshaderange(lo_end,hi_end,nearest,farthest);
/* turn on depthcue mode: the color index of each */
/* pixel in points and lines is determined from the */
/* z value of the pixel */
depthcue(TRUE);
/* until a key is pressed, rotate cube according to */
/* movement of the mouse */
while (!getbutton(MIDDLEMOUSE)) {
pushmatrix();
rotate(3*getvaluator(MOUSEY), 'x');
rotate(3*getvaluator(MOUSEX), 'y');
color(BLACK);
clear();
callobj(CUBE);
popmatrix();
swapbuffers();
}
/*Restore the colormap*/
restore_cmap(lo_end,hi_end);
/*Exit gracefully*/
gexit();
exit(1);
}
/* this routine returns random numbers in the */
/* specified range */
float hrand(low,high)
float low,high;
{
float val;
val = ((float)( (short)rand() & 0xffff)) / ((float)0xffff);
return( (2.0 * val * (high-low)) + low);
}
/*This saves the colormap*/
save_cmap(lo_end,hi_end)
short lo_end,hi_end;
{
CarrayR = calloc (lo_end+hi_end,sizeof(short));
CarrayG = calloc (lo_end+hi_end,sizeof(short));
CarrayB = calloc (lo_end+hi_end,sizeof(short));
getmcolors ((Int16 const)lo_end,(Int16 const)hi_end,
CarrayR, CarrayG, CarrayB);
}
/*This restores the colormap*/
restore_cmap(lo_end,hi_end)
short lo_end,hi_end;
{
mapcolors ((Int16 const)lo_end,(Int16 const)hi_end,
CarrayR, CarrayG, CarrayB);
}
/*Changes
- Put constants in the program for the colormap creation
- This program accept 2 arguments that are the nearest and
farthest z values which are used for the depth cueing.
This program called: lshaderange(128,255,-0x40000,0x3FFFFF);
now: lshaderange(lo_end,hi_end,nearest,farthest);
- Used a define for the object number (called CUBE)
- Save/restore the colormap
- The defaults of the near and far z mappings are no longer
hard coded ( near = -0x400000; far = 0x3fffff;), but are
initialized with getgdesc. Also the near and far mappings
are clamped to there min and max values respectively.
*/
Related Information
[ Next Article |
Previous Article |
Book Contents |
Library Home |
Legal |
Search ]