shithub: libgraphics

Download patch

ref: 121940e40ff014bfa991cfd658e34a6688566f7b
parent: 24e06421600b32847938c214c71893f27a0fe5b8
author: rodri <rgl@antares-labs.eu>
date: Wed Mar 4 12:24:34 EST 2026

header formatting changes mostly

succumb to OCD.

besides that, i got rid of the Projection enum type
and replaced the Camera.clip.[nf] with Camera.z(ne|f)ar
respectively.

--- a/camera.c
+++ b/camera.c
@@ -114,11 +114,11 @@
 	assert(c->view != nil);
 	if(c->projtype == PERSPECTIVE)
 		assert(c->fov > 0 && c->fov < 180*DEG);
-	assert(c->clip.n > 0 && c->clip.n < c->clip.f);
+	assert(c->znear > 0 && c->znear < c->zfar);
 }
 
 Camera *
-Camv(Viewport *v, Renderer *r, Projection p, double fov, double n, double f)
+Camv(Viewport *v, Renderer *r, int p, double fov, double n, double f)
 {
 	Camera *c;
 
@@ -133,7 +133,7 @@
 }
 
 Camera *
-Cam(Rectangle vr, Renderer *r, Projection p, double fov, double n, double f)
+Cam(Rectangle vr, Renderer *r, int p, double fov, double n, double f)
 {
 	Viewport *v;
 
@@ -179,11 +179,11 @@
 		t = Dy(c->view->r)/2;
 		l = -r;
 		b = -t;
-		orthographic(c->proj, l, r, b, t, c->clip.n, c->clip.f);
+		orthographic(c->proj, l, r, b, t, c->znear, c->zfar);
 		break;
 	case PERSPECTIVE:
 		a = (double)Dx(c->view->r)/Dy(c->view->r);
-		perspective(c->proj, c->fov, a, c->clip.n, c->clip.f);
+		perspective(c->proj, c->fov, a, c->znear, c->zfar);
 		break;
 	default: sysfatal("unknown projection type");
 	}
@@ -193,12 +193,12 @@
 }
 
 void
-configcamera(Camera *c, Projection p, double fov, double n, double f)
+configcamera(Camera *c, int p, double fov, double n, double f)
 {
 	c->projtype = p;
 	c->fov = fov;
-	c->clip.n = n;
-	c->clip.f = f;
+	c->znear = n;
+	c->zfar = f;
 	reloadcamera(c);
 }
 
--- a/graphics.h
+++ b/graphics.h
@@ -7,22 +7,16 @@
 #define _Xdotvec2(a,b)	((a).x*(b).x + (a).y*(b).y)
 #define _Xdotvec3(a,b)	((a).x*(b).x + (a).y*(b).y + (a).z*(b).z)
 
