home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / TP3D.C < prev    next >
C/C++ Source or Header  |  1991-07-07  |  8KB  |  285 lines

  1. #include "mpmath.h"
  2. #include "fractint.h"
  3.  
  4. /* 3D Transparency Variables, MCP 5-30-91 */
  5. extern double xxmin, xxmax, yymin, yymax, zzmin, zzmax, ttmin, ttmax;
  6. extern int Transparent3D, SolidCore, NumFrames;
  7. extern unsigned CoreRed, CoreGreen, CoreBlue;
  8. extern int tpdepth, tptime, symmetry, AntiAliasing;
  9.  
  10. extern void (*StkAdd)(void);
  11.  
  12. extern int xdots, ydots, colors, bitshift;
  13. extern int row, col, inside, ShadowColors;
  14. extern int TPlusFlag, MaxColorRes, NonInterlaced;
  15.  
  16. typedef unsigned char uchar;
  17.  
  18. typedef struct palett {
  19.    uchar red;
  20.    uchar green;
  21.    uchar blue;
  22. } Palettetype;
  23.  
  24. extern Palettetype    dacbox[ 256 ];
  25.  
  26.  
  27. int far NewTPFractal, far _MathType;
  28. static double dx, dy, dz, dt;
  29. static long ldx, ldy, ldz, ldt;
  30. static long lxxmin, lxxmax, lyymin, lyymax, lzzmin, lzzmax, lttmin, lttmax;
  31.  
  32. int TranspSymmetry = 0;
  33.  
  34. /* TARGA+ prototypes */
  35. void WriteTPlusBankedPixel(int, int, unsigned long);
  36. unsigned long ReadTPlusBankedPixel(int, int);
  37. int MatchTPlusMode(unsigned xdots, unsigned ydots, unsigned MaxColorRes,
  38.            unsigned PixelZoom, unsigned NonInterlaced);
  39. int CheckForTPlus(void);
  40.  
  41. void (*PutTC)(int col, int row, unsigned long color) = WriteTPlusBankedPixel;
  42. unsigned long (*GetTC)(int col, int row) = ReadTPlusBankedPixel;
  43.  
  44. /* Fractint prototypes (I don't like warning messages! MCP) */
  45. int calcfract(void);
  46.  
  47.  
  48. char far * far TCError[] =
  49. {
  50.    "Could not match this video resolution to a TARGA+ mode",
  51.    "True Color disk video isn't implement yet, maybe later . . .",
  52. };
  53.  
  54. char far *TrueColorAutoDetect(void)
  55. {
  56.    if(CheckForTPlus())
  57.    {
  58.       if(TPlusFlag)
  59.       {
  60.          if(MaxColorRes == 8)
  61.          {
  62.             if(!MatchTPlusMode(xdots, ydots, 24, 0, NonInterlaced))
  63.                return(TCError[0]);
  64.          }
  65.          else if(!MatchTPlusMode(xdots, ydots, MaxColorRes, 0, NonInterlaced))
  66.             return(TCError[0]);
  67.  
  68.          PutTC = WriteTPlusBankedPixel;
  69.          GetTC = ReadTPlusBankedPixel;
  70.          return(0);
  71.       }
  72.    }
  73.  
  74.    /* Check for other TC adapters here, such as the XGA , and place it
  75.       in a true color mode with a resolution identical to the one selected
  76.       by the user. */
  77.  
  78.  
  79.    /* If we don't have a TC adapter or it is being used as the display
  80.       screen, then simulate one using disk video */
  81.    return(TCError[1]); /* return the 'not implemented' message */
  82. }
  83.  
  84. void TranspPerPixel(int MathType, union Arg far *xy, union Arg far *zt)
  85. {
  86.    if(NewTPFractal)
  87.    {
  88.       double Fudge = (double)(1L << bitshift);
  89.       _MathType = MathType;              /* Save this for later */
  90.  
  91.       dx = (xxmax - xxmin) / xdots;
  92.       dy = (yymin - yymax) / ydots;  /* reverse direction of y-axis */
  93.       dz = (zzmax - zzmin) / xdots;
  94.       dt = (ttmax - ttmin) / NumFrames;
  95.  
  96.       lxxmin = (long)(xxmin * Fudge);
  97.       lxxmax = (long)(xxmax * Fudge);
  98.       ldx    = (long)(dx    * Fudge);
  99.  
  100.       lyymin = (long)(yymin * Fudge);
  101.       lyymax = (long)(yymax * Fudge);
  102.       ldy    = (long)(dy    * Fudge);
  103.  
  104.       lzzmin = (long)(zzmin * Fudge);
  105.       lzzmax = (long)(zzmax * Fudge);
  106.       ldz    = (long)(dz    * Fudge);
  107.  
  108.       lttmin = (long)(ttmin * Fudge);
  109.       lttmax = (long)(ttmax * Fudge);
  110.       ldt    = (long)(dt    * Fudge);
  111.  
  112.       NewTPFractal = 0;
  113.    }
  114.  
  115.    switch(MathType)
  116.    {
  117.       /* For calculation purposes, 'x' and 'z' are swapped */
  118.       case D_MATH:
  119.            xy->d.x = xxmin + (dx * col);
  120.            xy->d.y = yymax + (dy * row);
  121.            zt->d.x = zzmin + (dz * tpdepth);
  122.            zt->d.y = ttmin + (dt * tptime);
  123.            break;
  124.       case M_MATH:
  125.            xy->m.x = *d2MP(xxmin + (dx * col));
  126.            xy->m.y = *d2MP(yymax + (dy * row));
  127.            zt->m.x = *d2MP(zzmin + (dz * tpdepth));
  128.            zt->m.y = *d2MP(ttmin + (dt * tptime));
  129.            break;
  130.       case L_MATH:
  131.            xy->l.x = lxxmin + (ldx * col);
  132.            xy->l.y = lyymax + (ldy * row);
  133.            zt->l.x = lzzmin + (ldz * tpdepth);
  134.            zt->l.y = lttmin + (ldt * tptime);
  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.