home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / rayshade.lzh / raymath.c < prev    next >
Text File  |  1990-05-08  |  4KB  |  189 lines

  1. /*
  2.  * raymath.c
  3.  *
  4.  * Copyright (C) 1989, Craig E. Kolb
  5.  *
  6.  * This software may be freely copied, modified, and redistributed,
  7.  * provided that this copyright notice is preserved on all copies.
  8.  *
  9.  * There is no warranty or other guarantee of fitness for this software,
  10.  * it is provided solely .  Bug reports or fixes may be sent
  11.  * to the author, who may or may not act on them as he desires.
  12.  *
  13.  * You may not include this software in a program or other software product
  14.  * without supplying the source, or without informing the end-user that the
  15.  * source is available for no extra charge.
  16.  *
  17.  * If you modify this software, you should include a notice giving the
  18.  * name of the person performing the modification, the date of modification,
  19.  * and the reason for such modification.
  20.  *
  21.  * $Id: raymath.c,v 3.0.1.1 90/03/07 21:26:08 craig Exp $
  22.  *
  23.  * $Log:    raymath.c,v $
  24.  * Revision 3.0.1.1  90/03/07  21:26:08  craig
  25.  * patch4: Added VectCoordSys().
  26.  * 
  27.  * Revision 3.0  89/10/27  02:06:01  craig
  28.  * Baseline for first official release.
  29.  * 
  30.  */
  31. #include <math.h>
  32. #include "typedefs.h"
  33. #include "constants.h"
  34. #include "funcdefs.h"
  35.  
  36. /*
  37.  * Normalize a vector, return original length.
  38.  */
  39. double
  40. normalize(a)
  41. register Vector *a;
  42. {
  43.     double d;
  44.  
  45.     d = sqrt(a->x*a->x + a->y*a->y + a->z*a->z);
  46.     if(d == 0.)
  47.         return 0.;
  48.     a->x /= d;
  49.     a->y /= d;
  50.     a->z /= d;
  51.     return d;
  52. }
  53.  
  54. /*
  55.  * Compute cross-product of a and b, place normalized result in o.  Returns
  56.  * length of result before normalization.
  57.  */
  58. double
  59. crossp(o, a, b)
  60. Vector *o, *a, *b;
  61. {
  62.     rawcrossp(o, a, b);
  63.     return normalize(o);
  64. }
  65.  
  66. /*
  67.  * Compute cross-product of a and b, place result in o.
  68.  */
  69. rawcrossp(o, a, b)
  70. Vector *o, *a, *b;
  71. {
  72.     o->x = (a->y * b->z) - (a->z * b->y);
  73.     o->y = (a->z * b->x) - (a->x * b->z);
  74.     o->z = (a->x * b->y) - (a->y * b->x);
  75. }
  76.  
  77. /*
  78.  * Calculate direction of refracted ray using Heckbert's formula.  Returns TRUE
  79.  * if a total internal reflection occurs.
  80.  */
  81. refract(dir, from_index, to_index, I, N, cos1)
  82. double from_index, to_index, cos1;
  83. Vector *dir, I, N;
  84. {
  85.     double kn, cos2, k;
  86.  
  87.     if (cos1 < 0.) {
  88.         /*
  89.          * Hit the 'backside' of a surface -- flip the
  90.          * normal.
  91.          */
  92.         N.x = -N.x;
  93.         N.y = -N.y;
  94.         N.z = -N.z;
  95.         cos1 = -cos1;
  96.     }
  97.  
  98.     kn = from_index / to_index;
  99.     cos2 = 1. - kn*kn*(1. - cos1*cos1);
  100.     if (cos2 < 0.)
  101.         return TRUE;        /* Total internal reflection. */
  102.     k = kn * cos1 - sqrt(cos2);
  103.     veccomb(kn, I, k, N, dir);
  104.     return FALSE;
  105. }
  106.  
  107. /*
  108.  * Given a vector, find two additional vectors such that all three
  109.  * are mutually perpendicular and xaxis X yaxis = vector.  The given
  110.  * vector need not be normalized. xaxis and yaxis are normalized.
  111.  */
  112. VectCoordSys(vector, xaxis, yaxis)
  113. Vector *vector, *xaxis, *yaxis;
  114. {
  115.     xaxis->x = vector->y;
  116.     xaxis->y = -vector->x;
  117.     xaxis->z = 0.;
  118.     if (normalize(xaxis) == 0.) {
  119.         xaxis->x = 0.;
  120.         xaxis->y = -vector->z;
  121.         xaxis->z = vector->y;
  122.         if (normalize(xaxis) == 0.)
  123.             RSwarning("LightCoordSys: Can't find X axis!\n");
  124.         yaxis->x = (vector->y * vector->y) + (vector->z * vector->z);
  125.         yaxis->y = -vector->x * vector->y;
  126.         yaxis->z = -vector->x * vector->z;
  127.         (void)normalize(yaxis);
  128.     } else {
  129.         yaxis->x = vector->x * vector->z;
  130.         yaxis->y = vector->y * vector->z;
  131.         yaxis->z = -(vector->x * vector->x) -(vector->y * vector->y);
  132.         (void)normalize(yaxis);
  133.     }
  134. }
  135.  
  136. #ifdef DUMB_CPP
  137. /*
  138.  * Return difference between two vectors.
  139.  */
  140. vecsub(a, b, res)
  141. Vector a, b, *res;
  142. {
  143.     res->x = a.x - b.x;
  144.     res->y = a.y - b.y;
  145.     res->z = a.z - b.z;
  146. }
  147.  
  148. /*
  149.  * Return sum of two vectors.
  150.  */
  151. vecadd(a, b, res)
  152. Vector a, b, *res;
  153. {
  154.     res->x = a.x + b.x;
  155.     res->y = a.y + b.y;
  156.     res->z = a.z + b.z;
  157. }
  158.  
  159. /*
  160.  * Compute scalar product of a vector.
  161.  */
  162. scalar_prod(scale, vec, res)
  163. double scale;
  164. Vector vec, *res;
  165. {
  166.     res->x = vec.x * scale;
  167.     res->y = vec.y * scale;
  168.     res->z = vec.z * scale;
  169. }
  170.  
  171. veccomb(s1, v1, s2, v2, res)
  172. double s1, s2;
  173. Vector v1, v2, *res;
  174. {
  175.     res->x = s1 * v1.x + s2 * v2.x;
  176.     res->y = s1 * v1.y + s2 * v2.y;
  177.     res->z = s1 * v1.z + s2 * v2.z;
  178. }
  179.  
  180. addscaledvec(v1, s, v2, res)
  181. Vector v1, v2, *res;
  182. double s;
  183. {
  184.     res->x = v1.x + s * v2.x;
  185.     res->y = v1.y + s * v2.y;
  186.     res->z = v1.z + s * v2.z;
  187. }
  188. #endif
  189.