home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / rad386 / radiosit / src / intersec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-24  |  2.7 KB  |  97 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <math.h>
  5. #include "GraphicsGems.h"
  6. #include "data_structure.h"
  7. #include "objects.h"
  8.  
  9. #include "raddecl.h"
  10.  
  11. static get_ray_bound(s,d,t1,t2,b)
  12. Point3 *s;
  13. Vector3 *d;
  14. double t1,t2;
  15. Box3 *b;
  16. {
  17.     Point3 p1,p2;
  18.     point_on_line(*s,*d,t1,p1);
  19.     point_on_line(*s,*d,t2,p2);
  20.     b->min.x = MIN(p1.x,p2.x); b->max.x = MAX(p1.x,p2.x);
  21.     b->min.y = MIN(p1.y,p2.y); b->max.y = MAX(p1.y,p2.y);
  22.     b->min.z = MIN(p1.z,p2.z); b->max.z = MAX(p1.z,p2.z);
  23. }
  24.  
  25. int get_nearest_object_in_voxel(ray,t_entry,t_exit,vox,min_t,pu,pv)
  26. /*
  27.     Intersects the ray with all the objects intersecting
  28.     the given voxel (vox). In case of a valid intersection returns
  29.     the nearest object with the ray parameter (min_t) and surface 
  30.     parameters (pu,pv), otherwise returns UNDEFINED.
  31.  
  32.     To speed up the routine :
  33.     1. The mail_box is checked first.
  34.     2. The ray extent and the object extent are checked for overlap.
  35.        Only if there is any overlap there is a possibility of intersection.
  36. */
  37. Ray *ray;        /* 
  38.                 start        input
  39.                 direction    input
  40.                 number         input
  41.                 intersections    input & output
  42.             */
  43. double t_entry,t_exit;    /* Input */
  44. Voxel *vox;        /* Input */
  45. double *min_t,*pu,*pv;    /* Output */
  46. {
  47.     int i;
  48.     int nearest_object = UNDEFINED;
  49.     Box3 ray_bound;
  50.     double u=0.,v=0.;
  51.     double t= -1.0;
  52.     int bounds_overlap();
  53.  
  54.     *min_t = LARGE;
  55.  
  56.     get_ray_bound(&(ray->start),&(ray->direction),t_entry,t_exit,&ray_bound);
  57.     /* ray_bound will be used to speed of intersection of ray
  58.            with object in the voxel list */
  59.     for(i=0 ; i < vox->nobjects; i++){
  60. #if defined (EXTRADEBUG)
  61.     for(i=0; i < number_objects;i++){
  62. #endif
  63.         int object_num = vox->intersecting_object_list[i];
  64. #if defined (EXTRADEBUG)
  65.         int object_num = i;
  66. #endif
  67.         /* Enquire the mail box */
  68.         if (object[object_num].mail_box.ray_num == ray->number){
  69.             t = object[object_num].mail_box.t;
  70.             u = object[object_num].mail_box.u;
  71.             v = object[object_num].mail_box.v;
  72.         }
  73.         else if (bounds_overlap(&(object[object_num].bbox),&(ray_bound))){
  74. #if defined(DEBUG)
  75.             (ray->intersections)++;
  76. #endif
  77.             t  = ofunc[
  78.                 object[object_num].surface_geometry_type
  79.                  ].intersect_object
  80.                      (object[object_num].object_specific_structure,
  81.                 &(ray->start),&(ray->direction),&u,&v);
  82.             object[object_num].mail_box.ray_num = ray->number;
  83.             object[object_num].mail_box.t = t;
  84.             object[object_num].mail_box.u = u;
  85.             object[object_num].mail_box.v = v;
  86.         }
  87.         if (t > 0.0) /* If a valid intersection */
  88.             if ((t_entry <= t) && (t_exit >= t))
  89.                  /*If the intersection is within bound*/
  90.                 if (t < *min_t){
  91.                     nearest_object = object_num;
  92.                     *min_t = t; *pu = u; *pv = v;
  93.                 }
  94.     }
  95.     return(nearest_object);
  96. }
  97.