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