summaryrefslogtreecommitdiff
path: root/hw6/src/recursiveRaytracing.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'hw6/src/recursiveRaytracing.cpp')
-rw-r--r--hw6/src/recursiveRaytracing.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/hw6/src/recursiveRaytracing.cpp b/hw6/src/recursiveRaytracing.cpp
new file mode 100644
index 0000000..1163b2b
--- /dev/null
+++ b/hw6/src/recursiveRaytracing.cpp
@@ -0,0 +1,94 @@
+/******************************************************************/
+/* This file is part of the homework assignments for CSCI-427/527 */
+/* at The College of William & Mary and authored by Pieter Peers. */
+/* No part of this file, whether altered or in original form, can */
+/* be distributed or used outside the context of CSCI-427/527 */
+/* without consent of either the College of William & Mary or */
+/* Pieter Peers. */
+/******************************************************************/
+#include "ray_util.h"
+#include "random_number.h"
+#include "recursiveRaytracing.h"
+
+
+//////////////////
+// Constructors //
+//////////////////
+recursiveRaytracing::recursiveRaytracing(unsigned int maxDepth, unsigned int samplesPerPixel)
+{
+ _depth = maxDepth;
+ _samples = samplesPerPixel;
+}
+
+
+/////////////
+// Methods //
+/////////////
+image recursiveRaytracing::render(const scene& s) const
+{
+ image result(s.getCamera().width(), s.getCamera().height());
+
+ // for every pixel
+ for(image::size_type y=0; y < result.height(); y++)
+ for(image::size_type x=0; x < result.width(); x++)
+ {
+ // for every sample per pixel
+ for(unsigned int sample = 0; sample < _samples; sample++)
+ {
+ // select point in pixel
+ float px = (_samples == 1) ? (float)(x) + 0.5f : (float)(x) + random_float();
+ float py = (_samples == 1) ? (float)(y) + 0.5f : (float)(y) + random_float();
+
+ // generate view ray
+ ray r = s.getCamera()(px,py);
+
+ // recursive ray tracing
+ result(x,y) += traceRay(s, r, 1) / (float)(_samples);
+ }
+ }
+
+
+ // Done.
+ return result;
+}
+
+
+
+color recursiveRaytracing::traceRay(const scene& s, const ray& r, unsigned int currentDepth) const
+{
+ // intersect
+ intersectionPoint ip = s.intersect(r);
+
+ // if hit
+ color result;
+ if(ip.isHit())
+ {
+ // shade point
+ for(unsigned int l=0; l < s.numberOfLightsources(); l++)
+ {
+ // connect to light source
+ lightSample ls = s.getLightsource(l).intensityAt(ip.position());
+
+ // check if occluded
+ ray shadowRay = createRay(ip, ls.directionToLight());
+ intersectionPoint shadowIp = s.intersect(shadowRay);
+ if(ls < shadowIp)
+ result += ip.shade(ls);
+ }
+
+ // recurse
+ if(currentDepth < _depth && ip.getShaderProperties().specular)
+ {
+ ray reflectedRay = reflectRay(ip);
+ result += ip.shade(reflectedRay.direction()) * traceRay(s, reflectedRay, currentDepth+1);
+ }
+
+ }
+
+ // if not hit, check for environment map
+ else if(s.hasEnvironmentMap())
+ result += s.evaluateEnvironmentMap(r.direction());
+
+ // Done.
+ return result;
+}