home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / graphic / qrt / techman.doc < prev    next >
Text File  |  1989-03-26  |  21KB  |  595 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.         
  8.         
  9.                              QRT Technical Reference
  10.         
  11.         
  12.         INTRODUCTION
  13.         
  14.         The QRT ray tracing system  has  been  designed  to make the code
  15.         easily maintainable and  expandable.   An  object oriented design
  16.         philosophy was used for the  program,  and the resulting code has
  17.         been made as robust as practical given time constraints.
  18.         
  19.         This document explains the design of QRT.  First, the function of
  20.         each QRT C file will be listed.    Next,  the QRT data structures
  21.         will  be  explained,   and   finally,   some   details   of  code
  22.         implementation discussed.
  23.         
  24.         
  25.         QRT C CODE FILES
  26.         
  27.         The QRT code is broken down  into  16 C files and 3 header files,
  28.         each containing a group of related  functions.  The files are, in
  29.         alphabetical order:
  30.         
  31.            FILE              FUNCTION                              
  32.         
  33.            bbox.c            bounding box intersection routines
  34.            error.c           error reporting routines
  35.            inout.c           recursive decent input parser
  36.            instance.c        file for INSTANCE primitives
  37.            intersect.c       object intersection routines
  38.            lexer.c           simple lexical analyzer
  39.            mth.c             vector math support
  40.            norm.c            normal finding functions for objects
  41.            offset.c          offsets a primitive by dx, dy, dz
  42.            pattern.c         finds pattern info for objects
  43.            patternio.c       auxiliary parser for patterns
  44.            precomp.c         pre-computes some info for objects
  45.            qrt.c             main routine and initialization code
  46.            ray.c             actual ray tracing algorithims
  47.            relpos.c          finds relative position on an object
  48.            resize.c          resize any primitive
  49.            stack.c           stack and object creation routines
  50.         
  51.            header.h          instantiations of some global variables
  52.            pattern.h         pattern info
  53.            qrt.h             main data structure definitions
  54.         
  55.         
  56.         The function of each  group  of  routines  will  be  discussed in
  57.         greater detail in rest of this document.
  58.  
  59.  
  60.         QRT Ray Tracer               Page 1           Technical Reference
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.         
  74.         
  75.         DATA STRUCTURES
  76.         
  77.         All QRT data  structures  are  defined  in  qrt.h.   This file is
  78.         broken down into sections, as follows:
  79.         
  80.          OBJECT TYPE DEFINITIONS:
  81.            
  82.            C #define  statements  for  each  object  type  (sphere, lamp,
  83.            observer, etc).
  84.            
  85.            
  86.          MISC DEFINES
  87.            
  88.            Other #defines for screen size, etc.
  89.            
  90.            
  91.          VECTOR data structure
  92.            
  93.            A floating point 3-tuple vector definition.
  94.            
  95.            
  96.          COLOR VECTOR data structure
  97.            
  98.            A red,green,blue integer 3-tuple structure.
  99.            
  100.            
  101.          COLOR INFO structure
  102.            
  103.            This is an important structure. It lists color information for
  104.            an    object,    including    ambient    and   diffuse   light
  105.            characteristics,  reflection   and  transmission  data,  Phong
  106.            specular  reflection  coefficient,  index  of  refraction, and
  107.            object dithering data.
  108.            
  109.            
  110.          PRECOMPUTE structure
  111.            
  112.            This contains some fields which can be filled with precomputed
  113.            information before the ray-tracing  is begun.  This saves time
  114.            by eliminating redundant  calculations  for  every line/object
  115.            intersection.   The  use   of   these  fields  is  up  to  the
  116.            intersection routines.  Each  object  also  has a 'precompute'
  117.            routine which fills this structure.
  118.            
  119.            
  120.          PATTERN structure
  121.            
  122.            The  pattern  definition   structure  contains  a  color  info
  123.            structure.  It is used  to  change  the  characteristics  of a
  124.  
  125.  
  126.         QRT Ray Tracer               Page 2           Technical Reference
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.            primitive  object  over  the  surface  of  that  object.   For
  140.            example, checkered, brick, or tile patterns may be defined.
  141.            
  142.            
  143.          OBJECT structure
  144.            
  145.            The base structure of  QRT.   All  objects  are  defined as an
  146.            object structure.  This structure contains:
  147.            
  148.               A) A location vector for the object
  149.               B) 3 directional vectors
  150.               C) The object type flag
  151.               D) Some constants for QUADRATIC objects
  152.               E) A color info structure
  153.               F) Sibling and child pointers for the object tree
  154.               G) A pointer to a pattern structure
  155.               H) A pointer to a pattern for the REMOVE command
  156.               I) x and y multipliers for pattern sizing
  157.               J) A name field for the object
  158.            
  159.            
  160.          WORLD structure
  161.            
  162.            This structure contains all data about the world.  It contains
  163.            pointers to the object tree, the  lamp list, the observer, and
  164.            the sky, and the pattern  stack.   It  also contains statistic
  165.            information, such as  total  number  of  rays  traced, and the
  166.            output file name and pointer.
  167.            
  168.            
  169.          OBJECT_DATA structure
  170.            
  171.            The header.h file contains  an  array  of  this structure, one
  172.            array element per object type.   The structure itself contains
  173.            pointers to functions that perform  known operations on object
  174.            structures.  This design  allows  much cleaner code and easier
  175.            addition of object types.  The object function pointers are:
  176.            
  177.               ColTest:  Tests for object collisions
  178.            
  179.               FindNorm: Finds normal to surface at a given position
  180.            
  181.               FindBbox: Computes size of bounding box for object
  182.            
  183.               RelPos:   Finds relative position on object surface
  184.                         given a position in space
  185.            
  186.               PreComp:  Stuffs 'precomp' structure for each object
  187.                         before ray-tracing is begun.  The precomp and
  188.                         intersect routines must agree on the exact usage
  189.                         of the fields in this structure.
  190.  
  191.  
  192.         QRT Ray Tracer               Page 3           Technical Reference
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.            
  206.               Offset:   Moves a primitive by dx, dy, dz.  This is used
  207.                         for instances.
  208.            
  209.               Resize:   Resizes a primitive ( and scales its location
  210.                         vector).  This is also used for instances.
  211.            
  212.            This structure permits expressions such as:
  213.            
  214.               (*(ObjData[CurrObj->type].ColTest))(line,Currobj,&t);
  215.            
  216.            which means:
  217.            
  218.               (collision func for this obj type )(parameters);
  219.            
  220.            instead of a large case  statement.   Execution is faster, and
  221.            if new  objects  are  added,  the  code  does  not  have to be
  222.            changed.  If a  certain  operation  is  illegal  for a certain
  223.            object type, the ObjData  entry  points  to an Err() function.
  224.            This way, if something  goes  wrong,  execution  is terminated
  225.            gracefully instead of jumping to a random location in memory.
  226.            
  227.            
  228.          MATH defines
  229.            
  230.            MIN, MAX, SQRT, DotProd macros
  231.            
  232.            
  233.          DEFAULT structure
  234.            
  235.            Keeps default color information,  and a few other things, like
  236.            resolution and aspect ratio.
  237.            
  238.            
  239.          ERROR codes
  240.            
  241.            Defines for all possible error conditions
  242.            
  243.            
  244.          ROBUST flag
  245.            
  246.            There are many sections of code of the form:
  247.            
  248.               # ifdef ROBUST
  249.                  code
  250.               # endif
  251.            
  252.            such that, if ROBUST is defined, the bounds-checking code will
  253.            be compiled.  It is recommended  that this flag be always set,
  254.            although SLIGHTLY faster execution is possible with it reset.
  255.            
  256.  
  257.  
  258.         QRT Ray Tracer               Page 4           Technical Reference
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.            
  272.         CODE DESCRIPTION
  273.         
  274.         The function of each section of  code  will  be described in more
  275.         detail here.
  276.         
  277.         
  278.          BBOX.C
  279.            
  280.            Contains code for each  object  type  that  will determine the
  281.            size of the bounding box for that  object.  Bounding boxes are
  282.            logical objects that contain other objects.  If a ray does not
  283.            strike a bounding box, it  cannot  possibly  strike any object
  284.            inside the box.  This  can  increase  execution  speed if used
  285.            correctly.
  286.            
  287.            Routines in BBOX.C are of the form:
  288.            
  289.              BboxSphere(v1,v2,obj)
  290.                VECT_PTR v1,v2;
  291.                OBJ_PTR obj;
  292.            
  293.            where v1 and  v2  are  vectors  for  the  two  corners  of the
  294.            bounding box, and obj is a pointer to an object.
  295.            
  296.            The functions  are  called  based  upon  their  pointer in the
  297.            ObjData structure.
  298.            
  299.            
  300.          ERROR.C
  301.          
  302.            This is a simple file that  contains  just  two routines.  The
  303.            first one is the Err() routine that  fills empty places in the
  304.            ObjData structure.   The  second  routine  is the actual error
  305.            reporting routine.  It takes  two  arguments: an error number,
  306.            and an error code.   The  error  number  is  a #define such as
  307.            'SYNTAX_ERROR'  or  'INTERNAL_ERROR'.   The  error  code is an
  308.            integer used to find the routine  in which the error occurred.
  309.            The error code  number  is  arbitrary  but  should  be  unique
  310.            throughout the program.  A convention has been set up that all
  311.            routines within one C file return  an error code with the same
  312.            first digit (501, 502,  503...).   If the error occurred while
  313.            parsing the input file, the offending  line number is printed.
  314.            Execution is always terminated after calling this routine.
  315.            
  316.            
  317.          INOUT.C
  318.            
  319.            This is a recursive decent parser  for the input language. The
  320.            parser was hand coded, since  YACC  or a similar compiler tool
  321.            was  not  available.   The   input   routines  perform  syntax
  322.  
  323.  
  324.         QRT Ray Tracer               Page 5           Technical Reference
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.            checking, and some bounds checking.   The bounds checking will
  338.            catch some errors; however, it has not been idiot-proofed.  It
  339.            is up to the user to not make strange entries (radius=0, etc).
  340.            Most of these illogical errors will  be caught in the run-time
  341.            bounds checking.
  342.            
  343.            INOUT has one routine for  each  non-terminal  in the grammar.
  344.            The routine accepts parameters,  in any order, and branches to
  345.            another routine, or  inputs  a  value.   This  is  the largest
  346.            individual file, at over 1000 lines.
  347.            
  348.            
  349.          INSTANCE.C
  350.            
  351.            This contains the  routines  to  input  instance  definitions,
  352.            create a copy of a sub-tree, offset the subtree, and scale the
  353.            subtree.
  354.            
  355.            
  356.          INTERSECT.C
  357.            
  358.            This   file   contains   routines   to   perform   line/object
  359.            intersection tests.  Each routine is of the form:
  360.            
  361.              int LineSphere(line, sphere, t)
  362.                    OBJ_PTR line, sphere;
  363.                    float *t;
  364.            
  365.            where line and sphere are inputs, and t is an output.  Line is
  366.            always a line  object,  but  the  second  parameter  may  be a
  367.            pointer  a  different  object  for  a  different  intersection
  368.            routine.   The routine must  compute  the  parameter T for the
  369.            intersection point on the line, if any, and return TRUE if the
  370.            line hits the object, or FALSE if it does not.
  371.            
  372.            The functions  are  called  based  upon  their  pointer in the
  373.            ObjData structure.
  374.            
  375.            
  376.          LEXER.C
  377.            
  378.            This is a simple tokenizer  for  the  input language.  In this
  379.            implementation, #include directives for the input language are
  380.            not implemented, but they belong  in this routine. This module
  381.            also contains some  bounds  checking  code,   and  code to map
  382.            values  of   the   range   0..1   to   0..CNUM.    The   light
  383.            characteristics are stored as  an  integer from 0 to CNUM, but
  384.            the input values are a  floating  point  value from 0 to 1, so
  385.            these routines perform the conversion.
  386.            
  387.            
  388.  
  389.  
  390.         QRT Ray Tracer               Page 6           Technical Reference
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.          MTH.C
  404.            
  405.            MTH contains support routines for  vector math, such as vector
  406.            addition, subtraction, and cross product.
  407.            
  408.            
  409.          NORM.C
  410.            
  411.            These routines find the normal vector  to an object at a given
  412.            location in space.  They are called  based upon the pointer in
  413.            the ObjData structure.  A typical entry is:
  414.            
  415.               SphereNorm(norm, object, position)
  416.                 VECT_PTR norm, position;
  417.                 OBJ_PTR object;
  418.            
  419.            where object and position are input, and norm is output.
  420.            
  421.            Often, several objects share  one  routine.  This happens, for
  422.            example,  with  planar  objects,  since  the  code to find the
  423.            normal to a plane is the same  regardless  of the shape of the
  424.            planar object.
  425.            
  426.            
  427.          OFFSET.C
  428.            
  429.            These routines are called by  the  object_data  structure. One
  430.            routine  exists  per  primitive  type,  and  they  offset  the
  431.            primitive by the given amount.
  432.            
  433.            
  434.          PATTERN.C
  435.            
  436.            When a ray/object intersection is  found, the pattern function
  437.            is called.  If  the  objects  pattern  pointer  is  NULL,  the
  438.            default color info for that object  is returned.  If it is not
  439.            NULL, the the relative position  on the surface of that object
  440.            is found, and the color info for  the pattern at that location
  441.            is returned.   If  the  relative  location  is  not inside any
  442.            sub-pattern, then the object's default color info is returned.
  443.            
  444.            To determine if  a  given  point  is  inside  a sub-pattern, a
  445.            similar strategy to the object intersection  routines is used.
  446.            There are several sub-pattern types  (RECTANGLE, CIRCLE, etc),
  447.            and there is a table  listing  pointers  to  containment  test
  448.            routines for each type.  This makes it easy to add sub-pattern
  449.            types.
  450.            
  451.  
  452.  
  453.  
  454.  
  455.  
  456.         QRT Ray Tracer               Page 7           Technical Reference
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.          PATTERNIO.C
  470.            
  471.            An auxiliary parser for  patterns,  created  because INOUT was
  472.            getting  too  large  to  comfortably  edit.   This  file  also
  473.            contains routines to copy  subtrees,  as well as to offset and
  474.            scale subtrees by calling routines from offset.c and resize.c
  475.            
  476.            
  477.          PRECOMP.C
  478.            
  479.            Contains  routines  to  stuff   the  'precomp'  structure  for
  480.            objects.  See the  documentation  for  the qrt.h file for more
  481.            details.   This  structure   saves   time  by  computing  some
  482.            expressions  that  do  not   change   with   each  line/object
  483.            intersection test.
  484.            
  485.            
  486.          QRT.C
  487.            
  488.            This module contains initialization code, and the main routine
  489.            that loads the 'world'  and  performs  the  ray tracing. It is
  490.            quite short.
  491.            
  492.            
  493.          RAY.C
  494.            
  495.            All of the ray tracing  logic  is  in  this module.  The basic
  496.            routine  is  Ray_Trace.   Ray_Trace  looks  for  a  ray/object
  497.            intersection.  If it  finds  one,  various  color routines are
  498.            called to compute the color of the  object at this point. Some
  499.            of these color routines may  call  Ray_Trace  recursively (for
  500.            reflection or transmission).
  501.            
  502.            Another important routine,  called  by  Ray_Trace, is Ray_Hit.
  503.            This actually tests  to  see  if  the  ray  hits  an object by
  504.            walking through the object tree.   If the 'first' flag is set,
  505.            the routine will quit after it  finds  one object intersection
  506.            (used to see if a surface is in the shadow of another object).
  507.            Otherwise, all intersections are sorted in order of increasing
  508.            parameter T, and the first such intersection returned.
  509.            
  510.            
  511.          RELPOS.C
  512.            
  513.            Routines in RELPOS  are  called  by  the  entry in the ObjData
  514.            array.  A typical routine is:
  515.            
  516.            
  517.                SpherePos(obj,loc,pos1,pos2)
  518.                  OBJ_PTR  obj;
  519.                  VECT_PTR loc;
  520.  
  521.  
  522.         QRT Ray Tracer               Page 8           Technical Reference
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.                  float *pos1, *pos2;
  536.            
  537.            
  538.            where obj and loc are input, and  the routine must compute the
  539.            coordinates pos1 and pos2 on the  surface of the object.  This
  540.            is used to map patterns onto the surface of arbitrary objects.
  541.            It  is  straightforward   for   planar  surfaces.   For  other
  542.            surfaces, a mapping of the form:
  543.            
  544.                3-tuple(x,y,z) --> 2-tuple(x,y)
  545.            
  546.            must be created for the object.
  547.            
  548.            
  549.          RESIZE.C
  550.            
  551.            These routines resize objects, and  scale the location vector.
  552.            One routine exists per object.   They are used by the instance
  553.            routines (as are routines from offset.c).
  554.            
  555.            
  556.          STACK.C
  557.            
  558.            This file  contains  support  code  for  object  tree and list
  559.            manipulations.  The name "STACK"  is partially historical; the
  560.            initial implementation of the program did not permit arbitrary
  561.            object trees,  only  linear  lists.   STACK  also  contains an
  562.            object  creation  routine,   and  a  routine  to  print  world
  563.            statistics at the end of the ray tracing session.
  564.            
  565.            Note that  in  the  files  that  contain  object  manipulation
  566.            routines (indexed by the  object_data  structure array), there
  567.            is often a common routine for  several  similar  objects.  For
  568.            example, all planar objects  (PARALLELOGRAM,  RING,  TRIANGLE)
  569.            share a common normal finding routine.   If one of them should
  570.            suddenly need a different routine,  just create it, and change
  571.            the object_data pointer for that object.
  572.            
  573.            Patterns are dealt with in the same  manner as objects.  There
  574.            are currently three types of  pattern  primitives  (RECTANGLE,
  575.            CIRCLE,  and  POLYGON).   There   is  a  structure  (like  the
  576.            object_data structure) for patterns.  This structure currently
  577.            contains only one entry: a  pointer  to  a collision function.
  578.            Future capabilities  may  be  added  later,  along  with  more
  579.            primitive types.
  580.            
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.         QRT Ray Tracer               Page 9           Technical Reference
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.