src/scene.cc
branchpyrit
changeset 14 fc18ac4833f2
parent 12 f4fcabf05785
child 15 a0a3e334744f
--- a/src/scene.cc	Sat Nov 24 23:55:54 2007 +0100
+++ b/src/scene.cc	Sun Nov 25 15:50:01 2007 +0100
@@ -6,8 +6,8 @@
  */
 
 #include <math.h>
-#include <float.h>
 
+#include "common.h"
 #include "scene.h"
 
 /* http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm */
@@ -21,13 +21,13 @@
 	{
 		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])
+			if (ray.a.cell[i] < L.cell[i] || ray.a.cell[i] > H.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];
+			t2 = (H.cell[i] - ray.a.cell[i]) / ray.dir.cell[i];
 
 			if (t1 > t2)
 				swap(t1, t2);
@@ -114,32 +114,40 @@
 BBox Sphere::get_bbox()
 {
 	BBox bbox = BBox();
-	bbox.L.x = center.x - radius;
-	bbox.L.y = center.y - radius;
-	bbox.L.z = center.z - radius;
-	bbox.R.x = center.x + radius;
-	bbox.R.y = center.y + radius;
-	bbox.R.z = center.z + radius;
+	bbox.L = center - radius;
+	//bbox.L.y = center.y - radius;
+	//bbox.L.z = center.z - radius;
+	bbox.H = center + radius;
+	//bbox.H.y = center.y + radius;
+	//bbox.H.z = center.z + radius;
 	return bbox;
 }
 
-bool Plane::intersect(const Ray &ray, float &dist)
+bool Box::intersect(const Ray &ray, float &dist)
 {
-	float dir = dot(N, ray.dir);
-	if (dir != 0)
-	{
-		float newdist = -(dot(N, ray.a) + d) / dir;
-		if (newdist > 0 && newdist < dist) {
-			dist = newdist;
-			return true;
-		}
-	}
-	return false;
+	float b;
+	return get_bbox().intersect(ray, dist, b);
 }
 
-BBox Plane::get_bbox()
+Vector3 Box::normal(Vector3 &P)
 {
-	return BBox();
+	Vector3 N(0,1,0);
+	/*for (int i = 0; i < 3; i++)
+	{
+		if (P.cell[i] >= L.cell[i]-Eps && P.cell[i] <= L.cell[i]+Eps)
+		//if (P.cell[i] == L.cell[i])
+		{
+			N.cell[i] = -1.0;
+			break;
+		}
+		if (P.cell[i] >= H.cell[i]-Eps && P.cell[i] <= H.cell[i]+Eps)
+		//if (P.cell[i] == H.cell[i])
+		{
+			N.cell[i] = +1.0;
+			break;
+		}
+	}*/
+	return N;
 }
 
 // this initialization and following intersection methods implements
@@ -233,12 +241,12 @@
 	if (C.y < bbox.L.y)  bbox.L.y = C.y;
 	if (B.z < bbox.L.z)  bbox.L.z = B.z;
 	if (C.z < bbox.L.z)  bbox.L.z = C.z;
-	bbox.R = A;
-	if (B.x > bbox.R.x)  bbox.R.x = B.x;
-	if (C.x > bbox.R.x)  bbox.R.x = C.x;
-	if (B.y > bbox.R.y)  bbox.R.y = B.y;
-	if (C.y > bbox.R.y)  bbox.R.y = C.y;
-	if (B.z > bbox.R.z)  bbox.R.z = B.z;
-	if (C.z > bbox.R.z)  bbox.R.z = C.z;
+	bbox.H = A;
+	if (B.x > bbox.H.x)  bbox.H.x = B.x;
+	if (C.x > bbox.H.x)  bbox.H.x = C.x;
+	if (B.y > bbox.H.y)  bbox.H.y = B.y;
+	if (C.y > bbox.H.y)  bbox.H.y = C.y;
+	if (B.z > bbox.H.z)  bbox.H.z = B.z;
+	if (C.z > bbox.H.z)  bbox.H.z = C.z;
 	return bbox;
 };