home *** CD-ROM | disk | FTP | other *** search
/ Mega A/V / mega_av.zip / mega_av / GRAPHUTL / FRPOR172.ZIP / LINE3D.C < prev    next >
C/C++ Source or Header  |  1992-03-14  |  76KB  |  2,682 lines

  1. /******************************************************************************/
  2. /* This file contains a 3D replacement for the out_line function called            */
  3. /* by the decoder. The purpose is to apply various 3D transformations            */
  4. /* before displaying points. Called once per line of the input file.                */
  5. /*                                                                                                        */
  6. /* This module is linked as an overlay, use ENTER_OVLY and EXIT_OVLY.            */
  7. /*                                                                                                        */
  8. /* Original Author Tim Wegner, with extensive help from Marc Reinig.                */
  9. /*****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <limits.h>
  14. #ifndef XFRACT
  15. #include <dos.h>
  16. #endif
  17. #include "fractint.h"
  18.  
  19. struct point
  20. {
  21.     int x;
  22.     int y;
  23.     int color;
  24. };
  25.  
  26. /* routines in this module    */
  27.  
  28. void line3d_overlay(void);
  29. int  line3d();
  30.  
  31. static void _fastcall vdraw_line(double *,double *,int);
  32. static void (_fastcall *fillplot)(int,int,int);
  33. static void (_fastcall *normalplot)(int,int,int);
  34. static void _fastcall putminmax(int x,int y,int color);
  35. static void _fastcall clipcolor(int x,int y,int color);
  36. static void _fastcall T_clipcolor(int x,int y,int color);
  37. static void _fastcall interpcolor(int x,int y,int color);
  38. static void corners(),draw_light_box();
  39. static void _fastcall putatriangle(struct point pt1,struct point pt2,
  40.                     struct point pt3,int color);
  41. static int _fastcall offscreen(struct point pt);
  42. static int  H_R(),R_H();
  43. static int  startdisk1(char *File_Name2, FILE *Source, int overlay);
  44. static int  set_pixel_buff();
  45. static void line3d_cleanup();
  46. int _fastcall targa_color(int x,int y,int color);
  47. extern int glassestype, whichimage;
  48. void (_fastcall *standardplot)(int,int,int);
  49. int targa_validate(char *File_Name);
  50.  
  51. static    int line_length1;
  52. static    int T_header_24 = 18; /* Size of current Targa-24 header */
  53. extern    BYTE dacbox[256][3];
  54. MATRIX m;                /* transformation matrix */
  55. extern void mult_vec_c(VECTOR);
  56. extern void mult_vec_iit(VECTOR);
  57. void (*mult_vec)(VECTOR) = mult_vec_c;
  58. extern int iit;                 /* iit flag */
  59. static    FILE *File_Ptr1 = NULL;
  60. int Ambient;
  61. static unsigned int IAmbient;
  62. int RANDOMIZE;
  63. static int rand_factor;
  64. int haze;
  65. static int HAZE_MULT;
  66. int Real_V = 0; /* mrr Actual value of V for fillytpe>4 monochrome images */
  67.  
  68. char light_name[80] = "fract001";
  69. int Targa_Overlay, error;
  70. extern int Targa_Out;
  71. char targa_temp[14] = "fractemp.tga";
  72. static void File_Error (char *File_Name1, int ERROR);
  73. static BYTE T24=24;
  74. static BYTE T32=32;
  75. static BYTE upr_lwr[4];
  76. static int T_Safe; /* Original Targa Image successfully copied to targa_temp */
  77. int P = 250; /* Perspective dist used when viewing light vector */
  78. static void draw_rect (VECTOR V0, VECTOR V1, VECTOR V2, VECTOR V3, int color,
  79.                             int rect);
  80. static VECTOR light_direction;
  81.  
  82. BYTE back_color[3];
  83. static BYTE Real_Color; /* Actual color of cur pixel */
  84. extern int dotmode; /* video access method, 11 if really disk video */
  85. extern int calc_status;
  86. extern long calctime;
  87. extern int got_status,currow;
  88.  
  89.  
  90. static int RO, CO, CO_MAX; /*  For use in Acrospin support */
  91. static char far acro_s1[] =
  92.     {"Set Layer 1\nSet Color 2\nEndpointList X Y Z Name\n"};
  93. static char far acro_s2[] = {"LineList From To\n"};
  94. static char far s3[] = {"{ Created by FRACTINT Ver. "};
  95. static char far s3a[] = {" }\n\n"};
  96. #ifndef XFRACT
  97. static char banner[] ="%Fs%#4.2f%Fs";
  98. #else
  99. static char banner[] ="%s%#4.2f%s";
  100. #endif
  101. char ray_name[80] = "fract001";
  102.  
  103. char preview = 0;
  104. char showbox = 0;
  105. static int localpreviewfactor;
  106. int previewfactor = 20;
  107. int xadjust = 0;
  108. int yadjust = 0;
  109. int xxadjust;
  110. int yyadjust;
  111. int xshift;
  112. int yshift;
  113. extern int overflow;
  114. extern int filetype;
  115. extern char usr_floatflag;
  116. extern int xdots, ydots, colors, sxoffs, syoffs;
  117. extern int debugflag;
  118. extern int extraseg;
  119. extern unsigned height;
  120. extern int rowcount;        /* in general.asm */
  121. extern int init3d[];        /* 3D arguments (FRACTINT.C) */
  122. extern long far *lx0;
  123. extern int transparent[2];    /* transparency min/max */
  124. extern int pot16bit;
  125. extern int filecolors;
  126. extern void (*outln_cleanup)();
  127.  
  128. static int zcoord = 256;
  129. static double aspect;            /* aspect ratio */
  130. static int evenoddrow;
  131.  
  132. static float far *sinthetaarray;    /* all sine        thetas go here  */
  133. static float far *costhetaarray;    /* all cosine thetas go here */
  134.  
  135. static double rXrscale;         /* precalculation factor */
  136.  
  137. static int persp;    /* flag for indicating perspective transformations */
  138. int bad_value = -10000; /* set bad values to this */
  139. int bad_check = -3000;    /* check values against this to determine if good */
  140.  
  141. /* this array remembers the previous line */
  142. struct point far *lastrow;
  143.  
  144. static struct point p1,p2,p3;
  145.  
  146. struct f_point
  147. {
  148.     float x;
  149.     float y;
  150.     float color;
  151. }
  152. far *f_lastrow;
  153.  
  154.  
  155. int RAY = 0; /* Flag to generate Ray trace compatible files in 3d */
  156. int BRIEF = 0; /* 1 = short ray trace files */
  157. extern int release; /* Current release level of Fractint */
  158. static int _fastcall RAY_Header(void);
  159. static void _fastcall triangle_bounds(float pt_t[3][3]);
  160. static int _fastcall out_triangle(struct f_point pt1, struct f_point pt2,
  161.             struct f_point pt3, int c1, int c2, int c3);
  162. static float min_xyz[3], max_xyz[3]; /* For Raytrace output */
  163. static int _fastcall start_object(void);
  164. static int _fastcall end_object(int triout);
  165. extern unsigned numcolors; /* number of colors in original GIF */
  166. static long num_tris; /* number of triangles output to ray trace file */
  167.  
  168. /* array of min and max x values used in triangle fill */
  169. struct minmax
  170. {
  171.     int minx;
  172.     int maxx;
  173. }
  174. far *minmax_x;
  175.  
  176. VECTOR view;    /* position of observer for perspective */
  177. VECTOR cross;
  178. VECTOR tmpcross;
  179.  
  180. struct point oldlast =
  181. {
  182.     0, 0, 0
  183. };    /* old pixels */
  184.  
  185. void line3d_overlay() {
  186. }    /* for restore_active_ovly */
  187.  
  188. int line3d(BYTE pixels[], unsigned linelen)
  189. {
  190.     int tout; /*  triangle has been sent to ray trace file */
  191.     int RND;
  192.     float f_water; /* transformed WATERLINE for ray trace files */
  193.  
  194.     /* these values come from FRACTINT.C */
  195.  
  196.     /* These variables must preserve their values across calls */
  197.     static float    deltaphi;  /* increment of latitude, longitude */
  198.     static double rscale;            /* surface roughness factor */
  199.     static long xcenter,ycenter;            /* circle center */
  200.     double r0;
  201.     int xcenter0,ycenter0; /* Unfudged versions */
  202.  
  203.     static double sclx,scly,sclz;        /* scale factors */
  204.     static double R;                /* radius values */
  205.     static double Rfactor;            /* for intermediate calculation */
  206.     MATRIX lightm;                /* m w/no trans, keeps obj. on screen */
  207.     static LMATRIX lm;                /* "" */
  208.     static LVECTOR lview;            /* for perspective views */
  209.     static double zcutoff;            /* perspective backside cutoff value */
  210.     static float twocosdeltaphi;
  211.     static float cosphi,sinphi;            /* precalculated sin/cos of longitude */
  212.     static float oldcosphi1,oldsinphi1;
  213.     static float oldcosphi2,oldsinphi2;
  214.     double r;                    /* sphere radius */
  215.     double xval,yval,zval;            /* rotation values */
  216.     float costheta,sintheta;            /* precalculated sin/cos of latitude */
  217.     float twocosdeltatheta;
  218.  
  219.     int next; /* used by preview and grid */
  220.     int col;                    /* current column (original GIF) */
  221.     struct point cur;                /* current pixels */
  222.     struct point old;                /* old pixels */
  223.  
  224.     static struct point bad;            /* out of range value */
  225.  
  226.     struct f_point f_cur;
  227.     struct f_point f_old;
  228.     static struct f_point f_bad;            /* out of range value */
  229.  
  230.     static BYTE far *fraction;/* float version of pixels array */
  231.  
  232.     VECTOR v;                    /* double vector */
  233.     VECTOR v1,v2;
  234.     VECTOR crossavg;
  235.     char crossnotinit;                /* flag for crossavg init indication */
  236.  
  237.     double v_length;
  238.     VECTOR origin, direct, tmp;
  239.     LVECTOR lv;                    /* long equivalent of v */
  240.     LVECTOR lv0;                 /* long equivalent of v */
  241.  
  242.     /* corners of transformed xdotx by ydots x colors box */
  243.     double xmin, ymin, zmin, xmax, ymax, zmax;
  244.     int i,j;
  245.     int lastdot;
  246.  
  247.     long fudge;
  248.  
  249.     ENTER_OVLY(OVLY_LINE3D);
  250.  
  251.     fudge = 1L<<16;
  252.  
  253.  
  254.     if(transparent[0] || transparent[1])
  255.         plot = normalplot = T_clipcolor;    /*  Use transparent plot function */
  256.     else            /* Use the usual FRACTINT plot function with clipping */
  257.         plot = normalplot = clipcolor;
  258.  
  259.     currow = rowcount; /* use separate variable to allow for pot16bit files */
  260.     if (pot16bit)
  261.         currow >>= 1;
  262.  
  263.     /************************************************************************/
  264.     /* This IF clause is executed ONCE per image. All precalculations are    */
  265.     /* done here, with out any special concern about speed. DANGER -            */
  266.     /* communication with the rest of the program is generally via static    */
  267.     /* or global variables.                                                                    */
  268.     /************************************************************************/
  269.     if(rowcount++ == 0)
  270.     {
  271.         long check_extra;
  272.         float theta,theta1,theta2;    /* current,start,stop latitude */
  273.         float phi1,phi2;            /* current start,stop longitude */
  274.         float    deltatheta;        /* increment of latitude */
  275.         outln_cleanup = line3d_cleanup;
  276.  
  277.         calctime = evenoddrow = 0;
  278.         /* mark as in-progress, and enable <tab> timer display */
  279.         calc_status = 1;
  280.  
  281.         IAmbient = (unsigned int) (255 * (float)(100 - Ambient) / 100.0);
  282.         if (IAmbient < 1)        IAmbient = 1;
  283.  
  284.         tout = 0;
  285.         num_tris = 0;
  286.  
  287.         /* Open file for RAY trace output and write header */
  288.         if (RAY)
  289.         {
  290.             RAY_Header();
  291.             xxadjust = yyadjust = 0; /* Disable shifting in ray tracing */
  292.             xshift = yshift = 0;
  293.         }
  294.  
  295.         CO_MAX=CO=RO=0;
  296.  
  297.         upr_lwr[0] = xdots & 0xff;
  298.         upr_lwr[1] = xdots >> 8;
  299.         upr_lwr[2] = ydots &  0xff;
  300.         upr_lwr[3] = ydots >> 8;
  301.         line_length1 = 3 * xdots;        /*  line length @ 3 bytes per pixel  */
  302.         error = 0;
  303.  
  304.         if (whichimage < 2)
  305.             T_Safe = 0; /* Not safe yet to mess with the source image */
  306.  
  307.         if (Targa_Out && !((glassestype==1 || glassestype==2) && whichimage==2))
  308.         {
  309.             if (Targa_Overlay)    
  310.             {        
  311.                 /* Make sure target file is a supportable Targa File */    
  312.                 if(targa_validate (light_name))        
  313.                     return(-1);
  314.             }        
  315.             else
  316.             {
  317.                 check_writefile(light_name,".tga");
  318.                 if (startdisk1(light_name, NULL, 0))    /* Open new file */
  319.                     return(-1);
  320.             }
  321.         }
  322.  
  323.         rand_factor = 14 - RANDOMIZE;
  324.  
  325.         zcoord = filecolors;
  326.  
  327.         crossavg[0] = 0;
  328.         crossavg[1] = 0;
  329.         crossavg[2] = 0;
  330.  
  331.         /*********************************************************************/
  332.         /*  Memory allocation - assumptions - a 64K segment starting at        */
  333.         /*  extraseg has been grabbed. It may have other purposes elsewhere, */
  334.         /*  but it is assumed that it is totally free for use here. Our        */
  335.         /*  strategy is to assign all the far pointers needed here to various*/
  336.         /*  spots in the extra segment, depending on the pixel dimensions of */
  337.         /*  the video mode, and check whether we have run out. There is        */
  338.         /*  basically one case where the extra segment is not big enough        */
  339.         /*  -- SPHERE mode with a fill type that uses putatriangle() (array    */
  340.         /*  minmax_x) at the largest legal resolution of 2048x2048 or            */
  341.         /*  thereabouts. In that case we use farmemalloc to grab    memory        */
  342.         /*  for minmax_x. This memory is never freed.                                */
  343.         /*********************************************************************/
  344.  
  345.         /* lastrow stores the previous row of the original GIF image for
  346.             the purpose of filling in gaps with triangle procedure */
  347.         lastrow = MK_FP(extraseg,0);
  348.  
  349.         check_extra = sizeof(*lastrow)*xdots;
  350.         if(SPHERE)
  351.         {
  352.             sinthetaarray = (float far *)(lastrow+xdots);
  353.             check_extra += sizeof(*sinthetaarray)*xdots;
  354.  
  355.             costhetaarray = (float far *)(sinthetaarray+xdots);
  356.             check_extra += sizeof(*costhetaarray)*xdots;
  357.  
  358.             f_lastrow = (struct f_point far *)(costhetaarray+xdots);
  359.         }
  360.         else
  361.             f_lastrow = (struct f_point far *)(lastrow+xdots);
  362.         check_extra += sizeof(*f_lastrow)*(xdots);
  363.  
  364.         if(pot16bit)
  365.         {
  366.             fraction = (BYTE far *)(f_lastrow+xdots);
  367.             check_extra += sizeof(*fraction)*xdots;
  368.         }
  369.         minmax_x = (struct minmax *)NULL;
  370.  
  371.         /* these fill types call putatriangle which uses minmax_x */
  372.         if( FILLTYPE == 2 || FILLTYPE == 3 || FILLTYPE == 5 || FILLTYPE == 6)
  373.         {
  374.             /* end of arrays if we use extra segement */
  375.             check_extra += sizeof(struct minmax)*ydots;
  376.             if(check_extra > (1L<<16)) /* run out of extra segment? */
  377.             {
  378.                 static struct minmax far *got_mem = NULL;
  379.                 /* not using extra segment so decrement check_extra */
  380.                 check_extra -= sizeof(struct minmax)*ydots;
  381.                 if(got_mem == NULL)
  382.                     got_mem = (struct minmax far *)(farmemalloc(2048L *
  383.                 sizeof(struct minmax)));
  384.  
  385.                 if(got_mem)
  386.                     minmax_x = got_mem;
  387.                 else
  388.                 {
  389.                     EXIT_OVLY;
  390.                     return(-1);
  391.                 }
  392.             }
  393.             else /* ok to use extra segment */
  394.             {
  395.                 if(pot16bit)
  396.                     minmax_x = (struct minmax far *)(fraction+xdots);
  397.                 else
  398.                     minmax_x = (struct minmax far *)(f_lastrow+xdots);
  399.  
  400.             }
  401.         }
  402.  
  403.         if(debugflag == 2222 || check_extra > (1L<<16))
  404.         {
  405.             char tmpmsg[70];
  406.             static char far extramsg[] = {" of extra segment"};
  407. #ifndef XFRACT
  408.             sprintf(tmpmsg,"used %ld%Fs", check_extra, extramsg);
  409. #else
  410.             sprintf(tmpmsg,"used %ld%s", check_extra, extramsg);
  411. #endif
  412.             stopmsg(4,tmpmsg);
  413.         }
  414.  
  415.         /* get scale factors */
  416.         sclx =    XSCALE/100.0;
  417.         scly =    YSCALE/100.0;
  418.         if (ROUGH)
  419.             sclz = -ROUGH/100.0;
  420.         else
  421.             rscale = sclz = -0.0001; /* if rough=0 make it very flat but plot something */
  422.  
  423.         /* aspect ratio calculation - assume screen is 4 x 3 */
  424.         aspect = (double)xdots*.75/(double)ydots;
  425.  
  426.         if(SPHERE==FALSE)  /* skip this slow stuff in sphere case */
  427.         {
  428.         /*********************************************************************/
  429.         /* What is done here is to create a single matrix, m, which has        */
  430.         /* scale, rotation, and shift all combined. This allows us to use        */
  431.         /* a single matrix to transform any point. Additionally, we create      */
  432.         /* two perspective vectors.                                                        */
  433.         /*                                                                                            */
  434.         /* Start with a unit matrix. Add scale and rotation. Then calculate     */
  435.         /* the perspective vectors. Finally add enough translation to center    */
  436.         /* the final image plus whatever shift the user has set.                    */
  437.         /*********************************************************************/
  438.  
  439.             /* start with identity */
  440.             identity (m);
  441.             identity (lightm);
  442.  
  443.             /* translate so origin is in center of box, so that when we rotate */
  444.             /* it, we do so through the center */
  445.             trans ( (double)xdots/(-2.0),(double)ydots/(-2.0),
  446.                     (double)zcoord/(-2.0),m);
  447.             trans ( (double)xdots/(-2.0),(double)ydots/(-2.0),
  448.                     (double)zcoord/(-2.0),lightm);
  449.  
  450.             /* apply scale factors */
  451.             scale(sclx,scly,sclz,m);
  452.             scale(sclx,scly,sclz,lightm);
  453.  
  454.             /* rotation values - converting from degrees to radians */
  455.             xval = XROT / 57.29577;
  456.             yval = YROT / 57.29577;
  457.             zval = ZROT / 57.29577;
  458.  
  459.             if (RAY)    {xval = yval = zval = 0;}
  460.  
  461.             xrot (xval,m);
  462.             xrot (xval,lightm);
  463.             yrot (yval,m);
  464.             yrot (yval,lightm);
  465.             zrot (zval,m);
  466.             zrot (zval,lightm);
  467.  
  468.             /* Find values of translation that make all x,y,z negative */
  469.             /* m current matrix */
  470.             /* 0 means don't show box */
  471.             /* returns minimum and maximum values of x,y,z in fractal */
  472.             corners(m,0,&xmin,&ymin,&zmin,&xmax,&ymax,&zmax);
  473.         }
  474.  
  475.         /* perspective 3D vector - lview[2] == 0 means no perspective */
  476.  
  477.         /* set perspective flag */
  478.         persp = 0;
  479.         if (ZVIEWER != 0)
  480.         {
  481.             persp = 1;
  482.             if(ZVIEWER < 80) /* force float */
  483.                 usr_floatflag |= 2; /* turn on second bit */
  484.         }
  485.  
  486.         /* set up view vector, and put viewer in center of screen */
  487.         lview[0] = xdots >> 1;
  488.         lview[1] = ydots >> 1;
  489.  
  490.         /* z value of user's eye - should be more negative than extreme
  491.                                 negative part of image */
  492.         if(SPHERE) /* sphere case */
  493.             lview[2] = -(long)((double)ydots*(double)ZVIEWER/100.0);
  494.         else    /* non-sphere case */
  495.             lview[2] = (long)((zmin-zmax)*(double)ZVIEWER/100.0);
  496.  
  497.         view[0] = lview[0];
  498.         view[1] = lview[1];
  499.         view[2] = lview[2];
  500.         lview[0] = lview[0] << 16;
  501.         lview[1] = lview[1] << 16;
  502.         lview[2] = lview[2] << 16;
  503.  
  504.         if(SPHERE==FALSE) /* sphere skips this */
  505.         {
  506.             /* translate back exactly amount we translated earlier plus enough
  507.                 to center image so maximum values are non-positive */
  508.             trans(((double)xdots-xmax-xmin)/2,((double)ydots-ymax-ymin) / 2,
  509.                 -zmax,m);
  510.  
  511.             /* Keep the box centered and on screen regardless of shifts */
  512.             trans(((double)xdots-xmax-xmin)/2,((double)ydots-ymax-ymin)/2,
  513.                 -zmax,lightm);
  514.  
  515.             trans((double)(xshift),(double)(-yshift),0.0,m);
  516.  
  517.             /* matrix m now contains ALL those transforms composed together !!
  518.                 convert m to long integers shifted 16 bits */
  519.             for (i = 0; i < 4; i++)
  520.                 for (j = 0; j < 4; j++)
  521.                     lm[i][j] = m[i][j] * 65536.0;
  522.  
  523.         }
  524.         else /* sphere stuff goes here */
  525.         {
  526.             /* Sphere is on side - north pole on right. Top is -90 degrees
  527.                 latitude; bottom 90 degrees */
  528.  
  529.             /* Map X to this LATITUDE range */
  530.             theta1 = THETA1*PI/180.0;
  531.             theta2 = THETA2*PI/180.0;
  532.  
  533.             /* Map Y to this LONGITUDE range */
  534.             phi1    = PHI1*PI/180.0;
  535.             phi2    = PHI2*PI/180.0;
  536.  
  537.             theta = theta1;
  538.  
  539.         /*********************************************************************/
  540.         /* Thanks to Hugh Bray for the following idea: when calculating        */
  541.         /* a table of evenly spaced sines or cosines, only a few initial        */
  542.         /* values need be calculated, and the remaining values can be            */
  543.         /* gotten from a derivative of the sine/cosine angle sum formula        */
  544.         /* at the cost of one multiplication and one addition per value!        */
  545.         /*                                                                                            */
  546.         /* This idea is applied once here to get a complete table for            */
  547.         /* latitude, and near the bottom of this routine to incrementally        */
  548.         /* calculate longitude.                                                                */
  549.         /*                                                                                            */
  550.         /* Precalculate 2*cos(deltaangle), sin(start) and sin(start+delta).    */
  551.         /* Then apply recursively:                                                            */
  552.         /*    sin(angle+2*delta) = sin(angle+delta) * 2cosdelta - sin(angle)        */
  553.         /*                                                                                            */
  554.         /* Similarly for cosine. Neat!                                                     */
  555.         /*********************************************************************/
  556.  
  557.             deltatheta = (float)(theta2 - theta1)/(float)linelen;
  558.  
  559.             /* initial sin,cos theta */
  560.             sinthetaarray[0] = sin((double)theta);
  561.             costhetaarray[0] = cos((double)theta);
  562.             sinthetaarray[1] = sin((double)(theta + deltatheta));
  563.             costhetaarray[1] = cos((double)(theta + deltatheta));
  564.  
  565.             /* sin,cos delta theta */
  566.             twocosdeltatheta = 2.0*cos((double)deltatheta);
  567.  
  568.             /* build table of other sin,cos with trig identity */
  569.             for(i=2;i<linelen;i++)
  570.             {
  571.                 sinthetaarray[i] = sinthetaarray[i-1]*twocosdeltatheta-
  572.                     sinthetaarray[i-2];
  573.                 costhetaarray[i] = costhetaarray[i-1]*twocosdeltatheta-
  574.                     costhetaarray[i-2];
  575.             }
  576.  
  577.             /* now phi - these calculated as we go - get started here */
  578.             deltaphi    = (float)(phi2    - phi1  )/(float)height;
  579.  
  580.             /* initial sin,cos phi */
  581.  
  582.             sinphi = oldsinphi1 = sin((double)phi1);
  583.             cosphi = oldcosphi1 = cos((double)phi1);
  584.             oldsinphi2 = sin((double)(phi1+deltaphi));
  585.             oldcosphi2 = cos((double)(phi1+deltaphi));
  586.  
  587.             /* sin,cos delta phi */
  588.             twocosdeltaphi = 2*cos((double)deltaphi);
  589.  
  590.             xcenter0 = xcenter = xdots/2 + xshift;
  591.             ycenter0 = ycenter = ydots/2 - yshift;
  592.  
  593.             /* affects how rough planet terrain is */
  594.             if (ROUGH)
  595.                 rscale = .3*ROUGH/100.0;
  596.  
  597.             /* radius of planet */
  598.             R = (double)(ydots)/2;
  599.  
  600.             /* precalculate factor */
  601.             rXrscale = R*rscale;
  602.  
  603.             sclz = sclx = scly = RADIUS/100.0; /* Need x,y,z for RAY */
  604.  
  605.             /* adjust x scale factor for aspect */
  606.             sclx *= aspect;
  607.  
  608.             /* precalculation factor used in sphere calc */
  609.             Rfactor = rscale*R/(double)zcoord;
  610.  
  611.             if(persp) /* precalculate fudge factor */
  612.             {
  613.                 double radius;
  614.                 double zview;
  615.                 double angle;
  616.  
  617.                 xcenter  = xcenter << 16;
  618.                 ycenter  = ycenter << 16;
  619.  
  620.                 Rfactor *= 65536.0;
  621.                 R            *= 65536.0;
  622.  
  623.                 /* calculate z cutoff factor
  624.                     attempt to prevent out-of-view surfaces from being written */
  625.                 zview = -(long)((double)ydots*(double)ZVIEWER/100.0);
  626.                 radius = (double)(ydots)/2;
  627.                 angle = atan(-radius/(zview+radius));
  628.                 zcutoff = -radius - sin(angle)*radius;
  629.                 zcutoff *= 1.1; /* for safety */
  630.                 zcutoff *= 65536;
  631.             }
  632.         }
  633.  
  634.         /* set fill plot function */
  635.         if(FILLTYPE != 3)
  636.             fillplot = interpcolor;
  637.         else
  638.         {
  639.             fillplot = clipcolor;
  640.  
  641.             if(transparent[0] || transparent[1])
  642.                 /*    If transparent colors are set */
  643.                 fillplot = T_clipcolor;  /*  Use the transparent plot function  */
  644.         }
  645.  
  646.         /* Both Sphere and Normal 3D */
  647.         direct[0] = light_direction[0] = XLIGHT;
  648.         direct[1] = light_direction[1] = -YLIGHT;
  649.         direct[2] = light_direction[2] = ZLIGHT;
  650.  
  651.         /* Needed because sclz = -ROUGH/100 and light_direction is transformed
  652.             in FILLTYPE 6 but not in 5. */
  653.         if (FILLTYPE == 5)
  654.             direct[2] = light_direction[2] = -ZLIGHT;
  655.  
  656.         if(FILLTYPE==6) /* transform light direction */
  657.         {
  658.             /* Think of light direction  as a vector with tail at (0,0,0) and
  659.                 head at (light_direction). We apply the transformation to
  660.                 BOTH head and tail and take the difference */
  661.  
  662.             v[0] = 0.0;
  663.             v[1] = 0.0;
  664.             v[2] = 0.0;
  665.             vmult(v,m,v);
  666.             vmult(light_direction,m,light_direction);
  667.  
  668.             for (i=0;i<3;i++)        light_direction[i] -= v[i];
  669.         }
  670.         normalize_vector(light_direction);
  671.  
  672.         if(preview && showbox)
  673.         {
  674.             normalize_vector(direct);
  675.  
  676.             /* move light vector to be more clear with grey scale maps */
  677.             origin[0] = (3 * xdots) / 16;
  678.             origin[1] = (3 * ydots) / 4;
  679.             if (FILLTYPE == 6)
  680.                 origin[1] = (11 * ydots) / 16;
  681.  
  682.             origin[2] = 0.0;
  683.  
  684.             v_length = min (xdots, ydots) / 2;
  685.             if (persp && ZVIEWER <= P)
  686.                 v_length *= (long)(P + 600) /((long)(ZVIEWER+600) * 2);
  687.  
  688.             /* Set direct[] to point from origin[] in direction of
  689.                 untransformed light_direction (direct[]). */
  690.             for (i=0;i<3;i++)
  691.                 direct[i] = origin[i] + direct[i] * v_length;
  692.  
  693.             /* center light box */
  694.             for (i=0;i<2;i++)
  695.             {
  696.                 tmp[i] = (direct[i] - origin[i]) / 2;
  697.                 origin[i] -= tmp[i];
  698.                 direct[i] -= tmp[i];
  699.             }
  700.  
  701.             /* Draw light source vector and box containing it, draw_light_box
  702.                 will transform them if necessary. */
  703.             draw_light_box (origin,direct,lightm);
  704.             /* draw box around original field of view to help visualize effect
  705.                 of rotations 1 means show box - xmin etc. do nothing here */
  706.             if (!SPHERE)
  707.                 corners(m,1,&xmin,&ymin,&zmin,&xmax,&ymax,&zmax);
  708.         }
  709.  
  710.         /* bad has values caught by clipping */
  711.         f_bad.x            = bad.x            = bad_value;
  712.         f_bad.y            = bad.y            = bad_value;
  713.         f_bad.color = bad.color = bad_value;
  714.         for(i=0;i<linelen;i++)
  715.         {
  716.             lastrow[i]    = bad;
  717.             f_lastrow[i] = f_bad;
  718.         }
  719.         got_status = 3;
  720.         if(iit>0)
  721.         {
  722.             load_mat(m); /* load matrix into iit registers */
  723.             mult_vec = mult_vec_iit;
  724.         }
  725.         else
  726.             mult_vec = mult_vec_c;
  727.     } /* end of once-per-image intializations */
  728.  
  729.     crossnotinit = 1;
  730.     col = 0;
  731.  
  732.     CO = 0;
  733.  
  734.     /* make sure these pixel coordinates are out of range */
  735.     old        = bad;
  736.     f_old = f_bad;
  737.  
  738.     /* copies pixels buffer to float type fraction buffer for fill purposes */
  739.     if(pot16bit)
  740.         if (set_pixel_buff(pixels,fraction,linelen))
  741.         {
  742.             EXIT_OVLY;
  743.             return(0);
  744.         }
  745.  
  746.   /*************************************************************************/
  747.   /* This section of code allows the operation of a preview mode when the    */
  748.   /* preview flag is set. Enabled, it allows the drawing of only the first */
  749.   /* line of the source image, then every 10th line, until and including    */
  750.   /* the last line. For the undrawn lines, only necessary calculations are */
  751.   /* made. As a bonus, in non-sphere mode a box is drawn to help visualize */
  752.   /* the effects of 3D transformations. Thanks to Marc Reinig for this idea*/
  753.   /* and code -- BTW, Marc did NOT put the goto in, but WE did, to avoid    */
  754.   /* copying code here, and to avoid a HUGE "if-then" construct. Besides,    */
  755.   /* we have ALREADY sinned, so why not sin some more?                            */
  756.   /*************************************************************************/
  757.  
  758.  
  759.     lastdot = min(xdots-1, linelen-1);
  760.     if (FILLTYPE >= 5)
  761.         if (haze && Targa_Out)
  762.         {
  763.             HAZE_MULT = haze * (
  764.                 (float)((long)(ydots - 1 - currow) *
  765.                 (long)(ydots - 1 - currow)) /
  766.                 (float)((long)(ydots - 1) * (long)(ydots - 1)));
  767.             HAZE_MULT = 100 - HAZE_MULT;
  768.         }
  769.  
  770.     if (previewfactor >= ydots || previewfactor > lastdot)
  771.         previewfactor = min ( ydots - 1, lastdot);
  772.  
  773.     localpreviewfactor = ydots/previewfactor;
  774.  
  775.     tout = 0;
  776.     /* Insure last line is drawn in preview and filltypes <0  */
  777.     if ((RAY || preview || FILLTYPE < 0) && (currow != ydots-1) &&
  778.         (currow % localpreviewfactor) && /* Draw mod preview lines */
  779.         !(!RAY && (FILLTYPE > 4) && (currow == 1)))
  780.             /* Get init geometry in lightsource modes */
  781.             goto reallythebottom; /* skip over most of the line3d calcs */
  782.     if (dotmode == 11)
  783.     {
  784.         char s[40];
  785.         sprintf(s,"mapping to 3d, reading line %d", currow);
  786.         dvid_status(1,s);
  787.     }
  788.  
  789.     if(!col && RAY && currow != 0)        start_object();
  790.  
  791.     if(FILLTYPE==0 && !SPHERE && !pot16bit && !RAY && debugflag != 2224 )
  792.         /* This while loop contains just a limited non-sphere case for speed */
  793.         /* Other while loop still has the old logic. Use debugflag to compare*/
  794.         while(col < linelen)
  795.         {
  796.             Real_Color = cur.color = pixels[col];
  797.             if (cur.color > 0 && cur.color < WATERLINE)
  798.                 Real_Color = cur.color = WATERLINE;  /* "lake" */
  799.             if(!usr_floatflag)
  800.             {
  801.                 lv0[0] = 0; /* don't save vector before perspective */
  802.  
  803.                 /* use 32-bit multiply math to snap this out */
  804.                 lv[0] = col;
  805.                 lv[0] = lv[0] << 16;
  806.                 lv[1] = currow;
  807.                 lv[1] = lv[1] << 16;
  808.                 lv[2] = cur.color;
  809.                 lv[2] = lv[2] << 16;
  810.  
  811.                 if(longvmultpersp(lv,lm,lv0,lv,lview,16) == -1)
  812.                 {
  813.                     cur    = bad;
  814.                     goto loopbottom;
  815.                 }
  816.                 cur.x = ((lv[0]/* +32768L */) >> 16) + xxadjust;
  817.                 cur.y = ((lv[1]/* +32768L */) >> 16) + yyadjust;
  818.             }
  819.             /* do in float if integer math overflowed */
  820.             if(usr_floatflag || overflow)
  821.             {
  822.                 /* slow float version for comparison */
  823.                 v[0] = col;
  824.                 v[1] = currow;
  825.                 v[2] = cur.color;
  826.                 mult_vec(v); /* matrix*vector routine */
  827.                 if(persp)
  828.                     perspective(v);
  829.                 cur.x = v[0] + xxadjust /* + .5 */;
  830.                 cur.y = v[1] + yyadjust /* + .5 */;
  831.             }
  832.             (*plot)(cur.x,cur.y,cur.color);
  833.             col++;
  834.         }  /*  End of while statement for plotting line  */
  835.     else
  836.  
  837.         /* PROCESS ROW LOOP BEGINS HERE */
  838.         while(col < linelen)
  839.         {
  840.             if ((RAY || preview || FILLTYPE < 0) &&
  841.                 (col != lastdot) &&        /* if this is not the last col */
  842.                 /* if not the 1st or mod factor col*/
  843.                 (col % (int)(aspect * localpreviewfactor)) &&
  844.                 (!(!RAY && FILLTYPE > 4 && col == 1)))
  845.                     goto loopbottom;
  846.  
  847.             f_cur.color = Real_Color = cur.color = pixels[col];
  848.  
  849.             if (RAY || preview || FILLTYPE < 0)
  850.             {
  851.                 next = col + aspect * localpreviewfactor;
  852.                 if (next == col)        next = col + 1;
  853.             }
  854.             else
  855.                 next = col + 1;
  856.             if (next >= lastdot)
  857.                 next = lastdot;
  858.  
  859.             if (cur.color > 0 && cur.color < WATERLINE)
  860.                 f_cur.color = Real_Color = cur.color = WATERLINE;        /* "lake" */
  861.             else if(pot16bit)
  862.                 f_cur.color += ((float)fraction[col])/(float)(1<<8);
  863.  
  864.             if(SPHERE) /* sphere case */
  865.             {
  866.                 sintheta = sinthetaarray[col];
  867.                 costheta = costhetaarray[col];
  868.  
  869.                 if(sinphi < 0 && !(RAY || FILLTYPE < 0))
  870.                 {
  871.                     cur    = bad;
  872.                     f_cur = f_bad;
  873.                     goto loopbottom;    /* another goto ! */
  874.                 }
  875.             /******************************************************************/
  876.             /* KEEP THIS FOR DOCS - original formula --                                */
  877.             /* if(rscale < 0.0)                                                                 */
  878.             /*            r = 1.0+((double)cur.color/(double)zcoord)*rscale;            */
  879.             /* else                                                                                */
  880.             /*            r = 1.0-rscale+((double)cur.color/(double)zcoord)*rscale;*/
  881.             /* R = (double)ydots/2;                                                            */
  882.             /* r = r*R;                                                                         */
  883.             /* cur.x = xdots/2 + sclx*r*sintheta*aspect + xup ;                    */
  884.             /* cur.y = ydots/2 + scly*r*costheta*cosphi - yup ;                    */
  885.             /******************************************************************/
  886.  
  887.                 if(rscale < 0.0)
  888.                     r = R + Rfactor*(double)f_cur.color*costheta;
  889.                 else if(rscale > 0.0)
  890.                     r = R -rXrscale + Rfactor*(double)f_cur.color*costheta;
  891.                 else
  892.                     r = R;
  893.                 if(persp || RAY) /* Allow Ray trace to go through so display is ok */
  894.                 {    /* mrr how do lv[] and cur and f_cur all relate */
  895.                     /* NOTE: fudge was pre-calculated above in r and R */
  896.                     /* (almost) guarantee negative */
  897.                     lv[2] = -R - r*costheta*sinphi;    /* z */
  898.                     if((lv[2] > zcutoff) && !FILLTYPE < 0)
  899.                     {
  900.                         cur = bad;
  901.                         f_cur = f_bad;
  902.                         goto loopbottom;    /* another goto ! */
  903.                     }
  904.                     lv[0] = xcenter + sintheta*sclx*r;    /* x */
  905.                     lv[1] = ycenter + costheta*cosphi*scly*r;        /* y */
  906.  
  907.                     if((FILLTYPE >= 5) || RAY)
  908.                     {  /* calculate illumination normal before persp */
  909.  
  910.                         r0        = r/65536;
  911.                         f_cur.x        = xcenter0 + sintheta*sclx*r0;
  912.                         f_cur.y        = ycenter0 + costheta*cosphi*scly*r0;
  913.                         f_cur.color = -r0*costheta*sinphi;
  914.                     }
  915.                     if(!(usr_floatflag || RAY))
  916.                     {
  917.                         if(longpersp(lv,lview,16) == -1)
  918.                         {
  919.                             cur = bad;
  920.                             f_cur = f_bad;
  921.                             goto loopbottom;    /* another goto ! */
  922.                         }
  923.                         cur.x = ((lv[0]+32768L) >> 16) + xxadjust;
  924.                         cur.y = ((lv[1]+32768L) >> 16) + yyadjust;
  925.                     }
  926.                     if(usr_floatflag || overflow || RAY)
  927.                     {
  928.                         v[0] = lv[0];
  929.                         v[1] = lv[1];
  930.                         v[2] = lv[2];
  931.                         v[0] /= fudge;
  932.                         v[1] /= fudge;
  933.                         v[2] /= fudge;
  934.                         perspective(v);
  935.                         cur.x = v[0]+.5 + xxadjust;
  936.                         cur.y = v[1]+.5 + yyadjust;
  937.                     }
  938.                 }
  939.                 else if (!(persp && RAY)) /* mrr Not sure how this and 3rd if
  940.                                                     above relate */
  941.                 {    /* mrr Why the xx- and yyadjust here and not above? */
  942.                     cur.x = f_cur.x = xcenter + sintheta*sclx*r + xxadjust;
  943.                     cur.y = f_cur.y = ycenter + costheta*cosphi*scly*r + yyadjust;
  944.                     if(FILLTYPE >= 5 || RAY) /* mrr why do we do this for filltype>5? */
  945.                         f_cur.color = -r * costheta * sinphi * sclz;
  946.                     v[0]=v[1]=v[2]=0;    /* MRR Why do we do this? */
  947.                 }
  948.             }
  949.             else /* non-sphere 3D */
  950.             {
  951.                 if(!usr_floatflag && !RAY)
  952.                 {
  953.                     if(FILLTYPE >= 5) /* flag to save vector before perspective */
  954.                         lv0[0] = 1;        /* in longvmultpersp calculation */
  955.                     else
  956.                         lv0[0] = 0;
  957.  
  958.                     /* use 32-bit multiply math to snap this out */
  959.                     lv[0] = col;
  960.                     lv[0] = lv[0] << 16;
  961.                     lv[1] = currow;
  962.                     lv[1] = lv[1] << 16;
  963.                     if(filetype || pot16bit) /* don't truncate fractional part */
  964.                         lv[2] = f_cur.color*65536.0;
  965.                     else    /* there IS no fractaional part here! */
  966.                     {
  967.                         lv[2] = f_cur.color;
  968.                         lv[2] = lv[2] << 16;
  969.                     }
  970.  
  971.                     if(longvmultpersp(lv,lm,lv0,lv,lview,16) == -1)
  972.                     {
  973.                         cur    = bad;
  974.                         f_cur = f_bad;
  975.                         goto loopbottom;
  976.                     }
  977.  
  978.                     cur.x = ((lv[0]+32768L) >> 16) + xxadjust;
  979.                     cur.y = ((lv[1]+32768L) >> 16) + yyadjust;
  980.                     if(FILLTYPE >= 5 && !overflow)
  981.                     {
  982.                         f_cur.x        = lv0[0];
  983.                         f_cur.x            /= 65536.0;
  984.                         f_cur.y        = lv0[1];
  985.                         f_cur.y            /= 65536.0;
  986.                         f_cur.color  = lv0[2];
  987.                         f_cur.color /= 65536.0;
  988.                     }
  989.                 }
  990.  
  991.                 if(usr_floatflag || overflow || RAY)
  992.                 /* do in float if integer math overflowed or doing Ray trace */
  993.                 {
  994.                     /* slow float version for comparison */
  995.                     v[0] = col;
  996.                     v[1] = currow;
  997.                     v[2] = f_cur.color; /* Actually the z value */
  998.  
  999.                     mult_vec(v); /* matrix*vector routine */
  1000.  
  1001.                     if (FILLTYPE > 4 || RAY)
  1002.                     {
  1003.                         f_cur.x    = v[0];
  1004.                         f_cur.y    = v[1];
  1005.                         f_cur.color = v[2];
  1006.  
  1007.                         if(RAY == 6)
  1008.                         {
  1009.                             f_cur.x = f_cur.x * (2.0 / xdots) - 1;
  1010.                             f_cur.y = f_cur.y * (2.0 / ydots) - 1;
  1011.                             f_cur.color = -f_cur.color * (2.0 / numcolors) - 1;
  1012.                         }
  1013.                     }
  1014.  
  1015.                     if(persp && !RAY)
  1016.                         perspective(v);
  1017.                     cur.x = v[0] + xxadjust + .5;
  1018.                     cur.y = v[1] + yyadjust + .5;
  1019.  
  1020.                     v[0] = 0;
  1021.                     v[1] = 0;
  1022.                     v[2] = WATERLINE;
  1023.                     mult_vec(v);
  1024.                     f_water = v[2];
  1025.                 }
  1026.             }
  1027.  
  1028.             if (RANDOMIZE)
  1029.                 if (cur.color > WATERLINE)
  1030.                 {
  1031.                     RND = rand15() >> 8;    /* 7-bit number */
  1032.                     RND = RND * RND >> rand_factor;    /* n-bit number */
  1033.  
  1034.                     if (rand() & 1)
  1035.                         RND = -RND; /* Make +/- n-bit number */
  1036.  
  1037.                     if ((int)(cur.color) + RND >= colors)
  1038.                         cur.color = colors-2;
  1039.                     else if ((int)(cur.color) + RND <= WATERLINE)
  1040.                         cur.color = WATERLINE + 1;
  1041.                     else
  1042.                         cur.color = cur.color + RND;
  1043.                     Real_Color = cur.color;
  1044.                 }
  1045.  
  1046.             if (RAY)
  1047.             {
  1048.                 if (col && currow &&
  1049.                     old.x > bad_check &&
  1050.                     old.x < (xdots - bad_check) &&
  1051.                     lastrow[col].x > bad_check &&
  1052.                     lastrow[col].y > bad_check &&
  1053.                     lastrow[col].x < (xdots - bad_check) &&
  1054.                     lastrow[col].y < (ydots - bad_check))
  1055.                 {
  1056.                     /* Get rid of all the triangles in the plane
  1057.                         at the base of the object */
  1058.  
  1059.                     if (f_cur.color == f_water &&
  1060.                         f_lastrow[col].color == f_water &&
  1061.                         f_lastrow[next].color == f_water)
  1062.                             goto loopbottom;
  1063.  
  1064.                     if (RAY != 6) /* Output the vertex info */
  1065.                         out_triangle(f_cur, f_old, f_lastrow[col],
  1066.                             cur.color, old.color, lastrow[col].color);
  1067.  
  1068.                     tout = 1;
  1069.  
  1070.                     draw_line (old.x, old.y, cur.x, cur.y, old.color);
  1071.                     draw_line (old.x, old.y, lastrow[col].x,
  1072.                         lastrow[col].y, old.color);
  1073.                     draw_line (lastrow[col].x, lastrow[col].y,
  1074.                         cur.x, cur.y, cur.color);
  1075.                     num_tris++;
  1076.                 }
  1077.  
  1078.                 if (col < lastdot && currow &&
  1079.                     lastrow[col].x > bad_check &&
  1080.                     lastrow[col].y > bad_check &&
  1081.                     lastrow[col].x < (xdots - bad_check) &&
  1082.                     lastrow[col].y < (ydots - bad_check) &&
  1083.                     lastrow[next].x > bad_check &&
  1084.                     lastrow[next].y > bad_check &&
  1085.                     lastrow[next].x < (xdots - bad_check) &&
  1086.                     lastrow[next].y < (ydots - bad_check))
  1087.                 {
  1088.                     /* Get rid of all the triangles in the plane
  1089.                     at the base of the object */
  1090.  
  1091.                     if (f_cur.color == f_water &&
  1092.                         f_lastrow[col].color == f_water &&
  1093.                         f_lastrow[next].color == f_water)
  1094.                             goto loopbottom;
  1095.  
  1096.                     if (RAY != 6) /* Output the vertex info */
  1097.                         out_triangle(f_cur, f_lastrow[col], f_lastrow[next],
  1098.                             cur.color, lastrow[col].color, lastrow[next].color);
  1099.  
  1100.                     tout = 1;
  1101.  
  1102.                     draw_line (lastrow[col].x, lastrow[col].y, cur.x, cur.y,
  1103.                         cur.color);
  1104.                     draw_line (lastrow[next].x, lastrow[next].y, cur.x, cur.y,
  1105.                         cur.color);
  1106.                     draw_line (lastrow[next].x, lastrow[next].y, lastrow[col].x,
  1107.                         lastrow[col].y, lastrow[col].color);
  1108.                     num_tris++;
  1109.                 }
  1110.  
  1111.                 if (RAY == 6)  /* Output vertex info for Acrospin */
  1112.                 {
  1113.                     fprintf (File_Ptr1, "% #4.4f % #4.4f % #4.4f R%dC%d\n",
  1114.                         f_cur.x, f_cur.y, f_cur.color, RO, CO);
  1115.                     if (CO > CO_MAX)
  1116.                         CO_MAX = CO;
  1117.                     CO++;
  1118.                 }
  1119.                 goto loopbottom;
  1120.  
  1121.             }
  1122.  
  1123.             switch(FILLTYPE)
  1124.             {
  1125.                 case -1:
  1126.                     if (col &&
  1127.                     old.x > bad_check &&
  1128.                     old.x < (xdots - bad_check))
  1129.                         draw_line (old.x, old.y, cur.x, cur.y, cur.color);
  1130.                     if (currow &&
  1131.                     lastrow[col].x > bad_check &&
  1132.                     lastrow[col].y > bad_check &&
  1133.                     lastrow[col].x < (xdots - bad_check) &&
  1134.                     lastrow[col].y < (ydots - bad_check))
  1135.                         draw_line (lastrow[col].x,lastrow[col].y,cur.x,
  1136.                             cur.y,cur.color);
  1137.                     break;
  1138.         
  1139.                 case 0:
  1140.                     (*plot)(cur.x,cur.y,cur.color);
  1141.                     break;
  1142.         
  1143.                 case 1:            /* connect-a-dot */
  1144.                     if ((old.x < xdots) && (col) &&
  1145.                         old.x > bad_check &&
  1146.                         old.y > bad_check) /* Don't draw from old to cur on col 0 */
  1147.                             draw_line(old.x,old.y,cur.x,cur.y,cur.color);
  1148.                     break;
  1149.         
  1150.                 case 2: /* with interpolation */
  1151.                 case 3: /* no interpolation */
  1152.                     /***************************************************************/
  1153.                     /*  "triangle fill" - consider four points: current point,        */
  1154.                     /*  previous point same row, point opposite current point in    */
  1155.                     /*  previous row, point after current point in previous row.    */
  1156.                     /*  The object is to fill all points inside the two triangles.    */
  1157.                     /*                                                                                    */
  1158.                     /*    lastrow[col].x/y___ lastrow[next]                                    */
  1159.                     /*              /    1         /                                                        */
  1160.                     /*            /        1       /                                                        */
  1161.                     /*            /        1     /                                                         */
  1162.                     /*  oldrow/col _____ trow/col                                                */
  1163.                     /***************************************************************/
  1164.         
  1165.                     if(currow && !col)
  1166.                         putatriangle(lastrow[next],lastrow[col],cur,cur.color);
  1167.                     if(currow && col)        /* skip first row and first column */
  1168.                     {
  1169.                         if(col == 1)
  1170.                             putatriangle(lastrow[col],oldlast,old,old.color);
  1171.         
  1172.                         if(col < lastdot)
  1173.                             putatriangle(lastrow[next],lastrow[col], cur, cur.color);
  1174.                         putatriangle(old, lastrow[col], cur, cur.color);
  1175.                     }
  1176.                     break;
  1177.         
  1178.                 case 4: /* "solid fill" */
  1179.                     if (SPHERE)
  1180.                     {
  1181.                         if (persp)
  1182.                         {
  1183.                             old.x = xcenter>>16;
  1184.                             old.y = ycenter>>16;
  1185.                         }
  1186.                         else
  1187.                         {
  1188.                             old.x = xcenter;
  1189.                             old.y = ycenter;
  1190.                         }
  1191.                     }
  1192.                     else
  1193.                     {
  1194.                         lv[0] = col;
  1195.                         lv[1] = currow;
  1196.                         lv[2] = 0;
  1197.         
  1198.                         /* apply fudge bit shift for integer math */
  1199.                         lv[0] = lv[0] << 16;
  1200.                         lv[1] = lv[1] << 16;
  1201.                         /* Since 0, unnecessary lv[2] = lv[2] << 16;*/
  1202.         
  1203.                         if(longvmultpersp(lv,lm,lv0,lv,lview,16))
  1204.                         {
  1205.                             cur    = bad;
  1206.                             f_cur = f_bad;
  1207.                             goto loopbottom;    /* another goto ! */
  1208.                         }
  1209.         
  1210.                         /*    Round and fudge back to original  */
  1211.                         old.x = (lv[0]+32768L) >> 16;
  1212.                         old.y = (lv[1]+32768L) >> 16;
  1213.                     }
  1214.                     if (old.x < 0)
  1215.                         old.x = 0;
  1216.                     if (old.x >= xdots)
  1217.                         old.x = xdots-1;
  1218.                     if (old.y < 0)
  1219.                         old.y = 0;
  1220.                     if (old.y >= ydots)
  1221.                         old.y = ydots-1;
  1222.                     draw_line(old.x,old.y,cur.x,cur.y,cur.color);
  1223.                     break;
  1224.         
  1225.                 case 5:
  1226.                 case 6:
  1227.                     /* light-source modulated fill */
  1228.                     if(currow && col)        /* skip first row and first column */
  1229.                     {
  1230.                         if(f_cur.color < bad_check || f_old.color < bad_check ||
  1231.                             f_lastrow[col].color < bad_check)
  1232.                                 break;
  1233.         
  1234.                         v1[0] = f_cur.x        - f_old.x;
  1235.                         v1[1] = f_cur.y        - f_old.y;
  1236.                         v1[2] = f_cur.color - f_old.color;
  1237.         
  1238.                         v2[0] = f_lastrow[col].x            - f_cur.x;
  1239.                         v2[1] = f_lastrow[col].y            - f_cur.y;
  1240.                         v2[2] = f_lastrow[col].color - f_cur.color;
  1241.         
  1242.                         cross_product (v1, v2, cross);
  1243.         
  1244.                         /* normalize cross - and check if non-zero */
  1245.                         if(normalize_vector(cross))
  1246.                         {
  1247.                             if(debugflag)
  1248.                             {
  1249.                                 static char far msg[] = {"debug, cur.color=bad"};
  1250.                                 stopmsg(0,msg);
  1251.                             }
  1252.                             cur.color = f_cur.color = bad.color;
  1253.                         }
  1254.                         else
  1255.                         {
  1256.                             /* line-wise averaging scheme */
  1257.                             if(LIGHTAVG>0)
  1258.                             {
  1259.                                 if(crossnotinit)
  1260.                                 {
  1261.                                     /* initialize array of old normal vectors */
  1262.                                     crossavg[0] = cross[0];
  1263.                                     crossavg[1] = cross[1];
  1264.                                     crossavg[2] = cross[2];
  1265.                                     crossnotinit = 0;
  1266.                                 }
  1267.                                 tmpcross[0] = (crossavg[0]*LIGHTAVG+cross[0]) /
  1268.                                    (LIGHTAVG+1);
  1269.                                 tmpcross[1] = (crossavg[1]*LIGHTAVG+cross[1]) /
  1270.                                    (LIGHTAVG+1);
  1271.                                 tmpcross[2] = (crossavg[2]*LIGHTAVG+cross[2]) /
  1272.                                    (LIGHTAVG+1);
  1273.             
  1274.                                 cross[0] = tmpcross[0];
  1275.                                 cross[1] = tmpcross[1];
  1276.                                 cross[2] = tmpcross[2];
  1277.  
  1278.                                 if(normalize_vector(cross))
  1279.                                 {
  1280.                                     /* this shouldn't happen */
  1281.                                     if(debugflag)
  1282.                                     {
  1283.                                         static char far msg[] =
  1284.                                         {"debug, normal vector err2"};
  1285.                                         stopmsg(0,msg);
  1286.                                         /* use next instead if you ever need details:
  1287.                                         static char far tmp[] = {"debug, vector err"};
  1288.                                         char msg[200];
  1289. #ifndef XFRACT
  1290.                                         sprintf(msg,"%Fs\n%f %f %f\n%f %f %f\n%f %f %f",
  1291. #else
  1292.                                         sprintf(msg,"%s\n%f %f %f\n%f %f %f\n%f %f %f",
  1293. #endif
  1294.                                         tmp, f_cur.x, f_cur.y, f_cur.color,
  1295.                                         f_lastrow[col].x, f_lastrow[col].y,
  1296.                                         f_lastrow[col].color, f_lastrow[col-1].x,
  1297.                                         f_lastrow[col-1].y,f_lastrow[col-1].color);
  1298.                                         stopmsg(0,msg);
  1299.                                         */
  1300.                                     }
  1301.                                     cur.color = f_cur.color = colors;
  1302.                                 }
  1303.                             }
  1304.                             crossavg[0] = tmpcross[0];
  1305.                             crossavg[1] = tmpcross[1];
  1306.                             crossavg[2] = tmpcross[2];
  1307.  
  1308.                             /* dot product of unit vectors is cos of angle between */
  1309.                             /* we will use this value to shade surface */
  1310.         
  1311.                             cur.color = 1 + (colors-2) *
  1312.                                 (1.0-dot_product(cross,light_direction));
  1313.                         }
  1314.                         /* if colors out of range, set them to min or max color 
  1315.                             index but avoid background index. This makes colors
  1316.                             "opaque" so SOMETHING plots. These conditions shouldn't 
  1317.                             happen but just in case                    */
  1318.                         if(cur.color < 1)            /* prevent transparent colors */
  1319.                             cur.color = 1;        /* avoid background */
  1320.                         if(cur.color > colors-1)
  1321.                             cur.color = colors-1;
  1322.         
  1323.                     /* why "col < 2"? So we have sufficient geometry for the fill */
  1324.                     /* algorithm, which needs previous point in same row to have  */
  1325.                     /* already been calculated (variable old)        */
  1326.                     /* fix ragged left margin in preview */
  1327.                         if (col == 1 && currow > 1)
  1328.                             putatriangle(lastrow[next],lastrow[col],cur,cur.color);
  1329.         
  1330.                         if(col < 2 || currow < 2) /* don't have valid colors yet */
  1331.                             break;
  1332.         
  1333.                         if(col < lastdot)
  1334.                             putatriangle(lastrow[next],lastrow[col],cur,cur.color);
  1335.                         putatriangle(old,lastrow[col],cur,cur.color);
  1336.         
  1337.                         plot=standardplot;
  1338.                     }
  1339.                     break;
  1340.             }  /*    End of CASE statement for fill type  */
  1341.         
  1342. loopbottom:
  1343.  
  1344.             if (RAY || (FILLTYPE != 0 && FILLTYPE != 4))
  1345.             {
  1346.                 /* for triangle and grid fill purposes */
  1347.                 oldlast = lastrow[col];
  1348.                 old = lastrow[col] = cur;
  1349.         
  1350.                 /* for illumination model purposes */
  1351.                 f_old    = f_lastrow[col] = f_cur;
  1352.                 if (currow && RAY && col >= lastdot)
  1353.                 /* if we're at the end of a row, close the object */
  1354.                 {
  1355.                     end_object(tout);
  1356.                     tout=0;
  1357.                     if (ferror (File_Ptr1))
  1358.                     {
  1359.                         fclose (File_Ptr1);
  1360.                         remove (light_name);
  1361.                         File_Error(ray_name, 2);
  1362.                         EXIT_OVLY;
  1363.                         return(-1);
  1364.                     }
  1365.                 }
  1366.             }
  1367.         
  1368.             col++;
  1369.  
  1370.         }  /*  End of while statement for plotting line  */
  1371.  
  1372.     RO++;
  1373.  
  1374. reallythebottom:
  1375.         
  1376.     /* stuff that HAS to be done, even in preview mode, goes here */    
  1377.     if(SPHERE)    
  1378.     {    
  1379.         /* incremental sin/cos phi calc */    
  1380.         if(currow == 0)    
  1381.         {    
  1382.             sinphi = oldsinphi2;    
  1383.             cosphi = oldcosphi2;    
  1384.         }    
  1385.         else    
  1386.         {    
  1387.             sinphi = twocosdeltaphi*oldsinphi2 - oldsinphi1;    
  1388.             cosphi = twocosdeltaphi*oldcosphi2 - oldcosphi1;    
  1389.             oldsinphi1 = oldsinphi2;    
  1390.             oldsinphi2 = sinphi;    
  1391.             oldcosphi1 = oldcosphi2;    
  1392.             oldcosphi2 = cosphi;    
  1393.         }    
  1394.     }
  1395.     EXIT_OVLY;
  1396.     return(0); /* decoder needs to know all is well !!! */
  1397. }
  1398.  
  1399.  
  1400. /* vector version of line draw */
  1401. static void _fastcall vdraw_line(v1,v2,color)
  1402. double *v1,*v2;
  1403. int color;
  1404. {
  1405.     int x1,y1,x2,y2;
  1406.     x1 = (int)v1[0];
  1407.     y1 = (int)v1[1];
  1408.     x2 = (int)v2[0];
  1409.     y2 = (int)v2[1];
  1410.     draw_line(x1,y1,x2,y2,color);
  1411. }
  1412.  
  1413.  
  1414.  
  1415. static void corners(m,show,pxmin,pymin,pzmin,pxmax,pymax,pzmax)
  1416.  
  1417. MATRIX m;
  1418. int show; /* turns on box-showing feature */
  1419. double *pxmin,*pymin,*pzmin,*pxmax,*pymax,*pzmax;
  1420.  
  1421. {
  1422.     int i,j;
  1423.     VECTOR S[2][4]; /* Holds the top an bottom points, S[0][]=bottom */
  1424.  
  1425.     /*
  1426.     define corners of box fractal is in in x,y,z plane
  1427.     "b" stands for "bottom" - these points are the corners of the screen
  1428.     in the x-y plane. The "t"'s stand for Top - they are the top of
  1429.     the cube where 255 color points hit.
  1430.     */
  1431.  
  1432.     *pxmin = *pymin = *pzmin = (int)INT_MAX;
  1433.     *pxmax = *pymax = *pzmax = (int)INT_MIN; 
  1434.  
  1435.     for (j = 0; j < 4; ++j)
  1436.         for (i=0;i<3;i++)
  1437.             S[0][j][i]=S[1][j][i]=0;
  1438.  
  1439.     S[0][1][0] = S[0][2][0] = S[1][1][0] = S[1][2][0] = xdots-1;
  1440.     S[0][2][1] = S[0][3][1] = S[1][2][1] = S[1][3][1] = ydots-1;
  1441.     S[1][0][2] = S[1][1][2] = S[1][2][2] = S[1][3][2] = zcoord-1;
  1442.  
  1443.     for (i = 0; i < 4; ++i)
  1444.     {
  1445.         /* transform points */
  1446.         vmult(S[0][i],m,S[0][i]);
  1447.         vmult(S[1][i],m,S[1][i]);
  1448.  
  1449.         /* update minimums and maximums */
  1450.         if (S[0][i][0] <= *pxmin) *pxmin = S[0][i][0];
  1451.         if (S[0][i][0] >= *pxmax) *pxmax = S[0][i][0];
  1452.         if (S[1][i][0] <= *pxmin) *pxmin = S[1][i][0];
  1453.         if (S[1][i][0] >= *pxmax) *pxmax = S[1][i][0];
  1454.         if (S[0][i][1] <= *pymin) *pymin = S[0][i][1];
  1455.         if (S[0][i][1] >= *pymax) *pymax = S[0][i][1];
  1456.         if (S[1][i][1] <= *pymin) *pymin = S[1][i][1];
  1457.         if (S[1][i][1] >= *pymax) *pymax = S[1][i][1];
  1458.         if (S[0][i][2] <= *pzmin) *pzmin = S[0][i][2];
  1459.         if (S[0][i][2] >= *pzmax) *pzmax = S[0][i][2];
  1460.         if (S[1][i][2] <= *pzmin) *pzmin = S[1][i][2];
  1461.         if (S[1][i][2] >= *pzmax) *pzmax = S[1][i][2];
  1462.     }
  1463.  
  1464.     if(show)
  1465.     {
  1466.         if(persp)
  1467.         {
  1468.             for (i=0;i<4;i++)
  1469.             {
  1470.                 perspective(S[0][i]);
  1471.                 perspective(S[1][i]);
  1472.             }
  1473.         }
  1474.  
  1475.         /* Keep the box surrounding the fractal */
  1476.         for (j=0;j<2;j++)
  1477.             for (i = 0; i < 4; ++i)
  1478.             {
  1479.                 S[j][i][0] += xxadjust;
  1480.                 S[j][i][1] += yyadjust;
  1481.             }
  1482.  
  1483.         draw_rect(S[0][0],S[0][1],S[0][2],S[0][3],2,1);/* Bottom */
  1484.  
  1485.         draw_rect(S[0][0],S[1][0],S[0][1],S[1][1], 5, 0); /* Sides */
  1486.         draw_rect(S[0][2],S[1][2],S[0][3],S[1][3], 6, 0);
  1487.  
  1488.         draw_rect(S[1][0],S[1][1],S[1][2],S[1][3],8,1); /* Top */
  1489.     }
  1490. }
  1491.  
  1492.  
  1493.  
  1494. /* This function draws a vector from origin[] to direct[] and a box
  1495.     around it. The vector and box are transformed or not depending on
  1496.     FILLTYPE.
  1497.  
  1498. */
  1499.  
  1500. static void draw_light_box (origin, direct, light_m)
  1501. double *origin, *direct;
  1502. MATRIX light_m;
  1503.  
  1504.  
  1505. {
  1506.     VECTOR S[2][4];
  1507.     int i,j;
  1508.     double temp;
  1509.  
  1510.     S[1][0][0] = S[0][0][0] = origin[0];
  1511.     S[1][0][1] = S[0][0][1] = origin[1];
  1512.  
  1513.     S[1][0][2] = direct[2];
  1514.  
  1515.     for (i=0;i<2;i++)
  1516.     {
  1517.         S[i][1][0] = S[i][0][0];
  1518.         S[i][1][1] = direct[1];
  1519.         S[i][1][2] = S[i][0][2];
  1520.         S[i][2][0] = direct[0];
  1521.         S[i][2][1] = S[i][1][1];
  1522.         S[i][2][2] = S[i][0][2];
  1523.         S[i][3][0] = S[i][2][0];
  1524.         S[i][3][1] = S[i][0][1];
  1525.         S[i][3][2] = S[i][0][2];
  1526.     }
  1527.  
  1528.     /* transform the corners if necessary */
  1529.     if (FILLTYPE == 6)
  1530.         for (i=0;i<4;i++)
  1531.         {
  1532.             vmult (S[0][i],light_m,S[0][i]);
  1533.             vmult (S[1][i],light_m,S[1][i]);
  1534.         }
  1535.  
  1536.     /* always use perspective to aid viewing */
  1537.     temp = view[2]; /* save perspective distance for a later restore */
  1538.     view[2] = - P * 300.0 / 100.0;
  1539.  
  1540.     for (i=0;i<4;i++)
  1541.     {
  1542.         perspective(S[0][i]);
  1543.         perspective(S[1][i]);
  1544.     }
  1545.     view[2] = temp; /* Restore perspective distance*/
  1546.  
  1547.     /* Adjust for aspect */
  1548.     for (i=0;i<4;i++)
  1549.     {
  1550.         S[0][i][0] = S[0][i][0] * aspect;
  1551.         S[1][i][0] = S[1][i][0] * aspect;
  1552.     }
  1553.  
  1554.     /* draw box connecting transformed points. NOTE order and COLORS */
  1555.     draw_rect(S[0][0],S[0][1],S[0][2],S[0][3],2,1);
  1556.  
  1557.     vdraw_line (S[0][0],S[1][2],8);
  1558.  
  1559.     /* sides */
  1560.     draw_rect(S[0][0],S[1][0],S[0][1],S[1][1], 4, 0);
  1561.     draw_rect(S[0][2],S[1][2],S[0][3],S[1][3], 5, 0);
  1562.  
  1563.     draw_rect(S[1][0],S[1][1],S[1][2],S[1][3],3,1);
  1564.  
  1565.     /* Draw the "arrow head" */
  1566.     for (i=-3;i<4;i++)
  1567.         for (j=-3;j<4;j++)
  1568.             if (abs(i) + abs(j) < 6)
  1569.                 plot((int)(S[1][2][0]+i),(int)(S[1][2][1]+j),10);
  1570. }
  1571.  
  1572.  
  1573.  
  1574. static void draw_rect (V0, V1, V2, V3, color, rect)
  1575.  
  1576. VECTOR V0, V1, V2, V3;
  1577. int color, rect;
  1578.  
  1579. {
  1580. VECTOR V[4];
  1581. int i;
  1582.  
  1583.     for (i=0;i<2;i++) 
  1584.     { /* Since V[2] is not used by vdraw_line don't bother setting it */    
  1585.         V[0][i] = V0[i];    
  1586.         V[1][i] = V1[i];    
  1587.         V[2][i] = V2[i];    
  1588.         V[3][i] = V3[i];    
  1589.     }    
  1590.     if (rect) /* Draw a rectangle */
  1591.     {
  1592.         for (i=0;i<4;i++)
  1593.             if (fabs(V[i][0]-V[(i+1)%4][0]) < -2*bad_check &&
  1594.                 fabs(V[i][1]-V[(i+1)%4][1]) < -2*bad_check)
  1595.                     vdraw_line (V[i],V[(i+1)%4],color);
  1596.     }
  1597.     else /* Draw 2 lines instead */
  1598.     {
  1599.         for(i=0;i<3;i+=2)
  1600.             if (fabs(V[i][0]-V[i+1][0]) < -2*bad_check &&
  1601.                 fabs(V[i][1]-V[i+1][1]) < -2*bad_check)
  1602.                     vdraw_line (V[i],V[i+1],color);
  1603.     }
  1604.     return;
  1605. }
  1606.  
  1607.  
  1608.  
  1609. /* replacement for plot - builds a table of min and max x's instead of plot */
  1610. /* called by draw_line as part of triangle fill routine */
  1611. static void _fastcall putminmax(int x,int y,int color)
  1612. {
  1613.     if(y >= 0 && y < ydots)
  1614.     {
  1615.         if(x < minmax_x[y].minx) minmax_x[y].minx = x;
  1616.         if(x > minmax_x[y].maxx) minmax_x[y].maxx = x;
  1617.     }
  1618. }
  1619.  
  1620. /*
  1621.     This routine fills in a triangle. Extreme left and right values for
  1622.     each row are calculated by calling the line function for the sides.
  1623.     Then rows are filled in with horizontal lines
  1624. */
  1625. #define MAXOFFSCREEN  2 /* allow two of three points to be off screen */
  1626.  
  1627. static void _fastcall putatriangle(struct point pt1,struct point pt2,
  1628.                     struct point pt3,int color)
  1629. {
  1630.     extern struct point p1,p2,p3;
  1631.     int miny,maxy;
  1632.     int x,y,xlim;
  1633.  
  1634.     /* Too many points off the screen? */
  1635.     if(offscreen(pt1) + offscreen(pt2) + offscreen(pt3) > MAXOFFSCREEN)
  1636.         return;
  1637.  
  1638.     p1 = pt1; /* needed by interpcolor */
  1639.     p2 = pt2;
  1640.     p3 = pt3;
  1641.  
  1642.     /* fast way if single point or single line */
  1643.     if (p1.y == p2.y && p1.x == p2.x)
  1644.     {
  1645.         plot = fillplot;
  1646.         if (p1.y == p3.y && p1.x == p3.x)
  1647.             (*plot)(p1.x, p1.y, color);
  1648.         else
  1649.             draw_line(p1.x,p1.y,p3.x,p3.y,color);
  1650.         plot = normalplot;
  1651.         return;
  1652.     }
  1653.     else if ( (p3.y == p1.y && p3.x == p1.x) || (p3.y == p2.y && p3.x == p2.x) )
  1654.     {
  1655.         plot = fillplot;
  1656.         draw_line(p1.x,p1.y,p2.x,p2.y,color);
  1657.         plot = normalplot;
  1658.         return;
  1659.     }
  1660.  
  1661.     /* find min max y */
  1662.     miny = maxy = p1.y;
  1663.     if(p2.y < miny) miny = p2.y;
  1664.     else         maxy = p2.y;
  1665.     if(p3.y < miny) miny = p3.y;
  1666.     else if(p3.y > maxy) maxy = p3.y;
  1667.  
  1668.     /* only worried about values on screen */
  1669.     if(miny < 0)        miny = 0;
  1670.     if(maxy >= ydots) maxy = ydots-1;
  1671.  
  1672.     for(y=miny;y<=maxy;y++)
  1673.     {
  1674.         minmax_x[y].minx = (int)INT_MAX;
  1675.         minmax_x[y].maxx = (int)INT_MIN;
  1676.     }
  1677.  
  1678.     /* set plot to "fake" plot function */
  1679.     plot = putminmax;
  1680.  
  1681.     /* build table of extreme x's of triangle */
  1682.     draw_line(p1.x,p1.y,p2.x,p2.y,0);
  1683.     draw_line(p2.x,p2.y,p3.x,p3.y,0);
  1684.     draw_line(p3.x,p3.y,p1.x,p1.y,0);
  1685.  
  1686.     for(y=miny;y<=maxy;y++)
  1687.     {
  1688.         xlim = minmax_x[y].maxx;
  1689.         for(x=minmax_x[y].minx;x<=xlim;x++)
  1690.         (*fillplot)(x,y,color);
  1691.     }
  1692.     plot = normalplot;
  1693. }
  1694.  
  1695.  
  1696. static int _fastcall offscreen(struct point pt)
  1697. {
  1698.     if(pt.x >= 0)
  1699.         if(pt.x < xdots)
  1700.             if(pt.y >= 0)
  1701.                 if(pt.y < ydots)
  1702.                     return(0); /* point is ok */
  1703.     if (abs(pt.x) > 0-bad_check || abs(pt.y) > 0-bad_check)
  1704.         return(99); /* point is bad */
  1705.     return(1); /* point is off the screen */
  1706. }
  1707.  
  1708. static void _fastcall clipcolor(int x,int y,int color)
  1709. {
  1710.     if(0 <= x    && x < xdots    &&
  1711.         0 <= y    && y < ydots    &&
  1712.         0 <= color && color < filecolors)
  1713.     {
  1714.         standardplot(x,y,color);
  1715.  
  1716.         if (Targa_Out)
  1717.             if (!(glassestype==1 || glassestype==2)) /* standardplot modifies color in these types */
  1718.                 targa_color(x, y, color);
  1719.     }
  1720. }
  1721.  
  1722. /*********************************************************************/
  1723. /* This function is the same as clipcolor but checks for color being */
  1724. /* in transparent range. Intended to be called only if transparency  */
  1725. /* has been enabled.                                                 */
  1726. /*********************************************************************/
  1727.  
  1728. static void _fastcall T_clipcolor(int x,int y,int color)
  1729. {
  1730.     if (0 <= x        && x < xdots            && /*  is the point on screen?  */
  1731.         0 <= y    && y < ydots            && /*  Yes?  */
  1732.         0 <= color && color < colors    && /*  Colors in valid range?  */
  1733.         /*  Lets make sure its not a transparent color  */
  1734.         (transparent[0] > color || color > transparent[1]))
  1735.     {
  1736.         standardplot(x,y,color); /* I guess we can plot then  */
  1737.  
  1738.         if (Targa_Out)
  1739.             if (!(glassestype==1 || glassestype==2)) /* standardplot modifies color in these types */
  1740.                 targa_color(x, y, color);
  1741.     }
  1742. }
  1743.  
  1744. /************************************************************************/
  1745. /* A substitute for plotcolor that interpolates the colors according    */
  1746. /* to the x and y values of three points (p1,p2,p3) which are static in    */
  1747. /* this routine                                                            */
  1748. /*                                                                        */
  1749. /*    In Light source modes, color is light value, not actual color        */
  1750. /*    Real_Color always contains the actual color                            */
  1751. /************************************************************************/
  1752.  
  1753. static void _fastcall interpcolor(int x,int y,int color)
  1754. {
  1755.     int D,d1,d2,d3;
  1756.  
  1757.   /* this distance formula is not the usual one - but it has the virtue
  1758.         that it uses ONLY additions (almost) and it DOES go to zero as the
  1759.         points get close.
  1760.         */
  1761.  
  1762.     d1 = abs(p1.x-x)+abs(p1.y-y);
  1763.     d2 = abs(p2.x-x)+abs(p2.y-y);
  1764.     d3 = abs(p3.x-x)+abs(p3.y-y);
  1765.  
  1766.     D = (d1 + d2 + d3) << 1;
  1767.     if(D)
  1768.     {  /* calculate a weighted average of colors -
  1769.         long casts prevent integer overflow. This can evaluate to zero */
  1770.         color = ((long)(d2+d3)*(long)p1.color +
  1771.             (long)(d1+d3)*(long)p2.color +
  1772.             (long)(d1+d2)*(long)p3.color) / D;
  1773.     }
  1774.     
  1775.     if(0 <= x        && x < xdots    &&
  1776.         0 <= y        && y < ydots    &&
  1777.         0 <= color && color < colors &&
  1778.         (transparent[1] == 0 || Real_Color > transparent[1] ||
  1779.         transparent[0] > Real_Color))
  1780.     {
  1781.         if (Targa_Out)
  1782.             if (!(glassestype==1 || glassestype==2)) /* standardplot modifies color in these types */
  1783.                 D = targa_color(x, y, color);
  1784.  
  1785.         if (FILLTYPE >= 5)
  1786.             if (Real_V && Targa_Out)
  1787.                 color = D;
  1788.             else
  1789.                         {
  1790.                             color =  (1 + (unsigned)color * IAmbient)/256;
  1791.                             if (color == 0)
  1792.                                 color = 1;
  1793.                         }
  1794.         standardplot(x,y,color);
  1795.     }
  1796. }
  1797.  
  1798.  
  1799.  
  1800. /*
  1801.     In non light source modes, both color and Real_Color contain the
  1802.     actual pixel color. In light source modes, color contains the
  1803.     light value, and Real_Color contains the origninal color
  1804.  
  1805.     This routine takes a pixel modifies it for lightshading if appropriate
  1806.     and plots it in a Targa file. Used in plot3d.c
  1807. */
  1808.  
  1809. int _fastcall targa_color(int x,int y,int color)
  1810. {
  1811.     unsigned long H, S, V;
  1812.     BYTE RGB[3];
  1813.  
  1814.     if (FILLTYPE == 2 || glassestype==1 || glassestype==2)  
  1815.         Real_Color = color; /* So Targa gets interpolated color */
  1816.  
  1817.     RGB[0]  =  dacbox [Real_Color] [0] << 2; /* Move color space to */
  1818.     RGB[1]  =  dacbox [Real_Color] [1] << 2; /* 256 color primaries */
  1819.     RGB[2]  =  dacbox [Real_Color] [2] << 2; /* from 64 colors */
  1820.  
  1821.     /* Now lets convert it to HSV */
  1822.     R_H(RGB[0], RGB[1], RGB[2], &H, &S, &V);
  1823.  
  1824.     /* Modify original S and V components */
  1825.     if (FILLTYPE > 4 && !(glassestype==1 || glassestype==2)) /* Adjust for Ambient */
  1826.         V = (V * (65535 - (unsigned)(color * IAmbient))) / 65535;
  1827.  
  1828.     if (haze)
  1829.     {
  1830.         S = (unsigned long)(S * HAZE_MULT) / 100; /* Haze lowers sat of colors */
  1831.         if (V >= 32640) /* Haze reduces contrast */
  1832.         {
  1833.             V = V - 32640;
  1834.             V = (unsigned long)((V * HAZE_MULT) / 100);
  1835.             V = V + 32640;
  1836.         }
  1837.         else
  1838.         {
  1839.             V = 32640 - V;
  1840.             V = (unsigned long)((V * HAZE_MULT) / 100);
  1841.             V = 32640 - V;
  1842.         }
  1843.     }
  1844.     /* Now lets convert it back to RGB. Original Hue, modified Sat and Val */
  1845.     H_R(&RGB[0], &RGB[1], &RGB[2], H, S, V);
  1846.  
  1847.     if (Real_V)
  1848.         V = (35 * (int)RGB[0] + 45 * (int)RGB[1] + 20 * (int)RGB[2]) / 100;
  1849.  
  1850.     /* Now write the color triple to its transformed location */
  1851.     /* on the disk. */
  1852.     targa_writedisk (x+sxoffs, y+syoffs, RGB[0], RGB[1], RGB[2]);
  1853.  
  1854.     return(255-V);
  1855. }
  1856.  
  1857.  
  1858.  
  1859. static int set_pixel_buff(BYTE *pixels,BYTE far *fraction,unsigned linelen)
  1860. {
  1861.     int i;
  1862.     if ((evenoddrow++ & 1) == 0) /* even rows are color value */
  1863.     {
  1864.         for(i=0;i<linelen;i++) /* add the fractional part in odd row */
  1865.             fraction[i] = pixels[i];
  1866.         return(1);
  1867.     }
  1868.     else /* swap */
  1869.     {
  1870.         BYTE tmp;
  1871.         for(i=0;i<linelen;i++) /* swap so pixel has color */
  1872.         {
  1873.             tmp = pixels[i];
  1874.             pixels[i]=fraction[i];
  1875.             fraction[i]=tmp;
  1876.         }
  1877.     }
  1878.     return(0);
  1879. }
  1880.  
  1881.  
  1882. /* cross product  - useful because cross is perpendicular to v and w */
  1883. /**** PB commented this out - it is unused
  1884. static int chk_cross_product (int col, int row,VECTOR v, VECTOR w, VECTOR cross, VECTOR crossavg)
  1885. {
  1886.     static start = 1;
  1887.     static FILE *fp;
  1888.  
  1889.     if(start)
  1890.     {
  1891.         fp = fopen("blob","w");
  1892.         start = 0;
  1893.     }
  1894.  
  1895.     fprintf(fp,"row %+4d col %+4d v1 %+8.3e %+8.3e %+8.3e v2 %+8.3e %+8.3e %+8.3e cross %+8.3e %+8.3e %+8.3e crossavg %+8.3e %+8.3e %+8.3e \n",
  1896.     row,col,v[0],v[1],v[2],w[0],w[1],w[2],cross[0],cross[1],cross[2],crossavg[0],crossavg[1],crossavg[2]);
  1897.     return(0);
  1898. }
  1899. ***/
  1900.  
  1901. /**************************************************************************
  1902.  
  1903.         Common routine for printing error messages to the screen for Targa
  1904.         and other files
  1905.  
  1906. **************************************************************************/
  1907.  
  1908.  
  1909. #ifndef XFRACT
  1910. static char f[]                 = "%Fs%Fs";
  1911. #else
  1912. static char f[]                 = "%s%s";
  1913. #endif
  1914. static char far OOPS[]  = {"OOPS, "};
  1915. static char far E1[]        = {"can't handle this type of file.\n"};
  1916. static char far str1[]  = {"couldn't open  < "};
  1917. static char far str3[]  = {"image wrong size\n"};
  1918. static char far temp1[] = {"ran out of disk space. < "};
  1919.  
  1920. static void File_Error (char *File_Name1, int ERROR)
  1921. {
  1922.    char msgbuf[200];
  1923.  
  1924.     error = ERROR;
  1925.     switch(ERROR)
  1926.     {
  1927.     case 1: /* Can't Open */
  1928. #ifndef XFRACT
  1929.                 sprintf(msgbuf,"%Fs%Fs%s >", OOPS, str1, File_Name1);
  1930. #else
  1931.                 sprintf(msgbuf,"%s%s%s >", OOPS, str1, File_Name1);
  1932. #endif
  1933.         break;
  1934.     case 2: /* Not enough room */
  1935. #ifndef XFRACT
  1936.         sprintf(msgbuf,"%Fs%Fs%s >", OOPS, temp1, File_Name1);
  1937. #else
  1938.                 sprintf(msgbuf,"%s%s%s >", OOPS, temp1, File_Name1);
  1939. #endif
  1940.         break;
  1941.     case 3: /* Image wrong size */
  1942.         sprintf(msgbuf, f, OOPS, str3);
  1943.         break;
  1944.     case 4: /* Wrong file type */
  1945.         sprintf(msgbuf, f, OOPS, E1);
  1946.         break;
  1947.     }
  1948.     stopmsg(0,msgbuf);
  1949.     return;
  1950. }
  1951.  
  1952.  
  1953. /************************************************************************/
  1954. /*                                                                        */
  1955. /*        This function opens a TARGA_24 file for reading and writing. If    */
  1956. /*        its a new file, (overlay == 0) it writes a header. If it is to    */
  1957. /*        overlay an existing file (overlay == 1) it copies the original    */
  1958. /*        header whose lenght and validity was determined in                */
  1959. /*        Targa_validate.                                                    */
  1960. /*                                                                        */
  1961. /*        It Verifies there is enough disk space, and leaves the file     */
  1962. /*        at the start of the display data area.                            */
  1963. /*                                                                        */
  1964. /*        If this is an overlay, closes source and copies to "targa_temp"    */
  1965. /*        If there is an error close the file.                             */
  1966. /*                                                                        */
  1967. /* **********************************************************************/
  1968.  
  1969.  
  1970. static int startdisk1 (char *File_Name2, FILE *Source, int overlay)
  1971. {
  1972.     int i,j,k, inc;
  1973.     FILE *fps;
  1974.  
  1975.     /* Open File for both reading and writing */
  1976.     if ((fps=fopen(File_Name2,"w+b"))==NULL)
  1977.     {
  1978.         File_Error(File_Name2, 1);
  1979.         return(-1); /* Oops, somethings wrong! */
  1980.     }
  1981.  
  1982.     inc = 1; /* Assume we are overlaying a file */
  1983.  
  1984.     /* Write the header */
  1985.     if (overlay) /* We are overlaying a file */
  1986.         for (i=0;i<T_header_24;i++) /* Copy the header from the Source */
  1987.             fputc(fgetc(Source),fps);
  1988.     else
  1989.     {    /* Write header for a new file */
  1990.         /* ID field size = 0, No color map, Targa type 2 file */
  1991.         for (i=0;i<12;i++)
  1992.         {
  1993.             if (i == 2)
  1994.                 fputc(i,fps);
  1995.             else
  1996.                 fputc(0,fps);
  1997.         }
  1998.         /*  Write image size  */
  1999.         for (i=0;i<4;i++)
  2000.             fputc(upr_lwr[i],fps);
  2001.         fputc(T24,fps); /* Targa 24 file */
  2002.         fputc(T32,fps); /* Image at upper left */
  2003.         inc = 3;
  2004.     }
  2005.  
  2006.     /*  Finished with the header, now lets work on the display area  */
  2007.     for (i=0;i<ydots;i++)    /* "clear the screen" (write to the disk) */
  2008.     {
  2009.         for (j=0;j<line_length1;j=j+inc)
  2010.         {
  2011.             if (overlay)
  2012.                 fputc(fgetc(Source), fps);
  2013.             else
  2014.                 for (k=2;k>-1;k--)
  2015.                     fputc(back_color[k], fps);            /* Targa order (B, G, R) */
  2016.         }
  2017.         if (ferror (fps))        
  2018.         {        
  2019.             /*  Almost certainly not enough disk space  */        
  2020.             fclose (fps);
  2021.             fclose (Source);
  2022.             remove (File_Name2);        
  2023.             File_Error(File_Name2, 2);        
  2024.             return(-2);        
  2025.         }
  2026.         if (check_key())        return(-3);
  2027.     }
  2028.  
  2029.     if (targa_startdisk(fps, T_header_24) != 0)
  2030.     {
  2031.         enddisk();
  2032.         remove (File_Name2);
  2033.         return(-4);
  2034.     }
  2035.     return(0);
  2036. }
  2037.  
  2038. /**************************************************************************
  2039.  
  2040.  
  2041. **************************************************************************/
  2042.  
  2043. int targa_validate(char *File_Name)
  2044. {
  2045.     FILE *fp;
  2046.     int i, j = 0;
  2047.  
  2048.     /* Attempt to open source file for reading */
  2049.     if ((fp=fopen(File_Name,"rb"))==NULL)
  2050.     {
  2051.         File_Error(File_Name, 1);
  2052.         return(-1); /* Oops, file does not exist */
  2053.     }
  2054.  
  2055.     T_header_24 += fgetc(fp); /* Check ID field and adjust header size */
  2056.  
  2057.     if (fgetc(fp)) /* Make sure this is an unmapped file */
  2058.     {
  2059.         File_Error(File_Name, 4);
  2060.         return(-1);
  2061.     }
  2062.  
  2063.     if (fgetc(fp)!=2) /* Make sure it is a type 2 file */
  2064.     {
  2065.         File_Error(File_Name, 4);
  2066.         return(-1);
  2067.     }
  2068.  
  2069.     /* Skip color map specification */
  2070.     for (i=0;i<5;i++)
  2071.             fgetc(fp);
  2072.  
  2073.     for (i=0;i<4;i++)
  2074.     {
  2075.         /* Check image origin */
  2076.         fgetc(fp);
  2077.         if(j != 0)
  2078.         {
  2079.             File_Error(File_Name, 4);
  2080.             return(-1);
  2081.         }
  2082.     }
  2083.     /* Check Image specs */
  2084.     for (i=0;i<4;i++)
  2085.         if(fgetc(fp) != upr_lwr[i])
  2086.         {
  2087.             File_Error(File_Name,3);
  2088.             return(-1);
  2089.         }
  2090.  
  2091.     if(fgetc(fp) != T24) error=4; /* Is it a targa 24 file? */
  2092.     if(fgetc(fp) != T32) error=4; /* Is the origin at the upper left? */
  2093.     if (error == 4)
  2094.     {
  2095.         File_Error(File_Name,4);
  2096.         return(-1);
  2097.     }
  2098.     rewind(fp);
  2099.  
  2100.     /* Now that we know its a good file, create a working copy */
  2101.     if (startdisk1(targa_temp, fp, 1))
  2102.         return(-1);
  2103.     
  2104.     fclose(fp);    /* Close the source */
  2105.  
  2106.     T_Safe=1; /* Original file successfully copied to targa_temp */
  2107.     return(0);
  2108. }
  2109.  
  2110. static int R_H (R, G, B, H, S, V)
  2111. BYTE R,G,B;
  2112. unsigned long *H, *S, *V;
  2113. {
  2114.     unsigned long    R1, G1, B1, DENOM;
  2115.     BYTE MIN;
  2116.  
  2117.     *V = R;
  2118.     MIN = G;
  2119.     if (R < G)
  2120.     {
  2121.         *V = G;
  2122.         MIN = R;
  2123.         if (G < B)            *V = B;
  2124.         if (B < R)            MIN = B;
  2125.     }
  2126.     else
  2127.     {
  2128.         if (B < G)            MIN = B;
  2129.         if (R < B)            *V = B;
  2130.     }
  2131.     DENOM = *V - MIN;
  2132.     if (*V != 0 && DENOM !=0)
  2133.     {
  2134.         *S = ((DENOM << 16) / *V) - 1;
  2135.     }
  2136.     else  *S = 0;/* Color is black! and Sat has no meaning */
  2137.     if(*S == 0) /*  R=G=B => shade of grey and Hue has no meaning */
  2138.     {
  2139.         *H = 0;
  2140.         *V = *V << 8;
  2141.         return(1);  /* v or s or both are 0 */
  2142.     }
  2143.     if (*V == MIN)
  2144.     {
  2145.         *H = 0;
  2146.         *V = *V << 8;
  2147.         return(0);
  2148.     }
  2149.     R1 = (((*V - R) * 60) << 6) / DENOM;        /* distance of color from red    */
  2150.     G1 = (((*V - G) * 60) << 6) / DENOM;        /* distance of color from green */
  2151.     B1 = (((*V - B) * 60) << 6) / DENOM;        /* distance of color from blue  */
  2152.     if(*V == R)
  2153.         if (MIN == G)        *H = (300 << 6) + B1;
  2154.         else            *H = (60 << 6) - G1;
  2155.     if(*V == G)
  2156.         if (MIN == B)        *H = (60 << 6) + R1;
  2157.         else            *H = (180 << 6) - B1;
  2158.     if(*V == B)
  2159.         if(MIN == R)        *H = (180 << 6) + G1;
  2160.         else            *H = (300 << 6) - R1;
  2161.  
  2162.     *V = *V << 8;
  2163.     return(0);
  2164. }
  2165.  
  2166. static int H_R (R, G, B, H, S, V)
  2167. BYTE *R, *G, *B;
  2168. unsigned long  H, S, V;
  2169. {
  2170.     unsigned long        P1, P2, P3;
  2171.     int    RMD, I;
  2172.  
  2173.     if(H >= 23040)            H = H % 23040; /*  Makes h circular  */
  2174.     I = H / 3840;
  2175.     RMD = H % 3840;            /*  RMD = fractional part of H    */
  2176.  
  2177.     P1 = ((V * (65535 - S)) / 65280) >> 8;
  2178.     P2 = (((V * (65535 - (S * RMD) / 3840)) / 65280) - 1) >> 8;
  2179.     P3 = (((V * (65535 - (S * (3840 - RMD)) / 3840)) / 65280)) >> 8;
  2180.     V = V >> 8;
  2181.     switch (I)
  2182.     {
  2183.     case 0:
  2184.         *R = V;
  2185.         *G = P3;
  2186.         *B = P1;
  2187.         break;
  2188.     case 1:
  2189.         *R = P2;
  2190.         *G = V;
  2191.         *B = P1;
  2192.         break;
  2193.     case 2:
  2194.         *R = P1;
  2195.         *G = V;
  2196.         *B = P3;
  2197.         break;
  2198.     case 3:
  2199.         *R = P1;
  2200.         *G = P2;
  2201.         *B = V;
  2202.         break;
  2203.     case 4:
  2204.         *R = P3;
  2205.         *G = P1;
  2206.         *B = V;
  2207.         break;
  2208.     case 5:
  2209.         *R = V ;
  2210.         *G = P1;
  2211.         *B = P2;
  2212.         break;
  2213.     }
  2214.     return(0);
  2215. }
  2216.  
  2217.  
  2218. /********************************************************************/
  2219. /*                                                                    */
  2220. /*  This routine writes a header to a ray tracer data file. It        */
  2221. /*  Identifies the version of FRACTINT which created it an the        */
  2222. /*  key 3D parameters in effect at the time.                        */
  2223. /*                                                                    */
  2224. /********************************************************************/
  2225.  
  2226.  
  2227. static char far declare[] = {"DECLARE    "};
  2228. static char far frac_default[] = {"F_Dflt"};
  2229. static char far color[] = {"COLOR  "};
  2230. static char far dflt[] = {"RED 0.8 GREEN 0.4 BLUE 0.1\n"};
  2231. static char far d_color[] = {"0.8 0.4 0.1"};
  2232. static char far r_surf[] = {"0.95 0.05 5 0 0\n"};
  2233. static char far surf[] = {"surf={diff="};
  2234. static char far rs_surf[] = {"surface T diff "};
  2235. static char far end[] = {"END_"};
  2236. static char far plane[] = {"PLANE"};
  2237. static char far m1[] = {"-1.0 "};
  2238. static char far one[] = {" 1.0 "};
  2239. static char far z[]  = {" 0.0 "};
  2240. static char far bnd_by[]  = {" BOUNDED_BY\n"};
  2241. static char far end_bnd[]  = {" END_BOUND\n"};
  2242. static char far inter[]  = {"INTERSECTION\n"};
  2243. #ifndef XFRACT
  2244. static char fmt[] = "   %Fs <%Fs%Fs%Fs> % #4.3f %Fs%Fs\n";
  2245. #else
  2246. static char fmt[] = "   %s <%s%s%s> % #4.3f %s%s\n";
  2247. #endif
  2248.  
  2249. static char far composite[] = {"COMPOSITE"};
  2250. static char far object[]  = {"OBJECT"};
  2251. static char far triangle[] = {"TRIANGLE "};
  2252. static char far l_tri[] = {"triangle"};
  2253. static char far texture[] = {"TEXTURE\n"};
  2254. /* static char far end_texture[] = {" END_TEXTURE\n"}; */
  2255. static char far red[] = {"RED"};
  2256. static char far green[] = {"GREEN"};
  2257. static char far blue[] = {"BLUE"};
  2258.  
  2259. static char far frac_texture[] = {"    AMBIENT 0.25 DIFFUSE 0.75"};
  2260.  
  2261. static char far polygon[] = {"polygon={points=3;"};
  2262. static char far vertex[] = {" vertex =  "};
  2263. static char far d_vert[] = {"    <"};
  2264. static char f1[] = "% #4.4f ";
  2265. static char far grid[] = {"grid 20 20 20\n\n"};
  2266. static char n[] = "\n";
  2267. static char f2[] = "R%dC%d R%dC%d\n";
  2268.  
  2269.  
  2270. static int _fastcall RAY_Header(void)
  2271.  
  2272.  
  2273. {  /* Open the ray tracing output file */
  2274.     check_writefile(ray_name,".ray");
  2275.     if ((File_Ptr1=fopen(ray_name,"w"))==NULL)
  2276.         return(-1); /* Oops, somethings wrong! */
  2277.  
  2278.     if (RAY == 2)
  2279.         fprintf(File_Ptr1, "//");
  2280.     if (RAY == 4)
  2281.         fprintf(File_Ptr1, "#");
  2282.     if (RAY == 5)
  2283.         fprintf(File_Ptr1, "/*\n");
  2284.     if (RAY == 6)
  2285.         fprintf(File_Ptr1, "--");
  2286.  
  2287.     fprintf(File_Ptr1, banner, s3, release/100., s3a);
  2288.  
  2289.     if (RAY == 5)
  2290.         fprintf(File_Ptr1, "*/\n");
  2291.  
  2292.  
  2293.     /* Set the default color */
  2294.     if (RAY == 1)
  2295.     {
  2296.         fprintf (File_Ptr1, f, declare, frac_default);
  2297.         fprintf (File_Ptr1, " = ");
  2298.         fprintf (File_Ptr1, f, color, dflt);
  2299.     }
  2300.     if (BRIEF)
  2301.     {
  2302.         if (RAY == 2)
  2303.         {
  2304.             fprintf (File_Ptr1, f, surf, d_color);
  2305.             fprintf (File_Ptr1, ";}\n");
  2306.         }
  2307.         if (RAY == 4)
  2308.         {
  2309.             fprintf (File_Ptr1, "f ");
  2310.             fprintf (File_Ptr1, f, d_color, r_surf);
  2311.         }
  2312.         if (RAY == 5)
  2313.             fprintf (File_Ptr1, f, rs_surf, d_color);
  2314.     }
  2315.     fprintf (File_Ptr1, n);
  2316.     if (RAY == 6)
  2317. #ifndef XFRACT
  2318.                 fprintf (File_Ptr1, "%Fs", acro_s1);
  2319. #else
  2320.         fprintf (File_Ptr1, "%s", acro_s1);
  2321. #endif
  2322.  
  2323.     return(0);
  2324. }
  2325.  
  2326.  
  2327. /********************************************************************/
  2328. /*                                                                    */
  2329. /*  This routine describes the triangle to the ray tracer, it        */
  2330. /*  sets the color of the triangle to the average of the color        */
  2331. /*  of its verticies and sets the light parameters to arbitrary        */
  2332. /*  values.                                                            */
  2333. /*                                                                    */
  2334. /*  Note: numcolors (number of colors in the source                    */
  2335. /*  file) is used instead of colors (number of colors avail. with    */
  2336. /*  display) so you can generate ray trace files with your LCD        */
  2337. /*  or monochrome display                                            */
  2338. /*                                                                    */
  2339. /********************************************************************/
  2340.  
  2341. static int _fastcall out_triangle(struct f_point pt1, struct f_point pt2,
  2342.             struct f_point pt3, int c1, int c2, int c3)
  2343.  
  2344. {
  2345. int i, j;
  2346. float c[3];
  2347. float pt_t[3][3];
  2348.  
  2349.     /* Normalize each vertex to screen size and adjust coordinate system */
  2350.     pt_t[0][0]            =    2 * pt1.x            / xdots  - 1;
  2351.     pt_t[0][1]            =  (2 * pt1.y            / ydots  - 1);
  2352.     pt_t[0][2]            =  -2 * pt1.color / numcolors - 1;
  2353.     pt_t[1][0]            =    2 * pt2.x            / xdots  - 1;
  2354.     pt_t[1][1]            =  (2 * pt2.y            / ydots  - 1);
  2355.     pt_t[1][2]            =  -2 * pt2.color / numcolors - 1;
  2356.     pt_t[2][0]            =    2 * pt3.x            / xdots  - 1;
  2357.     pt_t[2][1]            =  (2 * pt3.y            / ydots  - 1);
  2358.     pt_t[2][2]            =  -2 * pt3.color / numcolors - 1;
  2359.  
  2360.     /* Color of triangle is average of colors of its verticies */
  2361.     if (!BRIEF)
  2362.         for (i=0;i<=2;i++)
  2363.         c[i] = (float)(dacbox[c1][i] + dacbox[c2][i] + dacbox[c3][i])
  2364.                 / (3 * 63);
  2365.  
  2366.         /* get rid of degenerate triangles: any two points equal */
  2367.         if (pt_t[0][0] == pt_t[1][0] &&
  2368.                 pt_t[0][1] == pt_t[1][1] &&
  2369.                 pt_t[0][2] == pt_t[1][2] ||
  2370.  
  2371.                 pt_t[0][0] == pt_t[2][0] &&
  2372.                 pt_t[0][1] == pt_t[2][1] &&
  2373.                 pt_t[0][2] == pt_t[2][2] ||
  2374.  
  2375.                 pt_t[2][0] == pt_t[1][0] &&
  2376.                 pt_t[2][1] == pt_t[1][1] &&
  2377.                 pt_t[2][2] == pt_t[1][2])
  2378.             return(0);
  2379.  
  2380.     /* Describe the triangle */
  2381. #ifndef XFRACT
  2382.     if (RAY == 1)
  2383.         fprintf (File_Ptr1, " %Fs\n  %Fs", object, triangle);
  2384.         if (RAY == 2 && !BRIEF)
  2385.         fprintf (File_Ptr1, "%Fs", surf);
  2386. #else
  2387.     if (RAY == 1)
  2388.         fprintf (File_Ptr1, " %s\n  %s", object, triangle);
  2389.         if (RAY == 2 && !BRIEF)
  2390.         fprintf (File_Ptr1, "%s", surf);
  2391. #endif
  2392.     if (RAY == 4 && !BRIEF)
  2393.         fprintf (File_Ptr1, "f");
  2394.     if (RAY == 5 && !BRIEF)
  2395. #ifndef XFRACT
  2396.         fprintf (File_Ptr1, "%Fs", rs_surf);
  2397. #else
  2398.         fprintf (File_Ptr1, "%s", rs_surf);
  2399. #endif
  2400.  
  2401.     if (!BRIEF && RAY != 1)
  2402.         for (i=0;i<=2;i++)
  2403.         fprintf (File_Ptr1, f1, c[i]);
  2404.  
  2405.     if (RAY == 2)
  2406.     {
  2407.         if (!BRIEF)
  2408.         fprintf (File_Ptr1, ";}\n");
  2409. #ifndef XFRACT
  2410.         fprintf (File_Ptr1, "%Fs", polygon);
  2411. #else
  2412.         fprintf (File_Ptr1, "%s", polygon);
  2413. #endif
  2414.     }
  2415.     if (RAY == 4)
  2416.     {
  2417.         if (!BRIEF)
  2418. #ifndef XFRACT
  2419.             fprintf (File_Ptr1, "%Fs", r_surf);
  2420. #else
  2421.             fprintf (File_Ptr1, "%s", r_surf);
  2422. #endif
  2423.         fprintf (File_Ptr1, "p 3");
  2424.     }
  2425.     if (RAY == 5)
  2426.     {
  2427.         if (!BRIEF)
  2428.             fprintf (File_Ptr1, n);
  2429. #ifndef XFRACT
  2430.         fprintf (File_Ptr1, "%Fs T", l_tri);
  2431. #else
  2432.         fprintf (File_Ptr1, "%s T", l_tri);
  2433. #endif
  2434.     }
  2435.  
  2436.     for(i=0;i<=2;i++)  /*  Describe each  Vertex  */
  2437.     {
  2438.         fprintf (File_Ptr1, n);
  2439.  
  2440. #ifndef XFRACT
  2441.         if (RAY == 1)
  2442.             fprintf (File_Ptr1, "%Fs", d_vert);
  2443.         if (RAY == 2)
  2444.             fprintf (File_Ptr1, "%Fs", vertex);
  2445. #else
  2446.         if (RAY == 1)
  2447.             fprintf (File_Ptr1, "%s", d_vert);
  2448.         if (RAY == 2)
  2449.             fprintf (File_Ptr1, "%s", vertex);
  2450. #endif
  2451.         if (RAY > 3)
  2452.             fprintf (File_Ptr1, " ");
  2453.  
  2454.         for(j=0;j<=2;j++)
  2455.         if (!(RAY == 4 || RAY == 5))
  2456.             fprintf (File_Ptr1, f1, pt_t[i][j]); /* Right handed */
  2457.         else
  2458.             fprintf (File_Ptr1, f1, pt_t[2-i][j]); /* Left handed */
  2459.  
  2460.         if (RAY == 1)
  2461.         fprintf (File_Ptr1, ">");
  2462.         if (RAY == 2)
  2463.         fprintf (File_Ptr1, ";");
  2464.     }
  2465.  
  2466.     if (RAY == 1)
  2467.     {
  2468. #ifndef XFRACT
  2469.         fprintf (File_Ptr1, " %Fs%Fs\n", end, triangle);
  2470. #else
  2471.         fprintf (File_Ptr1, " %s%s\n", end, triangle);
  2472. #endif
  2473.         if (!BRIEF)
  2474.         {
  2475. #ifndef XFRACT
  2476.         fprintf (File_Ptr1, "  %Fs"
  2477.             "    %Fs%Fs% #4.4f %Fs% #4.4f %Fs% #4.4f\n"
  2478.             "%Fs"
  2479.             " %Fs%Fs",
  2480. #else
  2481.         fprintf (File_Ptr1,
  2482.             "  %s   %s%s% #4.4f %s% #4.4f %s% #4.4f\n%s %s%s",
  2483. #endif
  2484.             texture,
  2485.             color, red, c[0], green, c[1], blue, c[2],
  2486.             frac_texture,
  2487.             end, texture);
  2488.         }
  2489. #ifndef XFRACT
  2490.         fprintf (File_Ptr1, "  %Fs%Fs  %Fs%Fs",
  2491. #else
  2492.         fprintf (File_Ptr1, "  %s%s  %s%s",
  2493. #endif
  2494.         color, frac_default,
  2495.         end, object);
  2496.         triangle_bounds(pt_t); /* update bounding info */
  2497.     }
  2498.     if (RAY == 2)
  2499.         fprintf (File_Ptr1, "}");
  2500.     if (RAY == 3 && !BRIEF)
  2501.         fprintf (File_Ptr1, n);
  2502.  
  2503.     fprintf (File_Ptr1, n);
  2504.  
  2505.     return(0);
  2506. }
  2507.  
  2508.  
  2509. /********************************************************************/
  2510. /*                                                                    */
  2511. /*  This routine calculates the min and max values of a triangle    */
  2512. /*  for use in creating ray tracer data files. The values of min    */
  2513. /*  and max x, y, and z are assumed to be global.                    */
  2514. /*                                                                    */
  2515. /********************************************************************/
  2516.  
  2517. static void _fastcall triangle_bounds(float pt_t[3][3])
  2518.  
  2519. {
  2520. int i,j;
  2521.  
  2522.     for (i=0;i<=2;i++)
  2523.     for (j=0;j<=2;j++)
  2524.     {
  2525.         if(pt_t[i][j] < min_xyz[j]) min_xyz[j] = pt_t[i][j];
  2526.         if(pt_t[i][j] > max_xyz[j]) max_xyz[j] = pt_t[i][j];
  2527.     }
  2528.     return;
  2529. }
  2530.  
  2531. /********************************************************************/
  2532. /*                                                                    */
  2533. /*  This routine starts a composite object for ray trace data files.*/
  2534. /*                                                                    */
  2535. /********************************************************************/
  2536.  
  2537. static int _fastcall start_object(void)
  2538.  
  2539. {
  2540.     if (RAY != 1)            return(0);
  2541.  
  2542.     /*    Reset the min/max values, for bounding box  */
  2543.     min_xyz[0] = min_xyz[1] = min_xyz[2] =  999999;
  2544.     max_xyz[0] = max_xyz[1] = max_xyz[2] = -999999;
  2545.  
  2546. #ifndef XFRACT
  2547.     fprintf (File_Ptr1, "%Fs\n", composite);
  2548. #else
  2549.     fprintf (File_Ptr1, "%s\n", composite);
  2550. #endif
  2551.     return(0);
  2552. }
  2553.  
  2554. /********************************************************************/
  2555. /*                                                                    */
  2556. /*  This routine adds a bounding box for the triangles drawn        */
  2557. /*  in the last block and completes the composite object created.    */
  2558. /*  It uses the globals min and max x,y and z calculated in            */
  2559. /*  z calculated in Triangle_Bounds().                                */
  2560. /*                                                                    */
  2561. /********************************************************************/
  2562.  
  2563. static int _fastcall end_object(int triout)
  2564. {
  2565.     if (RAY == 1)
  2566.     {
  2567.     if (triout)
  2568.         {
  2569.         /* Make sure the bounding box is slightly larger than the object */
  2570.         int i;
  2571.         for (i=0;i<=2;i++)
  2572.         {
  2573.             if (min_xyz[i] == max_xyz[i])
  2574.             {
  2575.                 min_xyz[i] -= 0.01;
  2576.             max_xyz[i] += 0.01;
  2577.             }
  2578.             else
  2579.             {
  2580.                 min_xyz[i] -= (max_xyz[i] - min_xyz[i]) * 0.01;
  2581.                 max_xyz[i] += (max_xyz[i] - min_xyz[i]) * 0.01;
  2582.             }
  2583.         }
  2584.  
  2585.         /* Add the bounding box info */
  2586. #ifndef XFRACT
  2587.         fprintf (File_Ptr1, "%Fs  %Fs", bnd_by, inter);
  2588. #else
  2589.         fprintf (File_Ptr1, "%s  %s", bnd_by, inter);
  2590. #endif
  2591.         fprintf (File_Ptr1, fmt, plane,m1,z,z,-min_xyz[0],end,plane);
  2592.         fprintf (File_Ptr1, fmt, plane,one,z,z,max_xyz[0],end,plane);
  2593.         fprintf (File_Ptr1, fmt, plane,z,m1,z,-min_xyz[1],end,plane);
  2594.         fprintf (File_Ptr1, fmt, plane,z,one,z,max_xyz[1],end,plane);
  2595.         fprintf (File_Ptr1, fmt, plane,z,z,m1,-min_xyz[2],end,plane);
  2596.         fprintf (File_Ptr1, fmt, plane,z,z,one,max_xyz[2],end,plane);
  2597. #ifndef XFRACT
  2598.         fprintf (File_Ptr1, "  %Fs%Fs%Fs", end, inter, end_bnd);
  2599. #else
  2600.         fprintf (File_Ptr1, "  %s%s%s", end, inter, end_bnd);
  2601. #endif
  2602.         }
  2603.  
  2604.         /* Complete the composite object statement */
  2605. #ifndef XFRACT
  2606.         fprintf (File_Ptr1, "%Fs%Fs\n", end, composite);
  2607. #else
  2608.         fprintf (File_Ptr1, "%s%s\n", end, composite);
  2609. #endif
  2610.     }
  2611.  
  2612.     if (RAY != 6)
  2613.         fprintf (File_Ptr1, n);
  2614.  
  2615.     return(0);
  2616. }
  2617.  
  2618.  
  2619. static void line3d_cleanup()
  2620. {
  2621.  int i,j;
  2622.     if(RAY && File_Ptr1)
  2623.     {  /*  Finish up the ray tracing files */
  2624.         static char far n_ta[] ={"{ No. Of Triangles = "};
  2625.         fprintf (File_Ptr1, n);
  2626.         if (RAY == 2)
  2627.             fprintf(File_Ptr1, "\n\n//");
  2628.         if (RAY == 4)
  2629.             fprintf(File_Ptr1, "\n\n#");
  2630.  
  2631.         if (RAY == 5)
  2632. #ifndef XFRACT
  2633.             fprintf (File_Ptr1, "%Fs/*\n", grid);
  2634. #else
  2635.             fprintf (File_Ptr1, "%s/*\n", grid);
  2636. #endif
  2637.         if (RAY == 6)
  2638.         {
  2639. #ifndef XFRACT
  2640.             fprintf (File_Ptr1, "%Fs", acro_s2);
  2641. #else
  2642.             fprintf (File_Ptr1, "%s", acro_s2);
  2643. #endif
  2644.             for (i=0;i<RO;i++)
  2645.                 for (j=0;j<=CO_MAX;j++)
  2646.                 {
  2647.                     if (j < CO_MAX)
  2648.                         fprintf (File_Ptr1, f2, i, j, i, j+1);
  2649.                     if (i < RO - 1)
  2650.                         fprintf (File_Ptr1, f2, i, j, i+1, j);
  2651.                     if (i && i < RO && j < CO_MAX)
  2652.                         fprintf (File_Ptr1, f2, i, j, i-1, j+1);
  2653.                 }
  2654.             fprintf (File_Ptr1, "\n\n--");
  2655.         }
  2656. #ifndef XFRACT
  2657.         fprintf(File_Ptr1, "%Fs%d%Fs",n_ta, num_tris, s3a);
  2658. #else
  2659.         fprintf(File_Ptr1, "%s%d%s",n_ta, num_tris, s3a);
  2660. #endif
  2661.         if (RAY == 5)
  2662.             fprintf (File_Ptr1, "*/\n");
  2663.         fclose(File_Ptr1);
  2664.         File_Ptr1 = NULL;
  2665.     }
  2666.     if (Targa_Out)
  2667.     {    /*  Finish up targa files */
  2668.         T_header_24 = 18; /* Reset Targa header size */
  2669.         enddisk();
  2670.         if (!debugflag && T_Safe && !error && Targa_Overlay)
  2671.         {
  2672.             remove(light_name);
  2673.             rename(targa_temp,light_name);
  2674.         }
  2675.         if (!debugflag && Targa_Overlay)
  2676.             remove (targa_temp);
  2677.     }
  2678.     usr_floatflag &= 1; /* strip second bit */
  2679.     error = T_Safe = 0;
  2680. }
  2681.  
  2682.