src/scene.cc
branchpyrit
changeset 21 79b516a3803d
parent 20 f22952603f29
child 22 76b7bd51d64a
--- a/src/scene.cc	Thu Nov 29 18:30:16 2007 +0100
+++ b/src/scene.cc	Fri Nov 30 00:44:51 2007 +0100
@@ -12,6 +12,8 @@
 
 void Camera::rotate(const Quaternion &q)
 {
+	/*
+	//non-optimized
 	Quaternion res;
 	res = q * Quaternion(u) * conjugate(q);
 	u = res.toVector();
@@ -19,10 +21,9 @@
 	v = res.toVector();
 	res = q * Quaternion(p) * conjugate(q);
 	p = res.toVector();
-	p.normalize();
-	u.normalize();
-	v.normalize();
-/*  // optimized version
+	*/
+
+	// optimized
 	float t2 =   q.a*q.b;
 	float t3 =   q.a*q.c;
 	float t4 =   q.a*q.d;
@@ -44,7 +45,10 @@
 	x = 2*( (t8 + t10)*v.x + (t6 -  t4)*v.y + (t3 + t7)*v.z ) + v.x;
 	y = 2*( (t4 +  t6)*v.x + (t5 + t10)*v.y + (t9 - t2)*v.z ) + v.y;
 	z = 2*( (t7 -  t3)*v.x + (t2 +  t9)*v.y + (t5 + t8)*v.z ) + v.z;
-	v = Vector3(x,y,z);*/
+	v = Vector3(x,y,z);
+	p.normalize();
+	u.normalize();
+	v.normalize();
 }
 
 void Camera::move(const float fw, const float left, const float up)
@@ -92,28 +96,15 @@
 
 bool Sphere::intersect(const Ray &ray, float &dist)
 {
-	Vector3 V = ((Ray)ray).o - center;
-
-	float Vd = - dot(V, ray.dir);
-	float Det = Vd * Vd - (dot(V,V) - sqr_radius);
-
+	Vector3 V = ray.o - center;
+	register float d = -dot(V, ray.dir);
+	register float Det = d * d - (dot(V,V) - sqr_radius);
 	if (Det > 0) {
-		Det = sqrtf(Det);
-		float t1 = Vd - Det;
-		if (t1 > 0)
+		d -= sqrtf(Det);
+		if (d > 0 && d < dist)
 		{
-			if (t1 < dist) {
-				dist = t1;
-				return true;
-			}
-		} else {
-			float t2 = Vd + Det;
-			if (t2 > 0)
-			{
-				// ray from inside of the sphere
-				dist = t2;
-				return true;
-			}
+			dist = d;
+			return true;
 		}
 	}
 	return false;
@@ -163,8 +154,15 @@
 
 bool Box::intersect(const Ray &ray, float &dist)
 {
-	float b;
-	return get_bbox().intersect(ray, dist, b);
+	float a,b;
+	bool res = get_bbox().intersect(ray, a, b);
+	if (res && a < dist)
+	{
+		dist = a;
+		return true;
+	}
+	else
+		return false;
 }
 
 Vector3 Box::normal(Vector3 &P)