source: golgotha/src/render/software/tint_manage.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 6.0 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "software/r1_software.hh"
10#include "software/r1_software_globals.hh"
11
12w32  *color_modify_list_low[128];
13sw32 num_color_modifies_low=0;
14
15w32  *color_modify_list_high[128];
16sw32 num_color_modifies_high=0;
17
18void insert_color_modify_address_low(w32 *address)
19{
20  if (num_color_modifies_low>=128)
21  {
22    i4_error("out of color modify address space low");
23  }
24 
25  color_modify_list_low[num_color_modifies_low] = address;
26  num_color_modifies_low++; 
27}
28
29void insert_color_modify_address_high(w32 *address)
30{
31  if (num_color_modifies_high>=128)
32  {
33    i4_error("out of color modify address space high");
34  }
35 
36  color_modify_list_high[num_color_modifies_high] = address;
37  num_color_modifies_high++; 
38}
39
40void update_color_modify_addresses(w32 *color_table_ptr)
41{
42  sw32 i;
43  for (i=0;i<num_color_modifies_low;i++)
44  {
45    (*color_modify_list_low[i]) = (w32)color_table_ptr;
46  }
47 
48  for (i=0;i<num_color_modifies_high;i++)
49  {
50    (*color_modify_list_high[i]) = ((w32)color_table_ptr) + ctable_size_bytes;
51  }
52}
53
54const int MAX_SOFTWARE_COLOR_TABLES = 10;
55software_color_table software_color_tables[MAX_SOFTWARE_COLOR_TABLES];
56sw32 num_software_color_tables=0;
57
58inline w16 light_pixel(w32 c, i4_pixel_format *s, double x,
59                       double r_factor, double g_factor, double b_factor,
60                       double &r_error, double &g_error, double &b_error)
61{
62  double r = (double)((c & s->red_mask)   >> s->red_shift);
63  double g = (double)((c & s->green_mask) >> s->green_shift);
64  double b = (double)((c & s->blue_mask)  >> s->blue_shift);
65
66  double f_nr = r * x * r_factor;
67  double f_ng = g * x * g_factor;
68  double f_nb = b * x * b_factor;
69
70  w32 nr,ng,nb;
71
72  nr = i4_f_to_i(f_nr+0.5);
73 
74  ng = i4_f_to_i(f_ng+0.5);
75 
76  nb = i4_f_to_i(f_nb+0.5);
77
78  return (s->red_mask & (nr << s->red_shift)) | (s->green_mask & (ng << s->green_shift)) | (s->blue_mask & (nb << s->blue_shift));
79}
80
81void generate_color_table(software_color_table *dest_table, i4_pixel_format *fmt,
82                          double r_factor, double g_factor, double b_factor)
83{
84  sw32 x,y;
85
86  double ytemp;
87 
88  double r_error,g_error,b_error;
89 
90  w32 lo;
91  w32 hi;
92 
93  for(x=255; x>=0; x--)
94  {
95    r_error = 0;
96    g_error = 0;
97    b_error = 0;
98
99    for(y=NUM_LIGHT_SHADES; y>=0; y--)
100    {
101      ytemp = (double)y / (double)NUM_LIGHT_SHADES;
102
103      lo = light_pixel(x, fmt, ytemp, r_factor, g_factor, b_factor, r_error, g_error, b_error);
104
105      dest_table->low_lookups()[y*256 + x] = lo;
106    }
107  }
108
109  for (x=255; x>=0; x--)
110  {
111    r_error = 0;
112    g_error = 0;
113    b_error = 0;
114
115    for(y=NUM_LIGHT_SHADES; y>=0; y--)
116    {
117      ytemp = (double)y / (double)NUM_LIGHT_SHADES;
118
119      hi = light_pixel(((w32)x) << 8, fmt, ytemp, r_factor, g_factor, b_factor, r_error, g_error, b_error);
120
121      dest_table->high_lookups()[y*256 + x] = hi;
122    }
123  }
124}
125
126void setup_alpha_table(i4_pixel_format *fmt)
127{
128  sw32 i;
129
130  for (i=0; i<4096;i++)
131  {
132    w16 r = ((i & 0x0F00) >> 4);
133    w16 g = ((i & 0x00F0) >> 0);
134    w16 b = ((i & 0x000F) << 4);
135   
136    r = (r >> (8-fmt->red_bits))   << fmt->red_shift;
137    g = (g >> (8-fmt->green_bits)) << fmt->green_shift;
138    b = (b >> (8-fmt->blue_bits))  << fmt->blue_shift;
139
140    alpha_table[i] = r | g | b;                               
141  }   
142}
143
144r1_color_tint_handle r1_software_class::register_color_tint(i4_float r, i4_float g, i4_float b)
145{
146  if (num_color_tints>=MAX_SOFTWARE_COLOR_TABLES) return 0;
147 
148  generate_color_table(&software_color_tables[num_software_color_tables],&fmt,r,g,b);
149  num_software_color_tables++;
150 
151  return num_software_color_tables-1;
152}
153
154void r1_software_class::set_color_tint(r1_color_tint_handle c)
155{
156  color_tint_on = i4_T;
157  if ((c != cur_color_tint) && (c < num_software_color_tables))
158  {
159    cur_color_tint = c;
160   
161    //dont actually modify the code while you're buffering up spans, nothing is
162    //actually being drawn - instead, the cur_color_tint variable is used to remember
163    //what tint to use for all polys inserted into the buffer for this duration
164    //of time, and then set_color_tint() is called again at the end of the frame for
165    //each polygon, with use_spans set to false, so that it actually modifies the code
166    if (!use_spans)
167      update_color_modify_addresses(software_color_tables[c].table);
168  }
169}
170
171void setup_color_modify_affine_lit();
172void setup_color_modify_perspective_lit();
173
174void setup_color_modify_affine_lit_amd3d();
175void setup_color_modify_perspective_lit_amd3d();
176
177void r1_software_class::init_color_tints()
178{
179  setup_alpha_table(&fmt);
180
181#ifdef USE_AMD3D
182  setup_color_modify_affine_lit_amd3d();
183  setup_color_modify_perspective_lit_amd3d();
184#endif
185
186  setup_color_modify_affine_lit();
187  setup_color_modify_perspective_lit();
188
189  w16 tr = (((fmt.red_mask)   >> (fmt.red_shift))-1)   << fmt.red_shift;
190  w16 tg = (((fmt.green_mask) >> (fmt.green_shift))-1) << fmt.green_shift;
191  w16 tb = (((fmt.blue_mask)  >> (fmt.blue_shift))-1)  << fmt.blue_shift;
192 
193  pre_blend_and  = (tr | tg | tb);
194  post_blend_and = pre_blend_and >> 1;
195
196  pre_blend_and  = pre_blend_and  | (pre_blend_and  << 16);
197  post_blend_and = post_blend_and | (post_blend_and << 16);
198 
199  red_clight_mul   = ((1<<fmt.red_bits)-1);
200  green_clight_mul = ((1<<fmt.green_bits)-1);
201  blue_clight_mul  = ((1<<fmt.blue_bits)-1);
202
203  num_software_color_tables = 1;
204
205  r1_software_class_instance.use_spans = i4_F;
206
207  generate_color_table(&software_color_tables[0],&fmt,1.0,1.0,1.0); 
208 
209  set_color_tint(0);   
210}
211
Note: See TracBrowser for help on using the repository browser.