]> icculus.org git repositories - taylor/freespace2.git/blob - src/graphics/tmapper.cpp
Initial revision
[taylor/freespace2.git] / src / graphics / tmapper.cpp
1 /*
2  * $Logfile: /Freespace2/code/Graphics/Tmapper.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Routines to draw a texture map.
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:09  root
11  * Initial revision
12  *
13  * 
14  * 3     12/06/98 3:08p Dave
15  * Fixed grx_tmapper to handle pixel fog flag. First run fog support for
16  * D3D.
17  * 
18  * 2     10/07/98 10:53a Dave
19  * Initial checkin.
20  * 
21  * 1     10/07/98 10:49a Dave
22  * 
23  * 78    5/13/98 2:53p John
24  * Made subspace effect work under software.  Had to add new inner loop to
25  * tmapper.  Added glows to end of subspace effect.  Made subspace effect
26  * levels use gamepalette-subspace palette.
27  * 
28  * 77    4/10/98 5:20p John
29  * Changed RGB in lighting structure to be ubytes.  Removed old
30  * not-necessary 24 bpp software stuff.
31  * 
32  * 76    4/09/98 7:58p John
33  * Cleaned up tmapper code a bit.   Put NDEBUG around some ndebug stuff.
34  * Took out XPARENT flag settings in all the alpha-blended texture stuff.
35  * 
36  * 75    4/09/98 7:16p John
37  * Fixed bug causing software to not render
38  * 
39  * 74    3/27/98 8:34p Mike
40  * Minor optimization.
41  * 
42  * 73    3/25/98 8:08p John
43  * Restructured software rendering into two modules; One for windowed
44  * debug mode and one for DirectX fullscreen.   
45  * 
46  * 72    3/23/98 5:00p John
47  * Improved missile trails.  Made smooth alpha under hardware.  Made end
48  * taper.  Made trail touch weapon.
49  * 
50  * 71    3/22/98 2:33p John
51  * Took out fx_v/v_right.  Made fx_u etc get calculated in tmapper.
52  * 
53  * 70    3/10/98 4:19p John
54  * Cleaned up graphics lib.  Took out most unused gr functions.   Made D3D
55  * & Glide have popups and print screen.  Took out all >8bpp software
56  * support.  Made Fred zbuffer.  Made zbuffer allocate dynamically to
57  * support Fred.  Made zbuffering key off of functions rather than one
58  * global variable.
59  * 
60  * 69    2/10/98 5:34p John
61  * 
62  * 68    2/05/98 9:21p John
63  * Some new Direct3D code.   Added code to monitor a ton of stuff in the
64  * game.
65  * 
66  * 67    1/29/98 8:18a John
67  * Put in some commented out hooks for RGB lighting
68  * 
69  * 66    1/28/98 1:27p John
70  * Really fixed bug with exception on mov al, [esi]
71  * 
72  * 65    1/28/98 1:22p John
73  * Fixed bug with unitialized dwdx_wide.
74  * 
75  * 64    1/27/98 5:13p John
76  * Moved all float to int conversions out of inner loops and into outer.
77  * Made outer loop use FISTP instead of ftol, saved about 10%.
78  * 
79  * 63    1/23/98 5:08p John
80  * Took L out of vertex structure used B (blue) instead.   Took all small
81  * fireballs out of fireball types and used particles instead.  Fixed some
82  * debris explosion things.  Restructured fireball code.   Restructured
83  * some lighting code.   Made dynamic lighting on by default. Made groups
84  * of lasers only cast one light.  Made fireballs not cast light.
85  * 
86  * 62    1/19/98 6:15p John
87  * Fixed all my Optimized Build compiler warnings
88  * 
89  * 61    12/15/97 11:32a John
90  * New Laser Code
91  * 
92  * 60    12/02/97 4:00p John
93  * Added first rev of thruster glow, along with variable levels of
94  * translucency, which retquired some restructing of palman.
95  * 
96  * 59    11/30/97 4:40p John
97  * made 32-bpp tiled tmapper call scanline
98  * 
99  * 58    11/30/97 3:57p John
100  * Made fixed 32-bpp translucency.  Made BmpMan always map translucent
101  * color into 255 even if you aren't supposed to remap and make it's
102  * palette black.
103  * 
104  * 57    11/30/97 12:18p John
105  * added more 24 & 32-bpp primitives
106  * 
107  * 56    11/29/97 2:06p John
108  * added mode 16-bpp support
109  * 
110  * 55    11/21/97 11:32a John
111  * Added nebulas.   Fixed some warpout bugs.
112  * 
113  * 54    11/14/97 12:30p John
114  * Fixed some DirectX bugs.  Moved the 8-16 xlat tables into Graphics
115  * libs.  Made 16-bpp DirectX modes know what bitmap format they're in.
116  * 
117  * 53    11/06/97 11:18a John
118  * added 16-bpp gouraud flat shader
119  * 
120  * 52    10/19/97 12:55p John
121  * new code to lock / unlock surfaces for smooth directx integration.
122  * 
123  * 51    10/16/97 10:55a John
124  * added tmapper to draw a monochrome alpha blended bitmap.
125  * 
126  * 50    10/14/97 8:08a John
127  * added a bunch more 16 bit support
128  * 
129  * 49    10/09/97 5:23p John
130  * Added support for more 16-bpp functions
131  * 
132  * 48    9/30/97 2:30p John
133  * test code for texture fading
134  * 
135  * 47    9/24/97 10:37a John
136  * made tmapper not trash uv values anymore.
137  * 
138  * 46    9/09/97 11:01a Sandeep
139  * fixed warning level 4 bugs
140  * 
141  * 45    7/11/97 11:54a John
142  * added rotated 3d bitmaps.
143  * 
144  * 44    6/18/97 5:02p John
145  * fixed bug with 32x32 and 16x16 tmapper
146  * 
147  * 43    6/12/97 5:04p John
148  * Initial rev of Glide support
149  * 
150  * 42    6/12/97 2:50a Lawrance
151  * bm_unlock() now passed bitmap number, not pointer
152  * 
153  * 41    6/02/97 11:45a John
154  * fixed bugs with 64x64 and 128x128 tmappers.
155  * 
156  * 40    6/01/97 3:41p John
157  * made non-tilable textures on tilable models bash uvs to 0-1.
158  * 
159  * 39    5/12/97 12:27p John
160  * Restructured Graphics Library to add support for multiple renderers.
161  * 
162  * 38    5/07/97 4:14p John
163  * Reenabled calls to gr_start/end_frame.
164  * 
165  * 37    4/21/97 10:06a John
166  * Got capital ships working again.
167  * 
168  * 36    4/17/97 6:06p John
169  * New code/data for v19 of BSPGEN with smoothing and zbuffer
170  * optimizations.
171  * 
172  * 35    4/08/97 5:18p John
173  * First rev of decent (dynamic, correct) lighting in FreeSpace.
174  * 
175  * 34    3/18/97 9:42a John
176  * 
177  * 33    3/15/97 2:44p John
178  * got scanline sorting method working.  Bummer it is slower than zbuffer!
179  * 
180  * 32    3/14/97 3:55p John
181  * Made tiled tmapper not always be zbuffered.
182  * 
183  * 31    3/13/97 10:32a John
184  * Added code for tiled 256x256 textures in certain models.
185  * 
186  * 30    3/12/97 2:51p John
187  * Added some test code for tmapper.  
188  * 
189  * 29    3/12/97 9:25a John
190  * fixed a bug with zbuffering.  Reenabled it by default.
191  * 
192  * 28    3/11/97 4:36p John
193  * added zbuffering to textest.  Made zbuffered tmapper a bit faster by
194  * rearranging some instructions.
195  * 
196  * 27    3/10/97 5:20p John
197  * Differentiated between Gouraud and Flat shading.  Since we only do flat
198  * shading as of now, we don't need to interpolate L in the outer loop.
199  * This should save a few percent.
200  * 
201  * 26    3/10/97 2:24p John
202  * added some commets about precompiled inner loop
203  * 
204  * 25    3/05/97 7:15p John
205  * took out the old z stop tmapper used for briefing. 
206  * 
207  * 24    1/20/97 4:17p John
208  * 
209  * 23    1/06/97 2:44p John
210  * Added in slow (but correct) zbuffering
211  * 
212  * 22    12/30/96 3:46p John
213  * 
214  * 21    12/23/96 10:56a John
215  * Totally restructured the POF stuff to support multiple 
216  * detail levels in one POF file.
217  *  
218  * 
219  * 20    12/10/96 10:37a John
220  * Restructured texture mapper to remove some overhead from each scanline
221  * setup.  This gave about a 30% improvement drawing trans01.pof, which is
222  * a really complex model.  In the process, I cleaned up the scanline
223  * functions and separated them into different modules for each pixel
224  * depth.   
225  * 
226  * 19    11/26/96 6:50p John
227  * Added some more hicolor primitives.  Made windowed mode run as current
228  * bpp, if bpp is 8,16,or 32.
229  * 
230  * 18    11/07/96 6:19p John
231  * Added a bunch of 16bpp primitives so the game sort of runs in 16bpp
232  * mode.
233  * 
234  * 17    11/07/96 3:08p John
235  * Inlined more Tmapper functions in preparation for cleaning up the Tmap1
236  * interface to the assembly.
237  * 
238  * 16    11/07/96 2:17p John
239  * Took out the OldTmapper stuff.
240  * 
241  * 15    11/07/96 12:04p John
242  * Sped up outer loop by 35% by inlining the incrementing of the variables
243  * for each scanline step and inlined the calculation for deltas at the
244  * start of each scanline.
245  * 
246  * 14    11/06/96 2:33p John
247  * Added more asserts for checking that non-tiled UV's are between 0 and
248  * 1.0.    Put code in the model_init code that checks for polys that have
249  * a vertex duplicated and throws them out.
250  * 
251  * 13    11/05/96 4:05p John
252  * Added roller.  Added code to draw a distant planet.  Made bm_load
253  * return -1 if invalid bitmap.
254  * 
255  * 12    10/31/96 7:20p John
256  * Added per,tiled tmapper.  Made models tile if they use 64x64 textures.
257  * 
258  * 11    10/26/96 1:40p John
259  * Added some now primitives to the 2d library and
260  * cleaned up some old ones.
261  *
262  * $NoKeywords: $
263  */
264
265 #include <math.h>
266 #include <limits.h>
267 #include <stdio.h>
268 #ifndef PLAT_UNIX
269 #include <conio.h>
270 #endif
271 #include <stdlib.h>
272
273 #include "2d.h"
274 #include "grinternal.h"
275 #include "3d.h"
276 #include "tmapper.h"
277 #include "bmpman.h"
278 #include "tmapscanline.h"
279 #include "key.h"
280 #include "floating.h"
281 #include "palman.h"
282
283 typedef void (* pscanline)();
284
285 pscanline tmap_scanline;
286
287 int Tmap_screen_flags = -1;
288 int Tmap_npolys=0;
289 int Tmap_nverts=0;
290 int Tmap_nscanlines=0;
291 int Tmap_npixels=0;
292 tmapper_data Tmap;
293 int Tmap_show_layers=0;
294
295 typedef struct tmap_scan_desc {
296         uint                    flags;
297         pscanline       scan_func;
298 } tmap_scan_desc;
299
300 // Convert from a 0-255 byte to a 0-1.0 float.
301 float Light_table[256];
302
303
304 //====================== 8-BPP SCANLINES ========================
305 tmap_scan_desc tmap_scanlines8[] = {
306         { 0, tmapscan_flat8 },
307         { TMAP_FLAG_TEXTURED, tmapscan_lnn8 },
308         { TMAP_FLAG_TEXTURED|TMAP_FLAG_XPARENT, tmapscan_lnt8 },
309         { TMAP_FLAG_TEXTURED|TMAP_FLAG_RAMP, tmapscan_lln8 },
310         { TMAP_FLAG_TEXTURED|TMAP_FLAG_RAMP|TMAP_FLAG_CORRECT, tmapscan_pln8 },
311         
312         { TMAP_FLAG_RAMP|TMAP_FLAG_GOURAUD, tmapscan_flat_gouraud },
313
314         { TMAP_FLAG_TEXTURED|TMAP_FLAG_RAMP|TMAP_FLAG_GOURAUD, tmapscan_lln8 },
315         { TMAP_FLAG_TEXTURED|TMAP_FLAG_RAMP|TMAP_FLAG_GOURAUD|TMAP_FLAG_CORRECT, tmapscan_pln8 },
316
317         { TMAP_FLAG_TEXTURED|TMAP_FLAG_RAMP|TMAP_FLAG_CORRECT|TMAP_FLAG_TILED, tmapscan_pln8_tiled },
318         { TMAP_FLAG_TEXTURED|TMAP_FLAG_RAMP|TMAP_FLAG_CORRECT|TMAP_FLAG_GOURAUD|TMAP_FLAG_TILED, tmapscan_pln8_tiled },
319
320         { TMAP_FLAG_RAMP|TMAP_FLAG_GOURAUD|TMAP_FLAG_NEBULA, tmapscan_nebula8 },
321
322 //      { TMAP_FLAG_TEXTURED|TMAP_FLAG_TILED, tmapscan_lnn8_tiled_256x256 },
323         // Totally non-general specific inner loop for subspace effect
324         { TMAP_FLAG_TEXTURED|TMAP_FLAG_CORRECT|TMAP_FLAG_TILED, tmapscan_pnn8_tiled_256x256_subspace },
325         
326
327         { 0, NULL },    // Dummy element to mark the end of fast scanlines.
328 };
329
330
331 pscanline tmap_scanline_table[TMAP_MAX_SCANLINES];
332
333
334 // -------------------------------------------------------------------------------------
335 // This sets up the tmapper at the start of a given frame, so everything
336 // can can be pre-calculated should be calculated in here.
337 // This just fills in the tmap_scanline_table for the
338 // appropriate scan lines.
339
340 void tmapper_setup()
341 {
342         int i;
343         tmap_scan_desc * func_table = NULL;
344
345         Tmap_screen_flags = gr_screen.mode;
346
347         // Some constants for the inner loop
348         Tmap.FixedScale = 65536.0f;
349         Tmap.FixedScale8 =      2048.0f;        //8192.0f;      // 2^16 / 8
350         Tmap.One = 1.0f;
351
352         // Set tmap_scanline to not call a function
353         for (i=0; i<TMAP_MAX_SCANLINES; i++ )   {
354                 tmap_scanline_table[i] = NULL;
355         }
356
357         func_table = tmap_scanlines8;
358
359         while(func_table->scan_func != NULL)    {
360                 tmap_scanline_table[func_table->flags] = func_table->scan_func;
361                 func_table++;
362         }
363
364         for (i=0; i<256; i++ )  {
365                 Light_table[i] = i2fl(i)/255.0f;
366         }
367
368
369 }
370
371 // Sets up flat-shaded lighting
372 void tmapper_set_light(vertex *v, uint flags)
373 {
374         if ( flags & TMAP_FLAG_GOURAUD ) return;
375
376         if ( (flags & (TMAP_FLAG_RAMP|TMAP_FLAG_RGB))==(TMAP_FLAG_RAMP|TMAP_FLAG_RGB))  {
377                 Int3();         // You're doing RGB and RAMP lighting!!!
378         }
379
380         if ( flags & TMAP_FLAG_RAMP )   {
381                 Tmap.l.b = Tmap.r.b = i2fl(v->b)/255.0f;
382                 Tmap.deltas.b = 0.0f;
383         }
384 }
385
386 void tmapper_show_layers()
387 {
388         int i;
389         ubyte * ptr = (ubyte *)Tmap.dest_row_data;
390
391         for (i=0; i<Tmap.loop_count; i++, ptr++ )       {
392                 *ptr = (unsigned char)(*ptr + 1);
393         }
394
395 }
396
397 /*
398 void tmap_scan_generic()
399 {
400         int ui,vi,i;
401         ubyte * dptr,c;
402         float l, dl;
403         float u, v, w, du, dv, dw;
404         
405         dptr = (ubyte *)Tmap.dest_row_data;
406
407         Tmap.fx_w = fl2i(Tmap.l.sw * GR_Z_RANGE)+gr_zoffset;
408         Tmap.fx_dwdx = fl2i(Tmap.deltas.sw * GR_Z_RANGE);
409
410         l = Tmap.l.b;
411         dl = Tmap.deltas.b;
412
413         u = Tmap.l.u;
414         v = Tmap.l.v;
415         w = Tmap.l.sw;
416         du = Tmap.deltas.u;
417         dv = Tmap.deltas.v;
418         dw = Tmap.deltas.sw;
419         
420         for (i=0; i<Tmap.loop_count; i++ )      {
421                 int tmp = (uint)dptr-Tmap.pScreenBits;
422                 if ( Tmap.fx_w > (int)gr_zbuffer[tmp] ) {
423                         gr_zbuffer[tmp] = Tmap.fx_w;
424
425                         ui = fl2i( u / w ) % Tmap.bp->w;
426                         vi = fl2i( v / w ) % Tmap.bp->h;
427
428                         c = Tmap.pixptr[vi*Tmap.bp->w+ui];
429                         *dptr = gr_fade_table[fl2i(l*31)*256+c];
430
431                 }
432                 Tmap.fx_w += Tmap.fx_dwdx;
433                 l+=dl;
434                 u+=du;
435                 v+=dv;
436                 w+=dw;
437                 dptr++;
438         }
439 }
440 */
441
442
443 // Same as ftol except that it might round up or down, 
444 // unlike C's ftol, which must always round down.  
445 // But, in the tmapper, we don't care, since this is
446 // just for Z and L.
447 inline int tmap_ftol(float f)
448 {
449         int x;
450 #ifdef PLAT_UNIX
451         STUB_FUNCTION;
452 #else
453         _asm fld f
454         _asm fistp x
455 #endif
456         return x; 
457 }
458
459 /*
460 #define tmap_ftol(f) ((int)(f))
461
462 __ftol:
463 004569c0   push      ebp
464 004569c1   mov       ebp,esp
465 004569c3   add       esp,fffffff4
466 004569c6   wait
467 004569c7   fnstcw    [ebp-02]
468 004569ca   wait
469 004569cb   mov       ax,word ptr [ebp-02]
470 004569cf   or        ah,0c
471 004569d2   mov       word ptr [ebp-04],ax
472 004569d6   fldcw     [ebp-04]
473 004569d9   fistp     qword ptr [ebp-0c]
474 004569dc   fldcw     [ebp-02]
475 004569df   mov       eax,dword ptr [ebp-0c]
476 004569e2   mov       edx,dword ptr [ebp-08]
477 004569e5   leave
478 004569e6   ret
479 */
480
481 #ifdef RGB_LIGHTING
482
483 extern ubyte gr_palette[768];
484
485 uint last_code = 0xf;
486 void change_fade_table(uint code)
487 {
488         int i,l;
489
490         if ( last_code == code ) return;
491         last_code = code;
492         
493         int r1=0, g1=0, b1=0;
494
495         for (i=0; i<256; i++ )  {
496                 int r, g, b;
497                 int ur, ug, ub;
498                 r = gr_palette[i*3+0];
499                 g = gr_palette[i*3+1];
500                 b = gr_palette[i*3+2];
501
502                 if ( (r == 255) && (g == 255) && (b == 255) )   {
503                         // Make pure white not fade
504                         for (l=0; l<32; l++ )   {
505                                 gr_fade_table[((l+1)*256)+i] = (unsigned char)i;
506                         }
507                 } else {
508                         for (l=24; l<32; l++ )  {
509
510                                 int x,y;
511                                 int gi, gr, gg, gb;
512         
513                                 gi = (r+g+b)/3;
514
515                                 //gr = r*2;
516                                 //gg = g*2;
517                                 //gb = b*2;
518
519                                 if ( code & 4 ) gr = gi*2; else gr = r;
520                                 if ( code & 2 ) gg = gi*2; else gg = g;
521                                 if ( code & 1 ) gb = gi*2; else gb = b;
522
523 //                              gr = r1;
524 //                              gg = g1;
525 //                              gb = b1;        //gi*2;
526                 
527                                 x = l-24;                       // x goes from 0 to 7
528                                 y = 31-l;                       // y goes from 7 to 0
529
530                                 ur = ((gr*x)+(r*y))/7; if ( ur > 255 ) ur = 255;
531                                 ug = ((gg*x)+(g*y))/7; if ( ug > 255 ) ug = 255;
532                                 ub = ((gb*x)+(b*y))/7; if ( ub > 255 ) ub = 255;
533
534                                 gr_fade_table[((l+1)*256)+i] = (unsigned char)palette_find( ur, ug, ub );
535
536                         }
537                 }
538                 gr_fade_table[ (0*256)+i ] = gr_fade_table[ (1*256)+i ];
539                 gr_fade_table[ (33*256)+i ] = gr_fade_table[ (32*256)+i ];
540         }
541
542         // Mirror the fade table
543         for (i=0; i<34; i++ )   {
544                 for ( l = 0; l < 256; l++ )     {
545                         gr_fade_table[ ((67-i)*256)+l ] = gr_fade_table[ (i*256)+l ];
546                 }
547         }
548
549 }
550 #endif
551
552
553 void grx_tmapper( int nverts, vertex **verts, uint flags )      
554 {
555         int i, y, li, ri, ly, ry, top, rem;
556         float ymin;
557         int next_break;
558         float ulist[TMAP_MAX_VERTS], vlist[TMAP_MAX_VERTS], llist[TMAP_MAX_VERTS];
559
560         flags &= (~TMAP_FLAG_ALPHA);
561         flags &= (~TMAP_FLAG_NONDARKENING);
562         flags &= (~TMAP_FLAG_PIXEL_FOG);
563
564         // Check for invalid flags
565 #ifndef NDEBUG
566         if ( (flags & (TMAP_FLAG_RAMP|TMAP_FLAG_RGB))==(TMAP_FLAG_RAMP|TMAP_FLAG_RGB))  {
567                 Int3();         // You're doing RGB and RAMP lighting!!!
568         }
569
570         if ( flags & TMAP_FLAG_RGB )    {
571                 Int3();         // RGB not supported!
572         }
573
574         if ( (flags & TMAP_FLAG_GOURAUD) && (!(flags & TMAP_FLAG_RAMP)) )       {
575                 Int3();         // Ramp mode required for gouraud!
576         }
577
578         if ( gr_screen.bits_per_pixel != 8 )    {
579                 Int3();                 // Only 8-bpp tmapper supported
580         }
581
582         Tmap_npolys++;
583         Tmap_nverts += nverts;
584
585         Assert(nverts <= TMAP_MAX_VERTS );
586
587 #endif
588         
589         if ( flags & (TMAP_FLAG_RAMP|TMAP_FLAG_GOURAUD) )       {
590                 for (i=0; i<nverts; i++ )       {
591                         llist[i] = Light_table[verts[i]->b];
592                 }
593         }
594
595         if ( Tmap_screen_flags != gr_screen.mode )      {
596                 tmapper_setup();
597         }
598
599         tmap_scanline = tmap_scanline_table[flags];
600 //      tmap_scanline = tmap_scan_generic;
601
602 #ifndef NDEBUG
603         Assert( tmap_scanline != NULL );
604
605         if (Tmap_show_layers)
606                 tmap_scanline = tmapper_show_layers;
607 #endif
608
609         if ( tmap_scanline == NULL ) return;
610
611         Tmap.FadeLookup = (uint)palette_get_fade_table();
612         Tmap.BlendLookup = (uint)palette_get_blend_table(gr_screen.current_alpha);
613
614         if ( flags & TMAP_FLAG_TEXTURED )       {
615
616                 Tmap.bp  = bm_lock( gr_screen.current_bitmap, 8, 0 );
617
618                 int was_tiled = 0, can_tile = 0;
619                 if ( flags & TMAP_FLAG_TILED )  {
620                         if ( (Tmap.bp->w==16) && (Tmap.bp->h==16) ) can_tile = 1;
621                         if ( (Tmap.bp->w==32) && (Tmap.bp->h==32) ) can_tile = 1;
622                         if ( (Tmap.bp->w==64) && (Tmap.bp->h==64) ) can_tile = 1;
623                         if ( (Tmap.bp->w==128) && (Tmap.bp->h==128) ) can_tile = 1;
624                         if ( (Tmap.bp->w==256) && (Tmap.bp->h==256) ) can_tile = 1;
625                 
626                         if ( !can_tile )        {
627                                 was_tiled = 1;
628                                 flags &= (~TMAP_FLAG_TILED);
629                         }
630                 }
631
632                 float max_u = i2fl(Tmap.bp->w) - 0.5f;
633                 float max_v = i2fl(Tmap.bp->h) - 0.5f;
634
635                 for (i=0; i<nverts; i++ )       {
636                         ulist[i] = verts[i]->u * Tmap.bp->w;
637                         vlist[i] = verts[i]->v * Tmap.bp->h;
638
639                         if ( !(flags & TMAP_FLAG_TILED) )       {
640                                 if ( ulist[i] < 1.0f ) ulist[i] = 1.0f;
641                                 if ( vlist[i] < 1.0f ) vlist[i] = 1.0f;
642                                 if ( ulist[i] > max_u ) ulist[i] = max_u;
643                                 if ( vlist[i] > max_v ) vlist[i] = max_v;
644                         }
645         
646                         // Multiply all u,v's by sw for perspective correction
647                         if ( flags & TMAP_FLAG_CORRECT )        {
648                                 ulist[i] *= verts[i]->sw;
649                                 vlist[i] *= verts[i]->sw;
650                         }
651                 }
652
653                 Tmap.pixptr = (unsigned char *)Tmap.bp->data;
654                 Tmap.src_offset = Tmap.bp->rowsize;
655         }
656         
657         // Find the topmost vertex
658         //top = -1;                     // Initialize to dummy value to avoid compiler warning
659         //ymin = 0.0f;          // Initialize to dummy value to avoid compiler warning
660         //      Instead of initializing to avoid compiler warnings, set to first value outside loop and remove (i==0)
661         //      comparison, which otherwise happens nverts times.  MK, 3/20/98 (was tracing code figuring out my shield effect bug...)
662         ymin = verts[0]->sy;
663         top = 0;
664         for (i=1; i<nverts; i++ ) {
665                 if (verts[i]->sy < ymin) {
666                         ymin = verts[i]->sy;
667                         top = i;
668                 }
669         }       
670
671         li = ri = top;
672         rem = nverts;
673         y = fl_round_2048(ymin);                //(int)floor(ymin + 0.5);
674         ly = ry = y - 1;
675
676         gr_lock();
677         Tmap.pScreenBits = (uint)gr_screen.offscreen_buffer_base;
678
679         while( rem > 0 )        {
680                 while ( ly<=y && rem>0 )        {       // Advance left edge?
681                         float dy, frac, recip;
682                         rem--;
683                         i = li-1;       
684                         if ( i<0 ) i = nverts-1;
685                         ly = fl_round_2048(verts[i]->sy);       //(int)floor(verts[i]->sy+0.5);
686
687                         dy = verts[i]->sy - verts[li]->sy;
688                         if ( dy == 0.0f ) dy = 1.0f;
689
690                         frac = y + 0.5f - verts[li]->sy;
691                         recip = 1.0f / dy;
692
693                         Tmap.dl.sx = (verts[i]->sx - verts[li]->sx)*recip; 
694                         Tmap.l.sx = verts[li]->sx + Tmap.dl.sx*frac;
695
696                         if ( flags & TMAP_FLAG_TEXTURED )       {
697                                 Tmap.dl.u = (ulist[i] - ulist[li])*recip; 
698                                 Tmap.l.u = ulist[li] + Tmap.dl.u*frac;
699                                 Tmap.dl.v = (vlist[i] - vlist[li])*recip; 
700                                 Tmap.l.v = vlist[li] + Tmap.dl.v*frac;
701                         }
702
703                         if ( (flags & TMAP_FLAG_CORRECT) || gr_zbuffering  )    {
704                                 Tmap.dl.sw = (verts[i]->sw - verts[li]->sw)*recip;
705                                 Tmap.l.sw = verts[li]->sw + Tmap.dl.sw*frac;
706                         }
707
708                         if ( flags & TMAP_FLAG_GOURAUD )        {
709                                 if ( flags & TMAP_FLAG_RAMP )   {
710                                         Tmap.dl.b = (llist[i] - llist[li])*recip;
711                                         Tmap.l.b = llist[li]  + Tmap.dl.b*frac;
712                                 }
713                         }
714
715                         li = i;
716                 }
717                 while ( ry<=y && rem>0 )        {       // Advance right edge?
718                         float dy, frac, recip;
719                         rem--;
720                         i = ri+1;
721                         if ( i>=nverts ) i = 0;
722                         ry = fl_round_2048(verts[i]->sy);       //(int)floor(verts[i]->sy+0.5);
723
724                         dy = verts[i]->sy - verts[ri]->sy;
725                         if ( dy == 0.0f ) dy = 1.0f;
726
727                         frac = y + 0.5f - verts[ri]->sy;
728                         recip = 1.0f / dy;
729
730                         Tmap.dr.sx = (verts[i]->sx - verts[ri]->sx)*recip; 
731                         Tmap.r.sx = verts[ri]->sx + Tmap.dr.sx*frac;
732
733                         if ( flags & TMAP_FLAG_TEXTURED )       {
734                                 Tmap.dr.u = (ulist[i] - ulist[ri])*recip;
735                                 Tmap.r.u = ulist[ri] + Tmap.dr.u*frac;
736                                 Tmap.dr.v = (vlist[i] - vlist[ri])*recip;
737                                 Tmap.r.v = vlist[ri] + Tmap.dr.v*frac;
738                         }
739
740                         if ( (flags & TMAP_FLAG_CORRECT) || gr_zbuffering  )    {
741                                 Tmap.dr.sw = (verts[i]->sw - verts[ri]->sw)*recip;
742                                 Tmap.r.sw = verts[ri]->sw + Tmap.dr.sw*frac;
743                         }
744
745                         if ( flags & TMAP_FLAG_GOURAUD )        {
746                                 if ( flags & TMAP_FLAG_RAMP )   {
747                                         Tmap.dr.b = (llist[i] - llist[ri])*recip;
748                                         Tmap.r.b = llist[ri] + Tmap.dr.b*frac;
749                                 }
750                         }
751
752                         ri = i;
753                 }
754
755                 if ( ly < ry )  
756                         next_break = ly;
757                 else
758                         next_break = ry;
759
760                 for ( ; y<next_break; y++ )     {
761                         if ( (y >= gr_screen.clip_top) && ( y<=gr_screen.clip_bottom) ) {
762                                 int lx, rx;
763
764
765                                 lx = fl_round_2048(Tmap.l.sx);
766                                 if ( lx < gr_screen.clip_left ) {       
767                                         Tmap.clipped_left = i2fl(gr_screen.clip_left) - Tmap.l.sx;
768                                         lx = gr_screen.clip_left;
769                                 } else {
770                                         Tmap.clipped_left = 0.0f;
771                                 }
772                                 rx = fl_round_2048(Tmap.r.sx-1.0f);
773         
774                                 if ( rx > gr_screen.clip_right ) rx = gr_screen.clip_right;
775                                 if ( lx <= rx ) {
776                                         float dx, recip;        //frac;
777
778                                         dx = Tmap.r.sx - Tmap.l.sx;
779                                         if ( dx == 0.0f ) dx = 1.0f;
780
781                                         //frac = lx + 0.5f - Tmap.l.sx;
782                                         recip = 1.0f / dx;
783
784                                         Tmap.y = y;
785                                         Tmap.rx = rx;
786                                         Tmap.lx = lx;
787                                         Tmap.loop_count = rx - lx + 1;
788                                         #ifndef NDEBUG
789                                                 Tmap_npixels += Tmap.loop_count;
790                                                 Tmap_nscanlines++;
791                                         #endif
792                                         
793                                         if ( (flags & TMAP_FLAG_CORRECT) || gr_zbuffering  )    {
794                                                 Tmap.deltas.sw = (Tmap.r.sw - Tmap.l.sw)*recip;
795                                                 Tmap.fl_dwdx_wide = Tmap.deltas.sw*32.0f;
796                                         }
797         
798                                         if ( flags & TMAP_FLAG_TEXTURED )       {
799                                                 Tmap.deltas.u = (Tmap.r.u - Tmap.l.u)*recip;
800                                                 Tmap.deltas.v = (Tmap.r.v - Tmap.l.v)*recip;
801
802                                                 if ( flags & TMAP_FLAG_CORRECT )        {
803                                                         Tmap.fl_dudx_wide = Tmap.deltas.u*32.0f;
804                                                         Tmap.fl_dvdx_wide = Tmap.deltas.v*32.0f;
805                                                 } else {
806                                                         Tmap.fx_u = tmap_ftol((Tmap.l.u+Tmap.clipped_left*Tmap.deltas.u)*65536.0f);
807                                                         Tmap.fx_v = tmap_ftol((Tmap.l.v+Tmap.clipped_left*Tmap.deltas.v)*65536.0f);
808                                                         Tmap.fx_du_dx = tmap_ftol(Tmap.deltas.u*65536.0f);
809                                                         Tmap.fx_dv_dx = tmap_ftol(Tmap.deltas.v*65536.0f);
810                                                 }
811                                         }
812
813                                         if ( flags & TMAP_FLAG_GOURAUD )        {
814                                                 if ( flags & TMAP_FLAG_RAMP )   {
815                                                         Tmap.deltas.b = (Tmap.r.b - Tmap.l.b)*recip;
816
817                                                         Tmap.fx_l = tmap_ftol(Tmap.l.b*32.0f*65536.0f); 
818                                                         Tmap.fx_l_right = tmap_ftol(Tmap.r.b*32.0f*65536.0f); 
819                                                         Tmap.fx_dl_dx = tmap_ftol(Tmap.deltas.b*32.0f*65536.0f);
820
821                                                         if ( Tmap.fx_dl_dx < 0 )        {
822                                                                 Tmap.fx_dl_dx = -Tmap.fx_dl_dx;
823                                                                 Tmap.fx_l = (67*F1_0)-Tmap.fx_l;
824                                                                 Tmap.fx_l_right = (67*F1_0)-Tmap.fx_l_right;
825                                                 //              Assert( Tmap.fx_l > 31*F1_0 );
826                                                 //              Assert( Tmap.fx_l < 66*F1_0 );
827                                                 //              Assert( Tmap.fx_dl_dx >= 0 );
828                                                 //              Assert( Tmap.fx_dl_dx < 31*F1_0 );
829                                                         }
830                                                 }
831                                         }
832
833                                         if ( gr_zbuffering )    {
834                                                 Tmap.fx_w = tmap_ftol(Tmap.l.sw * GR_Z_RANGE)+gr_zoffset;
835                                                 Tmap.fx_dwdx = tmap_ftol(Tmap.deltas.sw * GR_Z_RANGE);
836                                         }
837
838                                         Tmap.dest_row_data = GR_SCREEN_PTR_SIZE(gr_screen.bytes_per_pixel,Tmap.lx,Tmap.y);
839         
840                                         (*tmap_scanline)();
841
842                                 } 
843
844                         }
845
846                         Tmap.l.sx += Tmap.dl.sx;
847                         Tmap.r.sx += Tmap.dr.sx;
848
849                         if ( flags & TMAP_FLAG_TEXTURED )       {
850                                 Tmap.l.u += Tmap.dl.u;
851                                 Tmap.l.v += Tmap.dl.v;
852
853                                 Tmap.r.u += Tmap.dr.u;
854                                 Tmap.r.v += Tmap.dr.v;
855                         }
856
857                         if ( (flags & TMAP_FLAG_CORRECT) || gr_zbuffering  )    {
858                                 Tmap.l.sw += Tmap.dl.sw;
859                                 Tmap.r.sw += Tmap.dr.sw;
860                         }
861
862                         if ( flags & TMAP_FLAG_GOURAUD )        {
863                                 if ( flags & TMAP_FLAG_RAMP )   {
864                                         Tmap.l.b += Tmap.dl.b;
865                                         Tmap.r.b += Tmap.dr.b;
866                                 }
867                         }
868                 }
869         }
870
871         gr_unlock();
872
873         if ( flags & TMAP_FLAG_TEXTURED )       {
874                 bm_unlock(gr_screen.current_bitmap);
875         }
876
877
878 }
879
880
881