-typedef enum {
+enum {
+	/* projection types */
 	ORTHOGRAPHIC,
 	PERSPECTIVE,
-} Projection;
 
-enum {
 	/* culling modes */
-	CullNone,
+	CullNone = 0,
 	CullFront,
 	CullBack,
 
-	/* render options */
-	ROBlend	= 0x01,
-	RODepth	= 0x02,
-	ROAbuff	= 0x04,
-
 	/* primitive types */
 	PPoint = 0,
 	PLine,
@@ -34,81 +28,86 @@
 	LightSpot,
 
 	/* raster formats */
-	COLOR32 = 0,	/* RGBA32 */
-	FLOAT32,	/* F32 */
+	COLOR32 = 0,		/* RGBA32 */
+	FLOAT32,		/* F32 */
 
-	/* texture types */
-	RAWTexture = 0,	/* unmanaged */
+	/* texture formats */
+	RAWTexture = 0,		/* unmanaged */
 	sRGBTexture,
 
 	/* upscaling filters */
-	UFNone = 0,	/* nearest neighbour */
+	UFNone = 0,		/* nearest neighbour */
 	UFScale2x,
 	UFScale3x,
 	UFScale4x,
 
+	/* render options */
+	ROBlend	= 0x01,
+	RODepth	= 0x02,
+	ROAbuff	= 0x04,
+
 	/* vertex attribute types */
 	VAPoint = 0,
 	VANumber,
 
-	MAXVATTRS = 10,	/* change this if your shaders require it */
+	MAXVATTRS	= 10,	/* change this if your shaders require it */
 
 	/* itemarray */
 	NaI	= ~0ULL,	/* not an index */
 };
 
-typedef struct ItemArray ItemArray;
-typedef struct Color Color;
-typedef struct Texture Texture;
-typedef struct Cubemap Cubemap;
-typedef struct Vertexattr Vertexattr;
-typedef struct Vertexattrs Vertexattrs;
-typedef struct Vertex Vertex;
-typedef struct BVertex BVertex;
-typedef struct LightSource LightSource;
-typedef struct Material Material;
-typedef struct Primitive Primitive;
-typedef struct Model Model;
-typedef struct Entity Entity;
-typedef struct Scene Scene;
-typedef struct Shaderparams Shaderparams;
-typedef struct Shadertab Shadertab;
-typedef struct Rendertime Rendertime;
-typedef struct Renderer Renderer;
-typedef struct Renderjob Renderjob;
-typedef struct Fragment Fragment;
-typedef struct Astk Astk;
-typedef struct Abuf Abuf;
-typedef struct Raster Raster;
-typedef struct Framebuf Framebuf;
-typedef struct Framebufctl Framebufctl;
-typedef struct Viewdrawctx Viewdrawctx;
-typedef struct Viewport Viewport;
-typedef struct Camera Camera;
+typedef struct ItemArray	ItemArray;
+typedef struct Color		Color;
+typedef struct Texture		Texture;
+typedef struct Cubemap		Cubemap;
+typedef struct Vertexattr	Vertexattr;
+typedef struct Vertexattrs	Vertexattrs;
+typedef struct Vertex		Vertex;
+typedef struct BVertex		BVertex;
+typedef struct LightSource	LightSource;
+typedef struct Material		Material;
+typedef struct Primitive	Primitive;
+typedef struct Model		Model;
+typedef struct Entity		Entity;
+typedef struct Scene		Scene;
+typedef struct Shaderparams	Shaderparams;
+typedef struct Shadertab	Shadertab;
+typedef struct Rendertime	Rendertime;
+typedef struct Renderer		Renderer;
+typedef struct Renderjob	Renderjob;
+typedef struct Fragment		Fragment;
+typedef struct Astk		Astk;
+typedef struct Abuf		Abuf;
+typedef struct Raster		Raster;
+typedef struct Framebuf		Framebuf;
+typedef struct Framebufctl	Framebufctl;
+typedef struct Viewdrawctx	Viewdrawctx;
+typedef struct Viewport		Viewport;
+typedef struct Camera		Camera;
 
 struct ItemArray
 {
-	void *items;
-	usize nitems;
-	usize itemsize;
+	void	*items;
+	usize	nitems;
+	usize	itemsize;
 };
 
 struct Color
 {
-	double r, g, b, a;
+	double	r, g, b, a;
 };
 
 struct Texture
 {
-	Memimage *image;
-	int type;
-	char *file;
+	int		type;
+	char		*file;
+	Memimage	*image;
 };
 
 struct Cubemap
 {
-	char *name;
-	Texture *faces[6];
+	char		*name;
+	Texture		*faces[6];	/* -x, +x, -y, +y, -z, +z */
 };
 
 /*
@@ -135,26 +134,26 @@
 
 struct Vertexattr
 {
-	char *id;
-	int type;
+	char	*id;
+	int	type;
 	union {
-		Point3 p;
-		double n;
+		Point3	p;
+		double	n;
 	};
 };
 
 struct Vertexattrs
 {
-	Vertexattr attrs[MAXVATTRS];
-	ulong nattrs;
+	Vertexattr	attrs[MAXVATTRS];
+	ulong		nattrs;
 };
 
 struct Vertex
 {
-	usize p;		/* position */
-	usize n;		/* surface normal */
-	usize uv;		/* texture coordinate */
-	usize c;		/* shading color */
+	usize	p;	/* position idx */
+	usize	n;	/* surface normal idx */
+	usize	uv;	/* texture coordinate idx */
+	usize	c;	/* shading color idx */
 };
 
 /*
@@ -164,385 +163,388 @@
  */
 struct BVertex
 {
-	Point3 p;		/* position */
-	Point3 n;		/* surface normal */
-	Point2 uv;		/* texture coordinate */
-	Color c;		/* shading color */
-	Point3 tangent;
-	Material *mtl;
-	Vertexattrs;		/* attributes (varyings) */
+	Point3		p;		/* position */
+	Point3		n;		/* surface normal */
+	Point2		uv;		/* texture coordinate */
+	Color		c;		/* shading color */
+	Point3		tangent;	/* used for normal mapping */
+	Material	*mtl;
+	Vertexattrs;			/* attributes (varyings) */
 };
 
 struct LightSource
 {
-	int type;
-	Point3 p;
-	Point3 dir;
-	Color c;
-	double cutoff;	/* distance */
-	/* spotlights */
-	double θu;	/* umbra angle. anything beyond is unlit */
-	double θp;	/* penumbra angle. anything within is fully lit */
+	int		type;
+	Point3		p;
+	Point3		dir;
+	Color		c;
+	double		cutoff;		/* distance */
 
-	LightSource *prev, *next;
+	/* spotlights only */
+	double		θu;		/* umbra angle. anything beyond is unlit */
+	double		θp;		/* penumbra angle. anything within is fully lit */
+
+	LightSource	*prev, *next;
 };
 
 struct Material
 {
-	char *name;
-	Color ambient;
-	Color diffuse;
-	Color specular;
-	double shininess;
-	Texture *diffusemap;
-	Texture *specularmap;
-	Texture *normalmap;
+	char		*name;
+	Color		ambient;
+	Color		diffuse;
+	Color		specular;
+	double		shininess;
+	Texture		*diffusemap;
+	Texture		*specularmap;
+	Texture		*normalmap;
 };
 
 struct Primitive
 {
-	int type;
-	usize v[3];
-	usize tangent;		/* used for normal mapping */
-	Material *mtl;
+	int		type;
+	usize		v[3];		/* vertex indices */
+	usize		tangent;	/* tangent idx */
+	Material	*mtl;
 };
 
 struct Model
 {
-	ItemArray *positions;
-	ItemArray *normals;
-	ItemArray *texcoords;
-	ItemArray *colors;
-	ItemArray *tangents;
-	ItemArray *verts;
-	ItemArray *prims;
-	Material *materials;
-	ulong nmaterials;
+	char		*name;
+	ItemArray	*positions;
+	ItemArray	*normals;
+	ItemArray	*texcoords;
+	ItemArray	*colors;
+	ItemArray	*tangents;
+	ItemArray	*verts;
+	ItemArray	*prims;
+	Material	*materials;
+	ulong		nmaterials;
 
-	usize (*addposition)(Model*, Point3);
-	usize (*addnormal)(Model*, Point3);
-	usize (*addtexcoord)(Model*, Point2);
-	usize (*addcolor)(Model*, Color);
-	usize (*addtangent)(Model*, Point3);
-	usize (*addvert)(Model*, Vertex);
-	usize (*addprim)(Model*, Primitive);
-	int (*addmaterial)(Model*, Material);
-	Material *(*getmaterial)(Model*, char*);
+	usize		(*addposition)(Model*, Point3);
+	usize		(*addnormal)(Model*, Point3);
+	usize		(*addtexcoord)(Model*, Point2);
+	usize		(*addcolor)(Model*, Color);
+	usize		(*addtangent)(Model*, Point3);
+	usize		(*addvert)(Model*, Vertex);
+	usize		(*addprim)(Model*, Primitive);
+	int		(*addmaterial)(Model*, Material);
+	Material*	(*getmaterial)(Model*, char*);
 };
 
 struct Entity
 {
 	RFrame3;
-	char *name;
-	Model *mdl;
+	char		*name;
+	Model		*mdl;
 
-	Entity *prev, *next;
+	Entity		*prev, *next;
 };
 
 struct Scene
 {
-	char *name;
-	Entity ents;
-	ulong nents;
-	LightSource lights;
-	ulong nlights;
-	Cubemap *skybox;
+	char		*name;
+	Entity		ents;
+	ulong		nents;
+	LightSource	lights;
+	ulong		nlights;
+	Cubemap		*skybox;
 
-	void (*addent)(Scene*, Entity*);
-	void (*delent)(Scene*, Entity*);
-	Entity *(*getent)(Scene*, char*);
-	void (*addlight)(Scene*, LightSource*);
+	void		(*addent)(Scene*, Entity*);
+	void		(*delent)(Scene*, Entity*);
+	Entity*		(*getent)(Scene*, char*);
+	void		(*addlight)(Scene*, LightSource*);
 };
 
 struct Shaderparams
 {
-	Framebuf *fb;
-	Shadertab *stab;
-	Camera *camera;
-	Entity *entity;
-	Scene *scene;
-	BVertex *v;
-	Point p;	/* fragment position (fshader-only) */
-	uint idx;	/* vertex index (vshader-only) */
+	Framebuf	*fb;
+	Shadertab	*stab;
+	Camera		*camera;
+	Entity		*entity;
+	Scene		*scene;
+	BVertex		*v;
+	Point		p;	/* fragment position (fshader-only) */
+	uint		idx;	/* vertex index (vshader-only) */
 
-	Vertexattr *(*getuniform)(Shaderparams*, char*);
-	Vertexattr *(*getattr)(Shaderparams*, char*);
-	void (*setattr)(Shaderparams*, char*, int, void*);
-	void (*toraster)(Shaderparams*, char*, void*);
+	Vertexattr*	(*getuniform)(Shaderparams*, char*);
+	Vertexattr*	(*getattr)(Shaderparams*, char*);
+	void		(*setattr)(Shaderparams*, char*, int, void*);
+	void		(*toraster)(Shaderparams*, char*, void*);
 };
 
 struct Shadertab
 {
-	char *name;
-	Point3 (*vs)(Shaderparams*);	/* vertex shader */
-	Color (*fs)(Shaderparams*);	/* fragment shader */
-	Vertexattrs;			/* uniforms */
+	char		*name;
+	Point3		(*vs)(Shaderparams*);	/* vertex shader */
+	Color		(*fs)(Shaderparams*);	/* fragment shader */
+	Vertexattrs;				/* uniforms */
 };
 
 struct Rendertime
 {
-	uvlong t0, t1;
+	uvlong	t0, t1;
 };
 
 struct Renderer
 {
-	Channel *jobq;
-	ulong nprocs;
-	int doprof;	/* enable profiling */
+	Channel		*jobq;
+	ulong		nprocs;
+	int		doprof;	/* enable profiling */
 };
 
 struct Renderjob
 {
 	Ref;
-	uvlong id;
-	Renderer *rctl;
-	Framebuf *fb;
-	Camera *camera;
-	Shadertab *shaders;
-	Channel *donec;
-	Rectangle *cliprects;	/* one per rasterizer */
-	int ncliprects;
+	uvlong		id;
+	Renderer	*rctl;
+	Framebuf	*fb;
+	Camera		*camera;
+	Shadertab	*shaders;
+	Channel		*donec;
+	Rectangle	*cliprects;	/* one per rasterizer */
+	int		ncliprects;
 
 	struct {
-		/* renderer, entityproc, tilers, rasterizers */
-		Rendertime R, E, *Tn, *Rn;
+		Rendertime	R;	/* renderer */
+		Rendertime	E;	/* entityproc */
+		Rendertime*	Tn;	/* tilers */
+		Rendertime*	Rn;	/* rasterizers */
 	} times;
 
-	Renderjob *next;
+	Renderjob	*next;
 };
 
 struct Fragment
 {
-	Color c;
-	float z;
+	Color	c;
+	float	z;
 };
 
 struct Astk
 {
-	Point p;
-	Fragment *items;
-	ulong size;
+	Point		p;
+	Fragment	*items;
+	ulong		size;
 };
 
 struct Abuf
 {
 	QLock;
-	Astk *stk;	/* framebuffer fragment stacks */
-	Astk **act;	/* active fragment stacks */
-	ulong nact;
+	Astk		*stk;	/* framebuffer fragment stacks */
+	Astk*		*act;	/* active fragment stacks */
+	ulong		nact;
 };
 
 struct Raster
 {
-	Rectangle r;
-	char *name;
-	ulong chan;
-	ulong *data;
+	char		*name;
+	Rectangle	r;
+	ulong		chan;
+	ulong		*data;
 
-	Raster *next;
+	Raster		*next;
 };
 
 struct Framebuf
 {
-	Rectangle r;
-	Rectangle clipr;
-	Raster *rasters;	/* [0] color, [1] depth, [2..n] user-defined */
-	Abuf abuf;		/* A-buffer */
+	Rectangle	r;
+	Rectangle	clipr;
+	Raster		*rasters;	/* [0] color, [1] depth, [2..n] user-defined */
+	Abuf		abuf;		/* A-buffer */
 
-	void (*createraster)(Framebuf*, char*, ulong);
-	Raster *(*fetchraster)(Framebuf*, char*);
+	void		(*createraster)(Framebuf*, char*, ulong);
+	Raster*		(*fetchraster)(Framebuf*, char*);
 };
 
 struct Framebufctl
 {
 	QLock;
-	Framebuf *fb[2];	/* double buffering */
-	uint idx;		/* front buffer index */
-	uint upfilter;		/* upscaling filter */
+	Framebuf	*fb[2];		/* double buffer */
+	uint		idx;		/* front buffer index */
+	uint		upfilter;	/* upscaling filter */
 
-	void (*draw)(Framebufctl*, Image*, char*, Point, Point, Viewdrawctx*);
-	void (*memdraw)(Framebufctl*, Memimage*, char*, Point, Point, Viewdrawctx*);
-	void (*swap)(Framebufctl*);
-	void (*reset)(Framebufctl*);
-	void (*createraster)(Framebufctl*, char*, ulong);
-	Raster *(*fetchraster)(Framebufctl*, char*);
-	Framebuf *(*getfb)(Framebufctl*);
-	Framebuf *(*getbb)(Framebufctl*);
+	void		(*draw)(Framebufctl*, Image*, char*, Point, Point, Viewdrawctx*);
+	void		(*memdraw)(Framebufctl*, Memimage*, char*, Point, Point, Viewdrawctx*);
+	void		(*swap)(Framebufctl*);
+	void		(*reset)(Framebufctl*);
+	void		(*createraster)(Framebufctl*, char*, ulong);
+	Raster*		(*fetchraster)(Framebufctl*, char*);
+	Framebuf*	(*getfb)(Framebufctl*);
+	Framebuf*	(*getbb)(Framebufctl*);
 };
 
 struct Viewdrawctx
 {
 	/* draw */
-	Image *img;
+	Image		*img;
 	/* upscaled (mem)?draw */
-	ulong *blk;	/* upscaled scanline */
-	Rectangle blkr;
+	ulong		*blk;	/* upscaled scanline */
+	Rectangle	blkr;
 };
 
 struct Viewport
 {
 	RFrame;
-	Framebufctl *fbctl;
-	Rectangle r;
-	Viewdrawctx dctx;
+	Framebufctl	*fbctl;
+	Rectangle	r;
+	Viewdrawctx	dctx;
 
 	struct {
-		uvlong min, avg, max, acc, n, v;
-		uvlong nframes;
+		uvlong	min, avg, max, acc, n, v;
+		uvlong	nframes;
 	} stats;
 
-	void (*draw)(Viewport*, Image*, char*);
-	void (*memdraw)(Viewport*, Memimage*, char*);
-	void (*setscale)(Viewport*, double, double);
-	void (*setscalefilter)(Viewport*, int);
-	Framebuf *(*getfb)(Viewport*);
-	int (*getwidth)(Viewport*);
-	int (*getheight)(Viewport*);
-	void (*createraster)(Viewport*, char*, ulong);
-	Raster *(*fetchraster)(Viewport*, char*);
+	void		(*draw)(Viewport*, Image*, char*);
+	void		(*memdraw)(Viewport*, Memimage*, char*);
+	void		(*setscale)(Viewport*, double, double);
+	void		(*setscalefilter)(Viewport*, int);
+	Framebuf*	(*getfb)(Viewport*);
+	int		(*getwidth)(Viewport*);
+	int		(*getheight)(Viewport*);
+	void		(*createraster)(Viewport*, char*, ulong);
+	Raster*		(*fetchraster)(Viewport*, char*);
 };
 
 struct Camera
 {
-	RFrame3;		/* VCS */
-	Viewport *view;
-	Scene *scene;
-	Renderer *rctl;
-	double fov;		/* vertical FOV */
-	struct {
-		double n, f;	/* near and far clipping planes */
-	} clip;
-	Projection projtype;
-	Matrix3 proj;		/* VCS to clip space xform */
-	Matrix3 invproj;	/* clip space to VCS xform */
-	int cullmode;
-	uint rendopts;
+	RFrame3;			/* VCS */
+	Viewport	*view;
+	Scene		*scene;
+	Renderer	*rctl;
+	double		fov;		/* vertical FOV */
+	double		znear;
+	double		zfar;
+	int		cullmode;
+	int		rendopts;
+	int		projtype;
+	Matrix3		proj;		/* VCS to clip space xform */
+	Matrix3		invproj;	/* clip space to VCS xform */
 
 	struct {
-		uvlong min, avg, max, acc, n, v;
-		uvlong nframes;
+		uvlong	min, avg, max, acc, n, v;
+		uvlong	nframes;
 	} stats;
 };
 
