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.
16 * Start of conversion to new texture mapper.
27 #define HEADLIGHT_LIGHTING 0
45 #define EDITOR_TMAP 1 //if in, include extra stuff
48 #define F15_5 (F1_0*15 + F0_5)
50 // Temporary texture map, interface from Matt's 3d system to Mike's texture mapper.
53 grs_bitmap Texmap_ptrs[NUM_TMAPS];
54 grs_bitmap Texmap4_ptrs[NUM_TMAPS];
56 fix Range_max=0; // debug, kill me
58 int Interpolation_method=0; // 0 = choose best method
59 int Lighting_on=1; // initialize to no lighting
60 int Tmap_flat_flag = 0; // 1 = render texture maps as flat shaded polygons.
61 int Current_seg_depth; // HACK INTERFACE: how far away the current segment (& thus texture) is
62 int Max_perspective_depth;
66 extern int Window_clip_left, Window_clip_bot, Window_clip_right, Window_clip_top;
68 // These variables are the interface to assembler. They get set for each texture map, which is a real waste of time.
69 // They should be set only when they change, which is generally when the window bounds change. And, even still, it's
70 // a pretty bad interface.
72 unsigned char *write_buffer;
80 #define MAX_Y_POINTERS 1024
82 int y_pointers[MAX_Y_POINTERS];
84 fix fix_recip[FIX_RECIP_TABLE_SIZE];
87 int Fix_recip_table_computed=0;
89 fix fx_l, fx_u, fx_v, fx_z, fx_du_dx, fx_dv_dx, fx_dz_dx, fx_dl_dx;
90 int fx_xleft, fx_xright, fx_y;
91 unsigned char * pixptr;
93 int Transparency_on = 0;
94 int dither_intensity_lighting = 0;
96 ubyte * tmap_flat_cthru_table;
97 ubyte tmap_flat_color;
98 ubyte tmap_flat_shade_value;
102 // -------------------------------------------------------------------------------------
103 void init_fix_recip_table(void)
109 for (i=1; i<FIX_RECIP_TABLE_SIZE; i++)
110 fix_recip[i] = F1_0/i;
112 Fix_recip_table_computed = 1;
115 // -------------------------------------------------------------------------------------
116 // Initialize interface variables to assembler.
117 // These things used to be constants. This routine is now (10/6/93) getting called for
118 // every texture map. It should get called whenever the window changes, or, preferably,
119 // not at all. I'm pretty sure these variables are only being used for range checking.
120 void init_interface_vars_to_assembler(void)
124 bp = &grd_curcanv->cv_bitmap;
127 Assert(bp->bm_data!=NULL);
128 Assert(bp->bm_h <= MAX_Y_POINTERS);
130 // If bytes_per_row has changed, create new table of pointers.
131 if (bytes_per_row != (int) bp->bm_rowsize) {
134 bytes_per_row = (int) bp->bm_rowsize;
137 for (i=0; i<MAX_Y_POINTERS; i++) {
138 y_pointers[i] = y_val;
139 y_val += bytes_per_row;
143 write_buffer = (unsigned char *) bp->bm_data;
146 window_right = (int) bp->bm_w-1;
148 window_bottom = (int) bp->bm_h-1;
150 Window_clip_left = window_left;
151 Window_clip_right = window_right;
152 Window_clip_top = window_top;
153 Window_clip_bot = window_bottom;
155 window_width = bp->bm_w;
156 window_height = bp->bm_h;
158 if (!Fix_recip_table_computed)
159 init_fix_recip_table();
162 // -------------------------------------------------------------------------------------
164 extern g3ds_tmap Tmap1;
166 // -------------------------------------------------------------------------------------
167 // Returns number preceding val modulo modulus.
170 int prevmod(int val,int modulus)
176 // return (val + modulus - 1) % modulus;
180 // Returns number succeeding val modulo modulus.
183 int succmod(int val,int modulus)
190 // return (val + 1) % modulus;
193 // -------------------------------------------------------------------------------------
194 // Select topmost vertex (minimum y coordinate) and bottommost (maximum y coordinate) in
195 // texture map. If either is part of a horizontal edge, then select leftmost vertex for
196 // top, rightmost vertex for bottom.
197 // Important: Vertex is selected with integer precision. So, if there are vertices at
198 // (0.0,0.7) and (0.5,0.3), the first vertex is selected, because they y coordinates are
199 // considered the same, so the smaller x is favored.
201 // nv number of vertices
202 // v3d pointer to 3d vertices containing u,v,x2d,y2d coordinates
206 // -------------------------------------------------------------------------------------
207 void compute_y_bounds(g3ds_tmap *t, int *vlt, int *vlb, int *vrt, int *vrb,int *bottom_y_ind)
215 // Scan all vertices, set min_y_ind to vertex with smallest y coordinate.
216 min_y = f2i(t->verts[0].y2d);
219 min_x = f2i(t->verts[0].x2d);
222 for (i=1; i<t->nv; i++) {
223 if (f2i(t->verts[i].y2d) < min_y) {
224 min_y = f2i(t->verts[i].y2d);
226 min_x = f2i(t->verts[i].x2d);
227 } else if (f2i(t->verts[i].y2d) == min_y) {
228 if (f2i(t->verts[i].x2d) < min_x) {
230 min_x = f2i(t->verts[i].x2d);
233 if (f2i(t->verts[i].y2d) > max_y) {
234 max_y = f2i(t->verts[i].y2d);
239 //--removed mk, 11/27/94-- // Check for a non-upright-hourglass polygon and fix, if necessary, by bashing a y coordinate.
240 //--removed mk, 11/27/94-- // min_y_ind = index of minimum y coordinate, *bottom_y_ind = index of maximum y coordinate
241 //--removed mk, 11/27/94--{
242 //--removed mk, 11/27/94-- int max_temp, min_temp;
243 //--removed mk, 11/27/94--
244 //--removed mk, 11/27/94-- max_temp = *bottom_y_ind;
245 //--removed mk, 11/27/94-- if (*bottom_y_ind < min_y_ind)
246 //--removed mk, 11/27/94-- max_temp += t->nv;
247 //--removed mk, 11/27/94--
248 //--removed mk, 11/27/94-- for (i=min_y_ind; i<max_temp; i++) {
249 //--removed mk, 11/27/94-- if (f2i(t->verts[i%t->nv].y2d) > f2i(t->verts[(i+1)%t->nv].y2d)) {
250 //--removed mk, 11/27/94-- Int3();
251 //--removed mk, 11/27/94-- t->verts[(i+1)%t->nv].y2d = t->verts[i%t->nv].y2d;
252 //--removed mk, 11/27/94-- }
253 //--removed mk, 11/27/94-- }
254 //--removed mk, 11/27/94--
255 //--removed mk, 11/27/94-- min_temp = min_y_ind;
256 //--removed mk, 11/27/94-- if (min_y_ind < *bottom_y_ind)
257 //--removed mk, 11/27/94-- min_temp += t->nv;
258 //--removed mk, 11/27/94--
259 //--removed mk, 11/27/94-- for (i=*bottom_y_ind; i<min_temp; i++) {
260 //--removed mk, 11/27/94-- if (f2i(t->verts[i%t->nv].y2d) < f2i(t->verts[(i+1)%t->nv].y2d)) {
261 //--removed mk, 11/27/94-- Int3();
262 //--removed mk, 11/27/94-- t->verts[(i+1)%t->nv].y2d = t->verts[i%t->nv].y2d;
263 //--removed mk, 11/27/94-- }
264 //--removed mk, 11/27/94-- }
265 //--removed mk, 11/27/94--}
267 // Set "vertex left top", etc. based on vertex with topmost y coordinate
270 *vlb = prevmod(*vlt,t->nv);
271 *vrb = succmod(*vrt,t->nv);
273 // If right edge is horizontal, then advance along polygon bound until it no longer is or until all
274 // vertices have been examined.
275 // (Left edge cannot be horizontal, because *vlt is set to leftmost point with highest y coordinate.)
279 while (f2i(t->verts[*vrt].y2d) == f2i(t->verts[*vrb].y2d)) {
280 if (succmod(*vrt,t->nv) == original_vrt) {
283 *vrt = succmod(*vrt,t->nv);
284 *vrb = succmod(*vrt,t->nv);
288 // -------------------------------------------------------------------------------------
289 // Returns dx/dy given two vertices.
290 // If dy == 0, returns 0.0
291 // -------------------------------------------------------------------------------------
292 //--fix compute_dx_dy_lin(g3ds_tmap *t, int top_vertex,int bottom_vertex)
296 //-- // compute delta x with respect to y for any edge
297 //-- dy = f2i(t->verts[bottom_vertex].y2d - t->verts[top_vertex].y2d) + 1;
299 //-- return (t->verts[bottom_vertex].x2d - t->verts[top_vertex].x2d) / dy;
305 fix compute_du_dy_lin(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
307 return fixmul(t->verts[bottom_vertex].u - t->verts[top_vertex].u, recip_dy);
311 fix compute_dv_dy_lin(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
313 return fixmul(t->verts[bottom_vertex].v - t->verts[top_vertex].v, recip_dy);
316 fix compute_dl_dy_lin(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
318 return fixmul(t->verts[bottom_vertex].l - t->verts[top_vertex].l, recip_dy);
322 fix compute_dx_dy(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
324 return fixmul(t->verts[bottom_vertex].x2d - t->verts[top_vertex].x2d, recip_dy);
327 fix compute_du_dy(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
329 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);
333 fix compute_dv_dy(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
335 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);
339 fix compute_dz_dy(g3ds_tmap *t, int top_vertex,int bottom_vertex, fix recip_dy)
341 return fixmul(t->verts[bottom_vertex].z - t->verts[top_vertex].z, recip_dy);
344 int Skip_short_flag=0;
346 // -------------------------------------------------------------------------------------
347 // Texture map current scanline in perspective.
348 // -------------------------------------------------------------------------------------
349 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)
353 fx_xright = f2i(xright);
354 //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?
355 if (fx_xright < Window_clip_left)
357 fx_xleft = f2i(xleft);
358 if (fx_xleft > Window_clip_right)
362 dx = fx_xright - fx_xleft;
363 if ((dx < 0) || (xright < 0) || (xleft > xright)) // the (xleft > xright) term is not redundant with (dx < 0) because dx is computed using integers
366 // setup to call assembler scanline renderer
367 if (dx < FIX_RECIP_TABLE_SIZE)
368 recip_dx = fix_recip[dx];
376 fx_du_dx = fixmul(uright - uleft,recip_dx);
377 fx_dv_dx = fixmul(vright - vleft,recip_dx);
378 fx_dz_dx = fixmul(zright - zleft,recip_dx);
380 pixptr = srcb->bm_data;
382 switch (Lighting_enabled) {
384 //added 05/17/99 Matt Mueller - prevent writing before the buffer
385 if ((fx_y == 0) && (fx_xleft < 0))
388 if (fx_xright > Window_clip_right)
389 fx_xright = Window_clip_right;
391 cur_tmap_scanline_per();
396 if (lleft < 0) lleft = 0;
397 if (lright < 0) lright = 0;
398 if (lleft > (NUM_LIGHTING_LEVELS*F1_0-F1_0/2)) lleft = (NUM_LIGHTING_LEVELS*F1_0-F1_0/2);
399 if (lright > (NUM_LIGHTING_LEVELS*F1_0-F1_0/2)) lright = (NUM_LIGHTING_LEVELS*F1_0-F1_0/2);
402 fx_dl_dx = fixmul(lright - lleft,recip_dx);
404 // This is a pretty ugly hack to prevent lighting overflows.
405 mul_thing = dx * fx_dl_dx;
406 if (lleft + mul_thing < 0)
408 else if (lleft + mul_thing > (NUM_LIGHTING_LEVELS*F1_0-F1_0/2))
411 //added 05/17/99 Matt Mueller - prevent writing before the buffer
412 if ((fx_y == 0) && (fx_xleft < 0))
415 if (fx_xright > Window_clip_right)
416 fx_xright = Window_clip_right;
418 cur_tmap_scanline_per();
423 fx_xright = f2i(xright);
424 fx_xleft = f2i(xleft);
427 cur_tmap_scanline_flat();
429 Int3(); // Illegal, called an editor only routine!
436 int Do_vertical_scan=0;
440 // -------------------------------------------------------------------------------------
441 // Render a texture map with lighting using perspective interpolation in inner and outer loops.
442 // -------------------------------------------------------------------------------------
443 void ntexture_map_lighted(grs_bitmap *srcb, g3ds_tmap *t)
445 int vlt,vrt,vlb,vrb; // vertex left top, vertex right top, vertex left bottom, vertex right bottom
447 fix dx_dy_left,dx_dy_right;
448 fix du_dy_left,du_dy_right;
449 fix dv_dy_left,dv_dy_right;
450 fix dz_dy_left,dz_dy_right;
451 fix dl_dy_left,dl_dy_right;
452 fix recip_dyl, recip_dyr;
454 fix xleft,xright,uleft,vleft,uright,vright,zleft,zright,lleft,lright;
455 int next_break_left, next_break_right;
459 //remove stupid warnings in compile
467 // Determine top and bottom y coords.
468 compute_y_bounds(t,&vlt,&vlb,&vrt,&vrb,&max_y_vertex);
470 // Set top and bottom (of entire texture map) y coordinates.
471 topy = f2i(v3d[vlt].y2d);
472 boty = f2i(v3d[max_y_vertex].y2d);
473 if (topy > Window_clip_bot)
475 if (boty > Window_clip_bot)
476 boty = Window_clip_bot;
478 // Set amount to change x coordinate for each advance to next scanline.
479 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
480 if (dy < FIX_RECIP_TABLE_SIZE)
481 recip_dyl = fix_recip[dy];
485 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dyl);
486 du_dy_left = compute_du_dy(t,vlt,vlb, recip_dyl);
487 dv_dy_left = compute_dv_dy(t,vlt,vlb, recip_dyl);
488 dz_dy_left = compute_dz_dy(t,vlt,vlb, recip_dyl);
490 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
491 if (dy < FIX_RECIP_TABLE_SIZE)
492 recip_dyr = fix_recip[dy];
496 du_dy_right = compute_du_dy(t,vrt,vrb, recip_dyr);
497 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dyr);
498 dv_dy_right = compute_dv_dy(t,vrt,vrb, recip_dyr);
499 dz_dy_right = compute_dz_dy(t,vrt,vrb, recip_dyr);
501 if (Lighting_enabled) {
502 dl_dy_left = compute_dl_dy_lin(t,vlt,vlb, recip_dyl);
503 dl_dy_right = compute_dl_dy_lin(t,vrt,vrb, recip_dyr);
509 // Set initial values for x, u, v
510 xleft = v3d[vlt].x2d;
511 xright = v3d[vrt].x2d;
516 uleft = fixmul(v3d[vlt].u,zleft);
517 uright = fixmul(v3d[vrt].u,zright);
518 vleft = fixmul(v3d[vlt].v,zleft);
519 vright = fixmul(v3d[vrt].v,zright);
521 // scan all rows in texture map from top through first break.
522 next_break_left = f2i(v3d[vlb].y2d);
523 next_break_right = f2i(v3d[vrb].y2d);
525 for (y = topy; y < boty; y++) {
527 // See if we have reached the end of the current left edge, and if so, set
528 // new values for dx_dy and x,u,v
529 if (y == next_break_left) {
532 // Handle problem of double points. Search until y coord is different. Cannot get
533 // hung in an infinite loop because we know there is a vertex with a lower y coordinate
534 // because in the for loop, we don't scan all spanlines.
535 while (y == f2i(v3d[vlb].y2d)) {
537 vlb = prevmod(vlb,t->nv);
539 next_break_left = f2i(v3d[vlb].y2d);
541 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
542 if (dy < FIX_RECIP_TABLE_SIZE)
543 recip_dy = fix_recip[dy];
547 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dy);
549 xleft = v3d[vlt].x2d;
551 uleft = fixmul(v3d[vlt].u,zleft);
552 vleft = fixmul(v3d[vlt].v,zleft);
555 du_dy_left = compute_du_dy(t,vlt,vlb, recip_dy);
556 dv_dy_left = compute_dv_dy(t,vlt,vlb, recip_dy);
557 dz_dy_left = compute_dz_dy(t,vlt,vlb, recip_dy);
559 if (Lighting_enabled) {
560 dl_dy_left = compute_dl_dy_lin(t,vlt,vlb, recip_dy);
565 // See if we have reached the end of the current left edge, and if so, set
566 // new values for dx_dy and x. Not necessary to set new values for u,v.
567 if (y == next_break_right) {
570 while (y == f2i(v3d[vrb].y2d)) {
572 vrb = succmod(vrb,t->nv);
575 next_break_right = f2i(v3d[vrb].y2d);
577 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
578 if (dy < FIX_RECIP_TABLE_SIZE)
579 recip_dy = fix_recip[dy];
583 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dy);
585 xright = v3d[vrt].x2d;
587 uright = fixmul(v3d[vrt].u,zright);
588 vright = fixmul(v3d[vrt].v,zright);
590 du_dy_right = compute_du_dy(t,vrt,vrb, recip_dy);
591 dv_dy_right = compute_dv_dy(t,vrt,vrb, recip_dy);
592 dz_dy_right = compute_dz_dy(t,vrt,vrb, recip_dy);
594 if (Lighting_enabled) {
595 dl_dy_right = compute_dl_dy_lin(t,vrt,vrb, recip_dy);
600 if (Lighting_enabled) {
601 if (y >= Window_clip_top)
602 ntmap_scanline_lighted(srcb,y,xleft,xright,uleft,uright,vleft,vright,zleft,zright,lleft,lright);
604 lright += dl_dy_right;
606 if (y >= Window_clip_top)
607 ntmap_scanline_lighted(srcb,y,xleft,xright,uleft,uright,vleft,vright,zleft,zright,lleft,lright);
612 uright += du_dy_right;
613 vright += dv_dy_right;
616 xright += dx_dy_right;
619 zright += dz_dy_right;
623 // We can get lleft or lright out of bounds here because we compute dl_dy using fixed point values,
624 // but we plot an integer number of scanlines, therefore doing an integer number of additions of the delta.
627 // mprintf(0, "[%i %i %i] ", y, f2i(xleft), f2i(xright));
629 ntmap_scanline_lighted(srcb,y,xleft,xright,uleft,uright,vleft,vright,zleft,zright,lleft,lright);
633 // -------------------------------------------------------------------------------------
634 // Texture map current scanline using linear interpolation.
635 // -------------------------------------------------------------------------------------
636 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)
641 fix du_dx,dv_dx,dl_dx;
647 dx = f2i(xright) - f2i(xleft);
648 if ((dx < 0) || (xright < 0) || (xleft > xright)) // the (xleft > xright) term is not redundant with (dx < 0) because dx is computed using integers
651 // setup to call assembler scanline renderer
652 if (dx < FIX_RECIP_TABLE_SIZE)
653 recip_dx = fix_recip[dx];
657 du_dx = fixmul(uright - uleft,recip_dx);
658 dv_dx = fixmul(vright - vleft,recip_dx);
665 fx_xright = f2i(xright);
666 fx_xleft = f2i(xleft);
667 pixptr = srcb->bm_data;
669 switch (Lighting_enabled) {
671 //added 07/11/99 adb - prevent writing before the buffer
676 cur_tmap_scanline_lin_nolight();
684 if (lleft > MAX_LIGHTING_VALUE*NUM_LIGHTING_LEVELS)
685 lleft = MAX_LIGHTING_VALUE*NUM_LIGHTING_LEVELS;
686 if (lright > MAX_LIGHTING_VALUE*NUM_LIGHTING_LEVELS)
687 lright = MAX_LIGHTING_VALUE*NUM_LIGHTING_LEVELS;
689 //added 07/11/99 adb - prevent writing before the buffer
698 fx_dl_dx = fixmul(lright - lleft,recip_dx);
700 // This is a pretty ugly hack to prevent lighting overflows.
701 mul_thing = dx * fx_dl_dx;
702 if (lleft + mul_thing < 0)
704 else if (lleft + mul_thing > (NUM_LIGHTING_LEVELS*F1_0-F1_0/2))
709 dl_dx = fixmul(lright - lleft,recip_dx);
711 cur_tmap_scanline_lin();
715 fx_xright = f2i(xright);
716 fx_xleft = f2i(xleft);
718 cur_tmap_scanline_flat();
720 Int3(); // Illegal, called an editor only routine!
726 // -------------------------------------------------------------------------------------
727 // Render a texture map with lighting using perspective interpolation in inner and outer loops.
728 // -------------------------------------------------------------------------------------
729 void ntexture_map_lighted_linear(grs_bitmap *srcb, g3ds_tmap *t)
731 int vlt,vrt,vlb,vrb; // vertex left top, vertex right top, vertex left bottom, vertex right bottom
733 fix dx_dy_left,dx_dy_right;
734 fix du_dy_left,du_dy_right;
735 fix dv_dy_left,dv_dy_right;
736 fix dl_dy_left,dl_dy_right;
738 fix xleft,xright,uleft,vleft,uright,vright,lleft,lright;
739 int next_break_left, next_break_right;
740 fix recip_dyl, recip_dyr;
744 //remove stupid warnings in compile
752 // Determine top and bottom y coords.
753 compute_y_bounds(t,&vlt,&vlb,&vrt,&vrb,&max_y_vertex);
755 // Set top and bottom (of entire texture map) y coordinates.
756 topy = f2i(v3d[vlt].y2d);
757 boty = f2i(v3d[max_y_vertex].y2d);
759 if (topy > Window_clip_bot)
761 if (boty > Window_clip_bot)
762 boty = Window_clip_bot;
764 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
765 if (dy < FIX_RECIP_TABLE_SIZE)
766 recip_dyl = fix_recip[dy];
770 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
771 if (dy < FIX_RECIP_TABLE_SIZE)
772 recip_dyr = fix_recip[dy];
776 // Set amount to change x coordinate for each advance to next scanline.
777 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dyl);
778 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dyr);
780 du_dy_left = compute_du_dy_lin(t,vlt,vlb, recip_dyl);
781 du_dy_right = compute_du_dy_lin(t,vrt,vrb, recip_dyr);
783 dv_dy_left = compute_dv_dy_lin(t,vlt,vlb, recip_dyl);
784 dv_dy_right = compute_dv_dy_lin(t,vrt,vrb, recip_dyr);
786 if (Lighting_enabled) {
787 dl_dy_left = compute_dl_dy_lin(t,vlt,vlb, recip_dyl);
788 dl_dy_right = compute_dl_dy_lin(t,vrt,vrb, recip_dyr);
794 // Set initial values for x, u, v
795 xleft = v3d[vlt].x2d;
796 xright = v3d[vrt].x2d;
803 // scan all rows in texture map from top through first break.
804 next_break_left = f2i(v3d[vlb].y2d);
805 next_break_right = f2i(v3d[vrb].y2d);
807 for (y = topy; y < boty; y++) {
809 // See if we have reached the end of the current left edge, and if so, set
810 // new values for dx_dy and x,u,v
811 if (y == next_break_left) {
814 // Handle problem of double points. Search until y coord is different. Cannot get
815 // hung in an infinite loop because we know there is a vertex with a lower y coordinate
816 // because in the for loop, we don't scan all spanlines.
817 while (y == f2i(v3d[vlb].y2d)) {
819 vlb = prevmod(vlb,t->nv);
821 next_break_left = f2i(v3d[vlb].y2d);
823 dy = f2i(t->verts[vlb].y2d) - f2i(t->verts[vlt].y2d);
824 if (dy < FIX_RECIP_TABLE_SIZE)
825 recip_dy = fix_recip[dy];
829 dx_dy_left = compute_dx_dy(t,vlt,vlb, recip_dy);
831 xleft = v3d[vlt].x2d;
836 du_dy_left = compute_du_dy_lin(t,vlt,vlb, recip_dy);
837 dv_dy_left = compute_dv_dy_lin(t,vlt,vlb, recip_dy);
839 if (Lighting_enabled) {
840 dl_dy_left = compute_dl_dy_lin(t,vlt,vlb, recip_dy);
845 // See if we have reached the end of the current left edge, and if so, set
846 // new values for dx_dy and x. Not necessary to set new values for u,v.
847 if (y == next_break_right) {
850 while (y == f2i(v3d[vrb].y2d)) {
852 vrb = succmod(vrb,t->nv);
855 dy = f2i(t->verts[vrb].y2d) - f2i(t->verts[vrt].y2d);
856 if (dy < FIX_RECIP_TABLE_SIZE)
857 recip_dy = fix_recip[dy];
861 next_break_right = f2i(v3d[vrb].y2d);
862 dx_dy_right = compute_dx_dy(t,vrt,vrb, recip_dy);
864 xright = v3d[vrt].x2d;
868 du_dy_right = compute_du_dy_lin(t,vrt,vrb, recip_dy);
869 dv_dy_right = compute_dv_dy_lin(t,vrt,vrb, recip_dy);
871 if (Lighting_enabled) {
872 dl_dy_right = compute_dl_dy_lin(t,vrt,vrb, recip_dy);
877 if (Lighting_enabled) {
878 ntmap_scanline_lighted_linear(srcb,y,xleft,xright,uleft,uright,vleft,vright,lleft,lright);
880 lright += dl_dy_right;
882 ntmap_scanline_lighted_linear(srcb,y,xleft,xright,uleft,uright,vleft,vright,lleft,lright);
887 uright += du_dy_right;
888 vright += dv_dy_right;
891 xright += dx_dy_right;
895 // We can get lleft or lright out of bounds here because we compute dl_dy using fixed point values,
896 // but we plot an integer number of scanlines, therefore doing an integer number of additions of the delta.
898 ntmap_scanline_lighted_linear(srcb,y,xleft,xright,uleft,uright,vleft,vright,lleft,lright);
901 // fix DivNum = F1_0*12;
903 extern void draw_tmap_flat(grs_bitmap *bp,int nverts,g3s_point **vertbuf);
905 // -------------------------------------------------------------------------------------
906 // Interface from Matt's data structures to Mike's texture mapper.
907 // -------------------------------------------------------------------------------------
908 void draw_tmap(grs_bitmap *bp,int nverts,g3s_point **vertbuf)
912 // These variables are used in system which renders texture maps which lie on one scanline as a line.
913 // fix div_numerator;
914 int lighting_on_save = Lighting_on;
916 Assert(nverts <= MAX_TMAP_VERTS);
920 if ( !divide_table_filled ) fill_divide_table();
923 // -- now called from g3_start_frame -- init_interface_vars_to_assembler();
925 // If no transparency and seg depth is large, render as flat shaded.
926 if ((Current_seg_depth > Max_linear_depth) && ((bp->bm_flags & 3) == 0)) {
927 draw_tmap_flat(bp, nverts, vertbuf);
931 if ( bp->bm_flags & BM_FLAG_RLE )
932 bp = rle_expand_texture( bp ); // Expand if rle'd
934 Transparency_on = bp->bm_flags & BM_FLAG_TRANSPARENT;
935 if (bp->bm_flags & BM_FLAG_NO_LIGHTING)
939 // Setup texture map in Tmap1
940 Tmap1.nv = nverts; // Initialize number of vertices
942 // div_numerator = DivNum; //f1_0*3;
944 for (i=0; i<nverts; i++) {
945 g3ds_vertex *tvp = &Tmap1.verts[i];
946 g3s_point *vp = vertbuf[i];
948 tvp->x2d = vp->p3_sx;
949 tvp->y2d = vp->p3_sy;
951 // Check for overflow on fixdiv. Will overflow on vp->z <= something small. Allow only as low as 256.
952 if (vp->p3_z < 256) {
954 // Int3(); // we would overflow if we divided!
957 tvp->z = fixdiv(F1_0*12, vp->p3_z);
958 tvp->u = vp->p3_u << 6; //* bp->bm_w;
959 tvp->v = vp->p3_v << 6; //* bp->bm_h;
961 Assert(Lighting_on < 3);
964 tvp->l = vp->p3_l * NUM_LIGHTING_LEVELS;
968 Lighting_enabled = Lighting_on;
970 // Now, call my texture mapper.
972 switch (Interpolation_method) { // 0 = choose, 1 = linear, 2 = /8 perspective, 3 = full perspective
973 case 0: // choose best interpolation
975 if (Current_seg_depth > Max_perspective_depth)
976 ntexture_map_lighted_linear(bp, &Tmap1);
978 ntexture_map_lighted(bp, &Tmap1);
980 case 1: // linear interpolation
982 ntexture_map_lighted_linear(bp, &Tmap1);
984 case 2: // perspective every 8th pixel interpolation
986 ntexture_map_lighted(bp, &Tmap1);
988 case 3: // perspective every pixel interpolation
989 per2_flag = 0; // this hack means do divide every pixel
990 ntexture_map_lighted(bp, &Tmap1);
993 Assert(0); // Illegal value for Interpolation_method, must be 0,1,2,3
996 switch (Interpolation_method) { // 0 = choose, 1 = linear, 2 = /8 perspective, 3 = full perspective
997 case 0: // choose best interpolation
999 if (Current_seg_depth > Max_perspective_depth)
1000 ntexture_map_lighted_linear(bp, &Tmap1);
1002 ntexture_map_lighted(bp, &Tmap1);
1004 case 1: // linear interpolation
1006 ntexture_map_lighted_linear(bp, &Tmap1);
1008 case 2: // perspective every 8th pixel interpolation
1010 ntexture_map_lighted(bp, &Tmap1);
1012 case 3: // perspective every pixel interpolation
1013 per2_flag = 0; // this hack means do divide every pixel
1014 ntexture_map_lighted(bp, &Tmap1);
1017 Assert(0); // Illegal value for Interpolation_method, must be 0,1,2,3
1021 Lighting_on = lighting_on_save;