3D
Macaw OS has a 3D renderer for textured triangles. It has a depth buffer, a sun, up to eight lamps, and fog. On a computer with a graphics card, the card draws it at the screen's full size, sharp and smooth. Otherwise the processor draws it, in the screen's colours. Your program is the same either way.
There are three ways to use it, from the simplest to the most hands-on:
- Scenes (below). You make things once, such as a camera, a cube, a terrain or a light, then move them about, and
render()draws them all. Collisions and clicking on things are built in. Start here. - Meshes, further down. You draw each model yourself every frame, with your own camera.
- The graphics chip. OpenGL-style matrices and draw calls, for full control.
All three draw into the same picture with the same depth buffer, so you can mix them. In 3D space, x goes right, y goes up and z goes forward, away from you. Angles are in degrees.
Scenes
Type new3d world.lua in Terminal to get a scene to start from. Here's a small one:
WINDOW = { w = 240, h = 160, title = "My World", scale = 2 }
local cam = newcamera()
cam:position(0, 1.5, -6)
cam:clscolor(colors.sky)
local sun = newlight("sun")
local ground = newplane(nil, 20)
ground:scale(30, 1, 30)
ground:color(colors.green)
local box = newcube()
box:position(0, 1, 2)
box:color(colors.orange)
function _update()
if btn("left") then cam:turn(-2, 0, 0) end
if btn("right") then cam:turn(2, 0, 0) end
if btn("up") then cam:move(0, 0, 0.1) end
box:turn(1, 0, 0)
updateworld()
end
function _draw()
render()
end
Each frame, move things, then call updateworld() to work out collisions. Then render() draws everything from the camera.
Making things
| Call | What it makes |
|---|---|
newcamera() | A camera to look through |
newlight("sun") | Light from far away, like the sun. It shines the way it faces, so turn it with e:rotate to light things from another side |
newlight("point", [radius], [strength]) | A lamp that lights things within radius |
newcube([texture], [tiles]) | A 2x2x2 cube |
newsphere([segments], [texture]) | A ball |
newcylinder([segments], [texture], [capped]) | A cylinder |
newcone([segments], [texture]) | A cone |
newplane([texture], [tiles]) | A flat 2x2 square, facing up |
newterrain(cells, height, [texture], [tiles]) | Hills, 2 units across, which you scale up. height is either a picture, where brighter is higher, or a function (u, v) that returns a height from 0 to 1 for each point. u and v go from 0 to 1 across the terrain |
newmesh(verts, tris, [texture]) | Your own shape (see Meshes, below) |
newmodel(path, [textures]) | A model from an .obj file |
newsprite(picture, [height]) | A flat picture that always faces the camera, for trees and people. It's 1 unit tall unless you give a height |
newpivot() | An empty thing to group others under |
tiles is how many times the texture repeats across each face, or across the whole of a terrain.
What everything can do
| Call | What it does |
|---|---|
e:position(x, y, z, [global]) | Puts it at this place. If it has a parent, the place is measured from the parent unless global is true |
e:move(x, y, z) | Moves it relative to the way it's facing (z is forward) |
e:translate(x, y, z) | Moves it along the world's directions |
e:rotate(yaw, pitch, roll) | Turns it to face this way |
e:turn(yaw, pitch, roll) | Turns it by this much |
e:point(other) or e:point(x, y, z) | Turns it to face something |
e:scale(s) or e:scale(sx, sy, sz) | Sets its size |
e:parent(p) | Makes it move and turn with p, like a moon going round a planet |
e:pos() | Returns where it is, as x, y, z |
e:forward() | Returns the way it's facing |
e:up() | Returns the way its top is facing |
e:dist(other) | Returns how far it is from something |
e:hide(), e:show() | Stops and starts drawing it |
e:visible() | Returns whether it's drawn. It's false if it or anything it's parented to is hidden |
e:texture(picture, [n]), e:color(c) | Changes how it looks. For a model with several textures, n says which one to change (the first if you leave it out) |
e:fullbright() | Stops light shading it, for lamps, the sky and glowing things |
e:twosided() | Draws both sides of it |
e:free() | Gets rid of it |
terrain:height(x, z) | Returns the ground's height at a place |
Cameras also have cam:fov(degrees) (80 to start with), cam:clscolor(c) (or false to not clear the picture), cam:fog(near, far, [c]), cam:project(e) for where something is in the window, and cam:pick(x, y, [max]).
The rest of the scene
| Call | What it does |
|---|---|
render([camera]) | Draws everything. It uses the first camera you made unless you give it another |
updateworld() | Works out collisions for everything that moved |
collisions(a, b, [shape], [response]) | Makes things of type a bump into things of type b (see below) |
resetcollisions(e) | Forgets where e was. Call it after you move something a long way in one go, so it doesn't collide with everything in between |
pick(ox, oy, oz, dx, dy, dz, [max]) | Returns the nearest pickable thing along a ray from ox, oy, oz in the direction dx, dy, dz, or nil. With max, it ignores anything further away than that |
ambient(level) | Sets how bright the light is everywhere. It starts at 0.35 |
clearworld() | Throws the whole scene away. The next thing you make starts a new, empty one |
world3d() | Returns the scene itself. The calls above are short for its methods, so render(cam) is the same as world3d():render(cam) |
Bumping into things
Give things a collision type, which can be any number. Then say which types bump into which:
ball:collide(1, 0.5) -- type 1, a sphere of radius 0.5
wall:collide(2) -- type 2
collisions(1, 2, "mesh", "slide")
After updateworld(), a ball that ran into a wall has been pushed back out of it. ball.hits lists what it touched, with the direction the surface faces (nx, ny, nz). The third argument to collisions is the shape to test the wall as. It's "mesh" for its exact shape, "box" or "sphere". The last argument is what happens. With "slide" the ball slides along the wall, and with "stop" it stops.
Clicking on things
box:pickable() -- "mesh" (exact) or "sphere" ("box" works too, tested exactly)
local mx, my, _, pressed = mouse()
if pressed & 1 ~= 0 then
local hit = cam:pick(mx, my)
if hit then hit.entity:hide() end
end
cam:pick returns a table about what's under the pointer, or nil. It has the thing itself (hit.entity), where it was hit (x, y, z), the direction the surface faces (nx, ny, nz) and how far away it is (dist). pick returns the same table.
The demos in /games show all of this. orbit has things going round other things, flyover has terrain, fog and sprites, and gallery has clicking. marble, Marble Run, is a whole game: a marble rolling round five levels made in Brushwork (see Levels).
Meshes
To draw each model yourself, set up the frame and then draw:
WINDOW = { w = 240, h = 160, title = "Box", realtime = true }
local crate = image(16, 16)
crate:clear(colors.tan)
target(crate)
rect(0, 0, 16, 16, colors.brown)
line(0, 0, 15, 15, colors.brown)
target()
local box = cube3d(1, 1, 1, crate)
local angle = 0
function _update() angle = angle + 1 end
function _draw()
clear3d(colors.navy)
camera3d(0, 0, -3)
light3d(-1, -1, 1)
box:draw(0, 0, 0, angle, angle / 2, 0)
end
| Call | What it does |
|---|---|
clear3d([c]) | Starts a frame. It clears the depth buffer, and clears the picture to c if you give it |
camera3d(x, y, z, [yaw], [pitch], [fov]) | Sets where the camera is and which way it looks. fov is 90 unless you give it |
light3d(dx, dy, dz, [sun], [ambient]) | Sets the sun's direction and strength, and the light everywhere |
pointlight3d(n, x, y, z, radius, [strength]) | Sets lamp n (1 to 8). A radius of 0 turns it off |
fog3d(near, far, c) | Adds fog that starts at near and is solid by far. fog3d() turns it off |
m:draw([x, y, z], [yaw, pitch, roll], [scale]) | Draws a mesh |
m:drawm(a, b, c, d, e, f, g, h, i, x, y, z) | Draws a mesh with your own 3x3 transform. Each corner is multiplied by the rows a b c, d e f and g h i, then moved to x, y, z. Use it to stretch or squash a mesh, or to draw it with a rotation you've worked out yourself |
m:texture(n, picture) | Changes the mesh's texture n (from 1 to 16) |
m:size() | Returns how many corners and triangles the mesh has |
project3d(x, y, z) | Returns where a point lands in the window, as x, y, distance |
line3d(x0, y0, z0, x1, y1, z1, [col], [thick]) | Draws a line in the world, seen through the camera, over the picture. The part behind you is cut off. Use it for outlines and markers |
stats3d() | Returns how many triangles were drawn this frame |
For example, m:drawm(2, 0, 0, 0, 1, 0, 0, 0, 1, x, y, z) draws a mesh twice as wide as it is.
Making meshes:
| Call | What it makes |
|---|---|
cube3d(w, h, d, texture, [tiles]) | A box. texture can be one picture or { sides, top, bottom } |
room3d(w, h, d, floor, walls, [ceiling], [tiles]) | The inside of a box, with the floor at y = 0 |
loadobj(path, [textures]) | A model from an .obj file |
billboard3d(picture, x, y, z, [height], ["fullbright"]) | Draws a picture standing at a place, facing the camera |
mesh3d(verts, tris, [textures], [texids], [options]) | Any shape |
tiles is how many times the texture repeats across each face. With 0 it repeats once for every unit of size, which is what room3d does unless you say otherwise. cube3d shows the texture once on each face unless you give it tiles.
mesh3d takes the corners as one long list, with five numbers for each, x, y, z, u, v. u and v say which part of the texture goes at that corner, from 0 to 1 across. The triangles are three corner numbers each, counting from 1. The front of a triangle is the side its corners go round anticlockwise. The options are words in a string. "double" draws both sides, and "fullbright" ignores the light.
local tri = mesh3d({ -1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1.5, 0, 0.5, 0 },
{ 1, 2, 3 }, { crate }, nil, "double")
Textures
Any picture can be a texture. Textures repeat, and colour 0 is see-through. For photos, Macaw OS comes with a set in /system/textures, which you load with loadtexture:
local rock = loadtexture("/system/textures/rock.tex")
With a graphics card they're drawn in true colour. Without one they're drawn in the nearest of the screen's colours. Photos are kept at most 64 pixels across, which is plenty for a wall where the texture repeats. For something seen big and close, loadtexture(path, 128) keeps more detail, but it takes four times the memory.
The computers Macaw OS was made for had graphics cards with very little memory. An eMac has 32 MB, and a slot-loading iMac has 8 or 16. About and the Profiler both show how much is in use. If your program uses more than it's allowed, Macaw OS tells you once in an alert, and in Terminal each time.
The graphics card
glr is only there when a graphics card is doing the drawing, so check with if glr then.
| Call | What it gives |
|---|---|
glr.vram() | Roughly how many bytes of the card's memory are in use, how many are allowed (32 MB, or 16 MB on a Rage 128), and whether it's over |
glr.info() | A table about the card. It has renderer and version (the card's name and OpenGL version), depthbits, maxtexture (the biggest texture it takes), multitexture, cubemap (whether it can do baked reflections), extensions, and how its memory is split up (texturebytes, lightmapbytes and screenbytes) |
cubemaps() is a shortcut for glr ~= nil and glr.info().cubemap.