include/scene.h
branchpyrit
changeset 92 9af5c039b678
parent 91 9d66d323c354
child 93 96d65f841791
--- a/include/scene.h	Fri May 02 13:27:47 2008 +0200
+++ b/include/scene.h	Mon May 05 15:31:14 2008 +0200
@@ -42,23 +42,24 @@
 {
 public:
 	Vector o, dir;
+
 	Ray(): o(), dir() {};
 	Ray(const Vector &ao, const Vector &adir):
 		o(ao), dir(adir) {};
 };
 
-#ifndef NO_SSE
+#ifndef NO_SIMD
 /**
  * packet of 4 rays
  */
 class RayPacket
 {
 public:
-	VectorPacket o, dir;
+	VectorPacket o, dir, invdir;
 
 	RayPacket(): o(), dir() {};
 	RayPacket(const VectorPacket &ao, const VectorPacket &adir):
-		o(ao), dir(adir) {};
+		o(ao), dir(adir), invdir(mOne/adir) {};
 
 	// index operator - get a ray
 	Ray operator[](int i) const
@@ -75,10 +76,9 @@
  */
 class Camera
 {
-public:
 	Vector eye, p, u, v;
 	Float F;
-
+public:
 	Camera(): eye(0,0,10), p(0,0,-1), u(-1,0,0), v(0,1,0), F(2.*tan(M_PI/8.)) {};
 	Camera(const Vector &C, const Vector &ap, const Vector &au, const Vector &av):
 		eye(C), p(ap), u(au), v(av), F(2.*tan(M_PI/8.)) {};
@@ -89,68 +89,78 @@
 		p.normalize(); u.normalize();
 		v = cross(p, u);
 	};
+
+	const Vector &getEye() const { return eye; };
+	const Vector &getp() const { return p; };
+	const Vector &getu() const { return u; };
+	const Vector &getv() const { return v; };
+	const Float &getF() const { return F; };
 	void setEye(const Vector &aeye) { eye = aeye; };
-	void setAngle(const Float angle) { F = 2.*tan(angle/2.); };
+	void setp(const Vector &ap) { p = ap; };
+	void setu(const Vector &au) { u = au; };
+	void setv(const Vector &av) { v = av; };
+	void setF(const Float &aF) { F = aF; };
+	void setAngle(const Float angle) { F = 2.0f*tan(angle/2.0f); };
 	void rotate(const Quaternion &q);
 	void move(const Float fw, const Float left, const Float up);
 
-	Ray makeRay(Sample &samp)
+	const Ray makeRay(const Sample &samp) const
 	{
-		Vector dir = p - (u*samp.x + v*samp.y)*F;
-		dir.normalize();
+		Vector dir = normalize(p - (u*samp.x + v*samp.y)*F);
 		return Ray(eye, dir);
 	};
 
-#ifndef NO_SSE
-	void makeRayPacket(Sample *samples, RayPacket &rays)
+#ifndef NO_SIMD
+	void makeRayPacket(const Sample *samples, RayPacket &rays) const
 	{
-		__m128 m1x,m1y,m1z;
-		__m128 m2x,m2y,m2z;
-		__m128 m;
+		mfloat4 m1x,m1y,m1z;
+		mfloat4 m2x,m2y,m2z;
+		mfloat4 m;
 
 		// m1(xyz) = u * samples[i].x
-		m1x = _mm_set_ps1(u.x);
-		m1y = _mm_set_ps1(u.y);
-		m1z = _mm_set_ps1(u.z);
-		m = _mm_set_ps(samples[3].x, samples[2].x, samples[1].x, samples[0].x);
-		m1x = _mm_mul_ps(m1x, m);
-		m1y = _mm_mul_ps(m1y, m);
-		m1z = _mm_mul_ps(m1z, m);
+		m1x = mset1(u.x);
+		m1y = mset1(u.y);
+		m1z = mset1(u.z);
+		m = mset(samples[3].x, samples[2].x, samples[1].x, samples[0].x);
+		m1x = mmul(m1x, m);
+		m1y = mmul(m1y, m);
+		m1z = mmul(m1z, m);
 
 		// m2(xyz) = v * samples[i].y
-		m2x = _mm_set_ps1(v.x);
-		m2y = _mm_set_ps1(v.y);
-		m2z = _mm_set_ps1(v.z);
-		m = _mm_set_ps(samples[3].y, samples[2].y, samples[1].y, samples[0].y);
-		m2x = _mm_mul_ps(m2x, m);
-		m2y = _mm_mul_ps(m2y, m);
-		m2z = _mm_mul_ps(m2z, m);
+		m2x = mset1(v.x);
+		m2y = mset1(v.y);
+		m2z = mset1(v.z);
+		m = mset(samples[3].y, samples[2].y, samples[1].y, samples[0].y);
+		m2x = mmul(m2x, m);
+		m2y = mmul(m2y, m);
+		m2z = mmul(m2z, m);
 
 		// m1(xyz) = (m1 + m2) = (u*samples[i].x + v*samples[i].y)
-		m1x = _mm_add_ps(m1x, m2x);
-		m1y = _mm_add_ps(m1y, m2y);
-		m1z = _mm_add_ps(m1z, m2z);
+		m1x = madd(m1x, m2x);
+		m1y = madd(m1y, m2y);
+		m1z = madd(m1z, m2z);
 
 		// m1(xyz) = m1*F = (u*samples[i].x + v*samples[i].y)*F
-		m = _mm_set_ps1(F);
-		m1x = _mm_mul_ps(m1x, m);
-		m1y = _mm_mul_ps(m1y, m);
-		m1z = _mm_mul_ps(m1z, m);
+		m = mset1(F);
+		m1x = mmul(m1x, m);
+		m1y = mmul(m1y, m);
+		m1z = mmul(m1z, m);
 
 		// m1(xyz) = p - m1 = p - (u*samples[i].x + v*samples[i].y)*F = dir
-		m2x = _mm_set_ps1(p.x);
-		m2y = _mm_set_ps1(p.y);
-		m2z = _mm_set_ps1(p.z);
-		rays.dir.mx = _mm_sub_ps(m2x, m1x);
-		rays.dir.my = _mm_sub_ps(m2y, m1y);
-		rays.dir.mz = _mm_sub_ps(m2z, m1z);
+		m2x = mset1(p.x);
+		m2y = mset1(p.y);
+		m2z = mset1(p.z);
+		rays.dir.mx = msub(m2x, m1x);
+		rays.dir.my = msub(m2y, m1y);
+		rays.dir.mz = msub(m2z, m1z);
 
 		// copy origin
-		rays.o.mx = _mm_set_ps1(eye.x);
-		rays.o.my = _mm_set_ps1(eye.y);
-		rays.o.mz = _mm_set_ps1(eye.z);
+		rays.o.mx = mset1(eye.x);
+		rays.o.my = mset1(eye.y);
+		rays.o.mz = mset1(eye.z);
 
 		rays.dir.normalize();
+		rays.invdir = mOne/rays.dir;
 	};
 #endif
 };
@@ -180,14 +190,16 @@
 public:
 	Vector L;
 	Vector H;
+
 	BBox(): L(), H() {};
-	BBox(const Vector aL, const Vector aH): L(aL), H(aH) {};
-	Float w() { return H.x-L.x; };
-	Float h() { return H.y-L.y; };
-	Float d() { return H.z-L.z; };
-	bool intersect(const Ray &ray, Float &a, Float &b);
-#ifndef NO_SSE
-	__m128 intersect_packet(const RayPacket &rays, __m128 &a, __m128 &b);
+	BBox(const Vector &aL, const Vector &aH): L(aL), H(aH) {};
+
+	Float w() const { return H.x-L.x; };
+	Float h() const { return H.y-L.y; };
+	Float d() const { return H.z-L.z; };
+	bool intersect(const Ray &ray, Float &a, Float &b) const;
+#ifndef NO_SIMD
+	mfloat4 intersect_packet(const RayPacket &rays, mfloat4 &a, mfloat4 &b) const;
 #endif
 };