/*
 * rgbc.c - RGB Cube
 cc -o rgbc rgbc.c -lPEX5 -lm -lX11                    -lnsl

			Copyright (c) 1993 by 
			Oki Electric Industry Co., Ltd.
			All Rights Reserved
	
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Oki not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission. Oki
 * makes no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
*/
#include <X11/Xlib.h>
#include <X11/PEX5/PEXlib.h>

#define SET_POINT(p,a,b,c) {(p)->x=(a);(p)->y=(b);(p)->z=(c);}
#define SET_RGB(p,a,b,c) {(p)->red=(a);(p)->green=(b);(p)->blue=(c);}

#define RADICAL3 1.732
#define RAD3INV  (1.0/RADICAL3)

main (argc, argv)
     int argc;
     char  *argv[];
{
  Display		*theDisplay;
  char			*displayString = (char *)0;
  PEXExtensionInfo	*info_return;
  char			err_msg[PEXErrorStringLength];
  XID                   theStrux;  
  int                   i;
  PEXCoord p[2];

  for ( i = 1; i<argc; i++ ) {
    if ((strncmp(argv[i],"-display",strlen(argv[i]))) == 0) {
      if (++i > argc) { printf("not enough args"); exit(1); }
      displayString = argv[i];
    } else if ((strncmp(argv[i],"-strux",strlen(argv[i]))) == 0) {
      if (++i > argc) { printf("not enough args"); exit(1); }
      theStrux = atoi(argv[i]);
    }
  }

  /*
   * Open the display and initialize the PEX extension.
   */
  
  if (!(theDisplay = XOpenDisplay(displayString)))
    {
      printf ( "Could not open display %s\n",displayString);
      exit (1);
    }
  
  if (PEXInitialize(theDisplay, &info_return, PEXErrorStringLength, err_msg))
    {
      printf ("%s\n", err_msg);
      exit (1);	    
    }

  PEXSetLineColorIndex( theDisplay, theStrux, PEXOCStore, 2);
  p[0].x = 0; p[0].y = 0; p[0].z = 0;
  p[1].x = -1; p[1].y = -1; p[1].z = -1;
  PEXPolyline(theDisplay, theStrux, PEXOCStore, 2, p );

  PEXSetSurfaceColorIndex( theDisplay, theStrux, PEXOCStore, 7 );

  {
    PEXVertexRGB             coords[8];
    PEXArrayOfVertex         varr;
    PEXConnectivityData      c10y[6];
    PEXListOfUShort          clist[6];
    unsigned short           indices[24];
    
    PEXArrayOfFacetData      aFacetData;
    
    
    SET_POINT(&coords[0].point, 0,0,0); SET_RGB(&coords[0].rgb, 0,0,0);
    SET_POINT(&coords[1].point, 1,0,0); SET_RGB(&coords[1].rgb, 1,0,0);
    SET_POINT(&coords[2].point, 0,1,0); SET_RGB(&coords[2].rgb, 0,1,0);
    SET_POINT(&coords[3].point, 0,0,1); SET_RGB(&coords[3].rgb, 0,0,1);
    SET_POINT(&coords[4].point, 1,1,0); SET_RGB(&coords[4].rgb, 1,1,0);
    SET_POINT(&coords[5].point, 1,0,1); SET_RGB(&coords[5].rgb, 1,0,1);
    SET_POINT(&coords[6].point, 0,1,1); SET_RGB(&coords[6].rgb, 0,1,1);
    SET_POINT(&coords[7].point, 1,1,1); SET_RGB(&coords[7].rgb, 1,1,1);

    
    indices[0] = 0; indices[1] = 1; indices[2] = 4; indices[3] = 2;
    indices[4] = 0; indices[5] = 2; indices[6] = 6; indices[7] = 3;
    indices[8] = 0; indices[9] = 3; indices[10] = 5; indices[11] = 1;
    indices[12] = 7; indices[13] = 6; indices[14] = 2; indices[15] = 4;
    indices[16] = 7; indices[17] = 4; indices[18] = 1; indices[19] = 5;
    indices[20] = 7; indices[21] = 5; indices[22] = 3; indices[23] = 6;
    
    clist[0].count = 4;
    clist[0].shorts = &indices[0];
    clist[1].count = 4;
    clist[1].shorts = &indices[4];
    clist[2].count = 4;
    clist[2].shorts = &indices[8];
    clist[3].count = 4;
    clist[3].shorts = &indices[12];
    clist[4].count = 4;
    clist[4].shorts = &indices[16];
    clist[5].count = 4;
    clist[5].shorts = &indices[20];
    
    c10y[0].count = 1;
    c10y[0].lists = &clist[0];
    c10y[1].count = 1;
    c10y[1].lists = &clist[1];
    c10y[2].count = 1;
    c10y[2].lists = &clist[2];
    c10y[3].count = 1;
    c10y[3].lists = &clist[3];
    c10y[4].count = 1;
    c10y[4].lists = &clist[4];
    c10y[5].count = 1;
    c10y[5].lists = &clist[5];
    
    varr.rgb = coords;
    PEXSetOfFillAreaSets( theDisplay, theStrux, PEXOCStore,
			 PEXShapeUnknown, PEXGANone, PEXGAColor, 
			 PEXGANone, PEXContourUnknown, 1, PEXColorTypeRGB,
			 6, aFacetData, 8, varr, 24, (PEXSwitch *)0, c10y );
  }

  XSync(theDisplay,0);
}
