# Path tracer

This is a path tracer I wrote from scratch in C++ for my Advanced Modelling, Rendering and Animation assignment.

I used SDL to present the pixels to the user and I based the code in a ray tracer I wrote some months ago (here is the post). You can download the code (svn://quasar.dnsalias.com/eleni/path_tracer/), the report, the Linux 32 bit binary, the Linux 64 bit binary, the Win32 executable, the MacOSX binary and the README file with instructions on how to run the program. Below are some shots and explanations:

### Requirement 1: Many rays per pixels using stratification and jittering:

The pixel is divided recursively to 4 subpixels according to the desired number of rays per pixel (this number is passed as a parameter and is rounded to a power of four). Then the center of each sub-pixel is randomly moved in an area between the boundaries of the sub-pixel. Below, we can see some images produced for different numbers of pixels:

32 and 64 rays per pixel respectively

### Requirement 2: Use of spherical, triangular & quadrilateral area light sources:

The path tracer (that was still a Monte Carlo ray tracer in this stage) supports spheres, sphere flakes, planes, meshes of quads and meshes of triangles. From those objects, the spheres, the triangle – meshes and the quad – meshes can be used also as area lights. Instead of sending a ray to a point light source and test if our object of interest is in shadow or not, now we use a random point in the surface of the area light. In case of the sphere area lights, points are generated in the bounding cube until one is found to be inside the sphere (rejection sampling). In case of the triangular meshes, a triangle of the mesh is chosen randomly. Then, a point inside this triangle is chosen by calculating random α, β and γ barycentric coordinates between 0 and 1. If α, β and γ have sum greater than one we reject the point (rejection sampling) otherwise we calculate its Cartesian coordinates. In case of quadrilateral meshes, a quad of the mesh is chosen at random and then a random point in that quad is generated. In order to generate a random point in a quad, a random linear combination of 2 vectors of 2 consecutive edges of the quad is used. In the following 3 pictures a scene is lit firstly by using a sphere area light, then by using a quad area light and then using a triangular mesh (teapot) area light:

### Requirement 3: Sampling the Lambertian and the Phong BRDFs:

In case of specular surfaces the reflected ray directions form a lobe whereas in case of diffuse surfaces they are scattered in every direction in the hemisphere above the surface. In each case, a random vector was chosen inside the lobe or the hemisphere respectively and the probability to continue tracing or stop was calculated by evaluating the BRDF. In the pictures below, we can see the difference in the reflections of a sphere when sampling the Phong BRDF with various (decremented) specular exponents. We can observe that as the specular exponent is decreased the cone of directions is getting larger:

### Requirement 4: BRDF evaluation:

The formulas for the Phong and Lambertian models were used to evaluate the BRDF in case of specular and diffuse surfaces respectively. Case of diffuse interaction: $k_d frac{1}{pi}$. Case of specular interaction: $k_s frac{m+2}{2pi}cos^malpha$.

### Requirement 5: Put it all together to form paths that sample all the integrals:

The path tracer sends many rays per pixel using stratification and jittering. Then, it traces each ray by calling recursively the shade function. In this function it samples each BRDF to decide if the tracing will continue or stop after a specific intersection.

### Requirement 6: Russian roulette (unbiased path termination):

The method of Russian roulette was used to implement importance sampling. First, we decide if the next interaction will be diffuse or specular and second, we decide if the tracing will continue or not (unbiased path termination).  In the first case, a random number was chosen between 1 and sum of the means of diffuse kd and the specular ks and if it was larger than ks the next interaction was diffuse else it was specular.

In the second case (unbiased path termination), the dot product of the randomly generated vector inside the lobe or the hemisphere (see previous requirement) with the normal of the surface was calculated and compared to the probability evaluated by the BRDF. If it was less or equal to the BRDF value the tracing continued otherwise it stopped.

### Requirement 7: Scene creation:

Different scenes were created 🙂 Here are some of them:
Skylight images: a huge hemisphere was used as area light sources to give the impression of skylight. The following scenes were rendered:

Cornell Box: The Cornell Box was rendered with different numbers of rays per pixel:

### Other work:

I added also some features to improve the result… e.g. I used gamma-correction to improve the appearance of the colors and a shell script that transforms .OBJ scenes to my scene format to avoid extra pain… 😛 (the teapot for example was generated using a low-polygon version of the Utah teapot…)

Advertisements

## 22 thoughts on “Path tracer”

1. The gamma corrected image looks fantastic!

The large hemisphere trick for skylight-like results was neat too.

Like

2. neuhaus says:

its wonderful!!!

Like

3. Queen Elisabeth II says:

its very very cool

Like

4. Katerina says:

Great Project! Congratulations!

Like

5. Stelios says:

Those skylight pics look very impressive! I liked your ray tracer as well! Keep going!!

Like

6. Very good, good job hikiko!

Like

7. Sony says:

Very impresive!!

Like

8. Bekos says:

OMG! Looks great!! Great job!!

Like

9. ΕΥΑ says:

υπεροχες δημιουργιες μπραβο εισαι καταπληκτικη
συγχαρητηρια
πολυ ωραια και τα χρωματα που χρησιμοποιεις

Like

10. jmamt says:

Πολύ ωραία δουλειά! Αρκετά εντυπωσιακο!!
Συνέχισε έτσι!

Like

11. Εξαιρετική δουλειά μπράβο κορίτσι μου. Έχεις πολύ ταλέντο

Like

12. Ταμτάκος says:

Είναι καταπληκτική και πολύ εντυπωσιακή δουλειά!!!
Ο συνδυασμός χρωμάτων είναι φανταστικός και δείχνει καλλιτεχνικό ταλέντο!!!
Όσο για το βάθος που δίνει η προοπτική είναι το κάτι άλλο!!!
Έμεινα έκθαμβος ! ! ! ! !

Like

13. hikiko says:

ευχαριστώ :-p thnx…

Like

14. jona says:

i remember these good times… bring on the GPU ray tracer screen shots! i heard you finished coding! 😉 i need to show you some of mine!

Like

1. hey 🙂 hahaha 🙂
show me your snow!!!!! :))) pls!! did u finish the whole app? xD
I am curious!! :))))))

