src/scene.cc
branchpyrit
changeset 12 f4fcabf05785
parent 8 e6567b740c5e
child 14 fc18ac4833f2
--- a/src/scene.cc	Fri Nov 23 16:14:38 2007 +0100
+++ b/src/scene.cc	Sat Nov 24 21:55:41 2007 +0100
@@ -6,8 +6,48 @@
  */
 
 #include <math.h>
+#include <float.h>
+
 #include "scene.h"
 
+/* http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm */
+bool BBox::intersect(const Ray &ray, float &a, float &b)
+{
+	float tnear = -FLT_MAX;
+	float tfar = FLT_MAX;
+	float t1, t2;
+
+	for (int i = 0; i < 3; i++)
+	{
+		if (ray.dir.cell[i] == 0) {
+			/* ray is parallel to these planes */
+			if (ray.a.cell[i] < L.cell[i] || ray.a.cell[i] > R.cell[i])
+				return false;
+		} else
+		{
+			/* compute the intersection distance of the planes */
+			t1 = (L.cell[i] - ray.a.cell[i]) / ray.dir.cell[i];
+			t2 = (R.cell[i] - ray.a.cell[i]) / ray.dir.cell[i];
+
+			if (t1 > t2)
+				swap(t1, t2);
+
+			if (t1 > tnear)
+				tnear = t1; /* want largest Tnear */
+			if (t2 < tfar)
+				tfar = t2; /* want smallest Tfar */
+			if (tnear > tfar)
+				return false; /* box missed */
+			if (tfar < 0)
+				return false; /* box is behind ray */
+		}
+	}
+
+	a = tnear;
+	b = tfar;
+	return true;
+}
+
 bool Sphere::intersect(const Ray &ray, float &dist)
 {
 	Vector3 V = ((Ray)ray).a - center;
@@ -72,7 +112,7 @@
 }
 
 BBox Sphere::get_bbox()
-{	
+{
 	BBox bbox = BBox();
 	bbox.L.x = center.x - radius;
 	bbox.L.y = center.y - radius;
@@ -98,7 +138,7 @@
 }
 
 BBox Plane::get_bbox()
-{	
+{
 	return BBox();
 }