home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / texmap / tmapz.c < prev    next >
C/C++ Source or Header  |  1998-06-08  |  7KB  |  229 lines

  1. /*
  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.
  12. */
  13. /*                                
  14.  * $Source: f:/miner/source/texmap/rcs/tmapz.c $
  15.  * $Revision: 1.3 $
  16.  * $Author: mike $
  17.  * $Date: 1994/11/28 13:34:26 $
  18.  * 
  19.  * .
  20.  * 
  21.  * $Log: tmapz.c $
  22.  * Revision 1.3  1994/11/28  13:34:26  mike
  23.  * optimizations.
  24.  * 
  25.  * Revision 1.2  1994/07/08  17:43:13  john
  26.  * Added flat-shaded-zbuffered polygon.
  27.  * 
  28.  * Revision 1.1  1994/07/08  10:45:13  john
  29.  * Initial revision
  30.  * 
  31.  * 
  32.  */
  33.  
  34.  
  35. #pragma off (unreferenced)
  36. static char rcsid[] = "$Id: tmapz.c 1.3 1994/11/28 13:34:26 mike Exp $";
  37. #pragma on (unreferenced)
  38.  
  39.  
  40. #include <math.h>
  41. // #include <graph.h>
  42. #include <limits.h>
  43. #include <stdio.h>
  44. #include <conio.h>
  45. #include <stdlib.h>
  46.  
  47. // #include "hack3df.h"
  48. #include "fix.h"
  49. #include "mono.h"
  50. #include "gr.h"
  51. #include "grdef.h"
  52. // #include "ui.h"
  53. #include "texmap.h"
  54. #include "texmapl.h"
  55. #include "error.h"
  56.  
  57. //#include "tmapext.h"
  58.  
  59. extern fix * tmap_z_buffer;
  60. extern ubyte tmap_flat_color_z;
  61. extern void asm_tmap_scanline_flat_z();
  62. extern fix compute_dz_dy(g3ds_tmap *t, int top_vertex,int bottom_vertex);
  63.  
  64. // Temporary texture map, interface from Matt's 3d system to Mike's texture mapper.
  65. extern g3ds_tmap Tmap1;
  66.  
  67. // -------------------------------------------------------------------------------------
  68. //    Texture map current scanline.
  69. //    Uses globals Du_dx and Dv_dx to incrementally compute u,v coordinates
  70. // -------------------------------------------------------------------------------------
  71. void tmap_scanline_flat_z(int y, fix xleft, fix xright, fix zleft, fix zright )
  72. {
  73.     fix    dx,recip_dx;
  74.  
  75.     dx = f2i(xright) - f2i(xleft);
  76.  
  77.     if ((dx < 0) || (xright < 0) || (xleft > xright))        // the (xleft > xright) term is not redundant with (dx < 0) because dx is computed using integers
  78.         return;
  79.  
  80.     if (dx < FIX_RECIP_TABLE_SIZE)
  81.         recip_dx = fix_recip[dx];
  82.     else
  83.         recip_dx = F1_0/dx;
  84.  
  85.     fx_y = y << 16;
  86.     fx_z = zleft;
  87.     fx_dz_dx = fixmul(zright - zleft,recip_dx);
  88.     fx_xleft = xleft;
  89.     fx_xright = xright;
  90.     
  91.     asm_tmap_scanline_flat_z();
  92.  
  93. }
  94.  
  95. //    -----------------------------------------------------------------------------------------
  96. //    This is the gr_upoly-like interface to the texture mapper which uses texture-mapper compatible
  97. //    (ie, avoids cracking) edge/delta computation.
  98.  
  99. void texture_map_flat_z(g3ds_tmap *t, int color )
  100. {
  101.     int    vlt,vrt,vlb,vrb;    // vertex left top, vertex right top, vertex left bottom, vertex right bottom
  102.     int    topy,boty,y;
  103.     fix    dx_dy_left,dx_dy_right;
  104.     fix    dz_dy_left,dz_dy_right;
  105.     int    max_y_vertex;
  106.     fix    xleft,xright;
  107.     fix    zleft,zright;
  108.     g3ds_vertex *v3d;
  109.  
  110.     v3d = t->verts;
  111.  
  112.     tmap_flat_color_z = color;
  113.  
  114.     // Determine top and bottom y coords.
  115.     compute_y_bounds(t,&vlt,&vlb,&vrt,&vrb,&max_y_vertex);
  116.  
  117.     // Set top and bottom (of entire texture map) y coordinates.
  118.     topy = f2i(v3d[vlt].y2d);
  119.     boty = f2i(v3d[max_y_vertex].y2d);
  120.  
  121.     // Set amount to change x coordinate for each advance to next scanline.
  122.     dx_dy_left = compute_dx_dy_lin(t,vlt,vlb);
  123.     dx_dy_right = compute_dx_dy_lin(t,vrt,vrb);
  124.  
  125.     dz_dy_left = compute_dz_dy(t,vlt,vlb);
  126.     dz_dy_right = compute_dz_dy(t,vrt,vrb);
  127.  
  128.      // Set initial values for x, u, v
  129.     xleft = v3d[vlt].x2d;
  130.     xright = v3d[vrt].x2d;
  131.  
  132.     zleft = v3d[vlt].z;
  133.     zright = v3d[vrt].z;
  134.  
  135.     // scan all rows in texture map from top through first break.
  136.     // @mk: Should we render the scanline for y==boty?  This violates Matt's spec.
  137.  
  138.     for (y = topy; y < boty; y++) {
  139.  
  140.         // See if we have reached the end of the current left edge, and if so, set
  141.         // new values for dx_dy and x,u,v
  142.         if (y == f2i(v3d[vlb].y2d)) {
  143.             // Handle problem of double points.  Search until y coord is different.  Cannot get
  144.             // hung in an infinite loop because we know there is a vertex with a lower y coordinate
  145.             // because in the for loop, we don't scan all spanlines.
  146.             while (y == f2i(v3d[vlb].y2d)) {
  147.                 vlt = vlb;
  148.                 vlb = prevmod(vlb,t->nv);
  149.             }
  150.             dx_dy_left = compute_dx_dy_lin(t,vlt,vlb);
  151.  
  152.             xleft = v3d[vlt].x2d;
  153.             zleft = v3d[vlt].z;
  154.  
  155.             dz_dy_left = compute_dz_dy(t,vlt,vlb);
  156.  
  157.         }
  158.  
  159.         // See if we have reached the end of the current left edge, and if so, set
  160.         // new values for dx_dy and x.  Not necessary to set new values for u,v.
  161.         if (y == f2i(v3d[vrb].y2d)) {
  162.             while (y == f2i(v3d[vrb].y2d)) {
  163.                 vrt = vrb;
  164.                 vrb = succmod(vrb,t->nv);
  165.             }
  166.             dx_dy_right = compute_dx_dy_lin(t,vrt,vrb);
  167.  
  168.             xright = v3d[vrt].x2d;
  169.             zright = v3d[vrt].z;
  170.  
  171.             dz_dy_right = compute_dz_dy(t,vrt,vrb);
  172.         }
  173.  
  174.         tmap_scanline_flat_z(y, xleft, xright, zleft, zright );
  175.  
  176.         xleft += dx_dy_left;
  177.         xright += dx_dy_right;
  178.  
  179.         zleft += dz_dy_left;
  180.         zright += dz_dy_right;
  181.  
  182.     }
  183.     tmap_scanline_flat_z(y, xleft, xright, zleft, zright );
  184. }
  185.  
  186. // -------------------------------------------------------------------------------------
  187. // Interface from Matt's data structures to Mike's texture mapper.
  188. // -------------------------------------------------------------------------------------
  189. void draw_tmap_z(grs_bitmap *bp,int nverts,g3s_point **vertbuf)
  190. {
  191.     int    i;
  192.  
  193.     //    These variables are used in system which renders texture maps which lie on one scanline as a line.
  194.     fix    div_numerator;
  195.  
  196.     Assert(nverts <= MAX_TMAP_VERTS);
  197.  
  198.     bp = NULL;
  199.     
  200.     if (tmap_z_buffer==NULL) return;
  201.  
  202.     //--now called from g3_start_frame-- init_interface_vars_to_assembler();
  203.  
  204.     // Setup texture map in Tmap1
  205.     Tmap1.nv = nverts;                        // Initialize number of vertices
  206.  
  207.     div_numerator = f1_0*3;
  208.  
  209.     for (i=0; i<nverts; i++) {
  210.         g3ds_vertex    *tvp = &Tmap1.verts[i];
  211.         g3s_point    *vp = vertbuf[i];
  212.  
  213.         tvp->x2d = vp->p3_sx;
  214.         tvp->y2d = vp->p3_sy;
  215.         tvp->z = vp->z;
  216.  
  217.         //    Check for overflow on fixdiv.  Will overflow on vp->z <= 20.  Allow only as low as 24.
  218.         //if (vp->z < 24) {
  219.         //    vp->z = 24;
  220.         //    // Int3();        // we would overflow if we divided!
  221.         //}
  222.         //tvp->z = fixdiv(div_numerator,vp->z);
  223.  
  224.     }
  225.  
  226.     texture_map_flat_z( &Tmap1, COLOR );
  227.     
  228. }
  229.