home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / StereoScope-3.2 / PerspMat.m < prev    next >
Encoding:
Text File  |  1992-04-01  |  4.9 KB  |  205 lines

  1. #import "PerspMat.h"
  2. #import "math.h"
  3.  
  4. @implementation PerspMat
  5.  
  6. +new
  7. {
  8.   register float *fpt;
  9.   register short int n = 16;
  10.  
  11.   self = [super new];
  12.   fpt = mat; while(n--) *fpt++ = 0.;
  13.   mat[0] = mat[5] = mat[10] = mat[15] = 1.;
  14.   return self;
  15. }
  16.  
  17. +newCenter:(float)cx :(float)cy :(float)cz
  18. {
  19.   register float *fpt;
  20.   register short int n = 16;
  21.  
  22.   self = [super new];
  23.   fpt = mat; while(n--) *fpt++ = 0.;
  24.   mat[0] = mat[5] = mat[10] = mat[15] = 1.;
  25.   mat[3] = -cx; mat[7] = -cy; mat[11] = -cz;
  26.   return self;
  27. }
  28.  
  29. -reset
  30. {
  31.   register float *fpt = mat;
  32.   register short int n = 16;
  33.  
  34.   while(n--) *fpt++ = 0.;
  35.   mat[0] = mat[5] = mat[10] = mat[15] = 1.;
  36.   return self;
  37. }
  38.  
  39. -(float **)render:(int)n :(float *)x :(float *)y :(float *)z :(float **)results
  40. {
  41.   float h, *u = results[0], *v = results[1], *w = results[2];
  42.   while (n--) {
  43.     h = mat[12] * *x + mat[13] * *y + mat[14] * *z + mat[15];
  44.     *u++ = (mat[0] * *x + mat[1] * *y + mat[2] * *z + mat[3])/h;
  45.     *v++ = (mat[4] * *x + mat[5] * *y + mat[6] * *z + mat[7])/h;
  46.     *w++ = (mat[8] * *x++ + mat[9] * *y++ + mat[10] * *z++ + mat[11])/h;
  47.   }
  48.   return results;
  49. }
  50.  
  51. -(float *)as_DPSpath:(int)n :(float *)x :(float *)y :(float *)z :(float *)results
  52. {
  53.   float h, *res = results;
  54.   while (n--) {
  55.     h = mat[12] * *x + mat[13] * *y + mat[14] * *z + mat[15];
  56.     *res++ = (mat[0] * *x + mat[1] * *y + mat[2] * *z + mat[3])/h;
  57.     *res++ = (mat[4] * *x++ + mat[5] * *y++ + mat[6] * *z++ + mat[7])/h;
  58.   }
  59.   return results;
  60. }
  61.  
  62. -(float *)as_DPSpath:(int)n :(float *)x :(float *)y :(float *)z
  63.   :(float *)results offset:(float *)o_set
  64. {
  65.   float h, *res = results;
  66.   while (n--) {
  67.     h = mat[12] * *x + mat[13] * *y + mat[14] * *z + mat[15];
  68.     *res++ = (mat[0] * *x + mat[1] * *y + mat[2] * *z + mat[3])/h - o_set[0];
  69.     *res++ = (mat[4] * *x++ + mat[5] * *y++ + mat[6] * *z++ +
  70.           mat[7])/h - o_set[1];
  71.   }
  72.   return results;
  73. }
  74.  
  75. -translate:(float)dx :(float)dy :(float)dz
  76. {
  77.   register float *thisrow = mat, *lastrow = mat + 12;
  78.   register short int n = 4;
  79.  
  80.   while (n--) *thisrow++ += dx * *lastrow++;
  81.   n = 4; lastrow -= 4; while (n--) *thisrow++ += dy * *lastrow++;
  82.   n = 4; lastrow -= 4; while (n--) *thisrow++ += dz * *lastrow++;
  83.   return self;
  84. }
  85.  
  86. -scale:(float)sx :(float)sy :(float)sz
  87. {
  88.   register float *thisrow = mat;
  89.   register short int n = 4;
  90.  
  91.   while(n--) *thisrow++ *= sx;
  92.   n = 4; while (n--) *thisrow++ *= sy;
  93.   n = 4; while (n--) *thisrow++ *= sz;
  94.   return self;
  95. }
  96.  
  97. -x_rotation:(float)radians
  98. {
  99.   register float cos_angle = cos(radians), sin_angle = sin(radians), temp,
  100.     *thisrow = mat + 4, *otherrow = mat + 8;
  101.   register short int n = 4;
  102.  
  103.   while (n--) {
  104.     temp = cos_angle * *thisrow - sin_angle * *otherrow;
  105.     *otherrow++ = sin_angle * *thisrow + cos_angle * *otherrow;
  106.     *thisrow++ = temp;
  107.   }
  108.   return self;
  109. }
  110.  
  111. -x_rotation_cs:(float)cos_angle :(float)sin_angle
  112. {
  113.   register float temp, *thisrow = mat + 4, *otherrow = mat + 8;
  114.   register short int n = 4;
  115.  
  116.   while (n--) {
  117.     temp = cos_angle * *thisrow - sin_angle * *otherrow;
  118.     *otherrow++ = sin_angle * *thisrow + cos_angle * *otherrow;
  119.     *thisrow++ = temp;
  120.   }
  121.   return self;
  122. }
  123.  
  124. -y_rotation:(float)radians
  125. {
  126.   register float cos_angle = cos(radians), sin_angle = sin(radians), temp,
  127.     *thisrow = mat, *otherrow = mat + 8;
  128.   register short int n = 4;
  129.  
  130.   while (n--) {
  131.     temp = cos_angle * *thisrow + sin_angle * *otherrow;
  132.     *otherrow++ = -sin_angle * *thisrow + cos_angle * *otherrow;
  133.     *thisrow++ = temp;
  134.   }
  135.   return self;
  136. }
  137.  
  138. -y_rotation_cs:(float)cos_angle :(float)sin_angle
  139. {
  140.   register float temp, *thisrow = mat, *otherrow = mat + 8;
  141.   register short int n = 4;
  142.  
  143.   while (n--) {
  144.     temp = cos_angle * *thisrow + sin_angle * *otherrow;
  145.     *otherrow++ = -sin_angle * *thisrow + cos_angle * *otherrow;
  146.     *thisrow++ = temp;
  147.   }
  148.   return self;
  149. }
  150.  
  151. -z_rotation:(float)radians
  152. {
  153.   register float cos_angle = cos(radians), sin_angle = sin(radians), temp,
  154.     *thisrow = mat, *otherrow = mat + 4;
  155.   register short int n = 4;
  156.  
  157.   while (n--) {
  158.     temp = cos_angle * *thisrow - sin_angle * *otherrow;
  159.     *otherrow++ = sin_angle * *thisrow + cos_angle * *otherrow;
  160.     *thisrow++ = temp;
  161.   }
  162.   return self;
  163. }
  164.  
  165. -z_rotation_cs:(float)cos_angle :(float)sin_angle
  166. {
  167.   register float temp, *thisrow = mat, *otherrow = mat + 4;
  168.   register short int n = 4;
  169.  
  170.   while (n--) {
  171.     temp = cos_angle * *thisrow - sin_angle * *otherrow;
  172.     *otherrow++ = sin_angle * *thisrow + cos_angle * *otherrow;
  173.     *thisrow++ = temp;
  174.   }
  175.   return self;
  176. }
  177.  
  178. -perspective:(float)distance
  179. {
  180.   register float inv_d = 1/distance, *otherrow = mat + 8,
  181.     *thisrow = mat + 12;
  182.   register short int n = 4;
  183.  
  184.   while (n--) {
  185.     *thisrow++ -= inv_d * *otherrow;
  186.     *otherrow++ = 0.;
  187.   }
  188.   return self;
  189. }
  190.  
  191. -perspective_inv:(float)invdistance
  192. {
  193.   register float inv_d = invdistance, *otherrow = mat + 8,
  194.     *thisrow = mat + 12;
  195.   register short int n = 4;
  196.  
  197.   while (n--) {
  198.     *thisrow++ -= inv_d * *otherrow;
  199.     *otherrow++ = 0.;
  200.   }
  201.   return self;
  202. }
  203.  
  204. @end
  205.