Depth of Field


This is simple implementation of DOF lense's effect. Basic idea is to slightly move origin of each generated ray by small distance and change its direction in the way that it intersects original ray in distance equal to focus distance. Higher or lower aperture values could be easily simulated by setting Circle of Confusion size.

Rendered with DOF, 100 samples per pixel


Source code (core part):

// from PerspectiveCamera.hxx

virtual bool InitRayDOF(double x, double y, Ray &ray, double focusDistance, double circleOfConfusionSize)

{

       InitRay(x, y, ray);

 

       Vec3f focusedPoint = ray.org + focusDistance * ray.dir;

 

       // Move origin of camera ray on one random point on circle with radius of circleOfConfusionSize

       double fi = 2*M_PI*frand();

       Vec3f newOrigin = ray.org + cos(fi)*circleOfConfusionSize * xAxis + sin(fi) * circleOfConfusionSize * yAxis;

 

       ray.dir = focusedPoint - newOrigin;

       Normalize(ray.dir);

       ray.org = newOrigin;

 

       return true;

};


References: