2 * These transformation routines maintain stacks of transformations
4 * t=pushmat(t) push matrix stack
5 * t=popmat(t) pop matrix stack
6 * rot(t, a, axis) multiply stack top by rotation
7 * qrot(t, q) multiply stack top by rotation, q is unit quaternion
8 * scale(t, x, y, z) multiply stack top by scale
9 * move(t, x, y, z) multiply stack top by translation
10 * xform(t, m) multiply stack top by m
11 * ixform(t, m, inv) multiply stack top by m. inv is the inverse of m.
12 * look(t, e, l, u) multiply stack top by viewing transformation
13 * persp(t, fov, n, f) multiply stack top by perspective transformation
14 * viewport(t, r, aspect)
15 * multiply stack top by window->viewport transformation.
21 Space *pushmat(Space *t){
23 v=malloc(sizeof(Space));
33 Space *popmat(Space *t){
40 void rot(Space *t, double theta, int axis){
41 double s=sin(radians(theta)), c=cos(radians(theta));
43 int i=(axis+1)%3, j=(axis+2)%3;
56 void qrot(Space *t, Quaternion q){
60 for(i=0;i!=4;i++) for(j=0;j!=4;j++) inv[i][j]=m[j][i];
63 void scale(Space *t, double x, double y, double z){
75 void move(Space *t, double x, double y, double z){
87 void xform(Space *t, Matrix m){
89 if(invertmat(m, inv)==0) return;
92 void ixform(Space *t, Matrix m, Matrix inv){
94 matmulr(t->tinv, inv);
97 * multiply the top of the matrix stack by a view-pointing transformation
98 * with the eyepoint at e, looking at point l, with u at the top of the screen.
99 * The coordinate system is deemed to be right-handed.
100 * The generated transformation transforms this view into a view from
101 * the origin, looking in the positive y direction, with the z axis pointing up,
102 * and x to the right.
104 void look(Space *t, Point3 e, Point3 l, Point3 u){
108 u=unit3(vrem3(sub3(u, e), l));
110 /* make the matrix to transform from (rlu) space to (xyz) space */
112 m[0][0]=r.x; m[0][1]=r.y; m[0][2]=r.z;
113 m[1][0]=l.x; m[1][1]=l.y; m[1][2]=l.z;
114 m[2][0]=u.x; m[2][1]=u.y; m[2][2]=u.z;
116 inv[0][0]=r.x; inv[0][1]=l.x; inv[0][2]=u.x;
117 inv[1][0]=r.y; inv[1][1]=l.y; inv[1][2]=u.y;
118 inv[2][0]=r.z; inv[2][1]=l.z; inv[2][2]=u.z;
120 move(t, -e.x, -e.y, -e.z);
123 * generate a transformation that maps the frustum with apex at the origin,
124 * apex angle=fov and clipping planes y=n and y=f into the double-unit cube.
125 * plane y=n maps to y'=-1, y=f maps to y'=1
127 int persp(Space *t, double fov, double n, double f){
130 if(n<=0 || f<=n || fov<=0 || 180<=fov) /* really need f!=n && sin(v)!=0 */
132 z=1/tan(radians(fov)/2);
133 m[0][0]=z; m[0][1]=0; m[0][2]=0; m[0][3]=0;
134 m[1][0]=0; m[1][1]=(f+n)/(f-n); m[1][2]=0; m[1][3]=f*(1-m[1][1]);
135 m[2][0]=0; m[2][1]=0; m[2][2]=z; m[2][3]=0;
136 m[3][0]=0; m[3][1]=1; m[3][2]=0; m[3][3]=0;
141 * Map the unit-cube window into the given screen viewport.
142 * r has min at the top left, max just outside the lower right. Aspect is the
143 * aspect ratio (dx/dy) of the viewport's pixels (not of the whole viewport!)
144 * The whole window is transformed to fit centered inside the viewport with equal
145 * slop on either top and bottom or left and right, depending on the viewport's
147 * The window is viewed down the y axis, with x to the left and z up. The viewport
148 * has x increasing to the right and y increasing down. The window's y coordinates
149 * are mapped, unchanged, into the viewport's z coordinates.
151 void viewport(Space *t, Rectangle r, double aspect){
153 double xc, yc, wid, hgt, scale;
154 xc=.5*(r.min.x+r.max.x);
155 yc=.5*(r.min.y+r.max.y);
156 wid=(r.max.x-r.min.x)*aspect;
158 scale=.5*(wid<hgt?wid:hgt);
167 /* should get inverse by hand */