home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / frasrc18.zip / TP3D.C < prev    next >
C/C++ Source or Header  |  1992-07-10  |  8KB  |  285 lines

  1. #include <stdio.h>
  2. #include "mpmath.h"
  3. #include "fractint.h"
  4. #include "prototyp.h"
  5.  
  6. /* 3D Transparency Variables, MCP 5-30-91 */
  7. extern double xxmin, xxmax, yymin, yymax, zzmin, zzmax, ttmin, ttmax;
  8. extern int Transparent3D, SolidCore, NumFrames;
  9. extern unsigned CoreRed, CoreGreen, CoreBlue;
  10. extern int tpdepth, tptime, symmetry, AntiAliasing;
  11.  
  12. extern int xdots, ydots, colors, bitshift;
  13. extern int row, col, inside, ShadowColors;
  14. extern int TPlusFlag, MaxColorRes, NonInterlaced;
  15.  
  16. typedef struct palett {
  17.    BYTE red;
  18.    BYTE green;
  19.    BYTE blue;
  20. } Palettetype;
  21.  
  22. extern Palettetype    dacbox[ 256 ];
  23.  
  24.  
  25. int far NewTPFractal, far _MathType;
  26. static double dx, dy, dz, dt;
  27. static long ldx, ldy, ldz, ldt;
  28. static long lxxmin, lxxmax, lyymin, lyymax, lzzmin, lzzmax, lttmin, lttmax;
  29.  
  30. int TranspSymmetry = 0;
  31.  
  32. /* TARGA+ prototypes */
  33. int MatchTPlusMode(unsigned xdots, unsigned ydots, unsigned MaxColorRes,
  34.            unsigned PixelZoom, unsigned NonInterlaced);
  35. int CheckForTPlus(void);
  36.  
  37. #ifndef XFRACT
  38. void (*PutTC)(int col, int row, unsigned long color) = WriteTPlusBankedPixel;
  39. unsigned long (*GetTC)(int col, int row) = ReadTPlusBankedPixel;
  40. #else
  41. void (*PutTC)(int , int , unsigned long ) = WriteTPlusBankedPixel;
  42. unsigned long (*GetTC)(int , int ) = ReadTPlusBankedPixel;
  43. #endif
  44.  
  45.  
  46. char far * far TCError[] =
  47. {
  48.    "Could not match this video resolution to a TARGA+ mode",
  49.    "True Color disk video isn't implement yet, maybe later . . .",
  50. };
  51.  
  52. char far *TrueColorAutoDetect(void)
  53. {
  54.    if(CheckForTPlus())
  55.    {
  56.       if(TPlusFlag)
  57.       {
  58.          if(MaxColorRes == 8)
  59.          {
  60.             if(!MatchTPlusMode(xdots, ydots, 24, 0, NonInterlaced))
  61.                return(TCError[0]);
  62.          }
  63.          else if(!MatchTPlusMode(xdots, ydots, MaxColorRes, 0, NonInterlaced))
  64.             return(TCError[0]);
  65.  
  66.          PutTC = WriteTPlusBankedPixel;
  67.          GetTC = ReadTPlusBankedPixel;
  68.          return(0);
  69.       }
  70.    }
  71.  
  72.    /* Check for other TC adapters here, such as the XGA , and place it
  73.       in a true color mode with a resolution identical to the one selected
  74.       by the user. */
  75.  
  76.  
  77.    /* If we don't have a TC adapter or it is being used as the display
  78.       screen, then simulate one using disk video */
  79.    return(TCError[1]); /* return the 'not implemented' message */
  80. }
  81.  
  82. void TranspPerPixel(int MathType, union Arg far *xy, union Arg far *zt)
  83. {
  84.    if(NewTPFractal)
  85.    {
  86.       double Fudge = (double)(1L << bitshift);
  87.       _MathType = MathType;              /* Save this for later */
  88.  
  89.       dx = (xxmax - xxmin) / xdots;
  90.       dy = (yymin - yymax) / ydots;  /* reverse direction of y-axis */
  91.       dz = (zzmax - zzmin) / xdots;
  92.       dt = (ttmax - ttmin) / NumFrames;
  93.  
  94.       lxxmin = (long)(xxmin * Fudge);
  95.       lxxmax = (long)(xxmax * Fudge);
  96.       ldx    = (long)(dx    * Fudge);
  97.  
  98.       lyymin = (long)(yymin * Fudge);
  99.       lyymax = (long)(yymax * Fudge);
  100.       ldy    = (long)(dy    * Fudge);
  101.  
  102.       lzzmin = (long)(zzmin * Fudge);
  103.       lzzmax = (long)(zzmax * Fudge);
  104.       ldz    = (long)(dz    * Fudge);
  105.  
  106.       lttmin = (long)(ttmin * Fudge);
  107.       lttmax = (long)(ttmax * Fudge);
  108.       ldt    = (long)(dt    * Fudge);
  109.  
  110.       NewTPFractal = 0;
  111.    }
  112.  
  113.    switch(MathType)
  114.    {
  115.       /* For calculation purposes, 'x' and 'z' are swapped */
  116.       case D_MATH:
  117.            xy->d.x = xxmin + (dx * col);
  118.            xy->d.y = yymax + (dy * row);
  119.            zt->d.x = zzmin + (dz * tpdepth);
  120.            zt->d.y = ttmin + (dt * tptime);
  121.            break;
  122. #ifndef XFRACT
  123.       case M_MATH:
  124.            xy->m.x = *d2MP(xxmin + (dx * col));
  125.            xy->m.y = *d2MP(yymax + (dy * row));
  126.            zt->m.x = *d2MP(zzmin + (dz * tpdepth));
  127.            zt->m.y = *d2MP(ttmin + (dt * tptime));
  128.            break;
  129.       case L_MATH:
  130.            xy->l.x = lxxmin + (ldx * col);
  131.            xy->l.y = lyymax + (ldy * row);
  132.            zt->l.x = lzzmin + (ldz * tpdepth);
  133.            zt->l.y = lttmin + (ldt * tptime);
  134. #endif
  135.    }
  136. }
  137.  
  138.  
  139. int Transp3DFnct()
  140. {
  141.    unsigned x, y, z, savedinside;
  142.    int r;
  143.    unsigned long lcolor;
  144.  
  145.    for(x = tpdepth;  x < xdots; x++, tpdepth++)
  146.    {
  147.       /* Here we go!  Calculate 2D slices to create a 3D composite */
  148.       savedinside = inside;
  149.       inside = 255;
  150.       r = calcfract();
  151.       inside = savedinside;
  152.  
  153.       if(r < 0)
  154.          return(r);                 /* return if iterupted */
  155.  
  156.       for(y = 0; y < ydots; y++)
  157.       {
  158.          unsigned long Red = 0L, Green = 0L, Blue = 0L;
  159.          unsigned long r = 0L, g = 0L, b = 0L;
  160.          unsigned color;
  161.  
  162.          for(z = 0; z < xdots; z++)  /* Total the color guns */
  163.          {
  164.             color = (*getcolor)(z, y);
  165.             if(color == 255 && SolidCore)
  166.             {                           /* Display a solid core */
  167.                r = ((long)CoreRed)   * (xdots - z) / xdots;
  168.                g = ((long)CoreGreen) * (xdots - z) / xdots;
  169.                b = ((long)CoreBlue)  * (xdots - z) / xdots;
  170.                break;
  171.             }
  172.             else
  173.             {
  174.                Red   += (dacbox[color].red << 2);
  175.                Green += (dacbox[color].green << 2);
  176.                Blue  += (dacbox[color].blue << 2);
  177.             }
  178.          }
  179.  
  180.          /* Calculate an average color */
  181.          if(z)
  182.          {
  183.             Red /= z;
  184.             Green /= z;
  185.             Blue /= z;
  186.          }
  187.  
  188.          /* Overlay solid color */
  189.          Red   |= r;
  190.          Green |= g;
  191.          Blue  |= b;
  192.  
  193.          lcolor = (Red << 16) + (Green << 8) + Blue;
  194.          PutTC(x, y, lcolor);
  195.          if(TranspSymmetry == ORIGIN)
  196.             PutTC(xdots - x - 1, ydots - y - 1, lcolor);
  197.          else if(TranspSymmetry == XAXIS)
  198.          {
  199.             PutTC(x, ydots - y - 1, lcolor);
  200.             PutTC(xdots - x - 1, y, lcolor);
  201.             PutTC(xdots - x - 1, ydots - y - 1, lcolor);
  202.             if(y > (ydots >> 1))
  203.                break;
  204.          }
  205.       }
  206.       if(x > (xdots >> 1))
  207.       {
  208.          if(TranspSymmetry == ORIGIN || TranspSymmetry == XAXIS)
  209.             break;
  210.       }
  211.    }
  212.    return(0);
  213. }
  214.  
  215. void ShadowPutColor(unsigned xdot, unsigned ydot, unsigned color)
  216. {
  217.    ShadowVideo(0);
  218.    if(ShadowColors)
  219.       putcolor(xdot >> AntiAliasing, ydot >> AntiAliasing, color);
  220.    else
  221.    {
  222.       unsigned r, g, b;
  223.       unsigned long lcolor;
  224.  
  225.       r = (dacbox[color].red << 2);
  226.       g = (dacbox[color].green << 2);
  227.       b = (dacbox[color].blue << 2);
  228.       lcolor = ((long)r << 16) + (g << 8) + b;
  229.       PutTC(xdot >> AntiAliasing, ydot >> AntiAliasing, lcolor);
  230.    }
  231.    ShadowVideo(1);
  232. }
  233.  
  234. void AntiAliasPass(void)
  235. {
  236.    unsigned x, y, i, j, PixelSize, a;
  237.    unsigned xAct, yAct, xRef, yRef;
  238.  
  239.    PixelSize = (1 << AntiAliasing);
  240.    xAct = (xdots >> AntiAliasing);
  241.    yAct = (ydots >> AntiAliasing);
  242.  
  243.    for(yRef = y = 0; y < yAct; y++, yRef += PixelSize)
  244.    {
  245.       for(xRef = x = 0; x < xAct; x++, xRef += PixelSize)
  246.       {
  247.          unsigned total = 0;
  248.          unsigned long r = 0, g = 0, b = 0, lcolor;
  249.  
  250.          for(i = 0; i < PixelSize; i++)
  251.          {
  252.             for(j = 0; j < PixelSize; j++)
  253.             {
  254.                unsigned color;
  255.  
  256.                color = readdisk(xRef + i, yRef + j);
  257.                if(ShadowColors)
  258.                   total += color;
  259.                else
  260.                {
  261.                   r += (dacbox[color].red << 2);
  262.                   g += (dacbox[color].green << 2);
  263.                   b += (dacbox[color].blue << 2);
  264.                }
  265.             }
  266.          }
  267.  
  268.          a = AntiAliasing * AntiAliasing;
  269.          ShadowVideo(0);
  270.          if(ShadowColors)
  271.             putcolor(x, y, total >> a);
  272.          else
  273.          {
  274.             r >>= a;
  275.             g >>= a;
  276.             b >>= a;
  277.             lcolor = ((long)r << 16) + (g << 8) + b;
  278.             PutTC(x, y, lcolor);
  279.          }
  280.          ShadowVideo(1);
  281.       }
  282.    }
  283. }
  284.  
  285.