CG:Skeelogy
FEATURED
ARCHIVES
 
 
CURRENTLY VIEWING
DEPTH OF FIELD USING RAYTRACING
Using raytracing techniques to simulate depth of field
DATE
TYPE
EFFORT
Mar 2007
Rendering
Individual
Depth Of Field Using Raytracing
@ programming > computer graphics
SECTION MENU

This is a program written in openGL to simulate depth-of-field (DOF) effect using raytracing. User will define 3 parameters to specify the DOF look, namely the aperture size (which determines how blur the objects out of focus are), the focal length (which determines the z-distance in the view space which objects will be in focus) and the number of rays (which determines the quality of the blur).

DOF render with: Focal Length = 2, Aperture Size = 64, Rays = 16
DOF render with: Focal Length = 2, Aperture Size = 64, Rays = 16

DOF render with: Focal Length = 5, Aperture Size = 128, Rays = 16
DOF render with: Focal Length = 5, Aperture Size = 128, Rays = 16

An imaginary focal plane F is first defined behind the image plane. A ray is then cast from the eye through the current pixel to be rendered. The intersection point between this ray and F is the focal point P.

Focal point P is found by intersecting the current ray with focal plane F
Focal point P is found by intersecting the current ray with focal plane F

A square grid of length m (determined by the aperture size set by user) is placed on top of the current pixel and n rays are shot out from this grid to P, using stratified sampling. These DOF rays originate from the sampled grid point in image space and not from the eye. The results collected from these rays are blended to get the final color of the pixel to be rendered.

n rays are shot out from the newly placed grid, through the focal point P
n rays are shot out from the newly placed grid, through the focal point P

I used similar triangles to find out where P is at for the current ray. We first find:

  • d: The perpendicular distance from eye to image plane
  • d’: The distance from the eye to the pixel in question

The intersection point of the current ray (due to the current pixel to be rendered) with the focal plane is calculated as

equation

where e is the eye’s world position, v is the unit vector from eye to current pixel and f is the focal length as specified by the user.

Initially I did not jitter the sampling points on the grid and the result obtain had visible bands due to uniform sampling. It is the visual justification that we should use random sampling instead.

Undesired banded appearance due to uniform sampling
Undesired banded appearance due to uniform sampling

Note: As this is part of a group project, this DOF effect is created by modifying a distributed raytracer done by my groupmate Neo Jiet Shern.


 
ABOUT ME
Skeel Lee Skeel Lee
skeel@skeelogy.com
Facebook Google+ Twitter Tumblr
YouTube Vimeo Flickr Pinterest
Senior FX TD / R&D
Digital Domain 3.0 (Previously at Sony Pictures Imageworks, MPC, Industrial Light & Magic, Double Negative)
LinkedIn IMDb GitHub Stack Overflow
I am a Senior Technical Director with strong interests in both tech and art. My life evolves round VFX, photography, software engineering, tools programming and generally anything that looks / sounds cool.
I have done a variety of CG programming, including fluid sims, muscles, soft/rigid bodies, raytracing etc. These knowledge complement the visual works that I do as a TD in VFX.
I was interviewed by The Straits Times in May 2014 for my VFX work in X-Men: Days of Future Past.