home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / rayce27s / ray.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-02  |  8.8 KB  |  305 lines

  1. /*
  2.  * ray.h --  types, constants.
  3.  * 
  4.  * (c) 1993, 1994 Han-Wen Nienhuys, <hanwen@stack.urc.tue.nl>
  5.  * 
  6.  * Based on work by George Kyriazis,  1988
  7.  * 
  8.  * This program is free software; you can redistribute it and/or modify it
  9.  * under the terms of the GNU General Public License as published by the
  10.  * Free Software Foundation;
  11.  * 
  12.  * This program is distributed in the hope that it will be useful, but
  13.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU General Public License along
  18.  * with this program; if not, write to the Free Software Foundation, Inc.,
  19.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. #ifndef NULL
  23. #define NULL (void *) 0
  24. #endif
  25.  
  26. #ifndef    FALSE
  27. #define    FALSE    0
  28. #endif
  29.  
  30. #ifndef    TRUE
  31. #define    TRUE    1
  32. #endif
  33.  
  34. #define EXTERN extern        /* so space can easily be allocated, see
  35.                  * initalize.c */
  36.  
  37. #define PRIVATE static        /* local to a module */
  38. #define PUBLIC            /* global function */
  39.  
  40. #define    EPSILON    1e-10        /* ridiculously small */
  41. #define INFTY    1e20        /* ridiculously big, unity of MIN()  */
  42. #define BOUNDFUDGE 1e-3        /* make bounds this amount too big */
  43. #define DEFAULT_TOLERANCE 1e-3
  44.  
  45. #define    ALG_TOLERANCE DEFAULT_TOLERANCE / 2    /* max error for superqs,
  46.                          * and algebraics */
  47.  
  48. /* constants and bounds for the program */
  49. #define    DEFAULTMAXLEVEL    3    /* default maximum recursion level */
  50. #define    MAX_ORDER    12    /* maximum polynomial order */
  51. #define MAXMAPS        20    /* maximum number of maps to keep track of */
  52. #define MAXCACHE  DEFAULTMAXLEVEL    /* level of shadow caches to keep. */
  53. #define BUFFERSIZE    10240    /* size of input file buffers */
  54.  
  55. typedef signed char bool;    /* a boolean variable */
  56. typedef double matrix[4][4];    /* a 4x4 matrix, 3x3 for rotation, an
  57.                  * extra column for translation */
  58.  
  59. typedef unsigned char byte;
  60.  
  61. /* guess what */
  62. typedef struct {
  63.     double          x,
  64.                     y,
  65.                     z;
  66. }
  67.  
  68. vector;
  69.  
  70. /* the color structure */
  71. typedef struct {
  72.     double          r,
  73.                     g,
  74.                     b;
  75. }
  76.  
  77. color;
  78.  
  79.  
  80. /**************************************************************************/
  81. /* the intersection structure */
  82.  
  83. typedef struct _obj object;
  84.  
  85. /* Depth queue */
  86. typedef struct _dqstruct dqueue;
  87.  
  88. struct _dqstruct {
  89.     object         *obj,    /* the object which was hit. */
  90.                    *obj2;    /* needed for extrusion */
  91.     double          t;        /* where in the ray */
  92.     bool            entering;    /* entering or exiting? (relative to
  93.                  * ray.pos status) */
  94.     dqueue         *next;
  95. };
  96.  
  97. struct intersect {
  98.     dqueue          q_ent;
  99.     vector          n,        /* the normal at that point */
  100.                     x,        /* the intersection point in world
  101.                  * coordinates (with speed) */
  102.                     objectx,    /* intersection in shape coordinates */
  103.                     objectn;
  104. };
  105.  
  106. /* ray types */
  107. enum {
  108.     R_SHADOW = 0x01,
  109.     R_REFLECT = 0x02,
  110.     R_REFRACT = 0x04,
  111.     R_EYE = 0x08,
  112. };
  113.  
  114. /* the ray structure */
  115. struct ray {
  116.     vector          pos;    /* origin */
  117.     vector          dir;    /* direction */
  118.     char            type;    /* what kind, see above */
  119.     long            rayid;    /* not used. (yet) */
  120.     double          maxt,    /* biggest value to look for (if it is
  121.                  * more, than don't check intersection
  122.                  * details */
  123.                     mint;    /* smallest t values to look for. (if it
  124.                  * is less, then exit) */
  125. };
  126.  
  127.  
  128. /* standard manipulators for objects */
  129. struct methods {
  130.     bool            (*all_intersections_method) (dqueue * q, object *op, struct ray * r, int flags, bool *b);
  131.     vector          (*normal_method) (struct intersect, vector);
  132.     void            (*copy_method) (object *, object *);
  133.     bool            (*inside_method) (object *, vector);
  134.     void            (*rotate_method) (object *, matrix rotmat);
  135.     void            (*translate_method) (object *, vector);
  136.     void            (*scale_method) (object *, vector);
  137.     void            (*free_method) (object *);
  138.     void            (*print_method) (object *);
  139.     void            (*precompute_method) (object *);
  140.     char           *name;
  141.     long            test,
  142.                     hit,
  143.                     howmuch;
  144. };
  145.  
  146. enum {
  147.     CHKALL = 0x01,
  148.     CHKINSIDE = 0x02,
  149. };
  150.  
  151. /* some statistics variables */
  152. struct global_stat_struc {
  153.     long
  154.                     raytest,    /* total number of ray/intersection tests */
  155.                     rayintersections,    /* number of rays hitting
  156.                      * something */
  157.                     objtest,    /* number of objects tested. */
  158.                     objhit,    /* total no. objects hit */
  159.                     shadowtest,    /* total number shadow ray tests */
  160.                     reflected,    /* total number of reflected rays */
  161.                     refracted,    /* total number of refracted rays */
  162.                     eyerays,
  163.                     boundtest,
  164.                     boundhit,
  165.                     cliptest,
  166.                     cliphit,
  167.                     cachetest,
  168.                     cachehit,
  169.                     cachemiss,    /* how many do we have in one scene? */
  170.                     objects,
  171.                     bounds,
  172.                     clips,
  173.                     prims,
  174.                     nonprims;
  175. };
  176.  
  177. /* statistics for a single line. */
  178. struct line_stat_struc {
  179.     long
  180.                     raytest,    /* rays */
  181.                     rayintersections,    /* rays hitting something */
  182.                     shadowtest,    /* shadow test */
  183.                     reflected,    /* reflected rays */
  184.                     refracted,    /* refracted rays */
  185.                     eyerays;    /* eye rays */
  186.     short           maxdepth;    /* maximum depth reached. */
  187. };
  188.  
  189. enum {                /* texturing. */
  190.     T_EGAL,            /* smooth */
  191.     T_BLEND,            /* texture blend (not yet) */
  192.     T_IMAGEMAP,            /* image mapping */
  193.     T_COLORMAP,            /* color mapped texture: perlin style (not
  194.                  * yet) */
  195. };
  196.  
  197. enum {
  198.     PHONG_SHADING
  199. };
  200.  
  201.  
  202. #include "objects.h"
  203.  
  204. /* structure to hold texture info */
  205. struct texture_data {
  206.     int             type,
  207.                     shadingtype;
  208.  
  209.     union {
  210.     struct image_map *image;
  211.     } exttext;            /* extended info */
  212.     color          *PoVcolor;
  213.  
  214.     color           refl_color,    /* reflective color */
  215.                     refr_color,    /* refractive color */
  216.                     ambient,    /* ambient color */
  217.                     diffuse,    /* diffuse color */
  218.                     specular;    /* specular color */
  219.  
  220.     double          roughness,    /* specular roughness: 0 < roughn <=1 */
  221.                     index,    /* index of refraction */
  222.                     refl_diffuse,    /* circle of diffusion in
  223.                      * reflection */
  224.                     refr_diffuse,    /* circle of diffusion in
  225.                      * refraction */
  226.                     brilliance;    /* tightness of diffuse illum. */
  227.  
  228.     matrix          inv_trans;    /* inverse transformation */
  229. };
  230.  
  231. struct pixel_data {
  232.     int             imagex,
  233.                     imagey;    /* resolution */
  234.     byte           *pal,
  235.                    *data;    /* the data */
  236.     int             palsize;    /* palette size, 0 for 24 bit color */
  237. };
  238.  
  239. struct image_map {
  240.     struct pixel_data *pic;    /* the picture */
  241.     void
  242.                     (*inv_map) /* inverse mapping */ (double *u, double *v, vector
  243.                               x);
  244.     bool            once;    /* only one time? */
  245.     bool            interpolated;    /* not used */
  246.     bool            usenormal,
  247.                     uvswap;    /* swap output of inv_map? */
  248.     double          u_offs,
  249.                     v_offs;    /* translate image over surface */
  250.     double          u_range,
  251.                     v_range;    /* scale it. */
  252. };
  253.  
  254. /* eye viewing stuff */
  255. struct camera {
  256.     vector          eye_dir,
  257.                     eye,
  258.                     sky;
  259.     double          fov,
  260.                     aspect;
  261. };
  262.  
  263. /*
  264.  * the object structure.
  265.  */
  266. struct _obj {
  267.     int             type;    /* type of object */
  268.     union shape_data {        /* the data. */
  269.     struct plane_data *plane;
  270.     struct sphere_data *sphere;
  271.     struct quadric_data *quadric;
  272.     struct light_data *light;
  273.     struct box_data *box;
  274.     struct torus_data *torus;
  275.     struct algebraic_data *algebraic;
  276.     struct superq_data *superq;
  277.     struct polygon_data *polygon;
  278.     struct extrusion_data *extrusion;
  279.     struct triangle_data *triangle;
  280.     struct disc_data *disc;
  281.     object         *CSG;
  282.     struct composite_data *composite;
  283.     }
  284.     data;
  285.     vector          bmax,
  286.                     bmin;    /* for bounding */
  287.     object         *daddy;
  288.     object         *next;
  289.     struct texture_data *text;    /* the texture of the object */
  290.     vector          speed;    /* motion coefficients */
  291.     object         *bound;    /* bounding shape to the object */
  292.     object         *clip;    /* is the bounding shape a clip? */
  293.     bool            inverted;    /* for CSG purposes. */
  294.     struct methods *methods;
  295.     matrix         *inv_trans;
  296. };
  297.  
  298. /* structure for a polynomial in one var. */
  299. typedef struct _sply {
  300.     int             ord;
  301.     double          coef[MAX_ORDER + 1];
  302. }
  303.  
  304. spoly;
  305.