+extern Rectangle	UR;	/* unit rectangle */
+
 /* camera */
-Camera *Camv(Viewport*, Renderer*, Projection, double, double, double);
-Camera *Cam(Rectangle, Renderer*, Projection, double, double, double);
-Camera *newcamera(void);
-void delcamera(Camera*);
-void reloadcamera(Camera*);
-void configcamera(Camera*, Projection, double, double, double);
-void placecamera(Camera*, Scene*, Point3, Point3, Point3);
-void movecamera(Camera*, Point3);
-void rotatecamera(Camera*, Point3, double);
-void aimcamera(Camera*, Point3);
-void shootcamera(Camera*, Shadertab*);
+Camera*	Camv(Viewport*, Renderer*, int, double, double, double);
+Camera*	Cam(Rectangle, Renderer*, int, double, double, double);
+Camera*	newcamera(void);
+void	delcamera(Camera*);
+void	reloadcamera(Camera*);
+void	configcamera(Camera*, int, double, double, double);
+void	placecamera(Camera*, Scene*, Point3, Point3, Point3);
+void	movecamera(Camera*, Point3);
+void	rotatecamera(Camera*, Point3, double);
+void	aimcamera(Camera*, Point3);
+void	shootcamera(Camera*, Shadertab*);
 
 /* viewport */
