[comp.sys.sgi] transparency hack

mg@GODZILLA.CGL.RMIT.OZ.AU (Mike Gigante) (11/27/90)

you might find this useful if you want transparency on non GT/GTX/VGX
platforms. I whipped this up for our PIs.

It is a quick and dirty hack, but it works.

In your 3D code, do something like

	if(material[X] < 1.0)
		transp((int)(100*material[X]));

where X is the index of the  float m.n in the material definition:

	ALPHA, m.n,

To say state the obvious, this simulates transparency by useinh fill
patterns of varying percent coverages.

No guarantees of anything, take it or leave it.

Mike Gigante,
RMIT Australia

---
#include "stdio.h"
#include "gl.h"
#include "device.h"

int transp ( float percent );
void defhalftones ( void );
void sethalftone ( int index );
int main ( int argc , char **argv );

typedef struct {
    int		percent;
    short	pattern[16];
} pat;

#define NPATTERNS 17

pat	patterns[NPATTERNS] = {
	{0,
	   0x0000, 0x0000, 0x0000, 0x0000,
	   0x0000, 0x0000, 0x0000, 0x0000,
	   0x0000, 0x0000, 0x0000, 0x0000,
	   0x0000, 0x0000, 0x0000, 0x0000},
	{6,
	   0x1111, 0x0000, 0x0000, 0x0000,
	   0x1111, 0x0000, 0x0000, 0x0000,
	   0x1111, 0x0000, 0x0000, 0x0000,
	   0x1111, 0x0000, 0x0000, 0x0000},
	{12,
	   0x2222, 0x0000, 0x8888, 0x0000,
	   0x2222, 0x0000, 0x8888, 0x0000,
	   0x2222, 0x0000, 0x8888, 0x0000,
	   0x2222, 0x0000, 0x8888, 0x0000},
	{18,
	   0x8888, 0x2222, 0x0000, 0x1111,
	   0x8888, 0x2222, 0x0000, 0x1111,
	   0x8888, 0x2222, 0x0000, 0x1111,
	   0x8888, 0x2222, 0x0000, 0x1111},
	{25,
	   0x4444, 0x1111, 0x4444, 0x1111,
	   0x4444, 0x1111, 0x4444, 0x1111,
	   0x4444, 0x1111, 0x4444, 0x1111,
	   0x4444, 0x1111, 0x4444, 0x1111},
	{31,
	   0xAAAA, 0x1111, 0x4444, 0x8888,
	   0xAAAA, 0x1111, 0x4444, 0x8888,
	   0xAAAA, 0x1111, 0x4444, 0x8888,
	   0xAAAA, 0x1111, 0x4444, 0x8888},
	{37,
	   0xAAAA, 0x2222, 0x9999, 0x4444,
	   0xAAAA, 0x2222, 0x9999, 0x4444,
	   0xAAAA, 0x2222, 0x9999, 0x4444,
	   0xAAAA, 0x2222, 0x9999, 0x4444},
	{43,
	   0xBBBB, 0x6666, 0x9999, 0x4444,
	   0xBBBB, 0x6666, 0x9999, 0x4444,
	   0xBBBB, 0x6666, 0x9999, 0x4444,
	   0xBBBB, 0x6666, 0x9999, 0x4444},
	{50,
	   0xAAAA, 0x5555, 0xAAAA, 0x5555,
	   0xAAAA, 0x5555, 0xAAAA, 0x5555,
	   0xAAAA, 0x5555, 0xAAAA, 0x5555,
	   0xAAAA, 0x5555, 0xAAAA, 0x5555},
	{56,
	   0x4444, 0x9999, 0x6666, 0x7777,
	   0x4444, 0x9999, 0x6666, 0x7777,
	   0x4444, 0x9999, 0x6666, 0x7777,
	   0x4444, 0x9999, 0x6666, 0x7777},
	{62,
	   0x5555, 0xDDDD, 0x6666, 0xBBBB,
	   0x5555, 0xDDDD, 0x6666, 0xBBBB,
	   0x5555, 0xDDDD, 0x6666, 0xBBBB,
	   0x5555, 0xDDDD, 0x6666, 0xBBBB},
	{68,
	   0x5555, 0xEEEE, 0xBBBB, 0x7777,
	   0x5555, 0xEEEE, 0xBBBB, 0x7777,
	   0x5555, 0xEEEE, 0xBBBB, 0x7777,
	   0x5555, 0xEEEE, 0xBBBB, 0x7777},
	{75,
	   0xDDDD, 0x7777, 0xDDDD, 0x7777,
	   0xDDDD, 0x7777, 0xDDDD, 0x7777,
	   0xDDDD, 0x7777, 0xDDDD, 0x7777,
	   0xDDDD, 0x7777, 0xDDDD, 0x7777},
	{81,
	   0x7777, 0xDDDD, 0xFFFF, 0xEEEE,
	   0x7777, 0xDDDD, 0xFFFF, 0xEEEE,
	   0x7777, 0xDDDD, 0xFFFF, 0xEEEE,
	   0x7777, 0xDDDD, 0xFFFF, 0xEEEE},
	{87,
	   0xEEEE, 0xFFFF, 0x7777, 0xFFFF,
	   0xEEEE, 0xFFFF, 0x7777, 0xFFFF,
	   0xEEEE, 0xFFFF, 0x7777, 0xFFFF,
	   0xEEEE, 0xFFFF, 0x7777, 0xFFFF},
	{93,
	   0xEEEE, 0xFFFF, 0xFFFF, 0xFFFF,
	   0xEEEE, 0xFFFF, 0xFFFF, 0xFFFF,
	   0xEEEE, 0xFFFF, 0xFFFF, 0xFFFF,
	   0xEEEE, 0xFFFF, 0xFFFF, 0xFFFF},
	{100,
	   0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
	   0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
	   0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
	   0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF}
};

