tstack.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*% cc -gpc %
  10. * These transformation routines maintain stacks of transformations
  11. * and their inverses.
  12. * t=pushmat(t) push matrix stack
  13. * t=popmat(t) pop matrix stack
  14. * rot(t, a, axis) multiply stack top by rotation
  15. * qrot(t, q) multiply stack top by rotation, q is unit quaternion
  16. * scale(t, x, y, z) multiply stack top by scale
  17. * move(t, x, y, z) multiply stack top by translation
  18. * xform(t, m) multiply stack top by m
  19. * ixform(t, m, inv) multiply stack top by m. inv is the inverse of m.
  20. * look(t, e, l, u) multiply stack top by viewing transformation
  21. * persp(t, fov, n, f) multiply stack top by perspective transformation
  22. * viewport(t, r, aspect)
  23. * multiply stack top by window->viewport transformation.
  24. */
  25. #include <u.h>
  26. #include <libc.h>
  27. #include <draw.h>
  28. #include <geometry.h>
  29. Space *pushmat(Space *t){
  30. Space *v;
  31. v=malloc(sizeof(Space));
  32. if(t==0){
  33. ident(v->t);
  34. ident(v->tinv);
  35. }
  36. else
  37. *v=*t;
  38. v->next=t;
  39. return v;
  40. }
  41. Space *popmat(Space *t){
  42. Space *v;
  43. if(t==0) return 0;
  44. v=t->next;
  45. free(t);
  46. return v;
  47. }
  48. void rot(Space *t, double theta, int axis){
  49. double s=sin(radians(theta)), c=cos(radians(theta));
  50. Matrix m, inv;
  51. register i=(axis+1)%3, j=(axis+2)%3;
  52. ident(m);
  53. m[i][i] = c;
  54. m[i][j] = -s;
  55. m[j][i] = s;
  56. m[j][j] = c;
  57. ident(inv);
  58. inv[i][i] = c;
  59. inv[i][j] = s;
  60. inv[j][i] = -s;
  61. inv[j][j] = c;
  62. ixform(t, m, inv);
  63. }
  64. void qrot(Space *t, Quaternion q){
  65. Matrix m, inv;
  66. int i, j;
  67. qtom(m, q);
  68. for(i=0;i!=4;i++) for(j=0;j!=4;j++) inv[i][j]=m[j][i];
  69. ixform(t, m, inv);
  70. }
  71. void scale(Space *t, double x, double y, double z){
  72. Matrix m, inv;
  73. ident(m);
  74. m[0][0]=x;
  75. m[1][1]=y;
  76. m[2][2]=z;
  77. ident(inv);
  78. inv[0][0]=1/x;
  79. inv[1][1]=1/y;
  80. inv[2][2]=1/z;
  81. ixform(t, m, inv);
  82. }
  83. void move(Space *t, double x, double y, double z){
  84. Matrix m, inv;
  85. ident(m);
  86. m[0][3]=x;
  87. m[1][3]=y;
  88. m[2][3]=z;
  89. ident(inv);
  90. inv[0][3]=-x;
  91. inv[1][3]=-y;
  92. inv[2][3]=-z;
  93. ixform(t, m, inv);
  94. }
  95. void xform(Space *t, Matrix m){
  96. Matrix inv;
  97. if(invertmat(m, inv)==0) return;
  98. ixform(t, m, inv);
  99. }
  100. void ixform(Space *t, Matrix m, Matrix inv){
  101. matmul(t->t, m);
  102. matmulr(t->tinv, inv);
  103. }
  104. /*
  105. * multiply the top of the matrix stack by a view-pointing transformation
  106. * with the eyepoint at e, looking at point l, with u at the top of the screen.
  107. * The coordinate system is deemed to be right-handed.
  108. * The generated transformation transforms this view into a view from
  109. * the origin, looking in the positive y direction, with the z axis pointing up,
  110. * and x to the right.
  111. */
  112. void look(Space *t, Point3 e, Point3 l, Point3 u){
  113. Matrix m, inv;
  114. Point3 r;
  115. l=unit3(sub3(l, e));
  116. u=unit3(vrem3(sub3(u, e), l));
  117. r=cross3(l, u);
  118. /* make the matrix to transform from (rlu) space to (xyz) space */
  119. ident(m);
  120. m[0][0]=r.x; m[0][1]=r.y; m[0][2]=r.z;
  121. m[1][0]=l.x; m[1][1]=l.y; m[1][2]=l.z;
  122. m[2][0]=u.x; m[2][1]=u.y; m[2][2]=u.z;
  123. ident(inv);
  124. inv[0][0]=r.x; inv[0][1]=l.x; inv[0][2]=u.x;
  125. inv[1][0]=r.y; inv[1][1]=l.y; inv[1][2]=u.y;
  126. inv[2][0]=r.z; inv[2][1]=l.z; inv[2][2]=u.z;
  127. ixform(t, m, inv);
  128. move(t, -e.x, -e.y, -e.z);
  129. }
  130. /*
  131. * generate a transformation that maps the frustum with apex at the origin,
  132. * apex angle=fov and clipping planes y=n and y=f into the double-unit cube.
  133. * plane y=n maps to y'=-1, y=f maps to y'=1
  134. */
  135. int persp(Space *t, double fov, double n, double f){
  136. Matrix m;
  137. double z;
  138. if(n<=0 || f<=n || fov<=0 || 180<=fov) /* really need f!=n && sin(v)!=0 */
  139. return -1;
  140. z=1/tan(radians(fov)/2);
  141. m[0][0]=z; m[0][1]=0; m[0][2]=0; m[0][3]=0;
  142. m[1][0]=0; m[1][1]=(f+n)/(f-n); m[1][2]=0; m[1][3]=f*(1-m[1][1]);
  143. m[2][0]=0; m[2][1]=0; m[2][2]=z; m[2][3]=0;
  144. m[3][0]=0; m[3][1]=1; m[3][2]=0; m[3][3]=0;
  145. xform(t, m);
  146. return 0;
  147. }
  148. /*
  149. * Map the unit-cube window into the given screen viewport.
  150. * r has min at the top left, max just outside the lower right. Aspect is the
  151. * aspect ratio (dx/dy) of the viewport's pixels (not of the whole viewport!)
  152. * The whole window is transformed to fit centered inside the viewport with equal
  153. * slop on either top and bottom or left and right, depending on the viewport's
  154. * aspect ratio.
  155. * The window is viewed down the y axis, with x to the left and z up. The viewport
  156. * has x increasing to the right and y increasing down. The window's y coordinates
  157. * are mapped, unchanged, into the viewport's z coordinates.
  158. */
  159. void viewport(Space *t, Rectangle r, double aspect){
  160. Matrix m;
  161. double xc, yc, wid, hgt, scale;
  162. xc=.5*(r.min.x+r.max.x);
  163. yc=.5*(r.min.y+r.max.y);
  164. wid=(r.max.x-r.min.x)*aspect;
  165. hgt=r.max.y-r.min.y;
  166. scale=.5*(wid<hgt?wid:hgt);
  167. ident(m);
  168. m[0][0]=scale;
  169. m[0][3]=xc;
  170. m[1][1]=0;
  171. m[1][2]=-scale;
  172. m[1][3]=yc;
  173. m[2][1]=1;
  174. m[2][2]=0;
  175. /* should get inverse by hand */
  176. xform(t, m);
  177. }