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