-Viewport *mkviewport(Rectangle);
-void rmviewport(Viewport*);
+Viewport*	mkviewport(Rectangle);
+void		rmviewport(Viewport*);
 
 /* render */
-Renderer *initgraphics(void);
-void setuniform(Shadertab*, char*, int, void*);
+Renderer*	initgraphics(void);
+void		setuniform(Shadertab*, char*, int, void*);
 
 /* xform */
-Point3 model2world(Entity*, Point3);
-Point3 world2vcs(Camera*, Point3);
-Point3 vcs2clip(Camera*, Point3);
-Point3 world2clip(Camera*, Point3);
-Point3 clip2ndc(Point3);
-Point3 ndc2viewport(Framebuf*, Point3);
-Point3 viewport2ndc(Framebuf*, Point3);
-Point3 ndc2vcs(Camera*, Point3);
-Point3 viewport2vcs(Camera*, Point3);
-Point3 vcs2world(Camera*, Point3);
-Point3 viewport2world(Camera*, Point3);
-Point3 world2model(Entity*, Point3);
-void perspective(Matrix3, double, double, double, double);
-void orthographic(Matrix3, double, double, double, double, double, double);
+Point3	model2world(Entity*, Point3);
+Point3	world2vcs(Camera*, Point3);
+Point3	vcs2clip(Camera*, Point3);
+Point3	world2clip(Camera*, Point3);
+Point3	clip2ndc(Point3);
+Point3	ndc2viewport(Framebuf*, Point3);
+Point3	viewport2ndc(Framebuf*, Point3);
+Point3	ndc2vcs(Camera*, Point3);
+Point3	viewport2vcs(Camera*, Point3);
+Point3	vcs2world(Camera*, Point3);
+Point3	viewport2world(Camera*, Point3);
+Point3	world2model(Entity*, Point3);
+void	perspective(Matrix3, double, double, double, double);
+void	orthographic(Matrix3, double, double, double, double, double, double);
 
 /* marshal */
