modulate.c 3.49 KB

#include "graphic.h"
#include <stdio.h>

static struct Tex_color NULL_color = {0, 0, 0, 0};
static struct Tex_color ONE_color = {255, 255, 255, 255};

do_modulate(mask)
int mask;
{
struct Tex_color *tp;
struct Iter_color *ip;
struct Color_depth *cd;
struct Tex_color ic;
struct Tex_color  c1, c2, c3, m1;
int clr_1_sel, clr_2_sel, clr_3_sel, mult_sel, shade_en;
int r, g, b, a;
static int first_time = 1;

extern struct Tex_color PrimitiveColor;
extern struct Tex_color EnvironmentColor;
extern int CombineMode;

        tp = &tex_color[0];
	ip = &iter_color[0];
	cd = &col_dep[0];

	do_tc_step();
	do_tex_filter();
	do_cz_step(mask);

   /* convert to common structure type */
   ic.r = ip->r;
   ic.g = ip->g;
   ic.b = ip->b;
   ic.a = ip->a;

   shade_en  = CombineMode & 0x8000; 
   clr_1_sel = CombineMode & 0x3000; 
   clr_2_sel = CombineMode & 0x0300; 
   mult_sel  = CombineMode & 0x0070; 
   clr_3_sel = CombineMode & 0x0003; 

  switch(clr_1_sel)
  {
    case SEL_TEX_CLR_1:
	c1 = tex_color[0];
	break;
    case SEL_PRIM_CLR_1:
	if(shade_en)
	  c1 = ic;
	else
	  c1 = PrimitiveColor;
	break;
    case SEL_ENV_CLR_1:
	c1 = EnvironmentColor;
	break;
  }

  switch(clr_2_sel)
  {
    case SEL_TEX_CLR_2:
	c2 = tex_color[0];
	break;
    case SEL_PRIM_CLR_2:
	if(shade_en)
	  c2 = ic;
	else
	  c2 = PrimitiveColor;
	break;
    case SEL_NO_CLR_2:
	c2 = NULL_color;
	break;
  }

  switch(mult_sel)
  {
    case SEL_TEXC_MULT:
	m1 = tex_color[0];
	break;
    case SEL_PRIMC_MULT:
	if(shade_en)
	  m1 = ic;
	else
	  m1 = PrimitiveColor;
	break;
    case SEL_ENVC_MULT:
	m1 = EnvironmentColor;
	break;
    case SEL_TEXA_MULT:
	m1.r = tex_color[0].a;
	m1.g = tex_color[0].a;
	m1.b = tex_color[0].a;
	break;
    case SEL_PRIMA_MULT:
	m1.r = (shade_en) ? ic.a : PrimitiveColor.a;
	m1.g = (shade_en) ? ic.a : PrimitiveColor.a;
	m1.b = (shade_en) ? ic.a : PrimitiveColor.a;
   	break;
    case SEL_ENVA_MULT:
	m1.r = EnvironmentColor.a;
	m1.g = EnvironmentColor.a;
	m1.b = EnvironmentColor.a;
 	break;
    case SEL_ONE_MULT:
	m1 = ONE_color;
	break;
  }

  switch(clr_3_sel)
  {
    case SEL_TEX_CLR_3:
	c3 = tex_color[0];
	break;
    case SEL_PRIM_CLR_3:
	if(shade_en)
	  c3 = ic;
	else
	  c3 = PrimitiveColor;
	break;
    case SEL_ENV_CLR_3:
	c3 = EnvironmentColor;
	break;
    case SEL_NO_CLR_3:
	c3 = NULL_color;
	break;
  }

#ifdef DEBUG
  if(first_time)
  {
    fprintf(stderr,"Combine Mode = %04x\n", CombineMode);
    fprintf(stderr,"c1.r = %d c1.g = %d, c1.b = %d, c1.a = %d\n", c1.r,c1.g,c1.b,c1.a);
    fprintf(stderr,"c2.r = %d c2.g = %d, c2.b = %d, c2.a = %d\n", c2.r,c2.g,c2.b,c2.a);
    fprintf(stderr,"m1.r = %d m1.g = %d, m1.b = %d, m1.a = %d\n", m1.r,m1.g,m1.b,m1.a);
    fprintf(stderr,"c3.r = %d c3.g = %d, c3.b = %d, c3.a = %d\n", c3.r,c3.g,c3.b,c3.a);
    first_time = 0;
  }
#endif

  r = combine_color( c1.r, c2.r, m1.r, c3.r);
  g = combine_color( c1.g, c2.g, m1.g, c3.g);
  b = combine_color( c1.b, c2.b, m1.b, c3.b);
  a = ip->a;

if (render_mode == RENDER_TEXEDGE) {
int da, t;
	r = ip->r;
	g = ip->g;
	b = ip->b;
	a = tex_color[0].a;
	da = tex_coord[0].l;	/* texels per pixel */
	if (da > TI_FRAC_MASK) { /* >= 1, use alpha */
		;
	} else if (da <= 0) { /* zero, threshhold */
		if (a >= 128) a = 255;
		else a = 0;
	} else {
		da = da << (8-TI_FRAC_SHFT); /* da to 8b frac */
		t = a - (128 - (da>>1));
		if (t <= 0) a = 0;
		else if (t >= da) a = 255;
		else a = (int)((float)t/(float)da*(float)255);
		}
	}

  r = clamp_color( r);
  g = clamp_color( g);
  b = clamp_color( b);
  a = clamp_color( a);

  cd->c = pack_colora( r, g, b, a);
}