home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quake_src / d_edge.c < prev    next >
C/C++ Source or Header  |  2000-06-17  |  9KB  |  347 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // d_edge.c
  21.  
  22. #include "quakedef.h"
  23. #include "d_local.h"
  24.  
  25. #ifndef __STORM__
  26. #ifdef PPCASM
  27. #define VBCCPPCASM
  28. #endif
  29. #endif
  30.  
  31. #if !defined(M68KASM) && !defined(VBCCPPCASM)
  32. static int  miplevel;
  33. #else
  34. int miplevel;
  35. #endif
  36.  
  37. float   scale_for_mip;
  38. int     screenwidth;
  39. int     ubasestep, errorterm, erroradjustup, erroradjustdown;
  40. int     vstartscan;
  41.  
  42. // FIXME: should go away
  43. extern void     R_RotateBmodel (void);
  44. extern void     R_TransformFrustum (void);
  45.  
  46. #if defined(M68KASM) || defined(VBCCPPCASM)
  47. extern void D_CalcGradients(msurface_t *);
  48. #endif
  49.  
  50. vec3_t    transformed_modelorg;
  51.  
  52. /*
  53. ==============
  54. D_DrawPoly
  55.  
  56. ==============
  57. */
  58. void D_DrawPoly (void)
  59. {
  60. // this driver takes spans, not polygons
  61. }
  62.  
  63.  
  64. /*
  65. =============
  66. D_MipLevelForScale
  67. =============
  68. */
  69. int D_MipLevelForScale (float scale)
  70. {
  71.   int   lmiplevel;
  72.  
  73.   if (scale >= d_scalemip[0] )
  74.     lmiplevel = 0;
  75.   else if (scale >= d_scalemip[1] )
  76.     lmiplevel = 1;
  77.   else if (scale >= d_scalemip[2] )
  78.     lmiplevel = 2;
  79.   else
  80.     lmiplevel = 3;
  81.  
  82.   if (lmiplevel < d_minmip)
  83.     lmiplevel = d_minmip;
  84.  
  85.   return lmiplevel;
  86. }
  87.  
  88.  
  89. /*
  90. ==============
  91. D_DrawSolidSurface
  92. ==============
  93. */
  94.  
  95. // FIXME: clean this up
  96.  
  97. void D_DrawSolidSurface (surf_t *surf, int color)
  98. {
  99.   espan_t *span;
  100.   byte  *pdest;
  101.   int   u, u2, pix;
  102.   
  103.   pix = (color<<24) | (color<<16) | (color<<8) | color;
  104.   for (span=surf->spans ; span ; span=span->pnext)
  105.   {
  106.     pdest = (byte *)d_viewbuffer + screenwidth*span->v;
  107.     u = span->u;
  108.     u2 = span->u + span->count - 1;
  109.     ((byte *)pdest)[u] = pix;
  110.  
  111.     if (u2 - u < 8)
  112.     {
  113.       for (u++ ; u <= u2 ; u++)
  114.         ((byte *)pdest)[u] = pix;
  115.     }
  116.     else
  117.     {
  118.       for (u++ ; u & 3 ; u++)
  119.         ((byte *)pdest)[u] = pix;
  120.  
  121.       u2 -= 4;
  122.       for ( ; u <= u2 ; u+=4)
  123.         *(int *)((byte *)pdest + u) = pix;
  124.       u2 += 4;
  125.       for ( ; u <= u2 ; u++)
  126.         ((byte *)pdest)[u] = pix;
  127.     }
  128.   }
  129. }
  130.  
  131.  
  132. #if !defined(M68KASM) && !defined(VBCCPPCASM)
  133. /*
  134. ==============
  135. D_CalcGradients
  136. ==============
  137. */
  138. void D_CalcGradients (msurface_t *pface)
  139. {
  140.   mplane_t  *pplane;
  141.   float   mipscale;
  142.   vec3_t    p_temp1;
  143.   vec3_t    p_saxis, p_taxis;
  144.   float   t;
  145.  
  146.   pplane = pface->plane;
  147.  
  148.   mipscale = 1.0 / (float)(1 << miplevel);
  149.  
  150.   TransformVector (pface->texinfo->vecs[0], p_saxis);
  151.   TransformVector (pface->texinfo->vecs[1], p_taxis);
  152.  
  153.   t = xscaleinv * mipscale;
  154.   d_sdivzstepu = p_saxis[0] * t;
  155.   d_tdivzstepu = p_taxis[0] * t;
  156.  
  157.   t = yscaleinv * mipscale;
  158.   d_sdivzstepv = -p_saxis[1] * t;
  159.   d_tdivzstepv = -p_taxis[1] * t;
  160.  
  161.   d_sdivzorigin = p_saxis[2] * mipscale - xcenter * d_sdivzstepu -
  162.       ycenter * d_sdivzstepv;
  163.   d_tdivzorigin = p_taxis[2] * mipscale - xcenter * d_tdivzstepu -
  164.       ycenter * d_tdivzstepv;
  165.  
  166.   VectorScale (transformed_modelorg, mipscale, p_temp1);
  167.  
  168.   t = 0x10000*mipscale;
  169.   sadjust = ((fixed16_t)(DotProduct (p_temp1, p_saxis) * 0x10000 + 0.5)) -
  170.       ((pface->texturemins[0] << 16) >> miplevel)
  171.       + pface->texinfo->vecs[0][3]*t;
  172.   tadjust = ((fixed16_t)(DotProduct (p_temp1, p_taxis) * 0x10000 + 0.5)) -
  173.       ((pface->texturemins[1] << 16) >> miplevel)
  174.       + pface->texinfo->vecs[1][3]*t;
  175.  
  176. //
  177. // -1 (-epsilon) so we never wander off the edge of the texture
  178. //
  179.   bbextents = ((pface->extents[0] << 16) >> miplevel) - 1;
  180.   bbextentt = ((pface->extents[1] << 16) >> miplevel) - 1;
  181. }
  182.  
  183.  
  184. /*
  185. ==============
  186. D_DrawSurfaces
  187. ==============
  188. */
  189. void D_DrawSurfaces (void)
  190. {
  191.   surf_t      *s;
  192.   msurface_t    *pface;
  193.   surfcache_t   *pcurrentcache;
  194.   vec3_t      world_transformed_modelorg;
  195.   vec3_t      local_modelorg;
  196.  
  197.   currententity = &cl_entities[0];
  198.   TransformVector (modelorg, transformed_modelorg);
  199.   VectorCopy (transformed_modelorg, world_transformed_modelorg);
  200.  
  201. // TODO: could preset a lot of this at mode set time
  202.   if (r_drawflat.value)
  203.   {
  204.     for (s = &surfaces[1] ; s<surface_p ; s++)
  205.     {
  206.       if (!s->spans)
  207.         continue;
  208.  
  209.       d_zistepu = s->d_zistepu;
  210.       d_zistepv = s->d_zistepv;
  211.       d_ziorigin = s->d_ziorigin;
  212.  
  213.       D_DrawSolidSurface (s, (int)s->data & 0xFF);
  214.       D_DrawZSpans (s->spans);
  215.     }
  216.   }
  217.   else
  218.   {
  219.     for (s = &surfaces[1] ; s<surface_p ; s++)
  220.     {
  221.       if (!s->spans)
  222.         continue;
  223.  
  224.       r_drawnpolycount++;
  225.  
  226.       d_zistepu = s->d_zistepu;
  227.       d_zistepv = s->d_zistepv;
  228.       d_ziorigin = s->d_ziorigin;
  229.  
  230.       if (s->flags & SURF_DRAWSKY)
  231.       {
  232.         if (!r_skymade)
  233.         {
  234.           R_MakeSky ();
  235.         }
  236.  
  237.         D_DrawSkyScans8 (s->spans);
  238.         D_DrawZSpans (s->spans);
  239.       }
  240.       else if (s->flags & SURF_DRAWBACKGROUND)
  241.       {
  242.       // set up a gradient for the background surface that places it
  243.       // effectively at infinity distance from the viewpoint
  244.         d_zistepu = 0;
  245.         d_zistepv = 0;
  246.         d_ziorigin = -0.9;
  247.  
  248.         D_DrawSolidSurface (s, (int)r_clearcolor.value & 0xFF);
  249.         D_DrawZSpans (s->spans);
  250.       }
  251.       else if (s->flags & SURF_DRAWTURB)
  252.       {
  253.         pface = s->data;
  254.         miplevel = 0;
  255.         cacheblock = (pixel_t *)
  256.             ((byte *)pface->texinfo->texture +
  257.             pface->texinfo->texture->offsets[0]);
  258.         cachewidth = 64;
  259.  
  260.         if (s->insubmodel)
  261.         {
  262.         // FIXME: we don't want to do all this for every polygon!
  263.         // TODO: store once at start of frame
  264.           currententity = s->entity;  //FIXME: make this passed in to
  265.                         // R_RotateBmodel ()
  266.           VectorSubtract (r_origin, currententity->origin,
  267.               local_modelorg);
  268.           TransformVector (local_modelorg, transformed_modelorg);
  269.  
  270.           R_RotateBmodel ();  // FIXME: don't mess with the frustum,
  271.                     // make entity passed in
  272.         }
  273.  
  274.         D_CalcGradients (pface);
  275.         Turbulent8 (s->spans);
  276.         D_DrawZSpans (s->spans);
  277.  
  278.         if (s->insubmodel)
  279.         {
  280.         //
  281.         // restore the old drawing state
  282.         // FIXME: we don't want to do this every time!
  283.         // TODO: speed up
  284.         //
  285.           currententity = &cl_entities[0];
  286.           VectorCopy (world_transformed_modelorg,
  287.                 transformed_modelorg);
  288.           VectorCopy (base_vpn, vpn);
  289.           VectorCopy (base_vup, vup);
  290.           VectorCopy (base_vright, vright);
  291.           VectorCopy (base_modelorg, modelorg);
  292.           R_TransformFrustum ();
  293.         }
  294.       }
  295.       else
  296.       {
  297.         if (s->insubmodel)
  298.         {
  299.         // FIXME: we don't want to do all this for every polygon!
  300.         // TODO: store once at start of frame
  301.           currententity = s->entity;  //FIXME: make this passed in to
  302.                         // R_RotateBmodel ()
  303.           VectorSubtract (r_origin, currententity->origin, local_modelorg);
  304.           TransformVector (local_modelorg, transformed_modelorg);
  305.  
  306.           R_RotateBmodel ();  // FIXME: don't mess with the frustum,
  307.                     // make entity passed in
  308.         }
  309.  
  310.         pface = s->data;
  311.         miplevel = D_MipLevelForScale (s->nearzi * scale_for_mip
  312.         * pface->texinfo->mipadjust);
  313.  
  314.       // FIXME: make this passed in to D_CacheSurface
  315.         pcurrentcache = D_CacheSurface (pface, miplevel);
  316.  
  317.         cacheblock = (pixel_t *)pcurrentcache->data;
  318.         cachewidth = pcurrentcache->width;
  319.  
  320.         D_CalcGradients (pface);
  321.  
  322.         (*d_drawspans) (s->spans);
  323.  
  324.         D_DrawZSpans (s->spans);
  325.  
  326.         if (s->insubmodel)
  327.         {
  328.         //
  329.         // restore the old drawing state
  330.         // FIXME: we don't want to do this every time!
  331.         // TODO: speed up
  332.         //
  333.           currententity = &cl_entities[0];
  334.           VectorCopy (world_transformed_modelorg,
  335.                 transformed_modelorg);
  336.           VectorCopy (base_vpn, vpn);
  337.           VectorCopy (base_vup, vup);
  338.           VectorCopy (base_vright, vright);
  339.           VectorCopy (base_modelorg, modelorg);
  340.           R_TransformFrustum ();
  341.         }
  342.       }
  343.     }
  344.   }
  345. }
  346. #endif
  347.