-Model *readmodel(int);
-usize writemodel(int, Model*);
-int exportmodel(char*, Model*);
+Model*	readmodel(int);
+usize	writemodel(int, Model*);
+int	exportmodel(char*, Model*);
 
 /* scene */
-LightSource *newpointlight(Point3, Color);
-LightSource *newdireclight(Point3, Point3, Color);
-LightSource *newspotlight(Point3, Point3, Color, double, double);
-LightSource *duplight(LightSource*);
-void dellight(LightSource*);
-Entity *newentity(char*, Model*);
-Entity *dupentity(Entity*);
-void delentity(Entity*);
-Scene *newscene(char*);
-Scene *dupscene(Scene*);
-void delscene(Scene*);
-void clearscene(Scene*);
+LightSource*	newpointlight(Point3, Color);
+LightSource*	newdireclight(Point3, Point3, Color);
+LightSource*	newspotlight(Point3, Point3, Color, double, double);
+LightSource*	duplight(LightSource*);
+void		dellight(LightSource*);
+Entity*		newentity(char*, Model*);
+Entity*		dupentity(Entity*);
+void		delentity(Entity*);
+Scene*		newscene(char*);
+Scene*		dupscene(Scene*);
+void		delscene(Scene*);
+void		clearscene(Scene*);
 
 /* model */
