Ok, this is probably a dumb question: I get that the code traces the rays for this scene, obviously — but where is the scene, then? That is, where is the “data” (as opposed to the algorithm) that says “there’s a sphere at coordinates x,y,z with radius r, and here’s the other one, and here’s the checkerboard plane”?
The 3D scene is described by formulas instead of data. It's a bit similar to how SDF demos describe a 3D scene by combing shape formulas into a single formula for the whole scene (SDF = signed distance fields). E.g. see: https://iquilezles.org/articles/distfunctions/
BBC BASIC raytracer in 432 characters
41–50 of 96 posts
Re: BBC BASIC raytracer in 432 characters
#42Earlier quoted context omitted.
It’s mostly implicit. See how the spheres are symmetric about the centerpoint? (Well, almost, the camera is at Y = -0.1.) The (X, Y) coordinates of their centers are simply (-1, -1) and (1, 1), given by the `I = SGN U` variable at the end of line 40. The Z is implicitly zero. Their radius is 1. The plane is basically defined by `P = Y - 2` at line 60.
…to elucidate a bit, when tracing the rays on the left side of the image, the sign of u is -1, so we're trying to intersect a sphere at (x-1, y-1, z), and on the right side similarly one at (x+1, y+1, z). This works because none of the left-side rays can even in theory hit the right-side sphere and vice versa. It's a very simple version of space partitioning optimization. And when tracing the reflected rays, we flip…
Re: BBC BASIC raytracer in 432 characters
#43Ok, this is probably a dumb question: I get that the code traces the rays for this scene, obviously — but where is the scene, then? That is, where is the “data” (as opposed to the algorithm) that says “there’s a sphere at coordinates x,y,z with radius r, and here’s the other one, and here’s the checkerboard plane”?
IF V
then the (palette index of?) the color of the sky is computed from the square root of the y-coordinate of the direction vector as GCOL 0, 3 - (48*SQR V) DIV 16
plus a fixed dithering tableRe: BBC BASIC raytracer in 432 characters
#44Earlier quoted context omitted.
W=1/SQR(U*U+V*V+1) This makes a sphere. It gets multiplied by two coordinates to make spheres at different places, U and V.
i think this is incorrect, i think that's computing the z component of the initial direction vector of the ray being traced if you change the initialization of i from sgn u to 1.2 * sgn u you will get an image with the spheres a little further apart
// sphere_center = (E, F, Z)
E = X - I
F = Y - I
// solving the ray-sphere quadratic eqn...
P = U * E + V * F - W * Z
// discriminant
D = P * P - E * E - F * F - Z * Z + 1
// if solution (ie. intersection) exists
IF D > 0
// intersect_point = ray_orig + T * ray_dir
// this is the closer pt, -P + SQR D is the other
T = -P - SQR DRe: BBC BASIC raytracer in 432 characters
#45Earlier quoted context omitted.
i think this is incorrect, i think that's computing the z component of the initial direction vector of the ray being traced if you change the initialization of i from sgn u to 1.2 * sgn u you will get an image with the spheres a little further apart
Yeah, that's something of a red herring. The actual ray-sphere intersection happens on line 50: // sphere_center = (E, F, Z) E = X - I F = Y - I // solving the ray-sphere quadratic eqn... P = U * E + V * F - W * Z // discriminant D = P * P - E * E - F * F - Z * Z + 1 // if solution (ie. intersection) exists IF D > 0 // intersect_point = ray_orig + T * ray_dir // this is the closer pt, -P + SQR D is the other T = -P -…
Re: BBC BASIC raytracer in 432 characters
#46https://bbcmic.ro/#%7B%22v%22%3A1%2C%22program%22%3A%22MODE1...
It would look even better with bidirectional error diffusion, but that requires reading back memory which I don't know how to do.
Re: BBC BASIC raytracer in 432 characters
#47Earlier quoted context omitted.
The OP's code has REM based assembly it in to save space
is that assembly or is that the dithering table
Because BBC Basic had a built-in assembler it was pretty uncommon for BBC programs to inline machine code as raw data (unlike some other computers from BITD).
Re: BBC BASIC raytracer in 432 characters
#48Re: BBC BASIC raytracer in 432 characters
#49Re: BBC BASIC raytracer in 432 characters
#50Earlier quoted context omitted.
I'm running this on a BBC micro emulator running at period-accurate speed, and it's taking just under a minute to draw one line. So I think it's going to finish in about 4 hours, which would make sense - approximately twice as fast as the Acorn Electron.
Update: Finished in only 2 hours 30 minutes.