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