Weekends are a great chunk of time to get a significant project done, and limiting it to a weekend helps to stay on task. This blog is inspired by a how-to book on ray tracing, but that is just the initial driving example.

Sunday, March 27, 2016

Quartz composer (QC) is an interactive dataflow language from Apple (you need MacOS to run it). I prototype many things in QC because it is very easy to do many things like test image processing functions by feeding the web cam into it. Facebook prototypes their apps on a version of QC they have extended for their purposes. Lots of people, including me, love it. However, visual dataflow a pretty different way of programming and there are also conventions in QC that that are easy, but somebody needs to show them to you. I spent about a week learning QC to the point where I could do something useful because I was a bit overwhelmed by all of the documentation out there. However, key was I found an almost perfect set of youtube videos by Rob Duarte. Go through these videos on Day 1, and be sure to follow each one and actually duplicate his compositions as you go. On Day 2, do your own project. I, for example, did face detection on the webcam and then added devil horns. That was a really easy project in QC because there is a face detection module build in.

Thursday, March 24, 2016

This page is for further reading and a page to comment on. This book is for people who have already written a ray tracer as an entry portal into the world of graphics research. So this book has a narrower audience than the previous two mini-books. It covers a path tracer's probabilistic sampling in enough detail to get people up to speed to follow the literature and rendering trends.

Chapter 1: A Simple Monte Carlo Program

Don Mitchell has a nice paper on how jittering changes convergence rates.

The construction method in the book can be improved by using the surface area heuristic (SAH). When evaluating potential partitions, the one that minimized the surface area of the sum of volumes of the sub-trees is almost always good. Here is a SAH-based build that cuts on the longest axis.

The program in the book implicitly samples lights so there are no shadow rays. If you want to get more efficient direct lighting you can either send shadow rays, or importance sample by sending more rays toward the lights.

Chapter 7: Instances
A general instance usually stores transformation matrices. Composite transforms can all be in one node. When scales are allowed handling the surface normals must be done with care.

It is straightforward to add nonuniform densities by adding a more sophisticated intersection method. This is covered in this blog post. It's pretty common knowledge in the ray tracing community, but not really in the intersection.

Wednesday, January 27, 2016

The books are now available in unprotected pdf for PAY WHAT YOU WISH, with 50% going to not for profit programming education organizations. Here are the files.

Ray tracing was invented by Turner Whitted around 1980. His classic paper is really amazing; it had bounding volume hierarchies, adaptive sampling, and it's future work suggested randomized reflection. Since then it has branched into a ton of variations and is used in most movies for the indirect "bounce" lighting.

I've was assigned a 2D ray tracer in a physics class in 1984 and I was hooked. Since then I have written a bunch of ray tracers in Pascal, Fortran, Scheme, C, C++, Java, and most recently Swift (which is awesome for writing ray tracers). I've also taught classes with ray tracing about a dozen times, most recently at Westminster College in 2015. My approach over the years has evolved to do what I think are the most fun parts of ray tracing that are still in the direction of writing a production-quality ray tracer.

Ray Tracing in One Weekend is a kindle book that goes through all of the details to generate a rudimentary ray tracer. It's $2.99 on amazon. It uses C plus classes plus operator overloading. I have heard this referred to as "C plus" which I now call it. Most production renderers are written in C++ so I opted for that as the driving language. I put the most primal set of these assignments so that it's very doable in a weekend, the fundamental unit of coding self-improvement :) Here is the book:

I advocate using the same class for points, displacements, colors, etc. Some people like more structure and type checking (so for example multiplying two locations would be rejected by the compiler). An example article where points and vectors are different is here. Jim Arvo and Brian Smits experimented with not only distinguishing points and vectors, but using the compiler to do dimensional analysis (so velocity, length, and time would be different types for example). They found this to be too cumbersome in 1990s C++ but I'd love to hear about anybody's experience. Researchers at Dartmouth have taken a really serious effort at this and their code and paper are available at github.

There are a bunch of other object types you can add. Triangles are usually first and I am a fan of barycentric methods. After triangles, many people quit adding primitives because graphics has such a big infrastructure for triangles. Ellipses are an easy thing to add but instancing is usually a more "ray tracey" approach (let the software do the heavy lifting). Composite objects via CSG are surprisingly straightforward.

Chapter 5: Surface normals and multiple objects.

If you want your code to be more efficient for large numbers of objects, use a BVH-- they are as good as any other in efficiency and are the most robust and easiest to implement.

"Ideal" diffuse materials, also called "Lambertian" are used 99% of the time in graphics. The wikipedia article on this approximation is good. Real diffuse surfaces do not behave exactly as Lambertian (for example they get specular at grazing angle) but especially with interreflection in the mix the appearance differences are minor. So this is probably not where you should push your renderer until many many other features are addressed.

Chapter 8: Metal

The first improvement you might make is to have the color of the metal go to white at grazing angle. The Schlick approximation (used in Chapter 9 for glass where grazing behavior matters more) works for that. Full-bore Fresnel equations will describe color variation with angle, but in my experience getting normal incident color is good enough.

Camera parameter setting is just plain ugly. The system used in the book is relatively common and is in my opinion the prettiest. I avoid the matrix formulation wherever possible because I never understand my code when I am done. But it is very elegant and works.

You should have the core of a very serious ray tracer now. I would now take it in one of three directions. They are not mutually exclusive but explicitly deciding your goals will simplify architectural decisions.

Make it physically accurate. This will imply using spectra instead of RGB (I like just using a big array of wavelengths) and get something where you know the reflectances. Popular is to get a X-Rite MSCCC ColorChecker Classic whose data is available online.

Make it good for generating animations. Lots of movies use a ray traced system, and Disney, Pixar, and the Solid Angle teams have both disclosed a remarkable amount about their code. Work on features and then efficiency. I think you will be amazed how soon you can produce amazing images.

Make it fast. Here you can roll your own, or start using a commercial API. To see exactly where that community is now, go to the 2016 HPG conference. Or backtrack in their previous papers. They are a very open community and the papers go into much detail relative to many sub-fields in computer graphics.