#ifndef lint
static char *sccsid = "@(#)transform.c	1.2 (UKC) 9/12/87";
#endif  lint
/*
 *	Perform viewing transformations on all point in the space.
 *
 *	The arguments tilt and rotate turn the object before you.
 *	You can also think of this as moving the viewpoint on fixed axes.
 *	It's the same thing.
 *
 *	tilt and rotate are specified in degrees.
 *	Positive tilt tips the object towards you (raises the eye)
 *	Positive rotate turns the object clockwise, or moves the eye
 *		towards the X-axis.
 */

#define PI 3.141592653589793

#define EYEDIST (5.0)		/* distance from eye to origin */

#include <math.h>
#include "types.h"

static double sintilt, costilt;	/* precomputed for tilting */
static double sinrotate, cosrotate;	/* precomputed for tilting */

inittrans(tilt, rotate)
int rotate;		/* how much to rotate by */
int tilt;		/* how much to tip towards you in degrees */
{
	sintilt = sin(tilt * PI / 180);
	costilt = cos(tilt * PI / 180);

	sinrotate = sin(rotate * PI / 180);
	cosrotate = cos(rotate * PI / 180);
}

#if 0
transform()
{
	trilist_t *tp;
	linelist_t *lp;

	for (lp = linelist; lp != NULLLINE; lp=lp->link) {
		dotrans(&lp->P);
		dotrans(&lp->Q);
	}
	for (tp = trilist; tp != NULLTRI; tp = tp->link) {
		dotrans(&tp->A);
		dotrans(&tp->B);
		dotrans(&tp->C);
	}
}
#endif

dotrans(v)
vertex_t *v;
{
	double newx, newy, newz; /* each depends on the old other */

	/* rotate the object clockwise on the XZ plane */
	newx = v->x * cosrotate - v->z * sinrotate;
	newz = v->z * cosrotate + v->x * sinrotate;
	v->x = newx; v->z = newz;

	/* tilt towards you */
	newz = v->z * costilt - v->y * sintilt;
	newy = v->y * costilt + v->z * sintilt;
	v->z = newz; v->y = newy;

	/* perspective transformation */
	v->x = (v->x * (EYEDIST-1.01)) / (EYEDIST + v->z);
	v->y = (v->y * (EYEDIST-1.01)) / (EYEDIST + v->z);
}
