2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
14 * $Source: /cvs/cvsroot/d2x/texmap/ntmap.c,v $
17 * $Date: 2001-01-31 14:04:46 $
19 * Start of conversion to new texture mapper.
21 * $Log: not supported by cvs2svn $
22 * Revision 1.1.1.1 2001/01/19 03:30:16 bradleyb
25 * Revision 1.4 1999/10/18 00:31:55 donut
26 * allow FP_TMAP to be used without NO_ASM
28 * Revision 1.3 1999/08/05 22:53:41 sekmu
30 * D3D patch(es) from ADB
32 * Revision 1.2 1999/07/07 21:31:09 donut
33 * removed unused vars from ntmap_scanline_lighted and slightly optomized it
35 * Revision 1.1.1.1 1999/06/14 22:14:06 donut
36 * Import of d1x 1.37 source.
38 * Revision 1.52 1995/03/14 15:13:06 john
39 * Increased MAX_Y_Pointers to 480.
41 * Revision 1.51 1995/02/23 14:25:09 john
44 * Revision 1.50 1995/02/20 18:22:58 john
45 * Put all the externs in the assembly modules into tmap_inc.asm.
46 * Also, moved all the C versions of the inner loops into a new module,
49 * Revision 1.49 1995/02/20 17:09:11 john
50 * Added code so that you can build the tmapper with no assembly!
52 * Revision 1.48 1995/01/06 11:11:30 mike
53 * even when not in editor, have 400 lines in texture map scanline table.
55 * Revision 1.47 1994/12/15 16:43:25 matt
56 * Took out code only needed by editor
58 * Revision 1.46 1994/12/09 22:35:37 mike
59 * fix bug in before call to asm_tmap_scanline_per causing write of pixel onto past right border onto left.
61 * Revision 1.45 1994/12/06 16:31:06 mike
62 * fix bug in asm_tmap_scanline_matt interface.
64 * Revision 1.44 1994/12/04 20:37:18 mike
65 * *** empty log message ***
67 * Revision 1.43 1994/12/02 23:30:04 mike
70 * Revision 1.42 1994/11/30 00:57:43 mike
73 * Revision 1.41 1994/11/28 13:34:27 mike
76 * Revision 1.40 1994/11/28 01:30:01 mike
79 * Revision 1.39 1994/11/28 01:28:59 mike
82 * Revision 1.38 1994/11/21 14:08:07 john
83 * Took out all multiple instead of divide code.
85 * Revision 1.37 1994/11/19 15:21:52 mike
86 * rip out unused code.
88 * Revision 1.36 1994/11/14 11:42:51 mike
91 * Revision 1.35 1994/11/12 16:41:36 mike
92 * *** empty log message ***
94 * Revision 1.34 1994/11/10 21:28:41 mike
95 * remove call to init_interface_vars_to_assembler.
97 * Revision 1.33 1994/11/10 11:08:59 mike
100 * Revision 1.32 1994/11/09 22:55:52 matt
101 * Added variable Current_seg_depth for detail level optimization
103 * Revision 1.31 1994/11/09 19:57:31 john
104 * Added texture rle caching.
106 * Revision 1.30 1994/11/09 19:54:48 mike
107 * Call flat shader if Tmap_flat_flag set.
109 * Revision 1.29 1994/11/02 21:33:31 john
110 * Added Burger Bill's optimization, ie.. 2 muls per 8 pixels.
112 * Revision 1.28 1994/11/02 11:32:16 john
113 * Added code for c callable inner loop and code to
114 * test dividing out z0.
116 * Revision 1.27 1994/10/28 20:54:32 matt
117 * Added error checking
119 * Revision 1.26 1994/10/25 11:20:20 mike
120 * fix bug in lighting overflow checking for one scanline tall linear texture maps.
122 * Revision 1.25 1994/08/03 15:40:33 mike
123 * Prevent divide overflows, decrease occurrence of precision-caused glitches.
125 * Revision 1.24 1994/07/27 09:31:16 mike
126 * Fix concave texture map problem, decrease occurrence of unimportant int 3.
128 * Revision 1.23 1994/06/17 12:23:31 mike
129 * Support non-lighted texture maps.
131 * Revision 1.22 1994/06/11 08:10:24 mike
132 * Fix mysterious hang bug, lighting value was out of range.
134 * Revision 1.21 1994/06/09 16:10:16 mike
135 * Change SC2000 from constant to variable.
140 static char rcsid[] = "$Id: ntmap.c,v 1.2 2001-01-31 14:04:46 bradleyb Exp $";
148 #define HEADLIGHT_LIGHTING 0
151 #define PERSPECTIVE 1
164 #include "scanline.h"
167 //#include "../main/textures.h"
170 #define EDITOR_TMAP 1 //if in, include extra stuff
173 #define F15_5 (F1_0*15 + F0_5)
175 // Temporary texture map, interface from Matt's 3d system to Mike's texture mapper.
178 grs_bitmap Texmap_ptrs[NUM_TMAPS];
179 grs_bitmap Texmap4_ptrs[NUM_TMAPS];
181 fix Range_max=0; // debug, kill me
183 int Interpolation_method=0; // 0 = choose best method
184 int Lighting_on=1; // initialize to no lighting
185 int Tmap_flat_flag = 0; // 1 = render texture maps as flat shaded polygons.
186 int Current_seg_depth; // HACK INTERFACE: how far away the current segment (& thus texture) is
187 int Max_perspective_depth;
188 int Max_linear_depth;
191 extern int Window_clip_left, Window_clip_bot, Window_clip_right, Window_clip_top;
193 // These variables are the interface to assembler. They get set for each texture map, which is a real waste of time.
194 // They should be set only when they change, which is generally when the window bounds change. And, even still, it's
195 // a pretty bad interface.
196 int bytes_per_row=-1;
197 unsigned char *write_buffer;
206 #define MAX_Y_POINTERS 600
208 #define MAX_Y_POINTERS 600
210 int y_pointers[MAX_Y_POINTERS];
212 fix fix_recip[FIX_RECIP_TABLE_SIZE];
214 int Lighting_enabled;
215 int Fix_recip_table_computed=0;
217 fix fx_l, fx_u, fx_v, fx_z, fx_du_dx, fx_dv_dx, fx_dz_dx, fx_dl_dx;
218 int fx_xleft, fx_xright, fx_y;
219 unsigned char * pixptr;
221 int Transparency_on = 0;
222 int dither_intensity_lighting = 0;
224 ubyte * tmap_flat_cthru_table;
225 ubyte tmap_flat_color;
226 ubyte tmap_flat_shade_value;
230 // -------------------------------------------------------------------------------------
231 void init_fix_recip_table(void)
237 for (i=1; i<FIX_RECIP_TABLE_SIZE; i++)
238 fix_recip[i] = F1_0/i;
240 Fix_recip_table_computed = 1;
243 // -------------------------------------------------------------------------------------
244 // Initialize interface variables to assembler.
245 // These things used to be constants. This routine is now (10/6/93) getting called for
246 // every texture map. It should get called whenever the window changes, or, preferably,
247 // not at all. I'm pretty sure these variables are only being used for range checking.
248 void init_interface_vars_to_assembler(void)
252 bp = &grd_curcanv->cv_bitmap;
255 Assert(bp->bm_data!=NULL);
256 Assert(bp->bm_h <= MAX_Y_POINTERS);
258 // If bytes_per_row has changed, create new table of pointers.
259 if (bytes_per_row != (int) bp->bm_rowsize) {
262 bytes_per_row = (int) bp->bm_rowsize;
265 for (i=0; i<MAX_Y_POINTERS; i++) {
266 y_pointers[i] = y_val;
267 y_val += bytes_per_row;
271 write_buffer = (unsigned char *) bp->bm_data;
274 window_right = (int) bp->bm_w-1;
276 window_bottom = (int) bp->bm_h-1;
278 Window_clip_left = window_left;
279 Window_clip_right = window_right;
280 Window_clip_top = window_top;
281 Window_clip_bot = window_bottom;
283 window_width = bp->bm_w;
284 window_height = bp->bm_h;
286 if (!Fix_recip_table_computed)
287 init_fix_recip_table();
290 // -------------------------------------------------------------------------------------
292 extern g3ds_tmap Tmap1;
294 // -------------------------------------------------------------------------------------
295 // Returns number preceding val modulo modulus.
298 int prevmod(int val,int modulus)
304 // return (val + modulus - 1) % modulus;
308 // Returns number succeeding val modulo modulus.
311 int succmod(int val,int modulus)
318 // return (val + 1) % modulus;
321 // -------------------------------------------------------------------------------------
322 // Select topmost vertex (minimum y coordinate) and bottommost (maximum y coordinate) in
323 // texture map. If either is part of a horizontal edge, then select leftmost vertex for
324 // top, rightmost vertex for bottom.
325 // Important: Vertex is selected with integer precision. So, if there are vertices at
326 // (0.0,0.7) and (0.5,0.3), the first vertex is selected, because they y coordinates are
327 // considered the same, so the smaller x is favored.
329 // nv number of vertices
330 // v3d pointer to 3d vertices containing u,v,x2d,y2d coordinates
334 // -------------------------------------------------------------------------------------
335 void compute_y_bounds(g3ds_tmap *t, int *vlt, int *vlb, int *vrt, int *vrb,int *bottom_y_ind)
343 // Scan all vertices, set min_y_ind to vertex with smallest y coordinate.
344 min_y = f2i(t->verts[0].y2d);
347 min_x = f2i(t->verts[0].x2d);
350 for (i=1; i<t->nv; i++) {
351 if (f2i(t->verts[i].y2d) < min_y) {
352 min_y = f2i(t->verts[i].y2d);
354 min_x = f2i(t->verts[i].x2d);
355 } else if (f2i(t->verts[i].y2d) == min_y) {
356 if (f2i(t->verts[i].x2d) < min_x) {
358 min_x = f2i(t->verts[i].x2d);
361 if (f2i(t->verts[i].y2d) > max_y) {
362 max_y = f2i(t->verts[i].y2d);
367 //--removed mk, 11/27/94-- // Check for a non-upright-hourglass polygon and fix, if necessary, by bashing a y coordinate.
368 //--removed mk, 11/27/94-- // min_y_ind = index of minimum y coordinate, *bottom_y_ind = index of maximum y coordinate
369 //--removed mk, 11/27/94--{
370 //--removed mk, 11/27/94-- int max_temp, min_temp;
371 //--removed mk, 11/27/94--
372 //--removed mk, 11/27/94-- max_temp = *bottom_y_ind;
373 //--removed mk, 11/27/94-- if (*bottom_y_ind < min_y_ind)
374 //--removed mk, 11/27/94-- max_temp += t->nv;
375 //--removed mk, 11/27/94--
376 //--removed mk, 11/27/94-- for (i=min_y_ind; i<max_temp; i++) {
377 //--removed mk, 11/27/94-- if (f2i(t->verts[i%t->nv].y2d) > f2i(t->verts[(i+1)%t->nv].y2d)) {
378 //--removed mk, 11/27/94-- Int3();
379 //--removed mk, 11/27/94-- t->verts[(i+1)%t->nv].y2d = t->verts[i%t->nv].y2d;
380 //--removed mk, 11/27/94-- }
381 //--removed mk, 11/27/94-- }
382 //--removed mk, 11/27/94--
383 //--removed mk, 11/27/94-- min_temp = min_y_ind;
384 //--removed mk, 11/27/94-- if (min_y_ind < *bottom_y_ind)
385 //--removed mk, 11/27/94-- min_temp += t->nv;
386 //--removed mk, 11/27/94--
387 //--removed mk, 11/27/94-- for (i=*bottom_y_ind; i<min_temp; i++) {
388 //--removed mk, 11/27/94-- if (f2i(t->verts[i%t->nv].y2d) < f2i(t->verts[(i+1)%t->nv].y2d)) {
389 //--removed mk, 11/27/94-- Int3();
390 //--removed mk, 11/27/94-- t->verts[(i+1)%t->nv].y2d = t->verts[i%t->nv].y2d;
391 //--removed mk, 11/27/94-- }
392 //--removed mk, 11/27/94-- }
393 //--removed mk, 11/27/94--}
395 // Set "vertex left top", etc. based on vertex with topmost y coordinate
398 *vlb = prevmod(*vlt,t->nv);
399 *vrb = succmod(*vrt,t->nv);
401 // If right edge is horizontal, then advance along polygon bound until it no longer is or until all
402 // vertices have been examined.
403 // (Left edge cannot be horizontal, because *vlt is set to leftmost point with highest y coordinate.)
407 while (f2i(t->verts[*vrt].y2d) == f2i(t->verts[*vrb].y2d)) {
408 if (succmod(*vrt,t->nv) == original_vrt) {
411 *vrt = succmod(*vrt,t->nv);
412 *vrb = succmod(*vrt,t->nv);
416 // -------------------------------------------------------------------------------------
417 // Returns dx/dy given two vertices.
418 // If dy == 0, returns 0.0
419 // -------------------------------------------------------------------------------------
420 //--fix compute_dx_dy_lin(g3ds_tmap *t, int top_vertex,int bottom_vertex)
424 //-- // compute delta x with respect to y for any edge
425 //-- dy = f2i(t->verts[bottom_vertex].y2d - t->verts[top_vertex].y2d) + 1;
427 //-- return (t->verts[bottom_vertex].x2d - t->verts[top_vertex].x2d) / dy;
433 fix compute_du_dy_lin(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
435 return fixmul(t->verts[bottom_vertex].u - t->verts[top_vertex].u, recip_dy);
439 fix compute_dv_dy_lin(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
441 return fixmul(t->verts[bottom_vertex].v - t->verts[top_vertex].v, recip_dy);
444 fix compute_dl_dy_lin(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
446 return fixmul(t->verts[bottom_vertex].l - t->verts[top_vertex].l, recip_dy);
450 fix compute_dx_dy(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
452 return fixmul(t->verts[bottom_vertex].x2d - t->verts[top_vertex].x2d, recip_dy);
455 fix compute_du_dy(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
457 return fixmul(fixmul(t->verts[bottom_vertex].u,t->verts[bottom_vertex].z) - fixmul(t->verts[top_vertex].u,t->verts[top_vertex].z), recip_dy);
461 fix compute_dv_dy(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
463 return fixmul(fixmul(t->verts[bottom_vertex].v,t->verts[bottom_vertex].z) - fixmul(t->verts[top_vertex].v,t->verts[top_vertex].z), recip_dy);
467 fix compute_dz_dy(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
469 return fixmul(t->verts[bottom_vertex].z - t->verts[top_vertex].z, recip_dy);
472 int Skip_short_flag=0;
474 // -------------------------------------------------------------------------------------
475 // Texture map current scanline in perspective.
476 // -------------------------------------------------------------------------------------
477 void ntmap_scanline_lighted(grs_bitmap *srcb, int y, fix xleft, fix xright, fix uleft, fix uright, fix vleft, fix vright, fix zleft, fix zright, fix lleft, fix lright)
481 fx_xright = f2i(xright);
482 //edited 06/27/99 Matt Mueller - moved these tests up from within the switch so as not to do a bunch of needless calculations when we are just gonna return anyway. Slight fps boost?
483 if (fx_xright < Window_clip_left)
485 fx_xleft = f2i(xleft);
486 if (fx_xleft > Window_clip_right)
490 dx = fx_xright - fx_xleft;
491 if ((dx < 0) || (xright < 0) || (xleft > xright)) // the (xleft > xright) term is not redundant with (dx < 0) because dx is computed using integers
494 // setup to call assembler scanline renderer
495 if (dx < FIX_RECIP_TABLE_SIZE)
496 recip_dx = fix_recip[dx];
504 fx_du_dx = fixmul(uright - uleft,recip_dx);
505 fx_dv_dx = fixmul(vright - vleft,recip_dx);
506 fx_dz_dx = fixmul(zright - zleft,recip_dx);
508 pixptr = srcb->bm_data;
510 switch (Lighting_enabled) {
512 //added 05/17/99 Matt Mueller - prevent writing before the buffer
513 if ((fx_y == 0) && (fx_xleft < 0))
516 if (fx_xright > Window_clip_right)
517 fx_xright = Window_clip_right;
519 #if (defined(NO_ASM) && !defined(ASM_PER)) || defined(FP_TMAP)
520 c_tmap_scanline_per_nolight();
522 asm_tmap_scanline_per();
528 if (lleft < 0) lleft = 0;
529 if (lright < 0) lright = 0;
530 if (lleft > (NUM_LIGHTING_LEVELS*F1_0-F1_0/2)) lleft = (NUM_LIGHTING_LEVELS*F1_0-F1_0/2);
531 if (lright > (NUM_LIGHTING_LEVELS*F1_0-F1_0/2)) lright = (NUM_LIGHTING_LEVELS*F1_0-F1_0/2);
534 fx_dl_dx = fixmul(lright - lleft,recip_dx);
536 // This is a pretty ugly hack to prevent lighting overflows.
537 mul_thing = dx * fx_dl_dx;
538 if (lleft + mul_thing < 0)
540 else if (lleft + mul_thing > (NUM_LIGHTING_LEVELS*F1_0-F1_0/2))
543 //added 05/17/99 Matt Mueller - prevent writing before the buffer
544 if ((fx_y == 0) && (fx_xleft < 0))
547 if (fx_xright > Window_clip_right)
548 fx_xright = Window_clip_right;
550 #if (defined(NO_ASM) && !defined(ASM_PER)) || defined(FP_TMAP)
551 c_tmap_scanline_per();
553 asm_tmap_scanline_per();
559 fx_xright = f2i(xright);
560 fx_xleft = f2i(xleft);
562 #if defined(FL1_WITH_FLAT) || defined(NO_ASM)
565 c_tmap_scanline_flat();
567 asm_tmap_scanline_flat();
570 asm_tmap_scanline_matt();
573 Int3(); // Illegal, called an editor only routine!
580 int Do_vertical_scan=0;
584 // -------------------------------------------------------------------------------------
585 // Render a texture map with lighting using perspective interpolation in inner and outer loops.
586 // -------------------------------------------------------------------------------------
587 void ntexture_map_lighted(grs_bitmap *srcb, g3ds_tmap *t)
589 int vlt,vrt,vlb,vrb; // vertex left top, vertex right top, vertex left bottom, vertex right bottom
591 fix dx_dy_left,dx_dy_right;
592 fix du_dy_left,du_dy_right;
593 fix dv_dy_left,dv_dy_right;
594 fix dz_dy_left,dz_dy_right;
595 fix dl_dy_left,dl_dy_right;
596 fix recip_dyl, recip_dyr;
598 fix xleft,xright,uleft,vleft,uright,vright,zleft,zright,lleft,lright;
599 int next_break_left, next_break_right;
603 //remove stupid warnings in compile
611 // Determine top and bottom y coords.
612 compute_y_bounds(t,&vlt,&vlb,&vrt,&vrb,&max_y_vertex);
614 // Set top and bottom (of entire texture map) y coordinates.
615 topy = f2i(v3d[vlt].y2d);
616 boty = f2i(v3d[max_y_vertex].y2d);
617 if (topy > Window_clip_bot)
619 if (boty > Window_clip_bot)
620 boty = Window_clip_bot;
622 // Set amount to change x coordinate for each advance to next scanline.
623 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
624 if (dy < FIX_RECIP_TABLE_SIZE)
625 recip_dyl = fix_recip[dy];
629 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dyl);
630 du_dy_left = compute_du_dy(t,vlt,vlb, recip_dyl);
631 dv_dy_left = compute_dv_dy(t,vlt,vlb, recip_dyl);
632 dz_dy_left = compute_dz_dy(t,vlt,vlb, recip_dyl);
634 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
635 if (dy < FIX_RECIP_TABLE_SIZE)
636 recip_dyr = fix_recip[dy];
640 du_dy_right = compute_du_dy(t,vrt,vrb, recip_dyr);
641 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dyr);
642 dv_dy_right = compute_dv_dy(t,vrt,vrb, recip_dyr);
643 dz_dy_right = compute_dz_dy(t,vrt,vrb, recip_dyr);
645 if (Lighting_enabled) {
646 dl_dy_left = compute_dl_dy_lin(t,vlt,vlb, recip_dyl);
647 dl_dy_right = compute_dl_dy_lin(t,vrt,vrb, recip_dyr);
653 // Set initial values for x, u, v
654 xleft = v3d[vlt].x2d;
655 xright = v3d[vrt].x2d;
660 uleft = fixmul(v3d[vlt].u,zleft);
661 uright = fixmul(v3d[vrt].u,zright);
662 vleft = fixmul(v3d[vlt].v,zleft);
663 vright = fixmul(v3d[vrt].v,zright);
665 // scan all rows in texture map from top through first break.
666 next_break_left = f2i(v3d[vlb].y2d);
667 next_break_right = f2i(v3d[vrb].y2d);
669 for (y = topy; y < boty; y++) {
671 // See if we have reached the end of the current left edge, and if so, set
672 // new values for dx_dy and x,u,v
673 if (y == next_break_left) {
676 // Handle problem of double points. Search until y coord is different. Cannot get
677 // hung in an infinite loop because we know there is a vertex with a lower y coordinate
678 // because in the for loop, we don't scan all spanlines.
679 while (y == f2i(v3d[vlb].y2d)) {
681 vlb = prevmod(vlb,t->nv);
683 next_break_left = f2i(v3d[vlb].y2d);
685 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
686 if (dy < FIX_RECIP_TABLE_SIZE)
687 recip_dy = fix_recip[dy];
691 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dy);
693 xleft = v3d[vlt].x2d;
695 uleft = fixmul(v3d[vlt].u,zleft);
696 vleft = fixmul(v3d[vlt].v,zleft);
699 du_dy_left = compute_du_dy(t,vlt,vlb, recip_dy);
700 dv_dy_left = compute_dv_dy(t,vlt,vlb, recip_dy);
701 dz_dy_left = compute_dz_dy(t,vlt,vlb, recip_dy);
703 if (Lighting_enabled) {
704 dl_dy_left = compute_dl_dy_lin(t,vlt,vlb, recip_dy);
709 // See if we have reached the end of the current left edge, and if so, set
710 // new values for dx_dy and x. Not necessary to set new values for u,v.
711 if (y == next_break_right) {
714 while (y == f2i(v3d[vrb].y2d)) {
716 vrb = succmod(vrb,t->nv);
719 next_break_right = f2i(v3d[vrb].y2d);
721 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
722 if (dy < FIX_RECIP_TABLE_SIZE)
723 recip_dy = fix_recip[dy];
727 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dy);
729 xright = v3d[vrt].x2d;
731 uright = fixmul(v3d[vrt].u,zright);
732 vright = fixmul(v3d[vrt].v,zright);
734 du_dy_right = compute_du_dy(t,vrt,vrb, recip_dy);
735 dv_dy_right = compute_dv_dy(t,vrt,vrb, recip_dy);
736 dz_dy_right = compute_dz_dy(t,vrt,vrb, recip_dy);
738 if (Lighting_enabled) {
739 dl_dy_right = compute_dl_dy_lin(t,vrt,vrb, recip_dy);
744 if (Lighting_enabled) {
745 if (y >= Window_clip_top)
746 ntmap_scanline_lighted(srcb,y,xleft,xright,uleft,uright,vleft,vright,zleft,zright,lleft,lright);
748 lright += dl_dy_right;
750 if (y >= Window_clip_top)
751 ntmap_scanline_lighted(srcb,y,xleft,xright,uleft,uright,vleft,vright,zleft,zright,lleft,lright);
756 uright += du_dy_right;
757 vright += dv_dy_right;
760 xright += dx_dy_right;
763 zright += dz_dy_right;
767 // We can get lleft or lright out of bounds here because we compute dl_dy using fixed point values,
768 // but we plot an integer number of scanlines, therefore doing an integer number of additions of the delta.
771 // mprintf(0, "[%i %i %i] ", y, f2i(xleft), f2i(xright));
773 ntmap_scanline_lighted(srcb,y,xleft,xright,uleft,uright,vleft,vright,zleft,zright,lleft,lright);
777 // -------------------------------------------------------------------------------------
778 // Texture map current scanline using linear interpolation.
779 // -------------------------------------------------------------------------------------
780 void ntmap_scanline_lighted_linear(grs_bitmap *srcb, int y, fix xleft, fix xright, fix uleft, fix uright, fix vleft, fix vright, fix lleft, fix lright)
785 fix du_dx,dv_dx,dl_dx;
791 dx = f2i(xright) - f2i(xleft);
792 if ((dx < 0) || (xright < 0) || (xleft > xright)) // the (xleft > xright) term is not redundant with (dx < 0) because dx is computed using integers
795 // setup to call assembler scanline renderer
796 if (dx < FIX_RECIP_TABLE_SIZE)
797 recip_dx = fix_recip[dx];
801 du_dx = fixmul(uright - uleft,recip_dx);
802 dv_dx = fixmul(vright - vleft,recip_dx);
809 fx_xright = f2i(xright);
810 fx_xleft = f2i(xleft);
811 pixptr = srcb->bm_data;
813 switch (Lighting_enabled) {
815 //added 07/11/99 adb - prevent writing before the buffer
821 c_tmap_scanline_lin_nolight();
823 #ifdef NO_ASM_NOLIGHT
824 asm_tmap_scanline_lin_lighted();
826 asm_tmap_scanline_lin();
836 if (lleft > MAX_LIGHTING_VALUE*NUM_LIGHTING_LEVELS)
837 lleft = MAX_LIGHTING_VALUE*NUM_LIGHTING_LEVELS;
838 if (lright > MAX_LIGHTING_VALUE*NUM_LIGHTING_LEVELS)
839 lright = MAX_LIGHTING_VALUE*NUM_LIGHTING_LEVELS;
841 //added 07/11/99 adb - prevent writing before the buffer
850 fx_dl_dx = fixmul(lright - lleft,recip_dx);
852 // This is a pretty ugly hack to prevent lighting overflows.
853 mul_thing = dx * fx_dl_dx;
854 if (lleft + mul_thing < 0)
856 else if (lleft + mul_thing > (NUM_LIGHTING_LEVELS*F1_0-F1_0/2))
861 dl_dx = fixmul(lright - lleft,recip_dx);
864 c_tmap_scanline_lin();
866 asm_tmap_scanline_lin_lighted();
871 fx_xright = f2i(xright);
872 fx_xleft = f2i(xleft);
873 #if defined(FL1_WITH_FLAT) || defined(NO_ASM)
876 c_tmap_scanline_flat();
878 asm_tmap_scanline_flat();
881 asm_tmap_scanline_matt();
884 Int3(); // Illegal, called an editor only routine!
890 // -------------------------------------------------------------------------------------
891 // Render a texture map with lighting using perspective interpolation in inner and outer loops.
892 // -------------------------------------------------------------------------------------
893 void ntexture_map_lighted_linear(grs_bitmap *srcb, g3ds_tmap *t)
895 int vlt,vrt,vlb,vrb; // vertex left top, vertex right top, vertex left bottom, vertex right bottom
897 fix dx_dy_left,dx_dy_right;
898 fix du_dy_left,du_dy_right;
899 fix dv_dy_left,dv_dy_right;
900 fix dl_dy_left,dl_dy_right;
902 fix xleft,xright,uleft,vleft,uright,vright,lleft,lright;
903 int next_break_left, next_break_right;
904 fix recip_dyl, recip_dyr;
908 //remove stupid warnings in compile
916 // Determine top and bottom y coords.
917 compute_y_bounds(t,&vlt,&vlb,&vrt,&vrb,&max_y_vertex);
919 // Set top and bottom (of entire texture map) y coordinates.
920 topy = f2i(v3d[vlt].y2d);
921 boty = f2i(v3d[max_y_vertex].y2d);
923 if (topy > Window_clip_bot)
925 if (boty > Window_clip_bot)
926 boty = Window_clip_bot;
928 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
929 if (dy < FIX_RECIP_TABLE_SIZE)
930 recip_dyl = fix_recip[dy];
934 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
935 if (dy < FIX_RECIP_TABLE_SIZE)
936 recip_dyr = fix_recip[dy];
940 // Set amount to change x coordinate for each advance to next scanline.
941 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dyl);
942 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dyr);
944 du_dy_left = compute_du_dy_lin(t,vlt,vlb, recip_dyl);
945 du_dy_right = compute_du_dy_lin(t,vrt,vrb, recip_dyr);
947 dv_dy_left = compute_dv_dy_lin(t,vlt,vlb, recip_dyl);
948 dv_dy_right = compute_dv_dy_lin(t,vrt,vrb, recip_dyr);
950 if (Lighting_enabled) {
951 dl_dy_left = compute_dl_dy_lin(t,vlt,vlb, recip_dyl);
952 dl_dy_right = compute_dl_dy_lin(t,vrt,vrb, recip_dyr);
958 // Set initial values for x, u, v
959 xleft = v3d[vlt].x2d;
960 xright = v3d[vrt].x2d;
967 // scan all rows in texture map from top through first break.
968 next_break_left = f2i(v3d[vlb].y2d);
969 next_break_right = f2i(v3d[vrb].y2d);
971 for (y = topy; y < boty; y++) {
973 // See if we have reached the end of the current left edge, and if so, set
974 // new values for dx_dy and x,u,v
975 if (y == next_break_left) {
978 // Handle problem of double points. Search until y coord is different. Cannot get
979 // hung in an infinite loop because we know there is a vertex with a lower y coordinate
980 // because in the for loop, we don't scan all spanlines.
981 while (y == f2i(v3d[vlb].y2d)) {
983 vlb = prevmod(vlb,t->nv);
985 next_break_left = f2i(v3d[vlb].y2d);
987 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
988 if (dy < FIX_RECIP_TABLE_SIZE)
989 recip_dy = fix_recip[dy];
993 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dy);
995 xleft = v3d[vlt].x2d;
1000 du_dy_left = compute_du_dy_lin(t,vlt,vlb, recip_dy);
1001 dv_dy_left = compute_dv_dy_lin(t,vlt,vlb, recip_dy);
1003 if (Lighting_enabled) {
1004 dl_dy_left = compute_dl_dy_lin(t,vlt,vlb, recip_dy);
1009 // See if we have reached the end of the current left edge, and if so, set
1010 // new values for dx_dy and x. Not necessary to set new values for u,v.
1011 if (y == next_break_right) {
1014 while (y == f2i(v3d[vrb].y2d)) {
1016 vrb = succmod(vrb,t->nv);
1019 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
1020 if (dy < FIX_RECIP_TABLE_SIZE)
1021 recip_dy = fix_recip[dy];
1025 next_break_right = f2i(v3d[vrb].y2d);
1026 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dy);
1028 xright = v3d[vrt].x2d;
1029 uright = v3d[vrt].u;
1030 vright = v3d[vrt].v;
1032 du_dy_right = compute_du_dy_lin(t,vrt,vrb, recip_dy);
1033 dv_dy_right = compute_dv_dy_lin(t,vrt,vrb, recip_dy);
1035 if (Lighting_enabled) {
1036 dl_dy_right = compute_dl_dy_lin(t,vrt,vrb, recip_dy);
1037 lright = v3d[vrt].l;
1041 if (Lighting_enabled) {
1042 ntmap_scanline_lighted_linear(srcb,y,xleft,xright,uleft,uright,vleft,vright,lleft,lright);
1043 lleft += dl_dy_left;
1044 lright += dl_dy_right;
1046 ntmap_scanline_lighted_linear(srcb,y,xleft,xright,uleft,uright,vleft,vright,lleft,lright);
1048 uleft += du_dy_left;
1049 vleft += dv_dy_left;
1051 uright += du_dy_right;
1052 vright += dv_dy_right;
1054 xleft += dx_dy_left;
1055 xright += dx_dy_right;
1059 // We can get lleft or lright out of bounds here because we compute dl_dy using fixed point values,
1060 // but we plot an integer number of scanlines, therefore doing an integer number of additions of the delta.
1062 ntmap_scanline_lighted_linear(srcb,y,xleft,xright,uleft,uright,vleft,vright,lleft,lright);
1065 // fix DivNum = F1_0*12;
1067 extern void draw_tmap_flat(grs_bitmap *bp,int nverts,g3s_point **vertbuf);
1069 // -------------------------------------------------------------------------------------
1070 // Interface from Matt's data structures to Mike's texture mapper.
1071 // -------------------------------------------------------------------------------------
1072 void draw_tmap(grs_bitmap *bp,int nverts,g3s_point **vertbuf)
1076 // These variables are used in system which renders texture maps which lie on one scanline as a line.
1077 // fix div_numerator;
1078 int lighting_on_save = Lighting_on;
1080 Assert(nverts <= MAX_TMAP_VERTS);
1083 #ifdef USE_MULT_CODE
1084 if ( !divide_table_filled ) fill_divide_table();
1087 // -- now called from g3_start_frame -- init_interface_vars_to_assembler();
1089 // If no transparency and seg depth is large, render as flat shaded.
1090 if ((Current_seg_depth > Max_linear_depth) && ((bp->bm_flags & 3) == 0)) {
1091 draw_tmap_flat(bp, nverts, vertbuf);
1095 if ( bp->bm_flags & BM_FLAG_RLE )
1096 bp = rle_expand_texture( bp ); // Expand if rle'd
1098 Transparency_on = bp->bm_flags & BM_FLAG_TRANSPARENT;
1099 if (bp->bm_flags & BM_FLAG_NO_LIGHTING)
1103 // Setup texture map in Tmap1
1104 Tmap1.nv = nverts; // Initialize number of vertices
1106 // div_numerator = DivNum; //f1_0*3;
1108 for (i=0; i<nverts; i++) {
1109 g3ds_vertex *tvp = &Tmap1.verts[i];
1110 g3s_point *vp = vertbuf[i];
1112 tvp->x2d = vp->p3_sx;
1113 tvp->y2d = vp->p3_sy;
1115 // Check for overflow on fixdiv. Will overflow on vp->z <= something small. Allow only as low as 256.
1116 if (vp->p3_z < 256) {
1118 // Int3(); // we would overflow if we divided!
1121 tvp->z = fixdiv(F1_0*12, vp->p3_z);
1122 tvp->u = vp->p3_u << 6; //* bp->bm_w;
1123 tvp->v = vp->p3_v << 6; //* bp->bm_h;
1125 Assert(Lighting_on < 3);
1128 tvp->l = vp->p3_l * NUM_LIGHTING_LEVELS;
1132 Lighting_enabled = Lighting_on;
1134 // Now, call my texture mapper.
1136 switch (Interpolation_method) { // 0 = choose, 1 = linear, 2 = /8 perspective, 3 = full perspective
1137 case 0: // choose best interpolation
1139 if (Current_seg_depth > Max_perspective_depth)
1140 ntexture_map_lighted_linear(bp, &Tmap1);
1142 ntexture_map_lighted(bp, &Tmap1);
1144 case 1: // linear interpolation
1146 ntexture_map_lighted_linear(bp, &Tmap1);
1148 case 2: // perspective every 8th pixel interpolation
1150 ntexture_map_lighted(bp, &Tmap1);
1152 case 3: // perspective every pixel interpolation
1153 per2_flag = 0; // this hack means do divide every pixel
1154 ntexture_map_lighted(bp, &Tmap1);
1157 Assert(0); // Illegal value for Interpolation_method, must be 0,1,2,3
1160 switch (Interpolation_method) { // 0 = choose, 1 = linear, 2 = /8 perspective, 3 = full perspective
1161 case 0: // choose best interpolation
1163 if (Current_seg_depth > Max_perspective_depth)
1164 ntexture_map_lighted_linear(bp, &Tmap1);
1166 ntexture_map_lighted(bp, &Tmap1);
1168 case 1: // linear interpolation
1170 ntexture_map_lighted_linear(bp, &Tmap1);
1172 case 2: // perspective every 8th pixel interpolation
1174 ntexture_map_lighted(bp, &Tmap1);
1176 case 3: // perspective every pixel interpolation
1177 per2_flag = 0; // this hack means do divide every pixel
1178 ntexture_map_lighted(bp, &Tmap1);
1181 Assert(0); // Illegal value for Interpolation_method, must be 0,1,2,3
1185 Lighting_on = lighting_on_save;