home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / DKBSRC.ZIP / RENDER.C < prev    next >
C/C++ Source or Header  |  1991-05-04  |  16KB  |  462 lines

  1. /*****************************************************************************
  2. *
  3. *                                    render.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module implements the main raytracing loop.
  8. *
  9. * This software is freely distributable. The source and/or object code may be
  10. * copied or uploaded to communications services so long as this notice remains
  11. * at the top of each file.  If any changes are made to the program, you must
  12. * clearly indicate in the documentation and in the programs startup message
  13. * who it was who made the changes. The documentation should also describe what
  14. * those changes were. This software may not be included in whole or in
  15. * part into any commercial package without the express written consent of the
  16. * author.  It may, however, be included in other public domain or freely
  17. * distributed software so long as the proper credit for the software is given.
  18. *
  19. * This software is provided as is without any guarantees or warranty. Although
  20. * the author has attempted to find and correct any bugs in the software, he
  21. * is not responsible for any damage caused by the use of the software.  The
  22. * author is under no obligation to provide service, corrections, or upgrades
  23. * to this package.
  24. *
  25. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  26. * about them.  Also, if you have any comments or questions, you may contact me
  27. * at the following address:
  28. *
  29. *     David Buck
  30. *     22C Sonnet Cres.
  31. *     Nepean Ontario
  32. *     Canada, K2H 8W7
  33. *
  34. *  I can also be reached on the following bulleton boards:
  35. *
  36. *     OMX              (613) 731-3419
  37. *     Mystic           (613) 596-4249  or  (613) 596-4772
  38. *
  39. *  Fidonet:   1:163/109.9
  40. *  Internet:  dbuck@ccs.carleton.ca
  41. *  The "You Can Call Me RAY" BBS    (708) 358-5611
  42. *
  43. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  44. *
  45. *     The "You Can Call Me RAY" BBS (708) 358-5611
  46. *     The Information Exchange BBS  (708) 945-5575
  47. *
  48. *****************************************************************************/
  49.  
  50. #include "frame.h"
  51. #include "vector.h"
  52. #include "dkbproto.h"
  53.  
  54. extern FILE_HANDLE *Output_File_Handle;
  55. extern char Output_File_Name[FILE_NAME_LENGTH];
  56. extern char OutputFormat;
  57. extern int File_Buffer_Size;
  58. extern unsigned int Options;
  59. extern int Quality;
  60. volatile int Stop_Flag;
  61. extern int First_Line, Last_Line;
  62. extern long Number_Of_Pixels, Number_Of_Rays, Number_Of_Pixels_Supersampled;
  63.  
  64. extern short *hashTable;
  65. extern unsigned short crctab[256];
  66. #define rand3d(a,b) crctab[(int)(hashTable[(int)(hashTable[(int)((a)&0xfff)]^(b))&0xfff])&0xff]
  67.  
  68. FRAME Frame;
  69. RAY *VP_Ray;
  70. int Trace_Level, SuperSampleCount;
  71.  
  72.  
  73. #define MAX_TRACE_LEVEL 5
  74.  
  75. void Output_Line PARAMS((int y, COLOUR **Previous_Line, COLOUR **Current_Line,
  76.   char **Previous_Line_Antialiased_Flags, char **Current_Line_Antialiased_Flags));
  77.  
  78. COLOUR *Previous_Line, *Current_Line;
  79. char *Previous_Line_Antialiased_Flags, *Current_Line_Antialiased_Flags;
  80. RAY Ray;
  81.  
  82. void Create_Ray (ray, width, height, x, y)
  83.    RAY *ray;
  84.    int width, height;
  85.    DBL x, y;
  86.    {
  87.    register DBL X_Scalar, Y_Scalar;
  88.    VECTOR Temp_Vect_1, Temp_Vect_2;
  89.  
  90.    /* Convert the X Coordinate to be a DBL from 0.0 to 1.0 */
  91.    X_Scalar = (x - (DBL) width / 2.0) / (DBL) width;
  92.  
  93.    /* Convert the Y Coordinate to be a DBL from 0.0 to 1.0 */
  94.    Y_Scalar = (( (DBL)(Frame.Screen_Height - 1) - y) -
  95.           (DBL) height / 2.0) / (DBL) height;
  96.  
  97.    VScale (Temp_Vect_1, Frame.View_Point.Up, Y_Scalar);
  98.    VScale (Temp_Vect_2, Frame.View_Point.Right, X_Scalar);
  99.    VAdd (ray->Direction, Temp_Vect_1, Temp_Vect_2);
  100.    VAdd (ray->Direction, ray->Direction, Frame.View_Point.Direction);
  101.    VNormalize (ray->Direction, ray->Direction);
  102.    Initialize_Ray_Containers (ray);
  103.    ray->Quadric_Constants_Cached = FALSE;
  104.    }
  105.  
  106.  
  107. void Supersample (result, x, y, Width, Height)
  108.    COLOUR *result;
  109.    int x, y, Width, Height;
  110.    {
  111.    COLOUR colour;
  112.    register DBL dx, dy, Jitter_X, Jitter_Y;
  113.  
  114.    dx = (DBL) x;
  115.    dy = (DBL) y;
  116.  
  117.    Number_Of_Pixels_Supersampled++;
  118.  
  119.    Make_Colour (result, 0.0, 0.0, 0.0);
  120.  
  121.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  122.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  123.    Create_Ray (VP_Ray, Frame.Screen_Width, Frame.Screen_Height,
  124.              dx + Jitter_X, dy + Jitter_Y);
  125.  
  126.    Trace_Level = 0;
  127.    Trace (VP_Ray, &colour);
  128.    Clip_Colour (&colour, &colour);
  129.    Scale_Colour (&colour, &colour, 0.11111111);
  130.    Add_Colour (result, result, &colour);
  131.  
  132.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  133.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  134.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X - 0.3333333,
  135.                                       dy + Jitter_Y - 0.3333333);
  136.    Trace_Level = 0;
  137.    Trace (VP_Ray, &colour);
  138.    Clip_Colour (&colour, &colour);
  139.    Scale_Colour (&colour, &colour, 0.11111111);
  140.    Add_Colour (result, result, &colour);
  141.  
  142.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  143.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  144.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X - 0.3333333,
  145.                                       dy + Jitter_Y);
  146.    Trace_Level = 0;
  147.    Trace (VP_Ray, &colour);
  148.    Clip_Colour (&colour, &colour);
  149.    Scale_Colour (&colour, &colour, 0.11111111);
  150.    Add_Colour (result, result, &colour);
  151.  
  152.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  153.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  154.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X - 0.3333333,
  155.                                       dy + Jitter_Y + 0.3333333);
  156.    Trace_Level = 0;
  157.    Trace (VP_Ray, &colour);
  158.    Clip_Colour (&colour, &colour);
  159.    Scale_Colour (&colour, &colour, 0.11111111);
  160.    Add_Colour (result, result, &colour);
  161.  
  162.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  163.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  164.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X,
  165.                                       dy + Jitter_Y - 0.3333333);
  166.    Trace_Level = 0;
  167.    Trace (VP_Ray, &colour);
  168.    Clip_Colour (&colour, &colour);
  169.    Scale_Colour (&colour, &colour, 0.11111111);
  170.    Add_Colour (result, result, &colour);
  171.  
  172.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  173.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  174.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X,
  175.                                       dy + Jitter_Y + 0.3333333);
  176.    Trace_Level = 0;
  177.    Trace (VP_Ray, &colour);
  178.    Clip_Colour (&colour, &colour);
  179.    Scale_Colour (&colour, &colour, 0.11111111);
  180.    Add_Colour (result, result, &colour);
  181.  
  182.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  183.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  184.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X + 0.3333333,
  185.                                       dy + Jitter_Y - 0.3333333);
  186.    Trace_Level = 0;
  187.    Trace (VP_Ray, &colour);
  188.    Clip_Colour (&colour, &colour);
  189.    Scale_Colour (&colour, &colour, 0.11111111);
  190.    Add_Colour (result, result, &colour);
  191.  
  192.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  193.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  194.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X + 0.3333333,
  195.                                       dy + Jitter_Y);
  196.    Trace_Level = 0;
  197.    Trace (VP_Ray, &colour);
  198.    Clip_Colour (&colour, &colour);
  199.    Scale_Colour (&colour, &colour, 0.11111111);
  200.    Add_Colour (result, result, &colour);
  201.  
  202.    Jitter_X = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  203.    Jitter_Y = (rand3d(x, y) & 0x7FFF) / 32768.0 * 0.33333333 - 0.16666666;
  204.    Create_Ray (VP_Ray, Width, Height, dx + Jitter_X + 0.3333333,
  205.                                       dy + Jitter_Y + 0.3333333);
  206.    Trace_Level = 0;
  207.    Trace (VP_Ray, &colour);
  208.    Clip_Colour (&colour, &colour);
  209.    Scale_Colour (&colour, &colour, 0.11111111);
  210.    Add_Colour (result, result, &colour);
  211.    }
  212.  
  213. void initialize_renderer()
  214.    {
  215.    allocate_lines(&Previous_Line, &Current_Line, &Previous_Line_Antialiased_Flags, &Current_Line_Antialiased_Flags, &Ray);
  216.    }
  217.  
  218. void Read_Rendered_Part()
  219.    {
  220.    int rc, x, line_number;
  221.    char Red, Green, Blue;
  222.  
  223.    while ((rc = Read_Line(Output_File_Handle, Previous_Line, &line_number)) == 1) {
  224.       if (Options & DISPLAY)
  225.          for (x = 0 ; x < Frame.Screen_Width ; x++) {
  226.             Red = (char) (Previous_Line[x].Red * 255.0);
  227.             Green = (char) (Previous_Line[x].Green * 255.0);
  228.             Blue = (char) (Previous_Line[x].Blue * 255.0);
  229.             display_plot (x, line_number, Red, Green, Blue);
  230.             }
  231.       }
  232.  
  233.    First_Line = line_number+1;
  234.  
  235.    if (rc == 0) {
  236.       Close_File(Output_File_Handle);
  237.       if (Open_File (Output_File_Handle, Output_File_Name,
  238.               &Frame.Screen_Width, &Frame.Screen_Height, File_Buffer_Size,
  239.               APPEND_MODE) != 1) {
  240.          fprintf (stderr, "Error opening output file\n");
  241.          exit(1);
  242.          }
  243.       return;
  244.       }
  245.  
  246.    fprintf (stderr, "Error reading aborted data file\n");
  247.    }
  248.  
  249. void Start_Tracing ()
  250.    {
  251.    COLOUR Colour;
  252.    register int x, y;
  253.    char Red, Green, Blue, Antialias_Center_Flag;
  254.  
  255.    for (y = (Options & ANTIALIAS)?First_Line-1:First_Line; y<Last_Line; y++) {
  256.  
  257.       check_stats(y);
  258.  
  259.       for (x = 0 ; x < Frame.Screen_Width ; x++) {
  260.          Number_Of_Pixels++;
  261.          if (Stop_Flag) {
  262.             close_all();
  263.             exit(0);
  264.             }
  265.  
  266.          TEST_ABORT
  267.  
  268.          Create_Ray (VP_Ray, Frame.Screen_Width, Frame.Screen_Height, (DBL) x, (DBL) y);
  269.          Trace_Level = 0;
  270.          Trace (&Ray, &Colour);
  271.          Clip_Colour (&Colour, &Colour);
  272.  
  273.          Current_Line[x] = Colour;
  274.  
  275.          if (Options & ANTIALIAS) {
  276.             Antialias_Center_Flag = 0;
  277.             Current_Line_Antialiased_Flags[x] = 0;
  278.  
  279.             if (x != 0) {
  280.                if (Colour_Distance (&Current_Line[x-1], &Current_Line[x])
  281.                    >= Frame.Antialias_Threshold) {
  282.                   Antialias_Center_Flag = 1;
  283.                   if (!(Current_Line_Antialiased_Flags[x-1])) {
  284.                      Supersample (&Current_Line[x-1], 
  285.                                   x-1, y, Frame.Screen_Width, Frame.Screen_Height);
  286.                      Current_Line_Antialiased_Flags[x-1] = 1;
  287.                      SuperSampleCount++;
  288.                      }
  289.                   }
  290.                }
  291.  
  292.             if (y != First_Line-1) {
  293.                if (Colour_Distance (&Previous_Line[x], &Current_Line[x])
  294.                     >= Frame.Antialias_Threshold) {
  295.                   Antialias_Center_Flag = 1;
  296.                   if (!(Previous_Line_Antialiased_Flags[x])) {
  297.                      Supersample (&Previous_Line[x],
  298.                                   x, y-1, Frame.Screen_Width, Frame.Screen_Height);
  299.                      Previous_Line_Antialiased_Flags[x] = 1;
  300.                      SuperSampleCount++;
  301.                      }
  302.                   }
  303.                }
  304.  
  305.             if (Antialias_Center_Flag) {
  306.                Supersample (&Current_Line[x],
  307.                             x, y, Frame.Screen_Width, Frame.Screen_Height);
  308.                Current_Line_Antialiased_Flags[x] = 1;
  309.                Colour = Current_Line[x];
  310.                SuperSampleCount++;
  311.                }
  312.             }
  313.  
  314.          if (y != First_Line-1) {
  315.             Red = (char) (Colour.Red * 255.0);
  316.             Green = (char) (Colour.Green * 255.0);
  317.             Blue = (char) (Colour.Blue * 255.0);
  318.  
  319.             if (Options & DISPLAY)
  320.                display_plot (x, y, Red, Green, Blue);
  321.             }
  322.          }
  323.       Output_Line(y, &Previous_Line, &Current_Line, &Previous_Line_Antialiased_Flags, &Current_Line_Antialiased_Flags);
  324.       }
  325.  
  326.    if (Options & DISKWRITE) {
  327.       Write_Line (Output_File_Handle, Previous_Line, Last_Line - 1);
  328.       }
  329.    }
  330.  
  331. void check_stats(y)
  332.    register int y;
  333.    {
  334.       if (Options & VERBOSE)
  335.          printf ("Line %3d", y);
  336.  
  337.       if (Options & ANTIALIAS)
  338.          SuperSampleCount = 0;
  339.    }
  340.  
  341. void  allocate_lines(Previous_Line, Current_Line, Previous_Line_Antialiased_Flags, Current_Line_Antialiased_Flags, Ray)
  342.    COLOUR **Previous_Line, **Current_Line;
  343.    char **Previous_Line_Antialiased_Flags, **Current_Line_Antialiased_Flags;
  344.    RAY *Ray;
  345.    {
  346.    register int i;
  347.  
  348.    VP_Ray = Ray;
  349.  
  350.    *Previous_Line = (COLOUR *) malloc (sizeof (COLOUR)*(Frame.Screen_Width + 1));
  351.    *Current_Line = (COLOUR *) malloc (sizeof (COLOUR)*(Frame.Screen_Width + 1));
  352.  
  353.    for (i = 0 ; i <= Frame.Screen_Width ; i++) {
  354.       (*Previous_Line)[i].Red = 0.0;
  355.       (*Previous_Line)[i].Green = 0.0;
  356.       (*Previous_Line)[i].Blue = 0.0;
  357.  
  358.       (*Current_Line)[i].Red = 0.0;
  359.       (*Current_Line)[i].Green = 0.0;
  360.       (*Current_Line)[i].Blue = 0.0;
  361.       }
  362.  
  363.    if (Options & ANTIALIAS) {
  364.       *Previous_Line_Antialiased_Flags =
  365.           (char *) malloc (sizeof (char)*(Frame.Screen_Width + 1));
  366.       *Current_Line_Antialiased_Flags =
  367.           (char *)  malloc (sizeof (char)*(Frame.Screen_Width + 1));
  368.  
  369.       for (i = 0 ; i <= Frame.Screen_Width ; i++) {
  370.          (*Previous_Line_Antialiased_Flags)[i] = 0;
  371.          (*Current_Line_Antialiased_Flags)[i] = 0;
  372.          }
  373.    }
  374.  
  375.    Ray->Initial = Frame.View_Point.Location;
  376.    return;
  377.    }
  378.  
  379. void Output_Line (y, Previous_Line, Current_Line, Previous_Line_Antialiased_Flags, Current_Line_Antialiased_Flags)
  380.    register int y;
  381.    COLOUR **Previous_Line, **Current_Line;
  382.    char **Previous_Line_Antialiased_Flags, **Current_Line_Antialiased_Flags;
  383.    {
  384.       COLOUR *Temp_Colour_Ptr;
  385.       char *Temp_Char_Ptr;
  386.  
  387.       if (Options & DISKWRITE)
  388.          if (y > First_Line) {
  389.             Write_Line (Output_File_Handle, *Previous_Line, y-1);
  390.          }
  391.  
  392.       if (Options & VERBOSE)
  393.          if (Options & ANTIALIAS)
  394.             printf (" supersampled %d times\n", SuperSampleCount);
  395.          else
  396.             putchar ('\n');
  397.  
  398.       Temp_Colour_Ptr = *Previous_Line;
  399.       *Previous_Line = *Current_Line;
  400.       *Current_Line = Temp_Colour_Ptr;
  401.  
  402.       Temp_Char_Ptr = *Previous_Line_Antialiased_Flags;
  403.       *Previous_Line_Antialiased_Flags = *Current_Line_Antialiased_Flags;
  404.       *Current_Line_Antialiased_Flags = Temp_Char_Ptr;
  405.  
  406.       return;
  407.    }
  408.  
  409. void Trace (Ray, Colour)
  410.    RAY *Ray;
  411.    COLOUR *Colour;
  412.    {
  413.    OBJECT *Object;
  414.    INTERSECTION *Local_Intersection, *New_Intersection;
  415.    register int Intersection_Found;
  416.  
  417.    Number_Of_Rays++;
  418.    Make_Colour (Colour, 0.0, 0.0, 0.0);
  419.  
  420.    Intersection_Found = FALSE;
  421.    Local_Intersection = NULL;
  422.  
  423.    if (Trace_Level > MAX_TRACE_LEVEL) {
  424.       return;
  425.       }
  426.  
  427.    if (Frame.Fog_Distance == 0.0) {
  428.       Make_Colour (Colour, 0.0, 0.0, 0.0);
  429.       }
  430.    else
  431.       *Colour = Frame.Fog_Colour;
  432.  
  433.    if (Options & DEBUGGING)
  434.       printf ("Calculating intersections level %d\n", Trace_Level);
  435.  
  436.     /* What objects does this ray intersect? */
  437.    for (Object = Frame.Objects ; 
  438.         Object != NULL ;
  439.         Object = Object -> Next_Object) {
  440.  
  441.       if (New_Intersection = Intersection (Object, Ray)) {
  442.          if (Intersection_Found) {
  443.             if (Local_Intersection -> Depth > New_Intersection -> Depth) {
  444.                free (Local_Intersection);
  445.                Local_Intersection = New_Intersection;
  446.                }
  447.             else
  448.                free (New_Intersection);
  449.             }
  450.          else
  451.             Local_Intersection = New_Intersection;
  452.  
  453.          Intersection_Found = TRUE;
  454.          }
  455.       }
  456.  
  457.    if (Intersection_Found) {
  458.       Determine_Surface_Colour (Local_Intersection, Colour, Ray, FALSE);
  459.       free (Local_Intersection);
  460.       }
  461.    }
  462.