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

  1. /* 
  2. Adapted from 
  3.     "Essential Ray Tracing Algorithm" by Eric Haines
  4.     in 'An Introduction to Ray Tracing' ed by Andrew Glassner.
  5.     Academic Press 1989.
  6. -----
  7. s.n.pattanaik
  8. 8-April-1991
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <math.h>
  13.  
  14. #include "GraphicsGems.h"
  15. #include "data_structure.h"
  16.  
  17. #define NUMDIM    3
  18.  
  19. #define RIGHT    0
  20. #define LEFT    1
  21. #define MIDDLE    2
  22.  
  23. int HitBoundingBox(minB,maxB, origin, dir,entrypoint,exitpoint)
  24. /*
  25.     Returns 1 for Ray Box intersection
  26.         0 for no intersection.
  27.     Ray direction need not be normalised.
  28. */
  29. Point3 *minB,         /* Box's Minimum Extent */
  30.        *maxB;        /* Box's Maximum Extent */
  31. Point3 *origin, *dir;    /*ray */
  32. double *entrypoint,
  33.        *exitpoint;    /* Ray paramters at entry and exit */
  34. {
  35.     int intersect_parallel_planes();
  36.  
  37.     *entrypoint = -LARGE;
  38.     *exitpoint = LARGE;
  39.  
  40.     if (intersect_parallel_planes(minB->x,maxB->x,
  41.                       origin->x,dir->x,
  42.                       entrypoint,exitpoint)==0) return(0);
  43.     if (intersect_parallel_planes(minB->y,maxB->y,
  44.                       origin->y,dir->y,
  45.                       entrypoint,exitpoint)==0) return(0);
  46.     if (intersect_parallel_planes(minB->z,maxB->z,
  47.                       origin->z,dir->z,
  48.                       entrypoint,exitpoint)==0) return(0);
  49.     if (*entrypoint<0)*entrypoint=0;
  50.     return(1);
  51. }    
  52. int intersect_parallel_planes(L,R,O,D,tnear,tfar)
  53. double L,R,O,D;
  54. double *tnear,*tfar;
  55. {
  56.     if (D==0){
  57.         if ((O<L)||(O>R)) return(0);
  58.     }
  59.     else{
  60.         double t1=(L-O)/D,
  61.                t2=(R-O)/D;
  62.         if (t1 > t2) { 
  63.             double t=t1;
  64.             t1 = t2; t2 = t;
  65.         }
  66.         if (t1 > *tnear) *tnear = t1;
  67.         if (t2 < *tfar) *tfar = t2;
  68.         if (*tnear > *tfar) return(0);
  69.         if (*tfar < 0) return(0);
  70.     }
  71.     return(1);
  72. }
  73. /*
  74.     Point3 start={0,4,2},dir={0.218,-0.436,0.873};
  75.     Point3 L={0,0,0},H={3,3,3};
  76. main()
  77. {
  78.     double t1,t2;
  79.     printf("Give ray origin <x,y,z> : ");scanf("%lf%lf%lf",&(start.x),
  80.         &(start.y),&(start.z));
  81.     printf("Give ray direction <x,y,z> : ");scanf("%lf%lf%lf",&(dir.x),
  82.         &(dir.y),&(dir.z));
  83.     if(HitBoundingBox(&L,&H,&start,&dir,&t1,&t2))
  84.         printf("Hitting the box at %g and %g.\n",t1,t2);
  85.     else printf("Missed the box.\n");
  86. }
  87. */
  88.