src/scene.cc
branchpyrit
changeset 12 f4fcabf05785
parent 8 e6567b740c5e
child 14 fc18ac4833f2
equal deleted inserted replaced
11:4d192e13ee84 12:f4fcabf05785
     4  *
     4  *
     5  * Radek Brich, 2006
     5  * Radek Brich, 2006
     6  */
     6  */
     7 
     7 
     8 #include <math.h>
     8 #include <math.h>
       
     9 #include <float.h>
       
    10 
     9 #include "scene.h"
    11 #include "scene.h"
       
    12 
       
    13 /* http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm */
       
    14 bool BBox::intersect(const Ray &ray, float &a, float &b)
       
    15 {
       
    16 	float tnear = -FLT_MAX;
       
    17 	float tfar = FLT_MAX;
       
    18 	float t1, t2;
       
    19 
       
    20 	for (int i = 0; i < 3; i++)
       
    21 	{
       
    22 		if (ray.dir.cell[i] == 0) {
       
    23 			/* ray is parallel to these planes */
       
    24 			if (ray.a.cell[i] < L.cell[i] || ray.a.cell[i] > R.cell[i])
       
    25 				return false;
       
    26 		} else
       
    27 		{
       
    28 			/* compute the intersection distance of the planes */
       
    29 			t1 = (L.cell[i] - ray.a.cell[i]) / ray.dir.cell[i];
       
    30 			t2 = (R.cell[i] - ray.a.cell[i]) / ray.dir.cell[i];
       
    31 
       
    32 			if (t1 > t2)
       
    33 				swap(t1, t2);
       
    34 
       
    35 			if (t1 > tnear)
       
    36 				tnear = t1; /* want largest Tnear */
       
    37 			if (t2 < tfar)
       
    38 				tfar = t2; /* want smallest Tfar */
       
    39 			if (tnear > tfar)
       
    40 				return false; /* box missed */
       
    41 			if (tfar < 0)
       
    42 				return false; /* box is behind ray */
       
    43 		}
       
    44 	}
       
    45 
       
    46 	a = tnear;
       
    47 	b = tfar;
       
    48 	return true;
       
    49 }
    10 
    50 
    11 bool Sphere::intersect(const Ray &ray, float &dist)
    51 bool Sphere::intersect(const Ray &ray, float &dist)
    12 {
    52 {
    13 	Vector3 V = ((Ray)ray).a - center;
    53 	Vector3 V = ((Ray)ray).a - center;
    14 
    54 
    70 	}
   110 	}
    71 	return false;
   111 	return false;
    72 }
   112 }
    73 
   113 
    74 BBox Sphere::get_bbox()
   114 BBox Sphere::get_bbox()
    75 {	
   115 {
    76 	BBox bbox = BBox();
   116 	BBox bbox = BBox();
    77 	bbox.L.x = center.x - radius;
   117 	bbox.L.x = center.x - radius;
    78 	bbox.L.y = center.y - radius;
   118 	bbox.L.y = center.y - radius;
    79 	bbox.L.z = center.z - radius;
   119 	bbox.L.z = center.z - radius;
    80 	bbox.R.x = center.x + radius;
   120 	bbox.R.x = center.x + radius;
    96 	}
   136 	}
    97 	return false;
   137 	return false;
    98 }
   138 }
    99 
   139 
   100 BBox Plane::get_bbox()
   140 BBox Plane::get_bbox()
   101 {	
   141 {
   102 	return BBox();
   142 	return BBox();
   103 }
   143 }
   104 
   144 
   105 // this initialization and following intersection methods implements
   145 // this initialization and following intersection methods implements
   106 // Fast Triangle Intersection algorithm from
   146 // Fast Triangle Intersection algorithm from