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.
cap: the feature, one a call:"depth_test","cull_face","lighting","fog"or"texture".
gpu_disable
void gpu_disable(const char *cap)
Switches a feature of the chip off.
cap: the feature, one a call:"depth_test","cull_face","lighting","fog"or"texture".
gpu_isenabled
int gpu_isenabled(const char *cap)
Tells you whether a feature is switched on.
cap: the feature:"depth_test","cull_face","lighting","fog"or"texture".
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.
on: 1 to write depths (how it starts), 0 to leave the depth buffer alone.
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.
side:"back"(how it starts) to skip triangles facing away, or"front"to skip those facing you.
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.
way:"ccw"for anticlockwise (how it starts), or"cw"for clockwise.
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.
x: the part's left edge, in pixels from the window's left.y: the part's top edge, in pixels from the window's top.w: its width in pixels. 0 goes back to the whole window.h: its height in pixels. 0 goes back to the whole window.
gpu_clearcolor
void gpu_clearcolor(int colour)
Sets the colour gpu_clear fills the picture with.
colour: a colour number from 0 to 255, likeNAVY.
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.
what:"color"for the picture,"depth"for the depth buffer, orNULL(or"color depth") for both.
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.
m:"modelview"(how it starts) or"projection".
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.
x: how far to move along x, to the right.y: how far to move along y, upwards.z: how far to move along z, towards you. The eye looks down -z.
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.
degrees: how far to turn, in degrees.x: the axis's x.1, 0, 0turns round the x axis.y: the axis's y.0, 1, 0turns round the y axis.z: the axis's z.0, 0, 1turns round the z axis. All three 0 does nothing.
gpu_scale
void gpu_scale(double x, double y, double z)
Grows or shrinks everything you draw after it.
x: how much wider: 1 leaves it, 2 doubles it, 0.5 halves it.y: how much taller, in the same way.z: how much deeper, in the same way.
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.
fovy: how much the camera sees from top to bottom, in degrees. 60 is usual.aspect: the picture's width divided by its height. 0 uses the window's (or the viewport's).znear: the nearest distance it draws, more than 0. 0 means 0.1.zfar: the furthest distance it draws. 0 means 100.
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.
left: the x at the picture's left edge.right: the x at the picture's right edge.bottom: the y at the picture's bottom edge.top: the y at the picture's top edge.znear: the nearest distance it draws. Lua's usual is -1.zfar: the furthest distance it draws. Lua's usual is 1.
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.
left: the x of the left edge, at the near distance.right: the x of the right edge, at the near distance.bottom: the y of the bottom edge, at the near distance.top: the y of the top edge, at the near distance.znear: the nearest distance it draws, more than 0.zfar: the furthest distance it draws.
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.
ex: the eye's x.ey: the eye's y.ez: the eye's z.tx: the x of the place it looks at.ty: the y of the place it looks at.tz: the z of the place it looks at.ux: the x of which way is up. Give 0, 0, 0 for all three for straight up.uy: the y of which way is up.uz: the z of which way is up.
gpu_loadmatrix
void gpu_loadmatrix(const float *m)
Replaces the current matrix with 16 numbers of your own.
m: an array of 16floats, column by column, som[12],m[13]andm[14]are the move.
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.
m: an array of 16floats, column by column, som[12],m[13]andm[14]are the move.
gpu_getmatrix
void gpu_getmatrix(const char *which, float *m)
Copies a matrix out, so you can keep it or work with it.
which:"modelview","projection", orNULLfor the onegpu_matrixmodepicked.m: an array of 16floats it fills in, column by column.
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.
x: the point's x.y: the point's y.z: the point's z.sx: pass the address of adouble, like&sx, for pixels from the window's left.NULLskips it.sy: pass the address of adoublefor pixels from the window's top.NULLskips it.depth: pass the address of adoublefor how far away it is, 0 near to 1 far.NULLskips it.
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.
mode: what the corners make:"triangles","tristrip","trifan","quads","quadstrip","polygon","lines","linestrip","lineloop"or"points".
gpu_color
void gpu_color(int colour)
Sets the colour of the corners you send after it, for drawing without a texture.
colour: a colour number from 0 to 255, likeRED.
gpu_colorex
void gpu_colorex(int colour, double bright)
Sets the colour of the next corners, and how bright they are.
colour: a colour number from 0 to 255, likeRED.bright: 1 as it is, less for darker (0 is black), up to 1.3 for brighter.
gpu_texcoord
void gpu_texcoord(double u, double v)
Sets which place on the texture the next corner shows.
u: across the picture, 0 at its left edge and 1 at its right.v: down the picture, 0 at its top edge and 1 at its bottom.
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.
x: the direction's x.y: the direction's y.z: the direction's z. It needn't be 1 long. It starts as 0, 0, 1.
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.
x: the corner's x.y: the corner's y.z: the corner's z.
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.
x: the corner's x.y: the corner's y.z: the corner's z.w: 1 is the same asgpu_vertex. The place is x, y and z divided byw.
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.
numbers: an array offloats, corner after corner, each with the numbers its layout lists, in that order.count: how manyfloats there are in all, a whole number of corners.layout: what each corner has:pposition (3),ttexture place (2),nnormal (3),ccolour (1),llight baked in (1, where 1 is lit as usual). It needs ap.NULLmeans"p".
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.
buf: the buffer, fromgpu_buffer.numbers: an array offloats, as forgpu_buffer.count: how manyfloats there are in all, a whole number of corners.
gpu_buffercount
int gpu_buffercount(int buf)
Tells you how many corners a buffer holds.
buf: the buffer, fromgpu_buffer.
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.
buf: the buffer, fromgpu_buffer. 0, or one already let go, is ignored.
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.
list: an array ofintcorner numbers, counting from 0, up to 65535.count: how many numbers are inlist.
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.
indices: the index buffer, fromgpu_indices. 0, or one already let go, is ignored.
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.
mode: what the corners make, as forgpu_begin, like"triangles".buf: the buffer, fromgpu_buffer.first: the first corner to draw, counting from 0.count: how many corners to draw. -1 draws all the rest.
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.
mode: what the corners make, as forgpu_begin, like"triangles".buf: the buffer, fromgpu_buffer.indices: the index buffer, fromgpu_indices. A corner the buffer hasn't got is an error.
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.
picture: a picture number fromnewpictureorloadpicture. 0 draws with no texture.
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.
n: which light, from 0 to 7. Light 0 starts on, as a sun over your left shoulder.x: the x of the light's place, or of the direction it comes from.y: the y of its place or direction.z: the z of its place or direction.w: 0 for a direction the light comes from, like the sun; 1 for a lamp at x, y, z.strength: how bright it is. -1 is the usual: 0.75 for a direction, 1 for a lamp.radius: how far a lamp's light reaches, fading out on the way. 0 means it doesn't fade.
gpu_lightoff
void gpu_lightoff(int n)
Switches one light off.
n: which light, from 0 to 7.
gpu_ambient
void gpu_ambient(double a)
Sets the light that falls everywhere, even where no light reaches, so shadows aren't black.
a: how much, from 0 (none) to 1 (fully lit). It starts at 0.35.
gpu_shademodel
void gpu_shademodel(const char *m)
Sets how lighting is worked out across each triangle.
m:"flat"for one shade a face,"smooth"(how it starts) for blending between corners,"phong"for working it out across the face, so highlights land where they should.
gpu_material
void gpu_material(double shine, double sharpness)
Sets how shiny the surfaces you draw next are.
shine: how shiny, from 0 (not at all, how it starts) to 1 (very).sharpness: how tight the highlight is, from 2 (broad) to 64 (a pin-point). 0 means 16.
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.
znear: how far in front of the eye the fog starts. It starts at 10.zfar: how far away things are lost in the fog completely. It starts at 50.colour: the fog's colour number, from 0 to 255. -1 keeps the colour it had.
gpu_pointsize
void gpu_pointsize(double n)
Sets how big the dots are when you draw with "points".
n: the size in pixels. It starts at 1.
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.
tris: pass the address of anintfor the triangles drawn since the lastgpu_clear.NULLskips it.corners: pass the address of anintfor the corners sent since the lastgpu_clear.NULLskips it.used: pass the address of anintfor how many times a saved drawing (display list) was drawn again.NULLskips it.made: pass the address of anintfor how many display lists were made.NULLskips it.missed: pass the address of anintfor drawings worked out afresh instead.NULLskips it.
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.
on: 1 to start marking, 0 to go back to drawing.
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.
o: pass the address of agpu_wateropts, like&w. Every field is filled in.
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.
reflection: the picture drawn from the mirrored camera. 0 for none, when you give a cube map inenv.o: the address of yourgpu_wateropts, orNULLfor every option's usual value. A number below 0 (ortintat -1) means the usual, in brackets:tintamount: how much of the tint is mixed in (0.2).near,far: how much it reflects close to you (0.35) and far away (0.85).ripple: how far ripples bend the reflection, in pixels (1.5).time: the time in seconds, which moves the ripples (0).flow: how fast the surface texture drifts (1).surfaceamount: how much the surface texture shows (0.3).drops: where drips fell, each{ u, v, seconds ago }on the water's texture places.tint: the colour mixed in (BLUE).surface: a picture for the water's own surface (0: none).env: a cube map fromgpu_envmapto reflect instead of the picture (0: none).ndrops: how many ofdropsare used, from 0 to 16.
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.
faces: an array of six picture numbers, the views looking along +x, -x, +y, -y, +z and -z, in that order.x: the x of the place the views were drawn from.y: the y of that place.z: the z of that place.box: an array of sixdoubles,{ x0, y0, z0, x1, y1, z1 }: the room's lowest and highest corners.NULLfor none.
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.
bytes: the bytes fromgpu_envpixels.n: how many bytes there are.x: the x of the place the views were drawn from.y: the y of that place.z: the z of that place.box: an array of sixdoubles,{ x0, y0, z0, x1, y1, z1 }, as forgpu_envmap.NULLfor none.
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.
env: the cube map, fromgpu_envmap.
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.
env: the cube map, fromgpu_envmap.
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.
env: the cube map, fromgpu_envmap.buf: a buffer for the bytes: blue, green, red for each texel (texture pixel), face by face, top row first.NULLcopies nothing.size: how many bytesbufholds. It copies no more than that.across: pass the address of anintfor how many texels across each face is.NULLskips it.
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.
env: the cube map, fromgpu_envmap. 0, or one already let go, is ignored.