Last updated: 2026-11-26
6. Two eyes: stereo pictures
For new readers
Two cameras a short distance apart give two pictures that, seen one by each eye, show depth. This page builds the pair from one camera and checks the geometry that makes the pictures fuse.
Stereo Pictures
Two eyes a few centimetres apart see a scene from two viewpoints, and the brain turns the difference into depth. A pair of pictures, one for each eye, side by side, does the same for anyone who can look at them the right way (parallel, or cross-eyed). This page makes the pair.
The geometry
The obvious way to make a pair is to move the camera sideways and point both cameras at the same spot ("toeing in"). That does give two views, but it also gives a vertical disparity: a point off to one side is higher in one picture than in the other, because the two image planes are tilted toward each other. It strains the eyes and is not how they work. The way that is right for pictures viewed side by side is the off-axis (shifted-window) camera:vertical disparity is the real problem
- the eyes are
s/2either side of the viewer, along the viewer's right; - their axes are parallel (each looks the same way as the viewer, not toward the target);
- each takes its picture through a window moved back toward the point that both are to converge on: by
s / 2Dof the window's width-per-unit, whereDis the distance to that point.
So both pictures have the converging point in their middle and at zero parallax; points nearer than D shift one way between the pictures (the left eye sees them further right), points farther shift the other way, and nothing moves vertically.
The disparity of a point at depth Z along the middle line, in pixels, is
s (1/Z โ 1/D) / (pixel size)
with the pixel size of the camera (the width of the window at distance one divided by its width in pixels). That is the oracle the tests use.
A camera has an optional ninth element, the shift: ray_for_pixel_at adds it to the horizontal position the ray is aimed at, if there is one, and a camera without it is the camera as it was (so every earlier picture is unchanged).
camera_project goes the other way, from a point in the world to the pixel at which a camera sees it, which is how the tests look at disparity.
make a function called camera_with_shift takes c, shift returns c2
return list_set(c, 8, shift)
end
# the pixel (fractional, the centre of the first pixel being 0, 0) at which the camera sees the world point p
make a function called camera_project takes c, p returns px
let q = mat_tuple(c[3], p)
let x_plane = get_x(q) / (0 - get_z(q))
let y_plane = get_y(q) / (0 - get_z(q))
let shift = c[8]
return [(c[4] + shift - x_plane) / c[6] - (1/2), (c[5] - y_plane) / c[6] - (1/2)]
end
# the pair [left, right] of cameras for a view from `from` at `to`, `separation` apart (the eyes
# separation/2 either side of `from`, along the viewer's right), each width x height, with the field of view fov
make a function called stereo_cameras takes from, to, up, fov, width, height, separation returns pair
let eyes = stereo_eyes(from, to, up, separation)
let cl = set_camera_transform(camera(width, height, fov), view_transform(eyes[0][0], eyes[0][1], up))
let cr = set_camera_transform(camera(width, height, fov), view_transform(eyes[1][0], eyes[1][1], up))
return [camera_with_shift(cl, eyes[0][2]), camera_with_shift(cr, eyes[1][2])]
end
# [[from, to, shift] of the left eye, the same of the right]: each eye moved half the separation to its
# own side along the viewer's right, its target moved with it (so the axes are parallel), and its window
# shifted back by half the separation over the distance to the target, so that both see the target in the middle
make a function called stereo_eyes takes from, to, up, separation returns eyes
let look = normalize(tuple_sub(to, from))
let right = normalize(cross(up, look))
let half = separation / 2
let dist = magnitude(tuple_sub(to, from))
return [[tuple_sub(from, tuple_mul(right, half)), tuple_sub(to, tuple_mul(right, half)), 0 - half / dist], [tuple_add(from, tuple_mul(right, half)), tuple_add(to, tuple_mul(right, half)), half / dist]]
end
make a function called render_band_stereo takes cl, cr, w, y0, y1, gap returns bytes
return render_band_stereo_bounded(cl, cr, world_bounded(w), y0, y1, gap)
end
# render_example for a stereo pair: the picture is 2 width + gap across; --band and --assemble as there
make a function called render_example_stereo takes cl, cr, w, png_path, gap returns done
let width = cl[0] * 2 + gap
let bands = to_num(arg_value("--bands"))
let tag = ""
if has_arg("--tag") then
let tag = "." + arg_value("--tag")
end
if has_arg("--band") then
let k = to_num(arg_value("--band"))
let y0 = band_start(cl[1], bands, k)
let y1 = band_start(cl[1], bands, k + 1)
write_file_bytes(png_path + ".band" + k + tag, render_band_stereo(cl, cr, w, y0, y1, gap))
return true
end
if has_arg("--assemble") then
let image = canvas(width, cl[1])
let missing = 0
let k = 0
while k < bands do
let y0 = band_start(cl[1], bands, k)
let y1 = band_start(cl[1], bands, k + 1)
let want = (y1 - y0) * width * 3
let bytes = read_file_bytes(png_path + ".band" + k + tag, 0, want)
if bytes.length != want then
let missing = missing + 1
print("ERROR: band " + k + " has " + bytes.length + " of " + want + " bytes")
end
let i = 0
let y = y0
while y < y1 and bytes.length == want do
let x = 0
while x < width do
write_pixel(image, x, y, color(bytes[i] / 255, bytes[i + 1] / 255, bytes[i + 2] / 255))
let i = i + 3
let x = x + 1
end
let y = y + 1
end
let k = k + 1
end
if missing > 0 then
print("ERROR: " + missing + " of " + bands + " bands missing or short; no image written")
return false
end
print("assembled " + width + "x" + cl[1] + " stereo from " + bands + " bands")
save_png(image, png_path)
return true
end
if has_arg("--threads") then
let t0 = now_ms()
let n = to_num(arg_value("--threads"))
let progress = false
if not has_arg("--no-progress") then
let progress = render_progress(png_path + ".progress")
print("progress: the port is written to " + png_path + ".progress; ask with v2_interp.exe tools/render_status.patlang --file " + png_path + ".progress")
end
let cores = default_cores()
if has_arg("--cores") then
let cores = to_num(arg_value("--cores"))
end
let bytes = render_stereo_threaded_progress(cl, cr, w, gap, n, cores, progress)
let image = canvas(width, cl[1])
let i = 0
let y = 0
while y < cl[1] do
let x = 0
while x < width do
write_pixel(image, x, y, color(bytes[i] / 255, bytes[i + 1] / 255, bytes[i + 2] / 255))
let i = i + 3
let x = x + 1
end
let y = y + 1
end
print("rendered " + width + "x" + cl[1] + " stereo in " + n + " bands by threads in " + (now_ms() - t0) + " ms")
save_png(image, png_path)
return true
end
let t0 = now_ms()
let left = render(cl, w)
let right = render(cr, w)
let image = canvas(width, cl[1])
let y = 0
while y < cl[1] do
let x = 0
while x < cl[0] do
write_pixel(image, x, y, pixel_at(left, x, y))
write_pixel(image, cl[0] + gap + x, y, pixel_at(right, x, y))
let x = x + 1
end
let g = 0
while g < gap do
write_pixel(image, cl[0] + g, y, color((32/255), (32/255), (32/255)))
let g = g + 1
end
let y = y + 1
end
print("rendered " + width + "x" + cl[1] + " stereo serially in " + (now_ms() - t0) + " ms")
save_png(image, png_path)
return true
end
The picture
The scene language takes stereo separation s [gap g] [cross] and the renderer makes the left eye's picture, g pixels of dark grey (8 if not given), and the right eye's, as one picture. cross puts the
right eye's view on the left, for viewing cross-eyed. The pictures are rendered a band of rows at a time as before, each band holding the rows of both eyes, so a stereo picture is drawn on all the cores like any other, and is
the same as one drawn in a single process. --eye left or --eye right renders one eye's picture alone.
camera from 0 2 -9 to 0 1 0 fov 55
stereo separation 0.5 gap 6
Scenarios and oracles
- The mapping. A point at distance
talong the ray through pixel(px, py)projects back to(px, py), for 40 random pixels, with and without a window shift. A shift of 0 sees what a camera without one sees; a shifted window sees a different ray through the same pixel. - The pair. The eyes are the separation apart along the viewer's right; their axes are parallel; the converging point is at pixel
(50, 30)of both 101 ร 61 pictures; the ray through the middle pixel of each passes through it; the disparity at eight depths from 2 to 60 equals the formula to a millionth of a pixel; near points shift one way and far points the other; and 40 points anywhere in front have no vertical disparity. - Negative control: toeing the eyes in, looking at the target with no window shift, gives a vertical disparity of more than a hundredth of a pixel at one point, where the pair gives none.
- The picture. Rows 3 to 8 of the side-by-side picture are, byte for byte, the left eye's row, three pixels of grey, the right eye's row, as rendered one eye at a time; the two eyes see different pictures of the same scene.
- The language.
stereo separation 0.4 gap 6makes a pair and a gap; the left camera has the shift and transformation ofstereo_cameras; the eyes' positions and shifts are there for the exporter;crossis remembered and the gap defaults to 8; a scene without it has no pair; no separation, or a separation of 0, is reported; it keeps fog and antialiasing in the settings.
A pair
scenes/stereo_test.scene: three spheres at three depths, a row of spheres receding on a checkered floor, and stereo separation 0.5.
# spheres at several depths on a checkered floor, for a stereo pair
param w = 200
param h = 120
size w h
camera from 0 2 -9 to 0 1 0 fov 55
stereo separation 0.5 gap 6
light point at -8 12 -8
material floor
color 1 1 1
ambient 0.1 diffuse 0.9 specular 0
pattern checkers 0.9 0.9 0.9 0.3 0.3 0.3 scale 0.8
end
material red
color 0.85 0.2 0.2
ambient 0.1 diffuse 0.7 specular 0.5 shininess 100
end
material blue
color 0.2 0.3 0.85
ambient 0.1 diffuse 0.7 specular 0.5 shininess 100
end
material green
color 0.25 0.7 0.3
ambient 0.1 diffuse 0.7 specular 0.5 shininess 100
end
plane material floor
sphere scale 0.6 at -1.4 0.6 -4 material red
sphere scale 0.9 at 0.2 0.9 0 material blue
sphere scale 1.3 at 2.2 1.3 5 material green
for k in 0 to 4
sphere scale 0.4 at (-4 + k * 2) 0.4 (8 + k) material red
end
> tools/render_scene.sh scenes/stereo_test.scene out/stereo_test.png 20 --set w=300 --set h=180
progress: the port is written to D:/Projects_Organized/raytrace/out/stereo_test.png.progress; ask with v2_interp.exe tools/render_status.patlang --file D:/Projects_Organized/raytrace/out/stereo_test.png.progress
rendered 606x180 stereo in 20 bands by threads in 330859 ms
rendered by 18 threads in 383 s wall clock
Look at the left picture with the left eye and the right with the right (or cross your eyes, if you prefer, and swap the pictures with cross).
Checking it independently
A camera that is not the one the checker knows is an engine feature, so the checker looks at it. The exporter writes one eye (--eye left or right): that eye's position and target, and an H line with the window's shift.
The checker aims each ray through the shifted window by its own arithmetic. Each eye's picture, rendered alone, is compared with its own export:
> tools/render_scene.sh scenes/stereo_test.scene out/stereo_left.png 20 --eye left
progress: the port is written to D:/Projects_Organized/raytrace/out/stereo_left.png.progress; ask with v2_interp.exe tools/render_status.patlang --file D:/Projects_Organized/raytrace/out/stereo_left.png.progress
rendered 200x120 in 20 bands by threads in 73452 ms
rendered by 18 threads in 95 s wall clock
> tools/render_scene.sh scenes/stereo_test.scene out/stereo_right.png 20 --eye right
progress: the port is written to D:/Projects_Organized/raytrace/out/stereo_right.png.progress; ask with v2_interp.exe tools/render_status.patlang --file D:/Projects_Organized/raytrace/out/stereo_right.png.progress
rendered 200x120 in 20 bands by threads in 74108 ms
rendered by 18 threads in 96 s wall clock
> v2_interp.exe tools/export_scene.patlang --scene file --file scenes/stereo_test.scene --eye left --out out/stereo_left.txt
wrote out/stereo_left.txt (9 shapes)
> v2_interp.exe tools/export_scene.patlang --scene file --file scenes/stereo_test.scene --eye right --out out/stereo_right.txt
wrote out/stereo_right.txt (9 shapes)
> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/stereo_left.png -SceneFile out/stereo_left.txt -Stride 4
decoded by .NET: 200x120; pixels differing from the independent recursive tracer by more than 1 level: 0 (max difference 0); stride 4 in each direction, 1500 pixels checked
> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/stereo_right.png -SceneFile out/stereo_right.txt -Stride 4
decoded by .NET: 200x120; pixels differing from the independent recursive tracer by more than 1 level: 0 (max difference 0); stride 4 in each direction, 1500 pixels checked
Negative controls: the left picture against the right eye's export, and against its own export with the H line taken out (an unshifted window):
grep -v "^H " out/stereo_left.txt > out/stereo_left_noshift.txt
> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/stereo_left.png -SceneFile out/stereo_right.txt -Stride 4
decoded by .NET: 200x120; pixels differing from the independent recursive tracer by more than 1 level: 460 (max difference 195); stride 4 in each direction, 1500 pixels checked
> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/stereo_left.png -SceneFile out/stereo_left_noshift.txt -Stride 4
decoded by .NET: 200x120; pixels differing from the independent recursive tracer by more than 1 level: 527 (max difference 194); stride 4 in each direction, 1500 pixels checked
The garden, in stereo
The pond garden with --set eyes=0.5 (and a little mist, for depth):
> tools/render_scene.sh scenes/pond_garden.scene out/pond_stereo.png 60 --set eyes=0.5 --set mist=0.05 --set w=320 --set h=192
progress: the port is written to D:/Projects_Organized/raytrace/out/pond_stereo.png.progress; ask with v2_interp.exe tools/render_status.patlang --file D:/Projects_Organized/raytrace/out/pond_stereo.png.progress
rendered 648x192 stereo in 60 bands by threads in 7033336 ms
rendered by 18 threads in 7442 s wall clock
How it is checked
> v2_interp.exe tests/run.patlang --node picture/stereo --depth 2
[ ok ] Two eyes
[ ok ] The mapping
[ ok ] A point along the ray through pixel (px, py) projects back to (px, py): 40 pixels, camera ...
[ ok ] The pair
[ ok ] The eyes are the separation apart, along the viewer's right, round the middle
[ ok ] Rows 3 to 8 of the side-by-side picture are the left eye's row, 3 pixels of grey (32), ...
[ ok ] The language
[ ok ] Stereo separation 0.4 gap 6 makes a pair of cameras and a gap
checks: 4 passed, 0 failed, 0 blocked, 0 skipped, 0 not run
ALL PASSED