News for hackers. Stuff that matters.
Re:Deciphering the Business Card Raytracer by a_e_k on Sunday September 22, 2013 @01:59AM (#6425965)
Hi there, code author here. I've been lurking here for years, but couldn't resist an invitation like this.

I've always enjoyed Fabien's analyses so it's great fun to see what he makes of my own code. Anyway, a few clarifications from what I remember of this. (I wrote this '09, and I don't have my old notes in front of me right now):

* The `n` value returned by the trace function, `T()`, is the surface normal. It returns this whether it hit the plane or a sphere.

* The `r` vector in `S()` is the reflection vector off whatever was hit.

* The mystery `c` point in the `main()` function is the offset from the eye point (ignoring the lens perturbation `t`) to the corner of the focal plane. Note below that we're nominally tracing rays for pixel (x,y) in the direction of ax+by+c. At the image midpoint, this should be `g`.

* "although I suspect this to be a side effect: This is not how soft-shadows are done." True. There are true soft shadows in this, but this isn't where they're computed. That's the job of the randomization where `l` is computed in `S()`.

Anyway, please feel free to ask any questions about it.

Re:Deciphering the Business Card Raytracer by fabien_sanglard on Sunday September 22, 2013 @02:23AM (#6425997)
Thanks Andrew, that 'c' variable was giving me a headache ;) ! I have updated the article with your remarks.

BTW, I have listed a few good resources about raytracing at the bottom of the page: Do you know any other good books/websites ?

Re:Deciphering the Business Card Raytracer by a_e_k on Sunday September 22, 2013 @04:30AM (#6426233)
You're welcome!

Scratchapixel was new to me, so thanks for that link. PBRT (which you already have) is definitely a favorite. You also cited Graphics Gems IV but I enjoy dipping into all of them. Other suggestions:

* http://tog.acm.org/resources/RTNews/html/ (Somewhat dead, but the archives are great)

* http://ompf2.com/ (Active, lots of ray tracing specific discussion)

* http://www.realtimerendering.com/ (I'm fond of the book as well)

* http://kesen.realtimerendering.com/ (Especially the sections for "Symposium on Interactive Ray Tracing" and it's successor, "High Performance Graphics")

If I had to pick one, though, it'd be PBRT since it's a unified work that starts from the ground up.

Re:Deciphering the Business Card Raytracer by antirez on Sunday September 22, 2013 @03:28AM (#6426109)
Some time ago I wrote a raytracing program in javascript that is supposed to be very simple to understand: https://github.com/antirez/jsrt/blob/master/rt.html

Demo here: http://antirez.com/misc/rt.html

Re:Deciphering the Business Card Raytracer by Uchikoma on Monday September 23, 2013 @07:36AM (#6430143)
Nice!

We did a real time raytracer with 2 spheres where you could move around one with C and a cluster of DEC Alphas 20 years ago, funny to see this on my laptop in Javascript (!) now.

Re:Deciphering the Business Card Raytracer by fabien_sanglard on Sunday September 22, 2013 @02:31AM (#6426007)
How come you decided for a coordinate system with Z pointing up instead of the traditional right hand/left hands coordinate systems ?
Re:Deciphering the Business Card Raytracer by a_e_k on Sunday September 22, 2013 @04:11AM (#6426191)
Actually, Z-up coordinate systems can also be called right or left handed depending on which way Y points. :-)

But really, it was just personal preference. Way back when I was learning 3D graphics, I found it easiest to visualize Z as elevation above the XY plane. I'd also used some software that followed that convention (like trueSpace and later 3D Studio Max).

These days, I mainly work with software that tends to favor a Y-up convention by default.

Re:Deciphering the Business Card Raytracer by mistercow on Sunday September 22, 2013 @04:28AM (#6426227)
In non-programming math contexts, it's pretty standard for Z to point up. I have no idea why, as it seems to me like the typical way it works in graphics programming is the more obvious extension of 2D plotting into 3D.
Re:Deciphering the Business Card Raytracer by Sharlin on Sunday September 22, 2013 @06:01AM (#6426338)
As far as I know, in the CAD world Z is typically up/down. It makes sense if you look at the scene from above, like an architect a floor plan - world-space X and Y map to screen-space X and Y, and Z is then the depth. In a 3D virtual world, on the other hand, the perspective is usually of a person embedded in the world, in which case "Y is up" is more intuitive.
Re:Deciphering the Business Card Raytracer by mistercow on Sunday September 22, 2013 @05:12PM (#6428039)
OK, thinking about the architecture angle finally made it click for me.
Re:Deciphering the Business Card Raytracer by LeafStorm on Sunday September 22, 2013 @08:29AM (#6426539)
When I took Calculus III, Z was up. When I took Engineering Physics the same semester, Y was up.

According to legend, this is why the Math and Physics departments hate each other.

Re:Deciphering the Business Card Raytracer by bulte-rs on Monday September 23, 2013 @03:17AM (#6429512)
So the difference between Math and Physics can be described as a single rotation matrix?
Re:Deciphering the Business Card Raytracer by carbonik on Friday September 27, 2013 @06:08AM (#6456210)
I thought it was coming from the fact Nobel's wife had an affair with a mathematician. Thus explaining why there is no Nobel Price for Mathematics.
Re:Deciphering the Business Card Raytracer by dagw on Friday September 27, 2013 @06:29AM (#6456265)
There are several reasons to believe that that urban legend isn't true. One of them being that Nobel was never married.
Re:Deciphering the Business Card Raytracer by mynameisfiber on Sunday September 22, 2013 @06:26PM (#6428327)
With the right rotation, any direction can be pointing "up".
Re:Deciphering the Business Card Raytracer by Luc on Sunday September 22, 2013 @06:36AM (#6426395)
It makes sense if you think of the X and Y axis as being drawn on paper, which usually lies flat on the desk.
Re:Deciphering the Business Card Raytracer by mistercow on Sunday September 22, 2013 @12:04PM (#6426982)
Sort of, kind of, but it's a stretch. "Up and down" on a page are not oriented according to gravity, but according to the top and bottom of the page. If you say "Well, we'll make the z-axis vertical because vertical is perpendicular to the plane of the desk, and that's depth", and then you put the z-axis toward the top of the page, you're using two completely different definitions of "vertical" in the same sentence.
Re:Deciphering the Business Card Raytracer by qu4z-2 on Sunday September 22, 2013 @09:30PM (#6428778)
I always use Z for up/down even though I'm a programmer. For some reason I just find it easier to reason about. In fairness, my first forays into 3D were extending a 2D top-down game with an "altitude".
Re:Deciphering the Business Card Raytracer by pux0r3 on Tuesday September 24, 2013 @07:15PM (#6441475)
I'm a game programmer, and I have an answer for my preference of Z being up: I want positive values going into the screen (away from the viewer) but I also want a right handed coordinate system (mostly because physics become more intuitive - especially wrt torque).

X is almost always to the right, so this gives me Y and Z to work with. To satisfy both the positive values into the screen and right handedness I need either Z forward and Y down or Y forward and Z up. Since most of the time you draw a graph with an axis going up rather than down, that settles it.

It's entirely arbitrary in the end, a single matrix will fix any dispute you have amongst peers.

Re:Deciphering the Business Card Raytracer by raminf on Sunday September 22, 2013 @02:30AM (#6426004)
#define op operator

#define rt return

Then replacing the corresponding inline tokens shaves off another 25 bytes from the source without affecting the output or (arguably) the readability.

Sorry, my inner obfuscator couldn't resist :-)

Beautiful bit of coding, btw.

Re:Deciphering the Business Card Raytracer by a_e_k on Sunday September 22, 2013 @03:52AM (#6426166)
Thanks, glad you liked it!

Yes, it's true that even more could have been shaved off and I did explore some #defines. As I recall, there were also some other changes I could have made to shave off yet more. Ultimately, I decided against them for two main reasons. First, they'd need to occupy lines by themselves which I felt created a bigger gap at the top that ruined the aesthetics somewhat. I wanted something that would look nice and roughly fill the 1.75 aspect ratio of a typical US business card. Secondly, as noted in my reply to matthew-wegner below, I'd noticed the file size that I had achieved.

Re:Deciphering the Business Card Raytracer by zem on Sunday September 22, 2013 @03:22AM (#6426097)
#defines have to be one to a line, though - in this case, where you're optimising for layout rather than bytes, i'm not sure it would be a net win. counting, there are 5 instances of "operator", so that's a saving of 36 chars versus 40 taken up by the #define (including the whitespace at the end). there are 10 "return"s, so saving 40 chars there just about draws you even.
Re:Deciphering the Business Card Raytracer by phySi0 on Sunday September 22, 2013 @11:50AM (#6426939)
>Then replacing the corresponding inline tokens shaves off another 25 bytes from the source

I dunno, I kinda like the current size.

Re:Deciphering the Business Card Raytracer by fabien_sanglard on Sunday September 22, 2013 @02:32AM (#6426011)
Can you pastebin.com it so we can see what it looks like ?
Re:Deciphering the Business Card Raytracer by raminf on Sunday September 22, 2013 @02:27PM (#6427425)
http://pastebin.com/ktukPBHE

I reformatted it so it would justify right like the original but for some reason Pastebin likes to reflow things.

It turns out the single-character tokens 'u' and 'w' were not being used so repurposing them for the #defines leads to a saving of 46 characters at the cost of an extra line. There are a few other recurring tokens but you hit a point of diminishing return.

One should not, however, lose sight of what an awesomely clever hack the whole thing is. Reminds me of some of the text-flow layouts in old illuminated manuscripts, or the more modern variations here (like the one with the light-bulb): http://www.smashingmagazine.com/2008/02/11/award-winning-new...

Re:Deciphering the Business Card Raytracer by vldr on Wednesday September 25, 2013 @07:14AM (#6443787)
As a (poor :) 3d/raytrace excercise, I've translated the code to python: http://vanderwijk.info/blog/business-card-raytracer-in-pytho...