Programming Macaw OS

C reference: 3D

These calls come from #include <macaw3d.h>. Each thing in a scene (a camera, a light, a cube, a terrain) is a number, and 0 means none. There are two ways to do 3D: make a scene once and let render() draw it every frame, or make meshes and draw them yourself with your own camera. The 3D page explains both, and C shows how Lua's calls become C ones.

Making things

newcamera

int newcamera(void)

Makes a camera to look through. It starts at 0, 0, 0 looking along z, sees 80 degrees across and clears the picture to NIGHT. The first camera you make is the one render(0) uses.

It takes nothing.

Returns the new camera's number.

newlight

int newlight(const char *kind, double radius, double strength)

Makes a light. A sun lights everything from far away and shines the way it faces, so turn it with rotate3d. A lamp lights things near where you put it.

Returns the new light's number.

Shapes, terrain and models

newcube

int newcube(int texture, double tiles)

Makes a cube 2 units across, from -1 to 1 each way around its position. Use scale3d to change its size.

Returns the new cube's number.

newsphere

int newsphere(int segments, int texture)

Makes a ball 2 units across (a radius of 1).

Returns the new ball's number.

newcylinder

int newcylinder(int segments, int texture, int capped)

Makes a cylinder with a radius of 1, from y = -1 to y = 1.

Returns the new cylinder's number.

newcone

int newcone(int segments, int texture)

Makes a cone with its point at y = 1 and a closed, round base of radius 1 at y = -1.

Returns the new cone's number.

newplane

int newplane(int texture, double tiles)

Makes a flat square 2 units across, lying at y = 0 and facing up. Use it for floors. It can only be seen from above unless you use twosided3d.

Returns the new square's number.

newterrain

int newterrain(int cells, int heightpicture, int texture, double tiles)

Makes hills from a picture. The terrain goes from -1 to 1 across x and z, with heights from 0 to 1, so scale it up with scale3d.

Returns the new terrain's number.

newterrainfn

int newterrainfn(int cells, double (*height)(double u, double v), int texture, double tiles)

Makes hills like newterrain, but your own function works out how high each point is.

Returns the new terrain's number.

newmesh

int newmesh(const float *corners, int ncorners, const int *triangles, int ntriangles, const int *textures, int ntextures, const int *texids)

Makes a thing with a shape of your own, built from triangles.

Returns the new thing's number.

newmodel

int newmodel(const char *path, const int *textures, int ntextures)

Makes a thing from a model in an .obj file.

Returns the new thing's number, or 0 if the file couldn't be read or has no faces.

newsprite

int newsprite(int picture, double height)

Makes a flat picture that always turns to face the camera, for trees and people. Its bottom middle stands at its position, and its width follows the picture's shape. Colour 0 in the picture is see-through.

Returns the new sprite's number.

newpivot

int newpivot(void)

Makes an empty, invisible thing. Parent other things to it with parent3d so they move and turn together.

It takes nothing.

Returns the new pivot's number.

What every thing can do

position3d

void position3d(int e, double x, double y, double z, int global)

Puts a thing at a place. If it has a parent, the place is measured from the parent unless global is 1.

move3d

void move3d(int e, double x, double y, double z)

Moves a thing along the way it's facing, so you can walk a camera forward whichever way it points.

translate3d

void translate3d(int e, double x, double y, double z)

Moves a thing along the world's directions, whichever way it's facing. If it has a parent, these are the parent's directions.

rotate3d

void rotate3d(int e, double yaw, double pitch, double roll)

Turns a thing to face a particular way, whichever way it faced before.

turn3d

void turn3d(int e, double yaw, double pitch, double roll)

Turns a thing by this much from the way it faces now. Call it every frame to keep something spinning.

point3d

void point3d(int e, int other)

Turns a thing to face another thing, and takes away any lean.

pointat3d

void pointat3d(int e, double x, double y, double z)

Turns a thing to face a place in the world, and takes away any lean.

scale3d

void scale3d(int e, double sx, double sy, double sz)

Sets a thing's size. 1 is the size it was made at, and shapes start 2 units across.

parent3d

void parent3d(int e, int parent)

Makes a thing move, turn and grow with another, like a moon going round a planet. It stays where it is in the world when you do it.

pos3d

void pos3d(int e, double *x, double *y, double *z)

Finds out where a thing is in the world, counting any parents.

forward3d

void forward3d(int e, double *x, double *y, double *z)

Finds out which way a thing is facing in the world, as a direction 1 unit long.

up3d

void up3d(int e, double *x, double *y, double *z)

Finds out which way a thing's top is facing in the world, as a direction 1 unit long.

