by reikonomusha · 9y ago · 0 comments
(I say this in contrast to something which was explicitly "code golfed", which this code seems not to be.)
It ended up at probably 500 lines at first, and then 800 or so lines because I tried to port it to CUDA. It was mainly an exercise in learning CUDA -- i.e. how is CUDA different than C++? I got something running on my graphics card, but it didn't completely work and I moved onto other things.
I haven't run this code since 2015 but I did have a lot of fun learning from it.
$ wc -l smallpt.cpp
811 smallpt.cpp
Another nice codebase I learned from is tinypy:It's written in a similarly condensed style. I'm inclined to agree with you at first, but honestly I find that reformatting code helps me get a feel for it! It is a mechanical exercise that reminds you of everything that's there. Going through and renaming functions and types to "your style" seems kinda frivolous but it actually helps IMO.
Vec w=nl, u=((fabs(w.x)>.1?Vec(0,1):Vec(1))%w).norm(), v=w%u;
is completely unreadable and that's just one line.
Vec w=nl,
u=((fabs(w.x) > .1 ? Vec(0,1) : Vec(1)) % w).norm(),
v=w%u;
Simple, right? I can see how it would look like garbage if you miss the three separate variables being initialized, but that should be familiar to most people with C/C++ experience.It might be helpful if the instantiations were written fully too, ie. Vec(0,1,0) : Vec(1,0,0)
The only confusing thing in this line is the % operator being overloaded on Vecs to mean the cross product. What the calculation is actually for is what takes time to grok, but you cant expect that to be revealed by familiar formatting and expansive variable names.
PBRT fills that role nicely IMO.
PBRT was created for pedagogical reasons and has very easy to follow code with full documentation of everything relevant. Smallpt is designed to be super small and doesn't really say why it works; PBRT does.
As a bonus, a lot of other physically-based renderers you'll find on GitHub follow PBRT's overall architecture closely (such as Embree[1]), so they're also easy to read/understand if you already know PBRT.
[0] http://pbrt.org/