home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / winsrc.zip / PLOT3D.C < prev    next >
Text File  |  1990-11-07  |  11KB  |  421 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. extern int dotmode;
  32. extern void (*standardplot)();
  33.  
  34. int xxadjust1;
  35. int yyadjust1;
  36. int eyeseparation = 0;
  37. int glassestype = 0;
  38. int xshift1;
  39. int yshift1;
  40. int xtrans = 0;
  41. int ytrans = 0;
  42. int red_local_left;
  43. int red_local_right;
  44. int blue_local_left;
  45. int blue_local_right;
  46. int red_crop_left   = 4;
  47. int red_crop_right  = 0;
  48. int blue_crop_left  = 0;
  49. int blue_crop_right = 4;
  50. int red_bright        = 80;
  51. int blue_bright      = 100;
  52.  
  53. /* Bresenham's algorithm for drawing line */
  54. void draw_line (int X1, int Y1, int X2, int Y2, int color)
  55.  
  56. {                  /* uses Bresenham algorithm to draw a line */
  57.   int dX, dY;                        /* vector components */
  58.   int row, col,
  59.       final,                       /* final row or column number */
  60.       G,                   /* used to test for new row or column */
  61.       inc1,            /* G increment when row or column doesn't change */
  62.       inc2;               /* G increment when row or column changes */
  63.   char pos_slope;
  64.   extern int xdots,ydots;
  65.   extern int (*plot)();
  66.  
  67.   dX = X2 - X1;                    /* find vector components */
  68.   dY = Y2 - Y1;
  69.   pos_slope = (dX > 0);                    /* is slope positive? */
  70.   if (dY < 0)
  71.     pos_slope = !pos_slope;
  72.   if (abs (dX) > abs (dY))                /* shallow line case */
  73.   {
  74.     if (dX > 0)             /* determine start point and last column */
  75.     {
  76.       col = X1;
  77.       row = Y1;
  78.       final = X2;
  79.     }
  80.     else
  81.     {
  82.       col = X2;
  83.       row = Y2;
  84.       final = X1;
  85.     }
  86.     inc1 = 2 * abs (dY);           /* determine increments and initial G */
  87.     G = inc1 - abs (dX);
  88.     inc2 = 2 * (abs (dY) - abs (dX));
  89.     if (pos_slope)
  90.       while (col <= final)    /* step through columns checking for new row */
  91.       {
  92.     (*plot) (col, row, color);
  93.     col++;
  94.     if (G >= 0)                 /* it's time to change rows */
  95.     {
  96.       row++;         /* positive slope so increment through the rows */
  97.       G += inc2;
  98.     }
  99.     else                         /* stay at the same row */
  100.       G += inc1;
  101.       }
  102.     else
  103.       while (col <= final)    /* step through columns checking for new row */
  104.       {
  105.     (*plot) (col, row, color);
  106.     col++;
  107.     if (G > 0)                 /* it's time to change rows */
  108.     {
  109.       row--;         /* negative slope so decrement through the rows */
  110.       G += inc2;
  111.     }
  112.     else                         /* stay at the same row */
  113.       G += inc1;
  114.       }
  115.   }  /* if |dX| > |dY| */
  116.   else                              /* steep line case */
  117.   {
  118.     if (dY > 0)                /* determine start point and last row */
  119.     {
  120.       col = X1;
  121.       row = Y1;
  122.       final = Y2;
  123.     }
  124.     else
  125.     {
  126.       col = X2;
  127.       row = Y2;
  128.       final = Y1;
  129.     }
  130.     inc1 = 2 * abs (dX);           /* determine increments and initial G */
  131.     G = inc1 - abs (dY);
  132.     inc2 = 2 * (abs (dX) - abs (dY));
  133.     if (pos_slope)
  134.       while (row <= final)    /* step through rows checking for new column */
  135.       {
  136.     (*plot) (col, row, color);
  137.     row++;
  138.     if (G >= 0)                  /* it's time to change columns */
  139.     {
  140.       col++;      /* positive slope so increment through the columns */
  141.       G += inc2;
  142.     }
  143.     else                      /* stay at the same column */
  144.       G += inc1;
  145.       }
  146.     else
  147.       while (row <= final)    /* step through rows checking for new column */
  148.       {
  149.     (*plot) (col, row, color);
  150.     row++;
  151.     if (G > 0)                  /* it's time to change columns */
  152.     {
  153.       col--;      /* negative slope so decrement through the columns */
  154.       G += inc2;
  155.     }
  156.     else                      /* stay at the same column */
  157.       G += inc1;
  158.       }
  159.   }
  160. }  /* draw_line */
  161.  
  162. /* use this for continuous colors later */
  163. void plot3dsuperimpose16b(int x,int y,int color)
  164. {
  165.    int tmp;
  166.    if (color != 0)           /* Keeps index 0 still 0 */
  167.    {
  168.       color = colors - color; /*  Reverses color order */
  169.       color = color / 4;
  170.       if(color == 0)
  171.      color = 1;
  172.    }
  173.    color = 3;
  174.    tmp = getcolor(x,y);
  175.  
  176.    /* map to 4 colors */
  177.    if(whichimage == 1) /* RED */
  178.    {
  179.       if(red_local_left < x && x < red_local_right)
  180.      putcolor(x,y,color|tmp);
  181.    }
  182.    else if(whichimage == 2) /* BLUE */
  183.    {
  184.       if(blue_local_left < x && x < blue_local_right)
  185.       {
  186.      color = color <<2;
  187.      putcolor(x,y,color|tmp);
  188.       }
  189.    }
  190. }
  191.  
  192. void plot3dsuperimpose16(int x,int y,int color)
  193. {
  194.    int tmp;
  195.  
  196.    tmp = getcolor(x,y);
  197.  
  198.    if(whichimage == 1) /* RED */
  199.    {
  200.       color = PAL_RED;
  201.       if(tmp > 0 && tmp != color)
  202.      color = PAL_MAGENTA;
  203.       if(red_local_left < x && x < red_local_right)
  204.      putcolor(x,y,color);
  205.    }
  206.    else if(whichimage == 2) /* BLUE */
  207.    {
  208.       if(blue_local_left < x && x < blue_local_right)
  209.       {
  210.      color = PAL_BLUE;
  211.      if(tmp > 0 && tmp != color)
  212.         color = PAL_MAGENTA;
  213.      putcolor(x,y,color);
  214.       }
  215.    }
  216. }
  217.  
  218. void plot3dsuperimpose256(x,y,color)
  219. {
  220.    int tmp;
  221.    if (color != 0)           /* Keeps index 0 still 0 */
  222.    {
  223.       color = colors - color; /*  Reverses color order */
  224.       color = 1 + color / 18; /*  Maps colors 1-255 to 15 even ranges */
  225.    }
  226.    tmp = getcolor(x,y);
  227.    /* map to 16 colors */
  228.    if(whichimage == 1) /* RED */
  229.    {
  230.       if(red_local_left < x && x < red_local_right)
  231.       /* Overwrite prev Red don't mess w/blue */
  232.      putcolor(x,y,color|(tmp&240));
  233.    }
  234.    else if(whichimage == 2) /* BLUE */
  235.    {
  236.       if(blue_local_left < x && x < blue_local_right)
  237.       {
  238.      /* Overwrite previous blue, don't mess with existing red */
  239.      color = color <<4;
  240.      putcolor(x,y,color|(tmp&15));
  241.       }
  242.    }
  243. }
  244.  
  245. void plotIFS3dsuperimpose256(x,y,color)
  246. {
  247.    int tmp;
  248.    if (color != 0)           /* Keeps index 0 still 0 */
  249.    {
  250.       /* my mind is fried - lower indices = darker colors is EASIER! */
  251.       color = colors - color; /*  Reverses color order */
  252.       color = 1 + color / 18; /*  Looks weird but maps colors 1-255 to 15
  253.                      relatively even ranges */
  254.    }
  255.    tmp = getcolor(x,y);
  256.    /* map to 16 colors */
  257.    if(whichimage == 1) /* RED */
  258.    {
  259.       if(red_local_left < x && x < red_local_right)
  260.      putcolor(x,y,color|tmp);
  261.    }
  262.    else if(whichimage == 2) /* BLUE */
  263.    {
  264.       if(blue_local_left < x && x < blue_local_right)
  265.       {
  266.      color = color <<4;
  267.      putcolor(x,y,color|tmp);
  268.       }
  269.    }
  270. }
  271.  
  272. void plot3dalternate(x,y,color)
  273. {
  274.    /* lorez high color red/blue 3D plot function */
  275.    /* if which image = 1, compresses color to lower 128 colors */
  276.  
  277.    /* my mind is STILL fried - lower indices = darker colors is EASIER! */
  278.    color = colors - color;
  279.    if((whichimage == 1) && !((x+y)&1)) /* - lower half palette */
  280.    {
  281.       if(red_local_left < x && x < red_local_right)
  282.      putcolor(x,y,color>>1);
  283.    }
  284.    else if((whichimage == 2) && ((x+y)&1) ) /* - upper half palette */
  285.    {
  286.       if(blue_local_left < x && x < blue_local_right)
  287.      putcolor(x,y,(color>>1)+(colors>>1));
  288.    }
  289. }
  290.  
  291. void plot_setup()
  292. {
  293.    double d_red_bright, d_blue_bright;
  294.    int i;
  295.  
  296.    /* set funny glasses plot function */
  297.    switch(glassestype)
  298.    {
  299.    case 1:
  300.       standardplot = plot3dalternate;
  301.       break;
  302.    case 2:
  303.       if(colors == 256)
  304.      if (fractype != IFS3D)
  305.         standardplot = plot3dsuperimpose256;
  306.      else
  307.         standardplot = plotIFS3dsuperimpose256;
  308.       else
  309.      standardplot = plot3dsuperimpose16;
  310.       break;
  311.    default:
  312.       standardplot = putcolor;
  313.       break;
  314.    }
  315.  
  316.    xshift1 = xshift = (XSHIFT * (double)xdots)/100;
  317.    yshift1 = yshift = (YSHIFT * (double)ydots)/100;
  318.  
  319.    if(glassestype)
  320.    {
  321.       red_local_left   =    (red_crop_left         * (double)xdots)/100.0;
  322.       red_local_right  =    ((100 - red_crop_right)  * (double)xdots)/100.0;
  323.       blue_local_left  =    (blue_crop_left         * (double)xdots)/100.0;
  324.       blue_local_right =    ((100 - blue_crop_right) * (double)xdots)/100.0;
  325.       d_red_bright     =    (double)red_bright/100.0;
  326.       d_blue_bright    =    (double)blue_bright/100.0;
  327.  
  328.       switch(whichimage)
  329.       {
  330.       case 1:
  331.      xshift  += (eyeseparation* (double)xdots)/200;
  332.      xxadjust = ((xtrans+xadjust)* (double)xdots)/100;
  333.      xshift1  -= (eyeseparation* (double)xdots)/200;
  334.      xxadjust1 = ((xtrans-xadjust)* (double)xdots)/100;
  335.      break;
  336.       case 2:
  337.      xshift  -= (eyeseparation* (double)xdots)/200;
  338.      xxadjust = ((xtrans-xadjust)* (double)xdots)/100;
  339.      break;
  340.       }
  341.    }
  342.    else
  343.       xxadjust = (xtrans* (double)xdots)/100;
  344.    yyadjust = -(ytrans* (double)ydots)/100;
  345.  
  346.    if (mapset == 1)
  347.       ValidateLuts(MAP_name);  /* read the palette file */
  348.    else if (mapset == 2)
  349.    {
  350.       /*
  351.      Creates a palette for superimposed-pixel red/blue 3D.
  352.      Top four bits is blue, bottom 4 bits is read. Since
  353.      we need all combinations of red and blue for
  354.      superimposition purposes, we are allowed only
  355.      16 shades of red and blue.
  356.       */
  357.       if(colors == 256)
  358.      for(i=0;i<256;i++)
  359.      {
  360.         dacbox[i][0] = (i%16) << 2;
  361.         dacbox[i][1] = 0;
  362.         dacbox[i][2] = (i/16) << 2;
  363.      }
  364.       else
  365.      for(i=0;i<16;i++)
  366.      {
  367.         dacbox[i][0] = (i%4) << 4;
  368.         dacbox[i][1] = 0;
  369.         dacbox[i][2] = (i/4) << 4;
  370.      }
  371.       SetTgaColors();  /* setup Targa palette */
  372.    }
  373.    else if (mapset == 3)
  374.    {
  375.       /*
  376.      Creates a continuous palette map file for alternate
  377.      pixel funny glasses 3D. Colors are repeated, so we
  378.      need 128 color "pseudo" red and blue sequences.
  379.      Also, red seems a little brighter than the blue.
  380.       */
  381.       for(i=0;i<128;i++)
  382.       {
  383.      dacbox[i][0] = (unsigned char)(i >> 1);
  384.      dacbox[i][1] = dacbox[i][2] = 0;
  385.       }
  386.       for(i=0;i<128;i++)
  387.       {
  388.      dacbox[i+128][2] = (unsigned char)(i >> 1);
  389.      dacbox[i+128][0] = dacbox[i+128][1] = 0;
  390.       }
  391.       SetTgaColors();  /* setup Targa palette */
  392.    }
  393.    if (mapset)
  394.    {
  395.       if(glassestype==1 || glassestype==2)
  396.       {
  397.      if(glassestype == 2 && colors < 256)
  398.      {
  399.         dacbox[PAL_RED    ][0] = 63;
  400.         dacbox[PAL_RED    ][1] =  0;
  401.         dacbox[PAL_RED    ][2] =  0;
  402.  
  403.         dacbox[PAL_BLUE   ][0] =  0;
  404.         dacbox[PAL_BLUE   ][1] =  0;
  405.         dacbox[PAL_BLUE   ][2] = 63;
  406.  
  407.         dacbox[PAL_MAGENTA][0] = 63;
  408.         dacbox[PAL_MAGENTA][1] =  0;
  409.         dacbox[PAL_MAGENTA][2] = 63;
  410.      }
  411.      for (i=0;i<256;i++)
  412.      {
  413.         dacbox[i][0] = dacbox[i][0] * d_red_bright;
  414.         dacbox[i][2] = dacbox[i][2] * d_blue_bright;
  415.      }
  416.       }
  417.       if (dotmode != 11) /* not disk video */
  418.      spindac(0,1); /* load it, but don't spin */
  419.    }
  420. }
  421.