dist3d

double dist3d(int e, int other)

Works out how far apart two things are in the world.

Returns the distance between them, in world units.

hide3d

void hide3d(int e)

Stops drawing a thing, and anything parented to it. Hidden things can't be bumped into or clicked on.

show3d

void show3d(int e)

Starts drawing a thing again after hide3d.

visible3d

int visible3d(int e)

Finds out whether a thing is being drawn.

Returns 1 if it's drawn, or 0 if it or anything it's parented to is hidden.

texture3d

void texture3d(int e, int picture, int n)

Changes the picture a thing is covered with. It doesn't change a sprite's picture.

color3d

void color3d(int e, int colour)

Paints a thing all one colour, in place of its texture. On a model with several textures, only the first changes.

fullbright3d

void fullbright3d(int e, int on)

Stops light and shade changing how a thing looks, for lamps, the sky and glowing things.

twosided3d

void twosided3d(int e, int on)

Draws the back of each face as well as the front, for flat things seen from both sides.

free3d

void free3d(int e)

Takes a thing out of the scene for good. Anything parented to it goes too.

height3d

int height3d(int terrain, double x, double z, double *height)

Finds how high the ground of a terrain is at a place, so you can keep things standing on it. It counts the terrain's position and size, but not any turning.

Returns 1 if the place is over the terrain, or 0 if it isn't (and nothing is written).

collide3d

void collide3d(int e, int type, double radius)

Gives a thing a collision type, so collisions and updateworld can make it bump into things. When it moves, it's tested as a ball.

hitcount3d

int hitcount3d(int e)

Finds out how many things a thing bumped into during the last updateworld().

Returns how many hits it has. Get each one with gethit3d.

gethit3d

int gethit3d(int e, int i, hit3d *hit)

Gets one of the things a thing bumped into during the last updateworld(): the thing, where it touched, and the way the surface there faces.

Returns 1 if there was a hit i, or 0 if there wasn't.

pickable3d

void pickable3d(int e, const char *shape)

Lets rays and the mouse hit a thing, so pick and campick3d can find it.

Cameras

fov3d

void fov3d(int camera, double degrees)

Sets how wide a camera's view is. A small angle zooms in, a big one sees more.

clscolor3d

void clscolor3d(int camera, int colour)

Sets the colour the picture is cleared to before a camera draws, such as the sky.

camfog3d

void camfog3d(int camera, double near, double far, int colour)

Adds fog to what a camera sees, so far-off things fade into a colour.

camproject3d

int camproject3d(int camera, double x, double y, double z, double *sx, double *sy, double *dist)

Works out where a place in the world shows up in a camera's picture, for putting labels over things. It uses the size of the last picture the camera drew (320 by 200 before it has drawn one).

Returns 1 if the place is in front of the camera, or 0 if it's behind (and nothing is written).

campick3d

int campick3d(int camera, double x, double y, double max, hit3d *hit)

Finds the pickable thing under a point in a camera's picture, such as the mouse pointer.

Returns the number of the thing under the point, or 0 if there's nothing there.

The scene

render

void render(int camera)

