modulate.c
3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#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);
}