home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / PLOT3D.C < prev    next >
Text File  |  1991-04-11  |  10KB  |  372 lines

  1. /*
  2.    This file includes miscellaneous plot functions and logic
  3.    for 3D, used by lorenz.c and line3d.c
  4.    By Tim Wegner and Marc Reinig.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include "fractint.h"
  9. #include "fractype.h"
  10.  
  11. /* Use these palette indices for red/blue - same on ega/vga */
  12. #define PAL_BLUE    1
  13. #define PAL_RED     2
  14. #define PAL_MAGENTA    3
  15.  
  16. int whichimage;
  17. extern int fractype;
  18. extern int mapset;
  19. extern int xadjust;
  20. extern int yadjust;
  21. extern int xxadjust;
  22. extern int yyadjust;
  23. extern int xshift;
  24. extern int yshift;
  25. extern char MAP_name[];
  26. extern int init3d[];
  27. extern int xdots;
  28. extern int ydots;
  29. extern int colors;
  30. extern unsigned char dacbox[256][3];
  31.  
  32. int xxadjust1;
  33. int yyadjust1;
  34. int eyeseparation = 0;
  35. int glassestype = 0;
  36. int xshift1;
  37. int yshift1;
  38. int xtrans = 0;
  39. int ytrans = 0;
  40. int red_local_left;
  41. int red_local_right;
  42. int blue_local_left;
  43. int blue_local_right;
  44. int red_crop_left   = 4;
  45. int red_crop_right  = 0;
  46. int blue_crop_left  = 0;
  47. int blue_crop_right = 4;
  48. int red_bright        = 80;
  49. int blue_bright      = 100;
  50.  
  51.  
  52. /* Bresenham's algorithm for drawing line */
  53. void _fastcall draw_line (int X1, int Y1, int X2, int Y2, int color)
  54.  
  55. {                  /* uses Bresenham algorithm to draw a line */
  56.   int dX, dY;                        /* vector components */
  57.   int row, col,
  58.       final,                       /* final row or column number */
  59.       G,                   /* used to test for new row or column */
  60.       inc1,            /* G increment when row or column doesn't change */
  61.       inc2;               /* G increment when row or column changes */
  62.   char pos_slope;
  63.   extern int xdots,ydots;
  64.  
  65.   dX = X2 - X1;                    /* find vector components */
  66.   dY = Y2 - Y1;
  67.   pos_slope = (dX > 0);                    /* is slope positive? */
  68.   if (dY < 0)
  69.     pos_slope = !pos_slope;
  70.   if (abs (dX) > abs (dY))                /* shallow line case */
  71.   {
  72.     if (dX > 0)             /* determine start point and last column */
  73.     {
  74.       col = X1;
  75.       row = Y1;
  76.       final = X2;
  77.     }
  78.     else
  79.     {
  80.       col = X2;
  81.       row = Y2;
  82.       final = X1;
  83.     }
  84.     inc1 = 2 * abs (dY);           /* determine increments and initial G */
  85.     G = inc1 - abs (dX);
  86.     inc2 = 2 * (abs (dY) - abs (dX));
  87.     if (pos_slope)
  88.       while (col <= final)    /* step through columns checking for new row */
  89.       {
  90.     (*plot) (col, row, color);
  91.     col++;
  92.     if (G >= 0)                 /* it's time to change rows */
  93.     {
  94.       row++;         /* positive slope so increment through the rows */
  95.       G += inc2;
  96.     }
  97.     else                         /* stay at the same row */
  98.       G += inc1;
  99.       }
  100.     else
  101.       while (col <= final)    /* step through columns checking for new row */
  102.       {
  103.     (*plot) (col, row, color);
  104.     col++;
  105.     if (G > 0)                 /* it's time to change rows */
  106.     {
  107.       row--;         /* negative slope so decrement through the rows */
  108.       G += inc2;
  109.     }
  110.     else                         /* stay at the same row */
  111.       G += inc1;
  112.       }
  113.   }  /* if |dX| > |dY| */
  114.   else                              /* steep line case */
  115.   {
  116.     if (dY > 0)                /* determine start point and last row */
  117.     {
  118.       col = X1;
  119.       row = Y1;
  120.       final = Y2;
  121.     }
  122.     else
  123.     {
  124.       col = X2;
  125.       row = Y2;
  126.       final = Y1;
  127.     }
  128.     inc1 = 2 * abs (dX);           /* determine increments and initial G */
  129.     G = inc1 - abs (dY);
  130.     inc2 = 2 * (abs (dX) - abs (dY));
  131.     if (pos_slope)
  132.       while (row <= final)    /* step through rows checking for new column */
  133.       {
  134.     (*plot) (col, row, color);
  135.     row++;
  136.     if (G >= 0)                  /* it's time to change columns */
  137.     {
  138.       col++;      /* positive slope so increment through the columns */
  139.       G += inc2;
  140.     }
  141.     else                      /* stay at the same column */
  142.       G += inc1;
  143.       }
  144.     else
  145.       while (row <= final)    /* step through rows checking for new column */
  146.       {
  147.     (*plot) (col, row, color);
  148.     row++;
  149.     if (G > 0)                  /* it's time to change columns */
  150.     {
  151.       col--;      /* negative slope so decrement through the columns */
  152.       G += inc2;
  153.     }
  154.     else                      /* stay at the same column */
  155.       G += inc1;
  156.       }
  157.   }
  158. }  /* draw_line */
  159.  
  160.  
  161. /* use this for continuous colors later */
  162. void _fastcall plot3dsuperimpose16b(int x,int y,int color)
  163. {
  164.    int tmp;
  165.    if (color != 0)           /* Keeps index 0 still 0 */
  166.    {
  167.       color = colors - color; /*  Reverses color order */
  168.       color = color / 4;
  169.       if(color == 0)
  170.      color = 1;
  171.    }
  172.    color = 3;
  173.    tmp = getcolor(x,y);
  174.  
  175.    /* map to 4 colors */
  176.    if(whichimage == 1) /* RED */
  177.    {
  178.       if(red_local_left < x && x < red_local_right)
  179.      putcolor(x,y,color|tmp);
  180.    }
  181.    else if(whichimage == 2) /* BLUE */
  182.    {
  183.       if(blue_local_left < x && x < blue_local_right)
  184.       {
  185.      color = color <<2;
  186.      putcolor(x,y,color|tmp);
  187.       }
  188.    }
  189. }
  190.  
  191. void _fastcall plot3dsuperimpose16(int x,int y,int color)
  192. {
  193.    int tmp;
  194.  
  195.    tmp = getcolor(x,y);
  196.  
  197.    if(whichimage == 1) /* RED */
  198.    {
  199.       color = PAL_RED;
  200.       if(tmp > 0 && tmp != color)
  201.      color = PAL_MAGENTA;
  202.       if(red_local_left < x && x < red_local_right)
  203.      putcolor(x,y,color);
  204.    }
  205.    else if(whichimage == 2) /* BLUE */
  206.    {
  207.       if(blue_local_left < x && x < blue_local_right)
  208.       {
  209.      color = PAL_BLUE;
  210.      if(tmp > 0 && tmp != color)
  211.         color = PAL_MAGENTA;
  212.      putcolor(x,y,color);
  213.       }
  214.    }
  215. }
  216.  
  217. void _fastcall plot3dsuperimpose256(int x,int y,int color)
  218. {
  219.    int tmp;
  220.    if (color != 0)           /* Keeps index 0 still 0 */
  221.    {
  222.       color = colors - color; /*  Reverses color order */
  223.       color = 1 + color / 18; /*  Maps colors 1-255 to 15 even ranges */
  224.    }
  225.    tmp = getcolor(x,y);
  226.    /* map to 16 colors */
  227.    if(whichimage == 1) /* RED */
  228.    {
  229.       if(red_local_left < x && x < red_local_right)
  230.       /* Overwrite prev Red don't mess w/blue */
  231.      putcolor(x,y,color|(tmp&240));
  232.    }
  233.    else if(whichimage == 2) /* BLUE */
  234.    {
  235.       if(blue_local_left < x && x < blue_local_right)
  236.       {
  237.      /* Overwrite previous blue, don't mess with existing red */
  238.      color = color <<4;
  239.      putcolor(x,y,color|(tmp&15));
  240.       }
  241.    }
  242. }
  243.  
  244. void _fastcall plotIFS3dsuperimpose256(int x,int y,int color)
  245. {
  246.    int tmp;
  247.    if (color != 0)           /* Keeps index 0 still 0 */
  248.    {
  249.       /* my mind is fried - lower indices = darker colors is EASIER! */
  250.       color = colors - color; /*  Reverses color order */
  251.       color = 1 + color / 18; /*  Looks weird but maps colors 1-255 to 15
  252.                      relatively even ranges */
  253.    }
  254.    tmp = getcolor(x,y);
  255.    /* map to 16 colors */
  256.    if(whichimage == 1) /* RED */
  257.    {
  258.       if(red_local_left < x && x < red_local_right)
  259.      putcolor(x,y,color|tmp);
  260.    }
  261.    else if(whichimage == 2) /* BLUE */
  262.    {
  263.       if(blue_local_left < x && x < blue_local_right)
  264.       {
  265.      color = color <<4;
  266.      putcolor(x,y,color|tmp);
  267.       }
  268.    }
  269. }
  270.  
  271. void _fastcall plot3dalternate(int x,int y,int color)
  272. {
  273.    /* lorez high color red/blue 3D plot function */
  274.    /* if which image = 1, compresses color to lower 128 colors */
  275.  
  276.    /* my mind is STILL fried - lower indices = darker colors is EASIER! */
  277.    color = colors - color;
  278.    if((whichimage == 1) && !((x+y)&1)) /* - lower half palette */
  279.    {
  280.       if(red_local_left < x && x < red_local_right)
  281.      putcolor(x,y,color>>1);
  282.    }
  283.    else if((whichimage == 2) && ((x+y)&1) ) /* - upper half palette */
  284.    {
  285.       if(blue_local_left < x && x < blue_local_right)
  286.      putcolor(x,y,(color>>1)+(colors>>1));
  287.    }
  288. }
  289.  
  290. void plot_setup()
  291. {
  292.    double d_red_bright, d_blue_bright;
  293.    int i;
  294.  
  295.    /* set funny glasses plot function */
  296.    switch(glassestype)
  297.    {
  298.    case 1:
  299.       standardplot = plot3dalternate;
  300.       break;
  301.    case 2:
  302.       if(colors == 256)
  303.      if (fractype != IFS3D)
  304.         standardplot = plot3dsuperimpose256;
  305.      else
  306.         standardplot = plotIFS3dsuperimpose256;
  307.       else
  308.      standardplot = plot3dsuperimpose16;
  309.       break;
  310.    default:
  311.       standardplot = putcolor;
  312.       break;
  313.    }
  314.  
  315.    xshift1 = xshift = (XSHIFT * (double)xdots)/100;
  316.    yshift1 = yshift = (YSHIFT * (double)ydots)/100;
  317.  
  318.    if(glassestype)
  319.    {
  320.       red_local_left   =    (red_crop_left         * (double)xdots)/100.0;
  321.       red_local_right  =    ((100 - red_crop_right)  * (double)xdots)/100.0;
  322.       blue_local_left  =    (blue_crop_left         * (double)xdots)/100.0;
  323.       blue_local_right =    ((100 - blue_crop_right) * (double)xdots)/100.0;
  324.       d_red_bright     =    (double)red_bright/100.0;
  325.       d_blue_bright    =    (double)blue_bright/100.0;
  326.  
  327.       switch(whichimage)
  328.       {
  329.       case 1:
  330.      xshift  += (eyeseparation* (double)xdots)/200;
  331.      xxadjust = ((xtrans+xadjust)* (double)xdots)/100;
  332.      xshift1  -= (eyeseparation* (double)xdots)/200;
  333.      xxadjust1 = ((xtrans-xadjust)* (double)xdots)/100;
  334.      break;
  335.       case 2:
  336.      xshift  -= (eyeseparation* (double)xdots)/200;
  337.      xxadjust = ((xtrans-xadjust)* (double)xdots)/100;
  338.      break;
  339.       }
  340.    }
  341.    else
  342.       xxadjust = (xtrans* (double)xdots)/100;
  343.    yyadjust = -(ytrans* (double)ydots)/100;
  344.  
  345.    if (mapset) {
  346.       ValidateLuts(MAP_name);  /* read the palette file */
  347.       if(glassestype==1 || glassestype==2)
  348.       {
  349.      if(glassestype == 2 && colors < 256)
  350.      {
  351.         dacbox[PAL_RED    ][0] = 63;
  352.         dacbox[PAL_RED    ][1] =  0;
  353.         dacbox[PAL_RED    ][2] =  0;
  354.  
  355.         dacbox[PAL_BLUE   ][0] =  0;
  356.         dacbox[PAL_BLUE   ][1] =  0;
  357.         dacbox[PAL_BLUE   ][2] = 63;
  358.  
  359.         dacbox[PAL_MAGENTA][0] = 63;
  360.         dacbox[PAL_MAGENTA][1] =  0;
  361.         dacbox[PAL_MAGENTA][2] = 63;
  362.      }
  363.      for (i=0;i<256;i++)
  364.      {
  365.         dacbox[i][0] = dacbox[i][0] * d_red_bright;
  366.         dacbox[i][2] = dacbox[i][2] * d_blue_bright;
  367.      }
  368.       }
  369.       spindac(0,1); /* load it, but don't spin */
  370.    }
  371. }
  372.