Draws the whole scene through a camera, into the window (or the picture you're drawing into). Call it in _draw.

updateworld

void updateworld(void)

Works out collisions for everything with a collision type: things that ran into something are pushed back out, and their hits are noted. Call it each frame after you move things.

It takes nothing.

collisions

void collisions(int a, int b, const char *shape, const char *response)

Makes things of one collision type bump into things of another.

resetcollisions

void resetcollisions(int e)

Forgets where a thing was. Call it after you move something a long way in one go, so it doesn't collide with everything in between.

pick

int pick(double ox, double oy, double oz, double dx, double dy, double dz, double max, hit3d *hit)

Finds the nearest pickable thing along a ray, such as a line of sight or a shot.

Returns the number of the thing it hit, or 0 if it hit nothing.

ambient

void ambient(double level)

Sets how bright the light is everywhere in the scene, even where no light shines.

clearworld

void clearworld(void)

Throws the whole scene away. The next thing you make starts a new, empty one, so don't use the old numbers again.

It takes nothing.

Meshes you draw yourself

clear3d

void clear3d(int colour)

Starts a frame of meshes: it clears the depth buffer, and the picture too if you give a colour.

camera3d

void camera3d(double x, double y, double z, double yaw, double pitch, double fov)

Sets where the camera for meshes is and which way it looks.

light3d

void light3d(double dx, double dy, double dz, double sun, double amb)

Sets the sun for meshes: the way its light travels and how strong it is, and the light everywhere.

Lamps, fog and lines

pointlight3d

void pointlight3d(int n, double x, double y, double z, double radius, double strength)

Sets one of the eight lamps for meshes, which lights things near it, fading out at its radius.

fog3d

void fog3d(double near, double far, int colour)

Adds fog to meshes, so far-off things fade into a colour.

nofog3d

void nofog3d(void)

Turns the fog off again.

It takes nothing.

project3d

int project3d(double x, double y, double z, double *sx, double *sy, double *dist)

Works out where a place in the world shows up in the picture, seen through camera3d's camera.

Returns 1 if the place is in front of the camera, or 0 if it's behind (and nothing is written).

line3d

void line3d(double x0, double y0, double z0, double x1, double y1, double z1, int colour, int thick)

Draws a line between two places in the world, seen through camera3d's camera, over the picture. It isn't hidden behind things, and the part behind the camera is cut off. Use it for outlines and markers.

stats3d

int stats3d(void)

Finds out how much 3D drawing there was this frame, for checking your program's speed.

It takes nothing.

Returns how many triangles were drawn since the last clear3d.

Making meshes

cube3d

int cube3d(double w, double h, double d, int texture, double tiles)

Makes a box mesh, seen from outside, centred on 0, 0, 0.

Returns the new mesh's number.

cube3dsides

int cube3dsides(double w, double h, double d, int sides, int top, int bottom, double tiles)

Makes a box mesh like cube3d, with different pictures on its sides, top and bottom.

Returns the new mesh's number.

room3d

int room3d(double w, double h, double d, int floor, int walls, int ceiling, double tiles)

Makes the inside of a box, for standing in. It's centred on 0, 0, 0, so its floor is at y = -h / 2.

Returns the new mesh's number.

loadobj

int loadobj(const char *path, const int *textures, int ntextures)

Makes a mesh from a model in an .obj file.

Returns the new mesh's number, or 0 if the file couldn't be read or has no faces.

mesh3d

int mesh3d(const float *corners, int ncorners, const int *triangles, int ntriangles, const int *textures, int ntextures, const int *texids, const char *options)

Makes a mesh of any shape from your own corners and triangles.

Returns the new mesh's number.

Drawing meshes

billboard3d

void billboard3d(int picture, double x, double y, double z, double height, int fullbright)

Draws a picture standing upright at a place, turned to face camera3d's camera, for trees and people. Its width follows the picture's shape, and colour 0 is see-through.

drawmesh

void drawmesh(int mesh, double x, double y, double z, double yaw, double pitch, double roll, double scale)

Draws a mesh at a place, turned and sized as you say. Call it each frame, after clear3d and camera3d.

drawmeshm

void drawmeshm(int mesh, double a, double b, double c, double d, double e, double f, double g, double h, double i, double x, double y, double z)

Draws a mesh with a 3 by 3 transform of your own, to stretch or squash it or turn it in a way you've worked out. Each corner's new x is a·x + b·y + c·z, its y uses d e f and its z g h i, then it's moved to x, y, z.

meshtexture

void meshtexture(int mesh, int n, int picture)

Changes one of a mesh's textures.

meshsize

void meshsize(int mesh, int *corners, int *triangles)

Finds out how many corners and triangles a mesh has.

freemesh

void freemesh(int mesh)

Lets go of a mesh when you've finished with it, so its memory can be used again.

Textures

loadtexture

int loadtexture(const char *path, int size)

Loads a photo texture from a .tex file, such as one in /system/textures. Bigger photos are shrunk as they load, keeping their shape.

Returns the new picture's number, or 0 if the file couldn't be read or isn't a .tex.

The graphics card

glrpresent

int glrpresent(void)

Finds out whether a graphics card is doing the drawing. The other calls here only tell you anything when one is.

It takes nothing.

Returns 1 if a graphics card is drawing, or 0 if the processor is.

cubemaps

int cubemaps(void)

Finds out whether the graphics card can draw baked reflections (cube maps).

It takes nothing.

Returns 1 if it can, or 0 if it can't or there's no graphics card.

glrvram

int glrvram(int *used, int *allowed)

Finds out roughly how much of the graphics card's memory your program is using.

Returns 1 if it's using more than it's allowed, or 0 if not. With no graphics card it writes 0s and returns 0.

glrinfotext

int glrinfotext(const char *field, char *buf, int size)

Gets one fact about the graphics card as text, such as its name.

Returns how long the whole text is, so you can tell if buf was too small.

glrinfonumber

double glrinfonumber(const char *field)

Gets one fact about the graphics card as a number.

Returns the number, 1 or 0 for yes-or-no facts, and 0 for an unknown field or with no graphics card.