Last updated: 2026-11-26
9. A language for scenes and models
For new readers
The scenes of the film are not written in PatLang but in a small language of their own: shapes, materials, transforms, definitions with parameters, loops and includes. This page opens that language, which every scene of part III is written in. What its words make (shapes, surfaces, lights) is described in parts II, IV and V.
A Language for Scenes and Models
Until now a scene has been a PatLang program: calls to sphere(), set_transform, mat_mul,
with_material_deep, a page of them. This page gives scenes and models their own small language,
in a text file, with the things that make a model worth writing down: values, loops, objects defined
once and used by name, materials, functions typed as text, deformations, a camera and a light.
size 400 200
camera from 0 5 -16 to 0 1 0 fov 60
light point at -10 12 -10
material stone
color 0.82 0.78 0.7
ambient 0.1 diffuse 0.8 specular 0.1 shininess 30
end
# a column: a cylinder and a ball on top, any height and width
define column(h, r)
cylinder 0 h closed scale r 1 r
sphere scale (r * 1.4) at 0 h 0
end
for i in 0 to 4
use column(1.4 + 0.5 * i, 0.3) at (i * 1.8 - 3.6) 0 5 material stone
end
The language
One statement to a line; # starts a comment; a statement that opens a block ends with end.
| statement | meaning |
|---|---|
let name = expr |
a value, for the lines after it |
param name = expr |
a value that can be given from outside (--set name=value to tools/render_scene.sh), and is this default if not |
for i in a to b [step s] โฆ end |
a loop; both ends included; step may be negative |
if a < b โฆ else โฆ end |
one of the two parts, by a comparison (< > <= >= == !=) of two numbers |
material name โฆ end |
a named material: color r g b, ambient, diffuse, specular, shininess, reflective, transparency, ior, pattern stripes|checkers|rings|gradient and two colours, then modifiers for the pattern |
define name(a, b) โฆ end |
an object, with parameters, made when it is used; it may use itself, with an if to stop it |
expr name = "text" |
a piece of a function, available in every later surface |
func name(a, b) = "text" |
a piece with arguments |
sphere, cube, plane, cylinder [lo hi], cone [lo hi], torus R r |
the primitives |
surface "text" |
an implicit surface of a function typed as text (Surfaces typed as text and Surfaces of any function) |
use name(args) |
an object, made from its definition |
group, union, intersection, difference โฆ end |
the shapes inside, joined (a difference takes the later ones from the first) |
twist k, taper k, bend c, wave a w, shear k โฆ end |
the shapes inside, deformed (Surfaces of any function) |
camera from x y z to x y z [up x y z] [fov deg] [size w h] |
where the picture is taken from |
light point at x y z, light area at x y z width w depth d [steps n], light spot at x y z toward x y z angle a [soft b] |
the light, with color r g b after it if not white |
size w h, antialias n, antialias adaptive depth threshold |
the picture |
Numbers are expressions. A number is a word with no spaces (i*2.5) or a group in brackets
((i * 2.5 + 1)), written in the language of Surfaces typed as text and Surfaces of any function: + โ * / ^, sin cos sqrt abs min max โฆ, pi,
sind, cosd, tand, which take degrees, and rand(n), a number in [0, 1) that is the same every time for the same n. They are exact where they can be (0.1 + 0.2 is 3/10). The names are the values
set by let, by a loop, and by a definition's parameters; x, y, z are not names of values, they are the
variables of a surface.
Modifiers follow a shape and are applied in the order written. at x y z, scale s or
scale sx sy sz, rotate x|y|z degrees, material name, and for a surface bounds x0 y0 z0 x1 y1 z1
and for a cylinder or cone closed. So sphere scale 2 at 1 0 0 is scaled and then moved, and
sphere at 1 0 0 scale 2 is moved and then scaled (its centre ends at 2). A material on a block is given
to every shape in it.
A definition may use itself. define branch(len, n) can say use branch(len * 0.7, n - 1) inside an if n > 0, which is how a tree is made (Trees, plants and a garden). A definition that never stops is caught at 64 uses open at once, and a scene that makes more than 100 000 shapes is refused, so a mistake fails with a message instead of filling the memory. rand takes a number, not a function of x, y, z; it is the fractional part of sin(12.9898 n + 78.233) ยท 43758.5453, which is not random but varies as if it were.depth limit prevents stack overflow
A definition is run each time it is used, with its parameters as values and everything set before the
use still in scope, so it can use other definitions. A surface's text may use the values in scope, the expr
pieces, and the funcs: each call is replaced by its text with the arguments in place before the text is read,
so a text that was a polynomial is still one, and gets the exact solver of The torus and other polynomial surfaces.
Errors say where. line 7: unknown name 'b', line 12: unknown object 'column', line 3: this for i in 0 to 2 has no end.
Nothing is built from a scene with an error.
A world has one light, so a second light is refused.
Trees, plants and a garden adds include, and texture and bump lines in materials; they are in the listing below, after the rest.
How it is read
A line is cut into tokens: words, "strings", and ( groups ) with their brackets, a word running straight into
a group that follows it (column(3, 0.5)). The lines become statements, each with the statements of its block inside
it. Running them carries the values in scope down as a list (a let in a loop is that iteration's), and the things that
are not values (materials, definitions, the camera, the light) in a table. Each shape statement builds its base shape,
then applies its modifiers.
# a token ends at white space; it is a word, a "string", or a ( group ) with its brackets kept; a word
# runs straight into a ( group ) that follows it (column(3, 0.5)); # outside a string starts a comment
make a function called scene_tokens takes line returns toks
let bytes = ex_bytes(line)
let n = bytes.length
let toks = []
let i = 0
while i < n do
let c = bytes[i]
if c == 32 or c == 9 or c == 13 then
let i = i + 1
elif c == 35 then
let i = n
else
let sb = sb_new()
let going = true
while going and i < n do
let c = bytes[i]
if c == 32 or c == 9 or c == 13 or c == 35 then
let going = false
elif c == 34 then
# a string, to the closing quote
sb_push(sb, chr(c))
let i = i + 1
while i < n and bytes[i] != 34 do
sb_push(sb, chr(bytes[i]))
let i = i + 1
end
if i < n then
sb_push(sb, chr(34))
let i = i + 1
end
elif c == 40 then
# a bracketed group, to its closing bracket
let depth = 0
let inside = true
while inside and i < n do
sb_push(sb, chr(bytes[i]))
if bytes[i] == 40 then
let depth = depth + 1
elif bytes[i] == 41 then
let depth = depth - 1
if depth == 0 then
let inside = false
end
end
let i = i + 1
end
else
sb_push(sb, chr(c))
let i = i + 1
end
end
let toks = list_push(toks, sb_str(sb))
end
end
return toks
end
# the words that open a block, which `end` closes
make a function called scene_opens takes kw returns yes
return kw == "define" or kw == "material" or kw == "for" or kw == "if" or kw == "difference" or kw == "union" or kw == "intersection" or kw == "group" or kw == "twist" or kw == "taper" or kw == "bend" or kw == "wave" or kw == "shear"
end
# the statements of lines[k ...] up to the end that closes the block (or the end of the text at the top)
# [statements, index after the end] or [false, message]; a statement is [word, tokens, statements inside, line number]
make a function called scene_parse_block takes lines, k, depth, opened returns result
let stmts = []
let i = k
while i < lines.length do
let toks = scene_tokens(lines[i])
if toks.length > 0 then
let kw = toks[0]
if kw == "end" then
if depth == 0 then
return [false, "line " + (i + 1) + ": end with nothing to end"]
end
return [stmts, i + 1]
end
if scene_opens(kw) then
let inner = scene_parse_block(lines, i + 1, depth + 1, i + 1)
if inner[0] == false then
return inner
end
let stmts = list_push(stmts, [kw, toks, inner[0], i + 1])
let i = inner[1]
else
let stmts = list_push(stmts, [kw, toks, [], i + 1])
let i = i + 1
end
else
let i = i + 1
end
end
if depth > 0 then
return [false, "line " + opened + ": this " + lines[opened - 1] + " has no end"]
end
return [stmts, i]
end
make a function called scene_split_lines takes text returns lines
return scene_split_bytes(ex_bytes(text))
end
# the lines of a text held as a list of bytes (a file: indexing a String is slow for long ones)
make a function called scene_split_bytes takes bytes returns lines
let lines = []
let sb = sb_new()
let i = 0
while i < bytes.length do
if bytes[i] == 10 then
let lines = list_push(lines, sb_str(sb))
let sb = sb_new()
else
sb_push(sb, chr(bytes[i]))
end
let i = i + 1
end
let lines = list_push(lines, sb_str(sb))
return lines
end
make a function called scene_ctx takes dir, pics returns ctx
let ctx = vec_new()
vec_push(ctx, [])
vec_push(ctx, [])
vec_push(ctx, [])
vec_push(ctx, [])
vec_push(ctx, [point(0, 2, -8), point(0, 1, 0), vector(0, 1, 0), 60, 400, 200])
vec_push(ctx, false)
vec_push(ctx, [false, false, false, false, false])
vec_push(ctx, false)
# [8] how many uses are open at once, [9] how many shapes have been made
vec_push(ctx, 0)
vec_push(ctx, 0)
# [10] the directory of the scene file: the pictures it names are read relative to it
vec_push(ctx, dir)
# [11] how many files are open at once (includes)
vec_push(ctx, 0)
# [12] the pictures read so far (a Pictures)
vec_push(ctx, pics)
return ctx
end
make a function called scene_fail takes ctx, stmt, msg returns no
if vec_get(ctx, 7) == false then
vec_set(ctx, 7, "line " + stmt[3] + ": " + msg)
end
return false
end
make a function called scene_failed takes ctx returns yes
return vec_get(ctx, 7) != false
end
# the value of a name in a list of [name, value]
make a function called scene_lookup takes pairs, name returns entry
let k = pairs.length - 1
while k >= 0 do
if pairs[k][0] == name then
return pairs[k]
end
let k = k - 1
end
return false
end
# the number a token stands for: a word, or a ( group ); false (with the error set) if it is not one
make a function called scene_num takes tok, vars, ctx, stmt returns v
let text = tok
if tok.length >= 2 and char_code(tok, 0) == 40 and char_code(tok, tok.length - 1) == 41 then
let sb = sb_new()
let k = 1
while k < tok.length - 1 do
sb_push(sb, chr(char_code(tok, k)))
let k = k + 1
end
let text = sb_str(sb)
end
let env = []
let k = 0
while k < vars.length do
let env = list_push(env, [vars[k][0], fa_num(vars[k][1])])
let k = k + 1
end
let r = expr_parse_mode(text, env, "fn")
if not r[0] then
return scene_fail(ctx, stmt, r[1] + " (in '" + text + "')")
end
if r[1][0] != "num" then
return scene_fail(ctx, stmt, "'" + text + "' is not a number (x, y and z are the variables of a surface)")
end
return r[1][1]
end
# n numbers from the tokens at `at`
make a function called scene_nums takes toks, at, count, vars, ctx, stmt returns nums
if at + count > toks.length then
return scene_fail(ctx, stmt, "expected " + count + " numbers after " + toks[at - 1])
end
let out = []
let k = 0
while k < count do
let v = scene_num(toks[at + k], vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let out = list_push(out, v)
let k = k + 1
end
return out
end
make a function called scene_is_name takes s returns yes
if s.length == 0 then
return false
end
let c = char_code(s, 0)
if not ex_is_letter(c) then
return false
end
let k = 1
while k < s.length do
let d = char_code(s, k)
if not (ex_is_letter(d) or ex_is_digit(d)) then
return false
end
let k = k + 1
end
return true
end
make a function called scene_is_modifier takes tok returns yes
return tok == "at" or tok == "scale" or tok == "rotate" or tok == "material" or tok == "bounds" or tok == "closed"
end
# the tokens from `from` on, as [leading arguments, modifiers]: each modifier [word, tokens that follow it]
make a function called scene_split_mods takes toks, from returns parts
let lead = []
let mods = []
let k = from
while k < toks.length and not scene_is_modifier(toks[k]) do
let lead = list_push(lead, toks[k])
let k = k + 1
end
while k < toks.length do
let word = toks[k]
let args = []
let k = k + 1
while k < toks.length and not scene_is_modifier(toks[k]) do
let args = list_push(args, toks[k])
let k = k + 1
end
let mods = list_push(mods, [word, args])
end
return [lead, mods]
end
# the modifiers applied to a shape built by `base`: the matrix of the ones written, in order, and the
# material, if named. [matrix, material or false, bounds or false, closed]
make a function called scene_mod_values takes mods, vars, ctx, stmt returns vals
let m = identity_matrix()
let material = false
let bounds = false
let closed = false
let k = 0
while k < mods.length do
let word = mods[k][0]
let a = mods[k][1]
if word == "at" then
if a.length != 3 then
return scene_fail(ctx, stmt, "at takes 3 numbers")
end
let v = scene_nums(a, 0, 3, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let m = mat_mul(translation(v[0], v[1], v[2]), m)
elif word == "scale" then
if a.length != 1 and a.length != 3 then
return scene_fail(ctx, stmt, "scale takes 1 or 3 numbers")
end
let v = scene_nums(a, 0, a.length, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
if v.length == 1 then
let m = mat_mul(scaling(v[0], v[0], v[0]), m)
else
let m = mat_mul(scaling(v[0], v[1], v[2]), m)
end
elif word == "rotate" then
if a.length != 2 or not (a[0] == "x" or a[0] == "y" or a[0] == "z") then
return scene_fail(ctx, stmt, "rotate takes x, y or z and an angle in degrees")
end
let v = scene_nums(a, 1, 1, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let rad = v[0] * pi() / 180
if a[0] == "x" then
let m = mat_mul(rotation_x(rad), m)
elif a[0] == "y" then
let m = mat_mul(rotation_y(rad), m)
else
let m = mat_mul(rotation_z(rad), m)
end
elif word == "material" then
if a.length != 1 then
return scene_fail(ctx, stmt, "material takes a name")
end
let entry = scene_lookup(vec_get(ctx, 0), a[0])
if entry == false then
return scene_fail(ctx, stmt, "unknown material '" + a[0] + "'")
end
let material = entry[1]
elif word == "bounds" then
if a.length != 6 then
return scene_fail(ctx, stmt, "bounds takes 6 numbers: x0 y0 z0 x1 y1 z1")
end
let v = scene_nums(a, 0, 6, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let bounds = [[v[0], v[1], v[2]], [v[3], v[4], v[5]]]
else
let closed = true
end
let k = k + 1
end
return [m, material, bounds, closed]
end
# a shape with its modifiers' transformation and material
make a function called scene_finish takes s, vals returns shape
let shape = set_transform(s, vals[0])
if vals[1] != false then
if shape[5] == "group" or shape[5] == "csg" then
let shape = with_material_deep(shape, vals[1])
else
let shape = set_material(shape, vals[1])
end
end
return shape
end
# a text with the funcs' calls replaced by their bodies
make a function called scene_expand takes text, funcs, depth returns out
if depth > 20 then
return false
end
let bytes = ex_bytes(text)
let n = bytes.length
let sb = sb_new()
let i = 0
while i < n do
let c = bytes[i]
let boundary = true
if i > 0 and (ex_is_letter(bytes[i - 1]) or ex_is_digit(bytes[i - 1])) then
let boundary = false
end
if boundary and ex_is_letter(c) then
let name = sb_new()
let j = i
while j < n and (ex_is_letter(bytes[j]) or ex_is_digit(bytes[j])) do
sb_push(name, chr(bytes[j]))
let j = j + 1
end
let word = sb_str(name)
let f = scene_lookup(funcs, word)
let p = j
while p < n and (bytes[p] == 32 or bytes[p] == 9) do
let p = p + 1
end
if f != false and p < n and bytes[p] == 40 then
# the arguments, split at the commas outside brackets
let args = []
let cur = sb_new()
let d = 0
let q = p
let closed = false
while q < n and not closed do
let ch = bytes[q]
if ch == 40 then
if d > 0 then
sb_push(cur, chr(ch))
end
let d = d + 1
elif ch == 41 then
let d = d - 1
if d == 0 then
let args = list_push(args, sb_str(cur))
let closed = true
else
sb_push(cur, chr(ch))
end
elif ch == 44 and d == 1 then
let args = list_push(args, sb_str(cur))
let cur = sb_new()
else
sb_push(cur, chr(ch))
end
let q = q + 1
end
if not closed or args.length != f[1].length then
return false
end
# each argument expanded, then put in place of its parameter in the body
let body = f[2]
let k = 0
let subst = []
while k < args.length do
let a = scene_expand(args[k], funcs, depth + 1)
if a == false then
return false
end
let subst = list_push(subst, [f[1][k], "(" + a + ")"])
let k = k + 1
end
let replaced = scene_replace_words(body, subst)
let inner = scene_expand(replaced, funcs, depth + 1)
if inner == false then
return false
end
sb_push(sb, "(" + inner + ")")
let i = q
else
sb_push(sb, word)
let i = j
end
else
sb_push(sb, chr(c))
let i = i + 1
end
end
return sb_str(sb)
end
# the text with each whole word that is a name in `pairs` replaced by its text
make a function called scene_replace_words takes text, pairs returns out
let bytes = ex_bytes(text)
let n = bytes.length
let sb = sb_new()
let i = 0
while i < n do
let c = bytes[i]
let boundary = true
if i > 0 and (ex_is_letter(bytes[i - 1]) or ex_is_digit(bytes[i - 1])) then
let boundary = false
end
if boundary and ex_is_letter(c) then
let name = sb_new()
let j = i
while j < n and (ex_is_letter(bytes[j]) or ex_is_digit(bytes[j])) do
sb_push(name, chr(bytes[j]))
let j = j + 1
end
let word = sb_str(name)
let e = scene_lookup(pairs, word)
if e != false then
sb_push(sb, e[1])
else
sb_push(sb, word)
end
let i = j
else
sb_push(sb, chr(c))
let i = i + 1
end
end
return sb_str(sb)
end
# the text inside the quotes of a string token, or false
make a function called scene_unquote takes tok returns text
if tok.length < 2 or char_code(tok, 0) != 34 or char_code(tok, tok.length - 1) != 34 then
return false
end
let sb = sb_new()
let k = 1
while k < tok.length - 1 do
sb_push(sb, chr(char_code(tok, k)))
let k = k + 1
end
return sb_str(sb)
end
# a `surface "text"` statement's shape
make a function called scene_surface takes lead, vals, vars, ctx, stmt returns shape
if lead.length != 1 then
return scene_fail(ctx, stmt, "surface takes one string: surface \"x^2 + y^2 + z^2 - 1\"")
end
let text = scene_unquote(lead[0])
if text == false then
return scene_fail(ctx, stmt, "the text of a surface goes in quotes")
end
let funcs = vec_get(ctx, 3)
let expanded = scene_expand(text, funcs, 0)
if expanded == false then
return scene_fail(ctx, stmt, "a func is called wrongly in the text of this surface (the wrong number of arguments, or a call that does not end)")
end
# the exprs first, as statements, then the text
let script = ""
let pieces = vec_get(ctx, 2)
let k = 0
while k < pieces.length do
let e = scene_expand(pieces[k][1], funcs, 0)
if e == false then
return scene_fail(ctx, stmt, "a func is called wrongly in expr " + pieces[k][0])
end
let script = script + pieces[k][0] + " = " + e + ";"
let k = k + 1
end
let script = script + expanded
let bounds = vals[2]
if bounds == false then
let bounds = expr_default_bounds()
end
let made = surface_try(script, bounds, vars)
if not made[0] then
return scene_fail(ctx, stmt, made[1])
end
return scene_finish(made[1], vals)
end
# the shape of a statement, and everything its block holds. Returns the shape, or false with the error set.
make a function called scene_shape takes kw, toks, kids, vars, ctx, stmt returns shape
let parts = scene_split_mods(toks, 1)
let lead = parts[0]
let mods = parts[1]
if kw == "use" then
let call = toks[1]
let parts = scene_split_mods(toks, 2)
let lead = parts[0]
let mods = parts[1]
end
let vals = scene_mod_values(mods, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
if kw == "sphere" or kw == "cube" or kw == "plane" then
if lead.length > 0 then
return scene_fail(ctx, stmt, kw + " takes no numbers, only modifiers (at, scale, rotate, material)")
end
let s = sphere()
if kw == "cube" then
let s = cube()
end
if kw == "plane" then
let s = plane()
end
return scene_finish(s, vals)
end
if kw == "cylinder" or kw == "cone" then
let s = cylinder()
if kw == "cone" then
let s = cone()
end
if lead.length == 2 then
let v = scene_nums(lead, 0, 2, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let s = with_limits(s, v[0], v[1])
elif lead.length != 0 then
return scene_fail(ctx, stmt, kw + " takes no numbers, or two (the lowest and highest y)")
end
if vals[3] then
let s = with_closed(s, true)
end
return scene_finish(s, vals)
end
if kw == "torus" then
if lead.length != 2 then
return scene_fail(ctx, stmt, "torus takes two numbers: the major and the minor radius")
end
let v = scene_nums(lead, 0, 2, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
return scene_finish(torus(v[0], v[1]), vals)
end
if kw == "surface" then
return scene_surface(lead, vals, vars, ctx, stmt)
end
if kw == "use" then
return scene_use(toks[1], vals, vars, ctx, stmt)
end
# a block of shapes: what is inside, made into one shape
let out = vec_new()
scene_exec(kids, vars, ctx, out, stmt)
if scene_failed(ctx) then
return false
end
let inside = vec_to_list(out)
if kw == "group" then
return scene_finish(group_from(inside), vals)
end
if kw == "union" or kw == "intersection" or kw == "difference" then
if inside.length < 2 then
return scene_fail(ctx, stmt, kw + " needs at least two shapes")
end
let s = inside[0]
let k = 1
while k < inside.length do
let s = csg(kw, s, inside[k])
let k = k + 1
end
return scene_finish(s, vals)
end
# a deformation of the group inside
let nums = scene_nums(lead, 0, scene_warp_count(kw), vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
if lead.length != scene_warp_count(kw) then
return scene_fail(ctx, stmt, kw + " takes " + scene_warp_count(kw) + " number(s)")
end
let body = group_from(inside)
if inside.length == 1 then
let body = inside[0]
end
let w = false
if vals[2] == false then
let w = warped_shape_auto(body, kw, nums)
if w == false then
return scene_fail(ctx, stmt, "this cannot be deformed with no bounds written: it has no function (a triangle or a mesh), or no finite box (a plane); give bounds x0 y0 z0 x1 y1 z1 for the second")
end
else
let w = warped_shape(body, kw, nums, vals[2])
if w == false then
return scene_fail(ctx, stmt, "this cannot be deformed: it has no function (a triangle or a mesh)")
end
end
return scene_finish(w, vals)
end
make a function called scene_warp_count takes kw returns n
if kw == "wave" then
return 2
end
return 1
end
# `use name(args)` or `use name`: the definition run with the arguments as values
make a function called scene_use takes call, vals, vars, ctx, stmt returns shape
let open = -1
let k = 0
while k < call.length do
if char_code(call, k) == 40 and open < 0 then
let open = k
end
let k = k + 1
end
let name = call
let argtext = []
if open >= 0 then
let sb = sb_new()
let k = 0
while k < open do
sb_push(sb, chr(char_code(call, k)))
let k = k + 1
end
let name = sb_str(sb)
# the arguments, split at the commas outside brackets
let cur = sb_new()
let d = 0
let k = open + 1
while k < call.length - 1 do
let ch = char_code(call, k)
if ch == 40 then
let d = d + 1
sb_push(cur, chr(ch))
elif ch == 41 then
let d = d - 1
sb_push(cur, chr(ch))
elif ch == 44 and d == 0 then
let argtext = list_push(argtext, sb_str(cur))
let cur = sb_new()
else
sb_push(cur, chr(ch))
end
let k = k + 1
end
let last = sb_str(cur)
if last.length > 0 or argtext.length > 0 then
let argtext = list_push(argtext, last)
end
end
let def = scene_lookup(vec_get(ctx, 1), name)
if def == false then
return scene_fail(ctx, stmt, "unknown object '" + name + "'")
end
if argtext.length != def[1].length then
return scene_fail(ctx, stmt, name + " takes " + def[1].length + " argument(s), not " + argtext.length)
end
let inner = vars
let k = 0
while k < argtext.length do
let v = scene_num(argtext[k], vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let inner = list_push(inner, [def[1][k], v])
let k = k + 1
end
# a definition may use itself, to a depth the values decide (an if stops it); one that never stops is caught here
let open_uses = vec_get(ctx, 8) + 1
if open_uses > 64 then
return scene_fail(ctx, stmt, "'" + name + "' is used inside itself more than 64 deep: does it ever stop? (use an if)")
end
vec_set(ctx, 8, open_uses)
let out = vec_new()
scene_exec(def[2], inner, ctx, out, stmt)
vec_set(ctx, 8, open_uses - 1)
if scene_failed(ctx) then
return false
end
return scene_finish(group_from(vec_to_list(out)), vals)
end
make a function called scene_is_shape takes kw returns yes
return kw == "sphere" or kw == "cube" or kw == "plane" or kw == "cylinder" or kw == "cone" or kw == "torus" or kw == "surface" or kw == "use" or kw == "group" or kw == "union" or kw == "intersection" or kw == "difference" or kw == "twist" or kw == "taper" or kw == "bend" or kw == "wave" or kw == "shear"
end
# the colour at toks[at ...]: three numbers
make a function called scene_colour takes toks, at, vars, ctx, stmt returns c
let v = scene_nums(toks, at, 3, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
return color(v[0], v[1], v[2])
end
# a material from its block's property lines
make a function called scene_material takes kids, vars, ctx returns m
let m = material()
let k = 0
while k < kids.length do
let toks = kids[k][1]
let stmt = kids[k]
let i = 0
while i < toks.length do
let word = toks[i]
if word == "color" then
let c = scene_colour(toks, i + 1, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let m = with_color(m, c)
let i = i + 4
elif word == "ambient" or word == "diffuse" or word == "specular" or word == "shininess" or word == "reflective" or word == "transparency" or word == "ior" then
let v = scene_nums(toks, i + 1, 1, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
if word == "ambient" then
let m = with_attr(m, 1, v[0])
elif word == "diffuse" then
let m = with_attr(m, 2, v[0])
elif word == "specular" then
let m = with_attr(m, 3, v[0])
elif word == "shininess" then
let m = with_attr(m, 4, v[0])
elif word == "reflective" then
let m = with_reflective(m, v[0])
elif word == "transparency" then
let m = with_transparency(m, v[0])
else
let m = with_refractive_index(m, v[0])
end
let i = i + 2
elif word == "texture" or word == "bump" then
let r = scene_picture_line(toks, i, m, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let m = r
let i = toks.length
elif word == "pattern" then
if i + 7 > toks.length then
scene_fail(ctx, stmt, "pattern takes a kind (stripes, checkers, rings, gradient) and two colours")
return false
end
let kind = toks[i + 1]
let c1 = scene_colour(toks, i + 2, vars, ctx, stmt)
let c2 = scene_colour(toks, i + 5, vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let pat = false
if kind == "stripes" then
let pat = stripe_pattern(c1, c2)
elif kind == "checkers" then
let pat = checkers_pattern(c1, c2)
elif kind == "rings" then
let pat = ring_pattern(c1, c2)
elif kind == "gradient" then
let pat = gradient_pattern(c1, c2)
else
scene_fail(ctx, stmt, "unknown pattern '" + kind + "' (stripes, checkers, rings, gradient)")
return false
end
# the modifiers after the colours move the pattern
let rest = []
let j = i + 8
while j <= toks.length and j - 1 < toks.length do
let rest = list_push(rest, toks[j - 1])
let j = j + 1
end
let parts = scene_split_mods(rest, 0)
let pv = scene_mod_values(parts[1], vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let m = with_pattern(m, set_pattern_transform(pat, pv[0]))
let i = toks.length
else
scene_fail(ctx, stmt, "unknown property '" + word + "' of a material")
return false
end
end
let k = k + 1
end
return m
end
# run statements, adding the shapes they make to the vec `out`; `owner` is the statement that
# started this block (for errors with no line of their own). Returns the values in scope at the end.
make a function called scene_exec takes stmts, vars0, ctx, out, owner returns vars
let vars = vars0
let k = 0
while k < stmts.length and not scene_failed(ctx) do
let s = stmts[k]
let kw = s[0]
let toks = s[1]
if kw == "let" then
if toks.length < 4 or toks[2] != "=" then
scene_fail(ctx, s, "let takes a name, =, and a value: let a = 2 * 3")
elif not scene_is_name(toks[1]) then
scene_fail(ctx, s, "'" + toks[1] + "' is not a name")
elif toks[1] == "x" or toks[1] == "y" or toks[1] == "z" or toks[1] == "pi" then
scene_fail(ctx, s, "x, y and z (and pi) cannot be the names of values: they are for surfaces")
else
# the value is the rest of the line, as one expression
let tb = sb_new()
let j = 3
while j < toks.length do
sb_push(tb, toks[j])
let j = j + 1
end
let v = scene_num(sb_str(tb), vars, ctx, s)
if not scene_failed(ctx) then
let vars = list_push(vars, [toks[1], v])
end
end
elif kw == "ik" then
let vars = scene_ik_statement(s, vars, ctx)
elif kw == "include" then
let vars = scene_include(s, vars, ctx, out)
elif kw == "param" then
# param name = value: the value if none was given from outside (scene_build_with, or --set name=value)
if toks.length < 4 or toks[2] != "=" then
scene_fail(ctx, s, "param takes a name, =, and a default value: param depth = 5")
elif scene_lookup(vars, toks[1]) != false then
# given from outside: leave it
else
let pb = sb_new()
let j = 3
while j < toks.length do
sb_push(pb, toks[j])
let j = j + 1
end
let pv = scene_num(sb_str(pb), vars, ctx, s)
if not scene_failed(ctx) then
let vars = list_push(vars, [toks[1], pv])
end
end
elif kw == "for" then
# for i in a to b [step s]
if toks.length != 6 and toks.length != 8 then
scene_fail(ctx, s, "for takes: for i in a to b [step s]")
elif toks[2] != "in" or toks[4] != "to" or (toks.length == 8 and toks[6] != "step") then
scene_fail(ctx, s, "for takes: for i in a to b [step s]")
else
let a = scene_num(toks[3], vars, ctx, s)
let b = scene_num(toks[5], vars, ctx, s)
let st = 1
if toks.length == 8 then
let st = scene_num(toks[7], vars, ctx, s)
end
if not scene_failed(ctx) then
if st == 0 then
scene_fail(ctx, s, "the step of a loop cannot be 0")
else
# the n-th value is a + n step: no error builds up
let n = 0
let going = true
while going and not scene_failed(ctx) do
let v = a + n * st
if (st > 0 and v > b) or (st < 0 and v < b) then
let going = false
else
let inner = list_push(vars, [toks[1], v])
scene_exec(s[2], inner, ctx, out, s)
let n = n + 1
end
end
end
end
end
elif kw == "if" then
scene_if(s, vars, ctx, out)
elif kw == "material" then
if toks.length != 2 then
scene_fail(ctx, s, "material takes a name")
else
let m = scene_material(s[2], vars, ctx)
if not scene_failed(ctx) then
let all = vec_get(ctx, 0)
vec_set(ctx, 0, list_push(all, [toks[1], m]))
end
end
elif kw == "define" then
# define name or define name(a, b)
if toks.length < 2 then
scene_fail(ctx, s, "define takes a name, and parameters in brackets")
else
let head = toks[1]
let names = []
let name = head
let open = -1
let j = 0
while j < head.length do
if char_code(head, j) == 40 and open < 0 then
let open = j
end
let j = j + 1
end
if open >= 0 then
let sb = sb_new()
let j = 0
while j < open do
sb_push(sb, chr(char_code(head, j)))
let j = j + 1
end
let name = sb_str(sb)
let cur = sb_new()
let j = open + 1
while j < head.length do
let ch = char_code(head, j)
if ch == 44 or ch == 41 then
let word = sb_str(cur)
if word.length > 0 then
let names = list_push(names, word)
end
let cur = sb_new()
elif ch != 32 then
sb_push(cur, chr(ch))
end
let j = j + 1
end
end
let defs = vec_get(ctx, 1)
vec_set(ctx, 1, list_push(defs, [name, names, s[2]]))
end
elif kw == "expr" then
# expr name = "text"
if toks.length != 4 or toks[2] != "=" or scene_unquote(toks[3]) == false or not scene_is_name(toks[1]) then
scene_fail(ctx, s, "expr takes: expr name = \"text\"")
else
vec_set(ctx, 2, list_push(vec_get(ctx, 2), [toks[1], scene_unquote(toks[3])]))
end
elif kw == "func" then
# func name(a, b) = "text"
let sb = sb_new()
let j = 1
while j < toks.length - 2 do
sb_push(sb, toks[j])
let j = j + 1
end
let head = sb_str(sb)
if toks.length < 4 or toks[toks.length - 2] != "=" or scene_unquote(toks[toks.length - 1]) == false then
scene_fail(ctx, s, "func takes: func name(a, b) = \"text\"")
else
let open = -1
let j = 0
while j < head.length do
if char_code(head, j) == 40 and open < 0 then
let open = j
end
let j = j + 1
end
if open < 1 or char_code(head, head.length - 1) != 41 then
scene_fail(ctx, s, "func takes: func name(a, b) = \"text\"")
else
let nb = sb_new()
let j = 0
while j < open do
sb_push(nb, chr(char_code(head, j)))
let j = j + 1
end
let names = []
let cur = sb_new()
let j = open + 1
while j < head.length do
let ch = char_code(head, j)
if ch == 44 or ch == 41 then
let word = sb_str(cur)
if word.length > 0 then
let names = list_push(names, word)
end
let cur = sb_new()
elif ch != 32 then
sb_push(cur, chr(ch))
end
let j = j + 1
end
vec_set(ctx, 3, list_push(vec_get(ctx, 3), [sb_str(nb), names, scene_unquote(toks[toks.length - 1])]))
end
end
elif kw == "camera" then
scene_camera_statement(s, vars, ctx)
elif kw == "size" then
let v = scene_nums(toks, 1, 2, vars, ctx, s)
if not scene_failed(ctx) then
let c = vec_get(ctx, 4)
vec_set(ctx, 4, [c[0], c[1], c[2], c[3], v[0], v[1]])
end
elif kw == "antialias" then
if toks.length == 4 and toks[1] == "adaptive" then
let v = scene_nums(toks, 2, 2, vars, ctx, s)
if not scene_failed(ctx) then
scene_setting(ctx, 1, [v[0], v[1]])
end
elif toks.length == 2 then
let v = scene_nums(toks, 1, 1, vars, ctx, s)
if not scene_failed(ctx) then
scene_setting(ctx, 0, v[0])
end
else
scene_fail(ctx, s, "antialias takes a number, or: antialias adaptive depth threshold")
end
elif kw == "light" then
scene_light_statement(s, vars, ctx)
elif kw == "fog" then
scene_fog_statement(s, vars, ctx)
elif kw == "stereo" then
scene_stereo_statement(s, vars, ctx)
elif kw == "mirage" then
scene_mirage_statement(s, vars, ctx)
elif scene_is_shape(kw) then
let shape = scene_shape(kw, toks, s[2], vars, ctx, s)
if not scene_failed(ctx) then
vec_push(out, shape)
# a runaway recursion fails here instead of filling the memory
vec_set(ctx, 9, vec_get(ctx, 9) + 1)
if vec_get(ctx, 9) > 100000 then
scene_fail(ctx, s, "more than 100000 shapes: a definition that makes too many?")
end
end
else
scene_fail(ctx, s, "unknown statement '" + kw + "'")
end
let k = k + 1
end
return vars
end
# camera from x y z to x y z [up x y z] [fov deg] [size w h]
make a function called scene_camera_statement takes s, vars, ctx returns done
let toks = s[1]
let c = vec_get(ctx, 4)
let from = c[0]
let to = c[1]
let up = c[2]
let fov = c[3]
let w = c[4]
let h = c[5]
let i = 1
while i < toks.length and not scene_failed(ctx) do
let word = toks[i]
if word == "from" or word == "to" or word == "up" then
let v = scene_nums(toks, i + 1, 3, vars, ctx, s)
if not scene_failed(ctx) then
if word == "from" then
let from = point(v[0], v[1], v[2])
elif word == "to" then
let to = point(v[0], v[1], v[2])
else
let up = vector(v[0], v[1], v[2])
end
let i = i + 4
end
elif word == "fov" then
let v = scene_nums(toks, i + 1, 1, vars, ctx, s)
if not scene_failed(ctx) then
let fov = v[0]
let i = i + 2
end
elif word == "size" then
let v = scene_nums(toks, i + 1, 2, vars, ctx, s)
if not scene_failed(ctx) then
let w = v[0]
let h = v[1]
let i = i + 3
end
else
scene_fail(ctx, s, "unknown word '" + word + "' in a camera (from, to, up, fov, size)")
end
end
if not scene_failed(ctx) then
vec_set(ctx, 4, [from, to, up, fov, w, h])
end
return true
end
# light point at x y z | area at x y z width w depth d [steps n] | spot at x y z toward x y z angle a [soft b], then [color r g b]
make a function called scene_light_statement takes s, vars, ctx returns done
let toks = s[1]
if vec_get(ctx, 5) != false then
scene_fail(ctx, s, "a scene has one light (a second light is not possible: a world has one)")
return false
end
if toks.length < 2 then
scene_fail(ctx, s, "light takes point, area or spot")
return false
end
let kind = toks[1]
let at = point(0, 0, 0)
let toward = point(0, 0, 0)
let width = 2
let depth = 2
let steps = 4
let angle = 30
let soft = false
let col = color(1, 1, 1)
let i = 2
while i < toks.length and not scene_failed(ctx) do
let word = toks[i]
if word == "at" or word == "toward" then
let v = scene_nums(toks, i + 1, 3, vars, ctx, s)
if not scene_failed(ctx) then
if word == "at" then
let at = point(v[0], v[1], v[2])
else
let toward = point(v[0], v[1], v[2])
end
let i = i + 4
end
elif word == "color" then
let c = scene_colour(toks, i + 1, vars, ctx, s)
if not scene_failed(ctx) then
let col = c
let i = i + 4
end
elif word == "width" or word == "depth" or word == "steps" or word == "angle" or word == "soft" then
let v = scene_nums(toks, i + 1, 1, vars, ctx, s)
if not scene_failed(ctx) then
if word == "width" then
let width = v[0]
elif word == "depth" then
let depth = v[0]
elif word == "steps" then
let steps = v[0]
elif word == "angle" then
let angle = v[0]
else
let soft = v[0]
end
let i = i + 2
end
else
scene_fail(ctx, s, "unknown word '" + word + "' in a light")
end
end
if scene_failed(ctx) then
return false
end
let light = false
if kind == "point" then
let light = point_light(at, col)
elif kind == "area" then
let corner = point(get_x(at) - width / 2, get_y(at), get_z(at) - depth / 2)
let light = area_light(corner, vector(width, 0, 0), steps, vector(0, 0, depth), steps, col, "r2")
elif kind == "spot" then
if soft == false then
let soft = angle / 4
end
let light = spot_light(at, tuple_sub(toward, at), angle * pi() / 180, (angle - soft) * pi() / 180, col)
else
scene_fail(ctx, s, "unknown light '" + kind + "' (point, area, spot)")
return false
end
vec_set(ctx, 5, light)
return true
end
# [true, [world, camera, settings]] or [false, "line n: message"]
make a function called scene_build takes text returns result
return scene_build_lines(scene_split_lines(text), [], "", picture_cache())
end
# the same with some values given from outside, [[name, number], ...]: a param of the same name keeps them
make a function called scene_build_with takes text, defs returns result
return scene_build_lines(scene_split_lines(text), defs, "", picture_cache())
end
make a function called scene_build_lines takes lines, defs, dir, pics returns result
let parsed = scene_parse_block(lines, 0, 0, 0)
if parsed[0] == false then
return [false, parsed[1]]
end
let ctx = scene_ctx(dir, pics)
let out = vec_new()
scene_exec(parsed[0], defs, ctx, out, false)
if scene_failed(ctx) then
return [false, vec_get(ctx, 7)]
end
let light = vec_get(ctx, 5)
if light == false then
let light = point_light(point(-10, 10, -10), color(1, 1, 1))
end
let world = set_world_objects(set_world_light(world(), light), vec_to_list(out))
let c = vec_get(ctx, 4)
let cam = set_camera_transform(camera(c[4], c[5], c[3] * pi() / 180), view_transform(c[0], c[1], c[2]))
# a stereo pair, if the scene asked for one: [left camera, right camera, gap, cross], and each eye's [from, to, shift]
let spec = vec_get(ctx, 6)[3]
let pair = false
let eyes = false
if spec != false then
let pair = stereo_cameras(c[0], c[1], c[2], c[3] * pi() / 180, c[4], c[5], spec[0])
let pair = [pair[0], pair[1], spec[1], spec[2]]
let eyes = stereo_eyes(c[0], c[1], c[2], spec[0])
end
return [true, [world, cam, vec_get(ctx, 6), [c[0], c[1], c[2], c[3]], pair, eyes]]
end
make a function called scene_world takes s returns w
return s[0]
end
make a function called scene_camera takes s returns c
return s[1]
end
# [antialias n or false, adaptive [depth, threshold] or false, atmosphere [density, falloff, fog colour] or false]
make a function called scene_settings takes s returns st
return s[2]
end
# where the camera is: [from, to, up, field of view in degrees]
make a function called scene_view takes s returns v
return s[3]
end
make a function called scene_from_file takes path returns result
return scene_from_file_with(path, [])
end
make a function called scene_from_file_with takes path, defs returns result
return scene_from_file_with_pictures(path, defs, picture_cache())
end
# if a < b ... [else ...] end: one of the two lists of statements is run, with the values in scope
make a function called scene_if takes s, vars, ctx, out returns done
let toks = s[1]
if toks.length != 4 then
scene_fail(ctx, s, "if takes: if a < b (or > <= >= == !=), the two sides each one word or a ( group )")
return false
end
let a = scene_num(toks[1], vars, ctx, s)
let b = scene_num(toks[3], vars, ctx, s)
if scene_failed(ctx) then
return false
end
let op = toks[2]
let yes = false
if op == "<" then
let yes = a < b
elif op == ">" then
let yes = a > b
elif op == "<=" then
let yes = a <= b
elif op == ">=" then
let yes = a >= b
elif op == "==" then
let yes = a == b
elif op == "!=" then
let yes = a != b
else
scene_fail(ctx, s, "'" + op + "' is not a comparison (< > <= >= == !=)")
return false
end
let then_part = []
let else_part = []
let seen_else = false
let k = 0
while k < s[2].length do
if s[2][k][0] == "else" then
let seen_else = true
elif seen_else then
let else_part = list_push(else_part, s[2][k])
else
let then_part = list_push(then_part, s[2][k])
end
let k = k + 1
end
if yes then
scene_exec(then_part, vars, ctx, out, s)
else
scene_exec(else_part, vars, ctx, out, s)
end
return true
end
# a picture named in a material: the path as a word or in quotes, relative to the scene file's directory
make a function called scene_read_picture takes path, ctx, stmt returns img
let name = scene_unquote(path)
if name == false then
let name = path
end
let full = obj_join(vec_get(ctx, 10), name)
let pics = vec_get(ctx, 12)
let known = get(pics, full)
if known != unit then
return known
end
let img = read_ppm(full)
if img == false then
return scene_fail(ctx, stmt, "cannot read the picture '" + name + "' (a PPM file, P3 or P6, relative to the scene file)")
end
send(pics, "set", full, img)
return img
end
# texture <picture> <mapping> [modifiers] or bump normal|height <picture> <mapping> [strength s] [modifiers]
# in a material: the picture is the colour (texture), or tilts the normal (bump); the mapping is
# spherical, planar or cylindrical; the modifiers (at, scale, rotate) move the picture on the surface
make a function called scene_picture_line takes toks, i, m, vars, ctx, stmt returns m2
let word = toks[i]
let at = i + 1
let kind = false
if word == "bump" then
if at >= toks.length or not (toks[at] == "normal" or toks[at] == "height") then
let got = "nothing"
if at < toks.length then
let got = toks[at]
end
return scene_fail(ctx, stmt, "bump takes normal or height, a picture and a mapping, not '" + got + "'")
end
let kind = toks[at]
let at = at + 1
end
if at + 1 >= toks.length then
return scene_fail(ctx, stmt, word + " takes a picture and a mapping (spherical, planar, cylindrical)")
end
let mapping = toks[at + 1]
if not (mapping == "spherical" or mapping == "planar" or mapping == "cylindrical") then
return scene_fail(ctx, stmt, "'" + mapping + "' is not a mapping (spherical, planar, cylindrical)")
end
let img = scene_read_picture(toks[at], ctx, stmt)
if scene_failed(ctx) then
return false
end
let rest = []
let j = at + 2
while j < toks.length do
let rest = list_push(rest, toks[j])
let j = j + 1
end
let strength = 1
if word == "bump" and rest.length >= 2 and rest[0] == "strength" then
let s = scene_num(rest[1], vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
let strength = s
let rest = list_slice_from(rest, 2)
end
let parts = scene_split_mods(rest, 0)
let pv = scene_mod_values(parts[1], vars, ctx, stmt)
if scene_failed(ctx) then
return false
end
if word == "texture" then
return with_pattern(m, set_pattern_transform(texture_map(uv_image_smooth(img), mapping), pv[0]))
end
let b = normal_map(img, mapping)
if kind == "height" then
let b = height_map(img, mapping)
end
return with_bump(m, set_bump_transform(set_bump_strength(b, strength), pv[0]))
end
# include "file": the statements of another scene file, run here, with the values in scope and the same
# materials, definitions and shapes; the file is named relative to the one that names it. A file that
# includes itself, directly or through others, is caught at 16 deep.
make a function called scene_include takes s, vars, ctx, out returns vars2
let toks = s[1]
if toks.length != 2 then
scene_fail(ctx, s, "include takes one file name: include \"parts.scene\"")
return vars
end
let name = scene_unquote(toks[1])
if name == false then
let name = toks[1]
end
let depth = vec_get(ctx, 11)
if depth >= 16 then
scene_fail(ctx, s, "include is 16 deep: does '" + name + "' include itself?")
return vars
end
let here = vec_get(ctx, 10)
let path = obj_join(here, name)
let bytes = read_file_bytes(path, 0, 268435456)
if bytes.length == 0 then
scene_fail(ctx, s, "cannot read the scene file '" + name + "'")
return vars
end
let parsed = scene_parse_block(scene_split_bytes(bytes), 0, 0, 0)
if parsed[0] == false then
scene_fail(ctx, s, name + ": " + parsed[1])
return vars
end
vec_set(ctx, 10, obj_dir(path))
vec_set(ctx, 11, depth + 1)
let result = scene_exec(parsed[0], vars, ctx, out, s)
if scene_failed(ctx) then
# an error inside the file says which file, outermost first
vec_set(ctx, 7, name + ": " + vec_get(ctx, 7))
end
vec_set(ctx, 10, here)
vec_set(ctx, 11, depth)
return result
end
Scenarios and oracles
- Reading. A word and a group are tokens, a comment goes, a string keeps its spaces and its
#, a call keeps its arguments together, brackets nest. - Numbers.
letvalues and expressions, redefinition from the old value, exact decimals,cosdandsind; an unknown name is named with its line;x,y,zcannot be names. - Modifiers. Each primitive makes its kind;
atis a translation,scaleuniform or not; and the order the modifiers are written in is the order they are applied in, checked against the library's own matrices multiplied the other way round:scale 2 at 1 0 0istranslation ยท scaling,at 1 0 0 scale 2isscaling ยท translation, and four modifiers compose as written. - Materials against the values in the block, several to a line; a pattern with its colours and its scale, or a rotation and a scale in the order written; an unknown material or property reported.
- Loops. A 4 ร 3 grid is 12 shapes at
(2.5 i, 1, 3 j), the inner loop fastest; a circle of 12 every 30 degrees is at6 (cos a, 0, sin a), checked against the same sines and cosines taken in the test;step 0.25from 1 to 2 is 5 values and the last is exactly 2; a negative step counts down; a loop with nothing to do does nothing; a loop's ownihides an earlieriand the earlier one is back after it. - Definitions. Parameters take the values of the use; two uses are two groups; modifiers of a use move the whole group; a
definition used in a loop, and inside another definition; the wrong number of arguments, an unknown object,
and a missing
endare reported. - Blocks. A difference of three has the hits of the same CSG built by calls; union and intersection; a group; a material on a block reaches every shape; a difference with one shape is refused.
- Surfaces. The sphere of Surfaces typed as text as a
surfacestatement has its hits;letvalues are names in the text; anexpris a piece; afuncwith arguments is the unit sphere atx = 2; two uses of onefunc; onefuncusing another; wrong arity and a bad text are reported with their line. - Deformations against
warped_shape_autoon the same cube; a deformed shape moved withat; writtenbounds; a plane with no box is refused unless bounds are given. - Camera and light. Size, field of view, the transformation (against
view_transform), a point light, 9 sample points of a 3 ร 3 area light, the angles of a spot,antialias, a second light refused. - Choices.
ifandelsefor all six comparisons on three pairs of numbers; noelsewhen false; bracketed sides; ifs inside a loop and inside each other; a bad comparison, a strayelseand a missingendreported. - Chance.
rand(n)equals the fractional part of a large multiple of a sine, as written above, and is the same twice; 400 valuesrand(0)torand(399)are in [0, 1) with a mean near a half;randof something withx,yorzin it is refused. - Recursion.
chain(4)is a sphere andchain(3), and so on down; a definition that never stops is reported with its name and the limit; a tree by a definition that uses itself (a limb, then three branches turned 120 degrees apart and tilted 30, each 0.7 as long) has 1 + 3 + 9 + 27 = 40 branches, each exactly where the same tree built by library calls puts it: 40 matrices compared. - A whole scene against the same scene built by calls. Written in the language and rendered at 36 ร 18, it is exactly the picture of the same scene built with library calls; adding one sphere changes the picture.
Surfaces typed as text and Surfaces of any function (the expression language gained sind, cosd and tand) still pass:
A scene
scenes/garden.scene: five columns of rising height from one definition and one loop, twelve balls round a circle from
another, a pillar twisted a turn and a half, a ball with ripples (a function with no closed form), and two spheres smoothly
joined as a function, each written as text.
# A small garden, in the scene language: columns and a ring of balls placed by loops, a twisted pillar,
# a bumpy ball and a smooth blob each from a typed function.
size 400 200
camera from 0 5 -16 to 0 1 0 fov 60
light point at -10 12 -10
antialias adaptive 1 0.15
material grass
color 1 1 1
ambient 0.15 diffuse 0.9 specular 0
pattern checkers 0.55 0.75 0.45 0.3 0.5 0.25 scale 1.2
end
material stone
color 0.82 0.78 0.7
ambient 0.1 diffuse 0.8 specular 0.1 shininess 30
end
material gold
color 0.9 0.7 0.1
ambient 0.1 diffuse 0.6 specular 0.7 shininess 200
reflective 0.2
end
material ruby
color 0.85 0.1 0.2
ambient 0.1 diffuse 0.7 specular 0.6 shininess 150
reflective 0.1
end
material water
color 0.6 0.8 1
ambient 0.05 diffuse 0.3 specular 0.9 shininess 300
reflective 0.3 transparency 0.5 ior 1.33
end
plane material grass
# a column: a cylinder and a ball on top, any height and width
define column(h, r)
cylinder 0 h closed scale r 1 r
sphere scale (r * 1.4) at 0 h 0
end
# five columns in a row at the back, each taller than the last
for i in 0 to 4
use column(1.4 + 0.5 * i, 0.3) at (i * 1.8 - 3.6) 0 5 material stone
end
# twelve gold balls in a ring round the middle
for k in 0 to 11
sphere scale 0.35 at (4.2 * cosd(k * 30)) 0.35 (4.2 * sind(k * 30) - 1) material gold
end
# a pillar, twisted a turn and a half over its height
twist 2.1 at 0 0 -1 material ruby
cube scale 0.55 1.4 0.55 at 0 1.4 0
end
# a ball with ripples: a function with no closed form
expr r = "sqrt(x^2 + y^2 + z^2)"
surface "shape = r - 1 - 0.1 * sin(7x) * sin(5y) * sin(6z)" bounds -1.4 -1.4 -1.4 1.4 1.4 1.4 at -5.2 1.1 -3 material ruby
# two spheres smoothly joined, as a function
func ball(c, rad) = "sqrt((x - c)^2 + y^2 + z^2) - rad"
surface "a = ball(-0.4, 0.8); b = ball(0.4, 0.8); shape = min(a, b) - max(0.5 - abs(a - b), 0)^2 / 2" bounds -1.5 -1.2 -1.2 1.5 1.2 1.2 at 5.2 1 -3 material water
> tools/render_scene.sh scenes/garden.scene out/garden.png
progress: the port is written to D:/Projects_Organized/raytrace/out/garden.png.progress; ask with v2_interp.exe tools/render_status.patlang --file D:/Projects_Organized/raytrace/out/garden.png.progress
rendered 400x200 in 40 bands by threads in 3074947 ms
rendered by 18 threads in 3097 s wall clock
Checking it independently
What the independent tracer can check about a scene made by a language is the picture: the exporter sends the
world the interpreter built (the primitives, the CSG, the patterns, the function surfaces as text or tape) and the
checker renders it with its own code. What the language means (that at before scale is not scale before at, that a
loop runs the right number of times) is checked by the tests above against arithmetic done in the test.
> v2_interp.exe tools/export_scene.patlang --scene file --file scenes/garden.scene --out out/garden_scene.txt
wrote out/garden_scene.txt (26 shapes)
> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/garden.png -SceneFile out/garden_scene.txt -Adaptive 1,0.15 -Stride 16
decoded by .NET: 400x200; pixels differing from the independent recursive tracer by more than 1 level: 0 (max difference 0); stride 16 in each direction, 325 pixels checked
Negative control: the picture against the Surfaces of any function scene.
> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/garden.png -SceneFile out/warps_scene.txt -Adaptive 1,0.15 -Stride 16
decoded by .NET: 400x200; pixels differing from the independent recursive tracer by more than 1 level: 300 (max difference 147); stride 16 in each direction, 325 pixels checked
How it is checked
> v2_interp.exe tests/run.patlang --node scene/language --depth 2
[ ok ] The scene language
[ ok ] Reading the text
[ ok ] Words and a bracketed group are tokens, and a comment is dropped
[ ok ] Let: a = 7, b = 7/2, c = 1 + 4: the sphere is at (7, 3.5, 5)
[ ok ] Shapes
[ ok ] Each statement makes its shape, in order: sphere cube plane cylinder cone torus sphere ...
[ ok ] A material block sets its fields, several to a line
[ ok ] Loops and definitions
[ ok ] A 4 x 3 grid is 12 spheres at (2.5 i, 1, 3 j), the inner loop fastest
[ ok ] Two uses of a definition make two groups of its two shapes
[ ok ] Blocks, surfaces, deformations
[ ok ] A difference of three, written as a block, has the hits of the same CSG built by calls ...
[ ok ] A surface statement is the surface of Chapter 26: the same hits
[ ok ] A twist block has the hits of warped_shape_auto on the same cube (12 rays
[ ok ] Camera and light
[ ok ] A camera: size 120 x 60, field of view 50 degrees
[ ok ] Choices, chance and recursion
[ ok ] If a > 2 with a = 3 runs the first part
[ ok ] Rand(n) is the fractional part of a large multiple of a sine, as written in the table
[ ok ] A definition that uses itself, stopped by an if: chain(4) holds a sphere and chain(3), ...
[ ok ] A param is its default when nothing is given: n = 3 makes 3 spheres
[ ok ] Whole scenes
[ ok ] A scene written in the language renders to exactly the picture of the same scene built by ...
checks: 15 passed, 0 failed, 0 blocked, 0 skipped, 0 not run
ALL PASSED