Bit of background – almost 30 years ago, for my final year Computer Science degree project, I wrote (sort of) some code to achieve ray tracing on a Commodore Amiga.

I say “sort of” as to be honest, it wasn’t overly successful, for various reasons:

  • The brief involved writing this in a language called Modula-2. It’s a 1980s language which never really caught on. Probably for a whole host of reasons, many of which I probably encountered along the way..
  • This was 1989. Ray tracing was all the rage at the time, as a fledgling technique for rendering photo-realistic images using powerful computers. It’s an incredibly mathematically intensive technique which back then on a 7.09 MHz CPU-ed Amiga took even the proprietary packages of the time hours to render decent images.
  • The Amiga wasn’t actually capable of producing full-colour graphics. I mean it did have a full colour palette of sorts (can you call 12-bit colours “full colour” ?) but you couldn’t actually use them all at the same time. Well.. you kind of could, but with weird limitation – google “Hold-and-Modify” if you want a tragic history lesson!

Anyway, with all this in mind, my final year (remember that, year) project got as far as rendering monochrome images, in the kind of standard shiny sphere hovering over a chess board style of the time (I never got as far as jagged teapots), but never really solved a weird intermittent bug in the Module-2 compiler which randomly failed to render chunks of the image as it worked through the image pixel-by-pixel.

I was achieving similar rendering speeds to those proprietary packages I mentioned earlier, albeit for much simpler and far less advances techniques, but if you can imagine trying to debug a flakey non-deterministic problem in some code which takes 90 minutes to run for a full-sized image you get the idea.

I’ve mocked-up a demo of about where I got to below – complete with realistic test image size, and random missing pixel block slicing through the image. I think the Amiga could render this many shades of grey, might be wrong though..

I definitely achieved ray tracing, but it was never very satisfying.

Cut to nearly 30 years later

So I’d had a few notions over the years to revisit this in my own time. Originally when I learned C, but that never happened. It would have still suffered from 1990s clock speed anyway. And then I sold off all my ray tracing books and stuff.

But now I’m starting to get in to R. R’s kind of number-crunchy, and this is a number crunching exercise, so it seems like a releatively good project with which to get to grips with R.

Bear in mind, that despite a few literal “Hello world” examples, this really was my own version of “Hello world” in R. As an exercise to learn my way around coding in R, seems like a fairly good fit. I can learn my way through the techniques from the bottom up, and refine my “hello shiny spherical world” program as I go.

So far, I have solved a few basics – such as delving back 30 years in to my memory and reworking the “does a ray intersect with a sphere?” maths. In fact that’s all I’ve solved so far. But in its own way, it’s rather a satisfying couple of hours effort…

