blob: 1163b2b30f5625c8c6b5d1a5d620ca6c997b9105 (
plain) (
tree)
|
|
/******************************************************************/
/* 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;
}
|