Programming Macaw OS

C reference: the graphics chip

#include <macawgpu.h> to talk to the 3D hardware from C. Call these from your program's _draw function: they draw into the window (or into the picture drawinto picked), and they work the way old OpenGL 1.x does. They're Lua's gpu.* calls with gpu_ in front, so The graphics chip has the ideas and examples, and C says how C programs work.

State

gpu_enable

void gpu_enable(const char *cap)

Switches a feature of the chip on. It stays on until you switch it off or call gpu_reset.

gpu_disable

void gpu_disable(const char *cap)

Switches a feature of the chip off.

gpu_isenabled

int gpu_isenabled(const char *cap)

Tells you whether a feature is switched on.

Returns 1 if it's on, 0 if it's off.

gpu_depthmask

void gpu_depthmask(int on)

Sets whether drawing writes to the depth buffer, the hidden picture of how far away each pixel is. Turn it off while you draw see-through things.

gpu_cullface

void gpu_cullface(const char *side)

Picks which side of each triangle "cull_face" skips. It only matters while "cull_face" is on.

gpu_frontface

void gpu_frontface(const char *way)

Says which way round a triangle's corners go, on the screen, when you're looking at its front.

gpu_viewport

void gpu_viewport(int x, int y, int w, int h)

Makes drawing go into one part of the window, like a smaller window inside it.

gpu_clearcolor

void gpu_clearcolor(int colour)

Sets the colour gpu_clear fills the picture with.

gpu_clear

void gpu_clear(const char *what)

Fills the picture with the clear colour, empties the depth buffer, or both, inside the viewport. Do it at the start of each frame. It also starts gpu_stats's counts again.

gpu_reset

void gpu_reset(void)

Puts all of the chip's settings back how they started: features, matrices, lights, fog, the texture and the viewport.

It takes nothing.

Matrices

A matrix is a grid of 16 numbers that moves, turns, grows or projects every corner you draw. There are two: "modelview" places things in front of the camera, and "projection" turns 3D into the flat picture. The calls below change whichever one gpu_matrixmode picked.

gpu_matrixmode

void gpu_matrixmode(const char *m)

Picks which matrix the matrix calls after it change.

gpu_loadidentity

void gpu_loadidentity(void)

Resets the current matrix so it changes nothing. You usually do this before setting up a camera or a projection.

It takes nothing.

gpu_pushmatrix

void gpu_pushmatrix(void)

Saves a copy of the current matrix, so you can change it and get the saved one back with gpu_popmatrix. Each matrix can have 31 saved at once.

It takes nothing.

gpu_popmatrix

void gpu_popmatrix(void)

Gets back the matrix you last saved with gpu_pushmatrix. It's an error if nothing was saved.

It takes nothing.

gpu_translate

void gpu_translate(double x, double y, double z)

Moves everything you draw after it.

gpu_rotate

void gpu_rotate(double degrees, double x, double y, double z)

Turns everything you draw after it round an axis, anticlockwise when you look along the axis towards the middle.

gpu_scale

void gpu_scale(double x, double y, double z)

Grows or shrinks everything you draw after it.

gpu_perspective

void gpu_perspective(double fovy, double aspect, double znear, double zfar)

Sets up a perspective projection, where far things look smaller. Call it on the projection matrix, after gpu_loadidentity.

gpu_ortho

void gpu_ortho(double left, double right, double bottom, double top, double znear, double zfar)

Sets up a flat projection, where things stay the same size however far away they are. It's handy for 2D drawing and maps.

gpu_frustum

void gpu_frustum(double left, double right, double bottom, double top, double znear, double zfar)

Sets up a perspective projection by where its edges are. gpu_perspective is easier to think about, and this lets you make a lopsided view.

gpu_lookat

void gpu_lookat(double ex, double ey, double ez, double tx, double ty, double tz, double ux, double uy, double uz)

Points the camera from an eye at a target. Call it on the modelview matrix, before you place things.

gpu_loadmatrix

void gpu_loadmatrix(const float *m)

Replaces the current matrix with 16 numbers of your own.

gpu_multmatrix

void gpu_multmatrix(const float *m)

Multiplies the current matrix by 16 numbers of your own, as gpu_translate and the others do with theirs.

gpu_getmatrix

void gpu_getmatrix(const char *which, float *m)

Copies a matrix out, so you can keep it or work with it.

gpu_project

int gpu_project(double x, double y, double z, double *sx, double *sy, double *depth)

Works out where a point in your 3D world lands in the window, with the matrices as they are now. Use it to put a label over a 3D thing.

Returns 1, or 0 if the point is behind the camera (and then nothing is filled in).

Drawing a corner at a time

gpu_begin

void gpu_begin(const char *mode)

Starts drawing shapes a corner at a time. Send the corners with gpu_vertex, then call gpu_done.

gpu_color

void gpu_color(int colour)

Sets the colour of the corners you send after it, for drawing without a texture.

gpu_colorex

void gpu_colorex(int colour, double bright)

Sets the colour of the next corners, and how bright they are.

gpu_texcoord

void gpu_texcoord(double u, double v)

Sets which place on the texture the next corner shows.

gpu_normal

void gpu_normal(double x, double y, double z)

Sets which way the surface faces at the next corners, so lighting knows how much light falls on it.

gpu_vertex

void gpu_vertex(double x, double y, double z)

Sends a corner (a vertex), with the colour, texture place and normal set last.

