]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/shade.cpp
added copyright header
[taylor/freespace2.git] / src / graphics / shade.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Graphics/Shade.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Routines to shade an area.
16  *
17  * $Log$
18  * Revision 1.2  2002/06/09 04:41:18  relnev
19  * added copyright header
20  *
21  * Revision 1.1.1.1  2002/05/03 03:28:09  root
22  * Initial import.
23  *
24  * 
25  * 2     10/07/98 10:53a Dave
26  * Initial checkin.
27  * 
28  * 1     10/07/98 10:49a Dave
29  * 
30  * 19    3/12/98 5:36p John
31  * Took out any unused shaders.  Made shader code take rgbc instead of
32  * matrix and vector since noone used it like a matrix and it would have
33  * been impossible to do in hardware.   Made Glide implement a basic
34  * shader for online help.  
35  * 
36  * 18    3/10/98 4:19p John
37  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
38  * & Glide have popups and print screen.  Took out all >8bpp software
39  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
40  * support Fred.  Made zbuffering key off of functions rather than one
41  * global variable.
42  * 
43  * 17    11/30/97 12:18p John
44  * added more 24 & 32-bpp primitives
45  * 
46  * 16    10/19/97 12:55p John
47  * new code to lock / unlock surfaces for smooth directx integration.
48  * 
49  * 15    10/09/97 5:23p John
50  * Added support for more 16-bpp functions
51  * 
52  * 14    10/03/97 12:16p John
53  * optimized the shader.  About 50% faster.
54  * 
55  * 13    10/03/97 9:10a John
56  * added better antialiased line drawer
57  * 
58  * 12    9/09/97 10:41a Sandeep
59  * fixed warning level 4
60  * 
61  * 11    6/18/97 12:07p John
62  * fixed some color bugs
63  * 
64  * 10    6/17/97 7:04p John
65  * added d3d support for gradients.
66  * fixed some color bugs by adding screen signatures instead of watching
67  * flags and palette changes.
68  * 
69  * 9     5/28/97 8:59a John
70  * Fixed bug with shader not working when switching to fullscreen.
71  * 
72  * 8     5/12/97 12:27p John
73  * Restructured Graphics Library to add support for multiple renderers.
74  * 
75  * 7     11/19/96 2:44p Allender
76  * fix up shader for 15 bpp
77  * 
78  * 6     11/18/96 2:27p Allender
79  * made faster hacked version of shader for 16 bits
80  * 
81  * 5     11/18/96 1:48p Allender
82  * added 16 bit version of (very slow) shader
83  * 
84  * 4     11/15/96 3:34p Allender
85  * started on 16 bit support for the shader
86  * 
87  * 3     10/26/96 1:40p John
88  * Added some now primitives to the 2d library and
89  * cleaned up some old ones.
90  *
91  * $NoKeywords: $
92  */
93
94 #include "2d.h"
95 #include "grinternal.h"
96 #include "floating.h"
97 #include "line.h"
98 #include "palman.h"
99
100 void grx_create_shader(shader * shade, float r, float g, float b, float c )
101 {
102         int i;
103         float Sr, Sg, Sb;
104         float Dr, Dg, Db;
105         int ri, gi, bi;
106
107         shade->screen_sig = gr_screen.signature;
108         shade->r = r;
109         shade->g = g;
110         shade->b = b;
111         shade->c = c;
112
113         for (i=0; i<256; i++ )  {
114                 Sr = i2fl( gr_palette[i*3+0] );
115                 Sg = i2fl( gr_palette[i*3+1] );
116                 Sb = i2fl( gr_palette[i*3+2] );
117                 Dr = Sr*r + Sg*r + Sb*r + c*256.0f;
118                 Dg = Sr*g + Sg*g + Sb*g + c*256.0f;
119                 Db = Sr*b + Sg*b + Sb*b + c*256.0f;
120                 ri = fl2i(Dr); if ( ri < 0 ) ri = 0; else if (ri>255) ri = 255;
121                 gi = fl2i(Dg); if ( gi < 0 ) gi = 0; else if (gi>255) gi = 255;
122                 bi = fl2i(Db); if ( bi < 0 ) bi = 0; else if (bi>255) bi = 255;
123                 shade->lookup[i] = (unsigned char)(palette_find(ri,gi,bi));
124         }
125
126 }
127
128 void grx_set_shader( shader * shade )
129 {
130         if ( shade ) {
131                 if (shade->screen_sig != gr_screen.signature)   {
132                         gr_create_shader( shade, shade->r, shade->g, shade->b, shade->c );
133                 }
134                 gr_screen.current_shader = *shade;
135         } else {
136                 gr_create_shader( &gr_screen.current_shader, 0.0f, 0.0f, 0.0f, 0.0f );
137         }
138 }
139
140
141 void gr8_shade(int x,int y,int w,int h)
142 {
143         int x1, y1, x2, y2;
144         ubyte *xlat_table;
145
146         x1 = x; 
147         if (x1 < gr_screen.clip_left) x1 = gr_screen.clip_left;
148         if (x1 > gr_screen.clip_right) x1 = gr_screen.clip_right;
149
150         x2 = x+w-1; 
151         if (x2 < gr_screen.clip_left) x2 = gr_screen.clip_left;
152         if (x2 > gr_screen.clip_right) x2 = gr_screen.clip_right;
153
154         y1 = y; 
155         if (y1 < gr_screen.clip_top) y1 = gr_screen.clip_top;
156         if (y1 > gr_screen.clip_bottom) y1 = gr_screen.clip_bottom;
157
158         y2 = y+h-1; 
159         if (y2 < gr_screen.clip_top) y2 = gr_screen.clip_top;
160         if (y2 > gr_screen.clip_bottom) y2 = gr_screen.clip_bottom;
161
162         w = x2 - x1 + 1;
163         if ( w < 1 ) return;
164
165         h = y2 - y1 + 1;
166         if ( h < 1 ) return;
167
168         int i;
169         xlat_table = gr_screen.current_shader.lookup;
170
171         gr_lock();
172
173         for (i=0; i<h; i++ )    {
174                 ubyte * dp = GR_SCREEN_PTR(ubyte,x1,y1+i);
175                                 #ifdef USE_INLINE_ASM
176
177                                         int w1=w;
178
179                                         // 4 byte align
180                                         while ( (uint)dp & 3 )  {
181                                                 *dp = xlat_table[*dp];
182                                                 dp++;
183                                                 w1--;
184                                                 if ( w1 < 1 ) break;
185                                         }
186
187                                         if ( w1 < 1 ) continue;
188                                 
189                                         int wd4 = w1 / 4;
190                                         int left_over = w1 % 4;
191                         
192                                         if ( wd4 > 0 )  {
193 #ifdef PLAT_UNIX
194                                                 STUB_FUNCTION;
195 #else
196                                                 _asm push eax
197                                                 _asm push ebx
198                                                 _asm push ecx
199                                                 _asm push edx
200                                                 _asm push edi           
201                                                 _asm push esi
202                                                 _asm mov esi, xlat_table
203                                                 _asm mov edi, dp
204                                                 _asm mov edi, dp
205                                                 _asm mov ecx, wd4
206                                                 _asm mov eax, 0
207                                                 _asm mov ebx, 0
208                                                 _asm mov edx, 0
209
210         NextPixel:
211                                                 _asm mov eax, [edi]
212
213                                                 _asm mov dl, al
214                                                 _asm mov bl, ah
215
216                                                 _asm add edi, 4
217
218                                                 _asm mov al, [edx+esi]
219                                                 _asm mov ah, [ebx+esi]
220
221                                                 _asm ror eax, 16
222                                                 
223                                                 _asm mov dl, al
224                                                 _asm mov bl, ah
225
226                                                 _asm mov al, [edx+esi]
227                                                 _asm mov ah, [ebx+esi]
228
229                                                 _asm ror eax, 16
230
231                                                 _asm mov [edi-4], eax
232
233                                                 _asm dec ecx
234                                                 _asm jnz NextPixel
235
236
237                                                 _asm mov dp, edi
238
239                                                 _asm pop esi
240                                                 _asm pop edi
241                                                 _asm pop edx
242                                                 _asm pop ecx
243                                                 _asm pop ebx
244                                                 _asm pop eax
245 #endif
246                                         }
247
248                                         for (int j=0; j<left_over; j++ )        {
249                                                 *dp = xlat_table[*dp];
250                                                 dp++;
251                                         }
252
253                         
254                                 #else
255                                         for (int j=0; j<w; j++ )        {
256                                                 *dp = xlat_table[*dp];
257                                                 dp++;
258                                         }
259                                 #endif
260         }
261
262         gr_unlock();
263         
264 }
265