int
transp(percent)
float percent;
{
    int i; 

#ifdef DEBUG
    fprintf(stderr,"transp: %f\n", percent);
#endif
    percent = 100.0 - percent;
    if (percent < 0.0) {
      sethalftone(0);
      return 0;
    }
    for(i=1; i < NPATTERNS; i++) {
      if (percent <= (float)patterns[i].percent) {
	float d1, d2;

	d1 = percent - patterns[i - 1].percent;
	d2 = patterns[i].percent - percent;
	if (d1 <= d2) {
	  sethalftone(i-1);
	  return patterns[i-1].percent;
	} else {
	  sethalftone(i);
	  return patterns[i].percent;
	}
      }
    }
    if (percent > 100.0) {
      sethalftone(16);
      return 100;
    }
}

void
  defhalftones()
{
  int i;
  
  for(i=0; i < NPATTERNS; i++) {
    defpattern( i+1 , PATTERN_16, patterns[i].pattern);
  }
}

void
  sethalftone(index)
int index;
{
  setpattern(index+1);
#ifdef DEBUG
  fprintf(stderr,"using pattern %d\n", index);
#endif
}

main(argc, argv)
     int argc;
     char **argv;
{
  int i, j;
  short val;

  if(argc != 1) {
    fprintf(stderr, "usage: %s\n", argv[0]);
    exit(1);
  }

  keepaspect(1,1);
  foreground();
  winopen("transp");
  defhalftones();
  ortho2(-0.5, 10.5, -0.5, 10.5);
  qdevice(ESCKEY);

  color(BLACK);
  clear();

  color(RED);

  for(i=0; i < 10; i++) {
    for(j=0; j < 10; j++) {
      float p;

      p = i*10 + j;
      transp(p);
      rectfi(j,i, j+1, i+1);
    }
  }

  while(qread(&val) != ESCKEY)
    ;
}