gpu_vertex4

void gpu_vertex4(double x, double y, double z, double w)

Sends a corner with a fourth number, w, as OpenGL's glVertex4 does.

gpu_done

void gpu_done(void)

Finishes the shapes gpu_begin started and draws them.

It takes nothing.

gpu_end

void gpu_end(void)

The same as gpu_done, named as in OpenGL.

It takes nothing.

Buffers

A buffer keeps a model's corners, so you send them once and draw them each frame. Buffers and index buffers are numbers.

gpu_buffer

int gpu_buffer(const float *numbers, int count, const char *layout)

Makes a vertex buffer: a list of corners kept ready to draw.

Returns the buffer's number. It's an error if count isn't a whole number of corners.

gpu_bufferset

void gpu_bufferset(int buf, const float *numbers, int count)

Replaces what's in a buffer, keeping its layout. The number of corners can change.

gpu_buffercount

int gpu_buffercount(int buf)

Tells you how many corners a buffer holds.

Returns the number of corners.

gpu_freebuffer

void gpu_freebuffer(int buf)

Lets go of a buffer you've finished with. Its number doesn't work after this.

gpu_indices

int gpu_indices(const int *list, int count)

Makes an index buffer: a list of which corners of a vertex buffer to draw, so a corner shared by several triangles is only kept once.

Returns the index buffer's number. A number out of range is an error.

gpu_freeindices

void gpu_freeindices(int indices)

Lets go of an index buffer you've finished with.

gpu_drawarrays

void gpu_drawarrays(const char *mode, int buf, int first, int count)

Draws a buffer's corners in order, as if you'd sent them between gpu_begin and gpu_done.

gpu_drawelements

void gpu_drawelements(const char *mode, int buf, int indices)

Draws the corners of a buffer that an index buffer picks, in the index buffer's order.

Textures, light and fog

gpu_bindtexture

void gpu_bindtexture(int picture)

Draws the next shapes with a picture on them, a texture, placed by gpu_texcoord or a buffer's t. "texture" is switched on to start with.

gpu_light

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

Sets up one of the eight lights and switches it on. It's placed through the modelview matrix as it is now, so set your camera first. Switch on "lighting" too.

gpu_lightoff

void gpu_lightoff(int n)

Switches one light off.

gpu_ambient

void gpu_ambient(double a)

Sets the light that falls everywhere, even where no light reaches, so shadows aren't black.

gpu_shademodel

void gpu_shademodel(const char *m)

Sets how lighting is worked out across each triangle.

gpu_material

void gpu_material(double shine, double sharpness)

Sets how shiny the surfaces you draw next are.

gpu_fog

void gpu_fog(double znear, double zfar, int colour)

Sets the fog, which fades things into a colour the further away they are. Switch on "fog" as well.

gpu_pointsize

void gpu_pointsize(double n)

Sets how big the dots are when you draw with "points".

gpu_stats

int gpu_stats(int *tris, int *corners, int *used, int *made, int *missed)

Tells you how much was drawn, to help you find out what's slow.

Returns the number of triangles drawn since the last gpu_clear.

Reflecting water

To draw water that reflects: draw the scene into a picture from a camera mirrored in the water, draw the main scene, call gpu_mask(1), draw the water's surface, call gpu_mask(0), then call gpu_water.

gpu_mask

void gpu_mask(int on)

While it's on, the triangles you draw don't show: they mark which pixels are water, for gpu_water.

gpu_clearmask

void gpu_clearmask(void)

Clears the marked water pixels without drawing anything.

It takes nothing.

gpu_waterdefaults

void gpu_waterdefaults(gpu_wateropts *o)

Fills in every one of gpu_water's options with its usual value, so you can change only the ones you want. gpu_wateropts w = GPU_WATER_DEFAULTS; does the same.

gpu_water

void gpu_water(int reflection, const gpu_wateropts *o)

Mixes a reflection into the marked water pixels, with ripples, a tint and a drifting surface, then clears the marks.

Baked reflections

A cube map is six views from one place, drawn once, that water can reflect instead of a picture drawn again every frame. Cube maps are numbers, like buffers.

gpu_envmap

int gpu_envmap(const int *faces, double x, double y, double z, const double *box)

Makes a cube map from six square pictures. It goes onto the graphics card a face a frame the first time you draw with it.

Returns the cube map's number, or 0 if the graphics card can't do cube maps.

gpu_envmapbytes

int gpu_envmapbytes(const void *bytes, int n, double x, double y, double z, const double *box)

Makes a cube map again from the bytes gpu_envpixels gave you, such as ones you saved in a file.

Returns the cube map's number, or 0 if the bytes aren't six square faces or the card can't do cube maps.

gpu_envloaded

int gpu_envloaded(int env)

Tells you whether all six faces of a cube map are on the graphics card yet. Nothing shows until they are.

Returns 1 if they're all there, 0 if not.

gpu_envunload

void gpu_envunload(int env)

Takes a cube map off the graphics card to save its memory. The next time you draw with it, it goes back a face a frame.

gpu_envpixels

int gpu_envpixels(int env, void *buf, int size, int *across)

Copies a cube map's six faces out as bytes, so you can save them and make it again with gpu_envmapbytes. Call it with buf NULL first to find out how big a buffer you need.

Returns how many bytes there are in all, or 0 if there are none.

gpu_freeenvmap

void gpu_freeenvmap(int env)

Lets go of a cube map you've finished with.