-Vertex mkvert(void);
-Primitive mkprim(int);
-Material *newmaterial(char*);
-void delmaterial(Material*);
-Model *newmodel(void);
-Model *dupmodel(Model*);
-void delmodel(Model*);
-void compactmodel(Model*);
+Vertex		mkvert(void);
+Primitive	mkprim(int);
+Material*	newmaterial(char*);
+void		delmaterial(Material*);
+Model*		newmodel(void);
+Model*		dupmodel(Model*);
+void		delmodel(Model*);
+void		compactmodel(Model*);
 
 /* texture */
-Texture *alloctexture(int, Memimage*);
-Texture *duptexture(Texture*);
-void freetexture(Texture*);
-Color neartexsampler(Texture*, Point2);
-Color bilitexsampler(Texture*, Point2);
-Color sampletexture(Texture*, Point2, Color(*)(Texture*, Point2));
-Cubemap *readcubemap(char*[6]);
-Cubemap *dupcubemap(Cubemap*);
-void freecubemap(Cubemap*);
-Color samplecubemap(Cubemap*, Point3, Color(*)(Texture*, Point2));
+Texture*	alloctexture(int, Memimage*);
+Texture*	duptexture(Texture*);
+void		freetexture(Texture*);
+Color		neartexsampler(Texture*, Point2);
+Color		bilitexsampler(Texture*, Point2);
+Color		sampletexture(Texture*, Point2, Color(*)(Texture*, Point2));
+Cubemap*	readcubemap(char*[6]);
+Cubemap*	dupcubemap(Cubemap*);
+void		freecubemap(Cubemap*);
+Color		samplecubemap(Cubemap*, Point3, Color(*)(Texture*, Point2));
 
 /* util */
-Point minpt(Point, Point);
-Point maxpt(Point, Point);
-Point2 modulapt2(Point2, Point2);
-Point2 minpt2(Point2, Point2);
-Point2 maxpt2(Point2, Point2);
-int eqpt2(Point2, Point2);
-Point3 modulapt3(Point3, Point3);
-Point3 minpt3(Point3, Point3);
-Point3 maxpt3(Point3, Point3);
-int eqpt3(Point3, Point3);
-Memimage *rgba(ulong);
-Memimage *dupmemimage(Memimage*);
+Point		minpt(Point, Point);
+Point		maxpt(Point, Point);
+Point2		modulapt2(Point2, Point2);
+Point2		minpt2(Point2, Point2);
+Point2		maxpt2(Point2, Point2);
+int		eqpt2(Point2, Point2);
+Point3		modulapt3(Point3, Point3);
+Point3		minpt3(Point3, Point3);
+Point3		maxpt3(Point3, Point3);
+int		eqpt3(Point3, Point3);
+Memimage*	rgba(ulong);
+Memimage*	dupmemimage(Memimage*);
 
 /* itemarray */