here are some shots of the GPU ray tracer:
http://komodino.itc.auth.gr/~hikikot/shots_dissertation/thumbs.html

also u can check the videos (although the quality sucks):
[video src="http://komodino.itc.auth.gr/~hikikot/shots_dissertation/vid1.ogv" /]
[video src="http://komodino.itc.auth.gr/~hikikot/shots_dissertation/complex.ogv" /]
[video src="http://komodino.itc.auth.gr/~hikikot/shots_dissertation/vid2slow.ogv" /]

I ll capture new ones on a windows pc before the presentation :p the windows driver is faster than the linux one and they have better capturing tools (I hate when windows have sth better than linux 😛 :-P)

Ouf!! now the report… I spend an hour in every sentence 😛 😛 😛 how r u going?

Like

15. jona says:

that looks amazing!! the video quality is &£^\$*… after tuesday i’ll go over and check it out 😉 i like the kd-tree structure images!

yeah, i hate it when windows has better stuff too, but graphics drivers are mainly developed on that platform… slowly this changes… ATI catalyst 8.7 driver for ATI Radeon HD 5800 is not bad though!

report is going slow…pufff… hopefully i have the background and related work finished tonight so i can start writing the technical chapters..

time
is
running
out…

Like

1. I want to see the snow I want to see the snowwww I want to see the snoooowwwwww!!! 🙂
here is the final video (in quality that sucks as well – the video I ll show is an .avi 1.1GB just in case 😛 – paran0id!)
http://komodino.itc.auth.gr/~hikikot/videos_dissertation/
(choose the format u like…)

I still have no progress with the report I hate the blah blah blah… :p I ‘ll list the code now to finish with that stuff 😛

Like

16. P.S.: what I said about the windows driver is wrong!! the program seemed to run equally fast!! xD (but windows do have better capturing tools :p)

Like

17. Fuzz says:

yassou!

Like

1. hikiko says:

yassou fuzz!

Like