TMAPZ.C 6.5 KB

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