-ItemArray *mkitemarray(usize);
-usize itemarrayadd(ItemArray*, void*);
-void *itemarrayget(ItemArray*, usize);
-usize copyitemarray(ItemArray*, ItemArray*);
-ItemArray *dupitemarray(ItemArray*);
-void rmitemarray(ItemArray*);
+ItemArray*	mkitemarray(usize);
+usize		itemarrayadd(ItemArray*, void*);
+void*		itemarrayget(ItemArray*, usize);
+usize		copyitemarray(ItemArray*, ItemArray*);
+ItemArray*	dupitemarray(ItemArray*);
+void		rmitemarray(ItemArray*);
 
 /* color */
-ulong col2ul(Color);
-Color ul2col(ulong);
-int hasalpha(ulong);
-Color srgb2linear(Color);
-Color linear2srgb(Color);
-ulong srgb2linearul(ulong);
-ulong linear2srgbul(ulong);
-Color aces(Color);
-Color aces2(Color);
+ulong	col2ul(Color);
+Color	ul2col(ulong);
+int	hasalpha(ulong);
+Color	srgb2linear(Color);
+Color	linear2srgb(Color);
+ulong	srgb2linearul(ulong);
+ulong	linear2srgbul(ulong);
+Color	aces(Color);
+Color	aces2(Color);
 
 /* shadeop */
-double sign(double);
-double step(double, double);
-double smoothstep(double, double, double);
-Color getlightcolor(LightSource*, Point3, Point3);
-Color getscenecolor(Scene*, Point3, Point3);
+double	sign(double);
+double	step(double, double);
+double	smoothstep(double, double, double);
+Color	getlightcolor(LightSource*, Point3, Point3);
+Color	getscenecolor(Scene*, Point3, Point3);
 
 /* nanosec */
-uvlong nanosec(void);
-
-extern Rectangle UR;	/* unit rectangle */
+uvlong	nanosec(void);
--- a/internal.h
+++ b/internal.h
@@ -3,46 +3,46 @@
 	ε2 = 1e-6,
 };
 
-typedef struct BPrimitive BPrimitive;
-typedef struct Polygon Polygon;
-typedef struct Commontask Commontask;
-typedef struct Entityparam Entityparam;
-typedef struct Entitytask Entitytask;
-typedef struct Tilerparam Tilerparam;
-typedef struct Tilertask Tilertask;
-typedef struct Rasterparam Rasterparam;
-typedef struct Rastertask Rastertask;
-typedef struct fGradient fGradient;
-typedef struct pGradient pGradient;
-typedef struct vGradient vGradient;
-typedef struct Gradients Gradients;
+typedef struct BPrimitive	BPrimitive;
+typedef struct Polygon		Polygon;
+typedef struct Commontask	Commontask;
+typedef struct Entityparam	Entityparam;
+typedef struct Entitytask	Entitytask;
+typedef struct Tilerparam	Tilerparam;
+typedef struct Tilertask	Tilertask;
+typedef struct Rasterparam	Rasterparam;
+typedef struct Rastertask	Rastertask;
+typedef struct fGradient	fGradient;
+typedef struct pGradient	pGradient;
+typedef struct vGradient	vGradient;
+typedef struct Gradients	Gradients;
 
 struct BPrimitive
 {
-	int type;
-	BVertex v[3];
-	Point3 tangent;		/* used for normal mapping */
-	Material *mtl;
+	int		type;
+	BVertex		v[3];
+	Point3		tangent;	/* used for normal mapping */
+	Material	*mtl;
 };
 
 struct Polygon
 {
-	BVertex v[8];
-	ulong n;
+	BVertex		v[8];
+	ulong		n;
 };
 
 /* common task params */
 struct Commontask
 {
-	Renderjob *job;
-	Entity *entity;
-	int islast;
+	Renderjob	*job;
+	Entity		*entity;
+	int		islast;
 };
 
 struct Entityparam
 {
-	Renderer *rctl;
-	Channel *taskc;
+	Renderer	*rctl;
+	Channel		*taskc;
 };
 
 struct Entitytask