Rendered here in all its numerical, textual, rudimentary 30×30 pixel viewport which still won’t all fit on one screen glory…

 

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]    [,11]    [,12]    [,13]    [,14]    [,15]    [,16]    [,17]    [,18]
 [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA       NA
 [2,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA       NA
 [3,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA       NA
 [4,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA       NA
 [5,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA       NA
 [6,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA       NA
 [7,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA       NA
 [8,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA 6.954917
 [9,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA 6.951829 6.774487 6.665741
[10,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA 6.868754 6.694933 6.580859 6.500000
[11,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA 6.862615 6.670073 6.543146 6.451275 6.383123
[12,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA 6.925340 6.690322 6.542741 6.436352 6.356253 6.295466
[13,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA 7.105612 6.759760 6.578470 6.452080 6.357309 6.284433 6.228375
[14,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA 6.903416 6.656519 6.500000 6.385776 6.298260 6.230099 6.177212
[15,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA 6.797787 6.587421 6.444655 6.337894 6.255042 6.189995 6.139236
[16,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA 7.105612 6.734733 6.542343 6.407452 6.305200 6.225245 6.162164 6.112763
[17,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA 7.027786 6.700567 6.516667 6.385776 6.285884 6.207471 6.145446 6.096779
[18,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA 7.009590 6.690322 6.508315 6.378360 6.279036 6.201000 6.139236 6.090750
[19,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA 7.041657 6.702994 6.516818 6.384904 6.284433 6.205652 6.143377 6.094534
[20,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA 7.152527 6.740846 6.543146 6.405993 6.302488 6.221747 6.158137 6.108365
[21,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA 6.811191 6.590107 6.443271 6.334352 6.250178 6.184255 6.132887
[22,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA 6.935539 6.663799 6.500000 6.382214 6.292602 6.223086 6.169273
[23,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA 6.778507 6.582539 6.450018 6.351881 6.276912 6.219462
[24,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA 6.984545 6.704989 6.545297 6.433087 6.349603 6.286659
[25,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA 6.913203 6.685160 6.546221 6.448241 6.376468
[26,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA 6.932113 6.716370 6.588057 6.500000
[27,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA 7.093570 6.817318 6.685957
[28,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA 7.191827
[29,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA       NA
[30,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA       NA       NA       NA       NA       NA       NA       NA       NA
         [,19]    [,20]    [,21]    [,22]    [,23]    [,24]    [,25]    [,26]    [,27]    [,28]    [,29]    [,30]
 [1,]       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA
 [2,]       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA
 [3,]       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA
 [4,]       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA
 [5,]       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA
 [6,]       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA
 [7,]       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA
 [8,] 6.831806 6.761802 6.725256 6.716370 6.735235 6.788093 6.897107       NA       NA       NA       NA       NA
 [9,] 6.592977 6.545297 6.517960 6.509041 6.518498 6.548192 6.602906 6.694066 6.858637       NA       NA       NA
[10,] 6.442310 6.402940 6.379404 6.370600 6.376468 6.397996 6.437570 6.500000 6.595594 6.753216       NA       NA
[11,] 6.333139 6.298291 6.276912 6.268227 6.272176 6.289410 6.321471 6.371272 6.444295 6.552096 6.727734       NA
[12,] 6.250178 6.218172 6.198181 6.189600 6.192368 6.206969 6.234541 6.277166 6.338520 6.425498 6.553337 6.773138
[13,] 6.186188 6.156094 6.137051 6.128553 6.130541 6.143402 6.168052 6.206128 6.260422 6.335856 6.442097 6.603273
[14,] 6.137140 6.108365 6.089984 6.081549 6.083004 6.094678 6.117350 6.152399 6.202122 6.270423 6.364486 6.500000
[15,] 6.100599 6.072726 6.054798 6.046408 6.047499 6.058363 6.079696 6.112717 6.159432 6.223169 6.309820 6.431351
[16,] 6.075048 6.047755 6.030118 6.021753 6.022605 6.032942 6.053408 6.085127 6.129935 6.190829 6.273001 6.386566
[17,] 6.059563 6.032582 6.015100 6.006742 6.007454 6.017493 6.037473 6.068471 6.112231 6.171585 6.251375 6.360845
[18,] 6.053654 6.026745 6.009291 6.000923 6.001583 6.011525 6.031357 6.062141 6.105598 6.164516 6.243650 6.352029
[19,] 6.057190 6.030118 6.012571 6.004173 6.004864 6.014905 6.034914 6.065973 6.109841 6.169381 6.249496 6.359592
[20,] 6.070384 6.042899 6.025130 6.016681 6.017493 6.027838 6.048366 6.080219 6.125264 6.186565 6.269454 6.384429
[21,] 6.093819 6.065638 6.047499 6.038977 6.040012 6.050898 6.072343 6.105598 6.152726 6.217173 6.305101 6.429250
[22,] 6.128553 6.099324 6.080636 6.072017 6.073403 6.085127 6.107993 6.143430 6.193835 6.263323 6.359592 6.500000
[23,] 6.176321 6.145570 6.126095 6.117350 6.119264 6.132236 6.157220 6.195942 6.251375 6.328841 6.439107 6.610802
[24,] 6.239935 6.206969 6.186365 6.177457 6.180167 6.195000 6.223169 6.266919 6.330286 6.421061 6.557459 6.813319
[25,] 6.324187 6.287863 6.265584 6.256467 6.260422 6.278150 6.311348 6.363292 6.440338 6.556726 6.760471       NA
[26,] 6.438133 6.396243 6.371272 6.361879 6.367958 6.390629 6.432687 6.500000 6.606145 6.798077       NA       NA
[27,] 6.602906 6.549788 6.519681 6.509907 6.520331 6.553337 6.615481 6.724651 6.980414       NA       NA       NA
[28,] 6.907075 6.810043 6.763580 6.753216 6.778747 6.854229       NA       NA       NA       NA       NA       NA
[29,]       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA
[30,]       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA

 

But the numbers tell the story. There’s no denying my does the ray intersect with a sphere and how far away is it? code is working.

And that my friends is ray tracing!

Now on to coding the rest of it. Watch this space…