src/raytracer.cc
branchpyrit
changeset 19 4e0955fca797
parent 16 20bceb605f48
child 20 f22952603f29
--- a/src/raytracer.cc	Mon Nov 26 17:31:37 2007 +0100
+++ b/src/raytracer.cc	Mon Nov 26 23:12:40 2007 +0100
@@ -161,35 +161,33 @@
 static void *renderrow(void *data)
 {
 	RenderrowData *d = (RenderrowData*) data;
+	Vector3 dir = d->dfix;
 	for (int x = 0; x < d->w; x++) {
 		// generate a ray from eye passing through this pixel
-#if 1
-		// no oversampling
-		Vector3 dir = Vector3(d->vx, d->vy, 0) - d->eye;
-		dir.normalize();
-		Ray ray(d->eye, dir);
-		Colour c = d->rt->raytrace(ray, 0, NULL);
-#else
+#if OVERSAMPLING
 		// 5x oversampling
-		Vector3 dir = Vector3();
 		Colour c = Colour();
 
 		for (int i = 0; i < 5; i++)
 		{
 			float osax[] = {0.0, -0.4, +0.4, +0.4, -0.4};
 			float osay[] = {0.0, -0.4, -0.4, +0.4, +0.4};
-			dir = Vector3(d->vx + osax[i] * d->dx,
-				d->vy + osay[i]*d->dy , 0) - d->eye;
-			dir.normalize();
-			Ray ray(d->eye, dir);
+			Vector3 tmpdir = dir + osax[i]*d->dx + osay[i]*d->dy;
+			tmpdir.normalize();
+			Ray ray(d->eye, tmpdir);
 			c += d->rt->raytrace(ray, 0, NULL);
 		}
 		c = c * (1./5);
+#else
+		// no oversampling
+		dir.normalize();
+		Ray ray(d->eye, dir);
+		Colour c = d->rt->raytrace(ray, 0, NULL);
 #endif
 		*(d->iter++) = c.r;
 		*d->iter++ = c.g;
 		*d->iter++ = c.b;
-		d->vx += d->dx;
+		dir += d->dx;
 	}
 #ifdef PTHREADS
 	pthread_exit((void *)d);
@@ -199,26 +197,25 @@
 
 float *Raytracer::render(int w, int h)
 {
+	if (!camera || !top)
+		return NULL;
+
 	float *data;
 
 	data = (float *) malloc(w*h*3*sizeof(float));
 	if (!data)
 		return NULL;
 
-	float startx = -1.0 * 4, starty = (float)h/w * 4;
-	float dx = -2*startx/w, dy = -2*starty/h;
-	float vy;
 	RenderrowData *d;
 
 	printf("* building kd-tree\n");
 	top->optimize();
 
-	//srand(time(NULL));
-
-	/* eye - viewing point */
-	Vector3 eye(0, 0, -5);
-
-	vy = starty;
+	float S = 0.5/w;
+	Vector3 dfix = camera->u*(-w/2.0*S/camera->f)
+		+ camera->v*(h/2.0*S/camera->f) + camera->p;
+	Vector3 dx = camera->u * (S/camera->f);
+	Vector3 dy = camera->v * (-S/camera->f);
 
 #ifdef PTHREADS
 	printf("* pthreads enabled, using %d threads\n", num_threads);
@@ -234,11 +231,12 @@
 		d = (RenderrowData*) malloc(sizeof(RenderrowData));
 		d->rt = this;
 		d->w = w;
-		d->vx = startx;
-		d->vy = vy;
+		d->eye = camera->eye;
+		d->dfix = dfix;
 		d->dx = dx;
+#if OVERSAMPLING
 		d->dy = dy;
-		d->eye = eye;
+#endif
 		d->iter = data + y*3*w;
 #ifdef PTHREADS
 		/* create new thread and increase 't' */
@@ -259,7 +257,7 @@
 		renderrow((void *)d);
 		free(d);
 #endif
-		vy += dy;
+		dfix += dy;
 		printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b%4d (%2d%% done)", y, y*100/(h-1));
 	}
 	printf("\n");