@@ -52,105 +52,105 @@
 
 struct Tilerparam
 {
-	int id;
-	Channel *taskc;
-	Channel **taskchans;	/* Channel*[nproc] */
-	Rectangle *wr;		/* Rectangle[nproc] */
-	ulong nproc;
+	int		id;
+	Channel		*taskc;
+	Channel		**taskchans;	/* Channel*[nproc] */
+	Rectangle	*wr;		/* Rectangle[nproc] */
+	ulong		nproc;
 };
 
 struct Tilertask
 {
 	Commontask;
-	Primitive *eb, *ee;
+	Primitive	*eb, *ee;
 };
 
 struct Rasterparam
 {
-	int id;
-	Channel *taskc;
+	int		id;
+	Channel		*taskc;
 };
 
 struct Rastertask
 {
 	Commontask;
-	Shaderparams *fsp;
-	Rectangle wr;		/* working rect */
-	Rectangle *clipr;
-	BPrimitive p;
+	Shaderparams	*fsp;
+	Rectangle	wr;		/* working rect */
+	Rectangle	*clipr;
+	BPrimitive	p;
 };
 
 struct fGradient
 {
-	double f0;
-	double dx;
-	double dy;
+	double	f0;
+	double	dx;
+	double	dy;
 };
 
 struct pGradient
 {
-	Point3 p0;
-	Point3 dx;
-	Point3 dy;
+	Point3	p0;
+	Point3	dx;
+	Point3	dy;
 };
 
 struct vGradient
 {
-	BVertex v0;
-	BVertex dx;
-	BVertex dy;
+	BVertex	v0;
+	BVertex	dx;
+	BVertex	dy;
 };
 
 struct Gradients
 {
-	vGradient v;
-	pGradient bc;
-	fGradient z;
-	fGradient pcz;
+	vGradient	v;
+	pGradient	bc;
+	fGradient	z;
+	fGradient	pcz;
 };
 
 /* alloc */
-void *_emalloc(ulong);
-void *_erealloc(void*, ulong);
-char *_estrdup(char*);
-char *_equotestrdup(char*);
-Memimage *_eallocmemimage(Rectangle, ulong);
+void*		_emalloc(ulong);
+void*		_erealloc(void*, ulong);
+char*		_estrdup(char*);
+char*		_equotestrdup(char*);
+Memimage*	_eallocmemimage(Rectangle, ulong);
 
 /* raster */
-Raster *_allocraster(char*, Rectangle, ulong);
-void _clearraster(Raster*, ulong);
-void _fclearraster(Raster*, float);
-uchar *_rasterbyteaddr(Raster*, Point);
-void _rasterput(Raster*, Point, void*);
-void _rasterget(Raster*, Point, void*);
-void _rasterputcolor(Raster*, Point, ulong);
-ulong _rastergetcolor(Raster*, Point);
-void _rasterputfloat(Raster*, Point, float);
-float _rastergetfloat(Raster*, Point);
-void _freeraster(Raster*);
+Raster*	_allocraster(char*, Rectangle, ulong);
+void	_clearraster(Raster*, ulong);
+void	_fclearraster(Raster*, float);
+uchar*	_rasterbyteaddr(Raster*, Point);
+void	_rasterput(Raster*, Point, void*);
+void	_rasterget(Raster*, Point, void*);
+void	_rasterputcolor(Raster*, Point, ulong);
+ulong	_rastergetcolor(Raster*, Point);
+void	_rasterputfloat(Raster*, Point, float);
+float	_rastergetfloat(Raster*, Point);
+void	_freeraster(Raster*);
 
 /* fb */
-Framebuf *_mkfb(Rectangle);
-void _rmfb(Framebuf*);
-Framebufctl *_mkfbctl(Rectangle);
-void _rmfbctl(Framebufctl*);
+Framebuf*	_mkfb(Rectangle);
+void		_rmfb(Framebuf*);
+Framebufctl*	_mkfbctl(Rectangle);
+void		_rmfbctl(Framebufctl*);
 
 /* vertex */
-void _lerpvertex(BVertex*, BVertex*, BVertex*, double);
-void _berpvertex(BVertex*, BVertex*, BVertex*, BVertex*, Point3);
-void _addvertex(BVertex*, BVertex*);
-void _mulvertex(BVertex*, double);
-void _fprintvattrs(int, Vertexattrs*);
-void _addvattr(Vertexattrs*, char*, int, void*);
-Vertexattr *_getvattr(Vertexattrs*, char*);
+void		_lerpvertex(BVertex*, BVertex*, BVertex*, double);
+void		_berpvertex(BVertex*, BVertex*, BVertex*, BVertex*, Point3);
+void		_addvertex(BVertex*, BVertex*);
+void		_mulvertex(BVertex*, double);
+void		_fprintvattrs(int, Vertexattrs*);
+void		_addvattr(Vertexattrs*, char*, int, void*);
+Vertexattr*	_getvattr(Vertexattrs*, char*);
 
 /* clip */
-int _clipprimitive(BPrimitive*, BPrimitive*);
-void _adjustlineverts(Point*, Point*, BVertex*, BVertex*);
-int _rectclipline(Rectangle, Point*, Point*);
+int	_clipprimitive(BPrimitive*, BPrimitive*);
+void	_adjustlineverts(Point*, Point*, BVertex*, BVertex*);
+int	_rectclipline(Rectangle, Point*, Point*);
 
 /* util */
-void _memsetl(void*, ulong, usize);
+void	_memsetl(void*, ulong, usize);
 
 #define getpixel(fb, p)		_rastergetcolor(fb, p)
 #define putpixel(fb, p, c)	_rasterputcolor(fb, p, c)
--