home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume21 / rayshade / part01 / src / raymath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-21  |  3.1 KB  |  157 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 89/10/27 02:06:01 craig Exp $
  22.  *
  23.  * $Log:    raymath.c,v $
  24.  * Revision 3.0  89/10/27  02:06:01  craig
  25.  * Baseline for first official release.
  26.  * 
  27.  */
  28. #include <math.h>
  29. #include "typedefs.h"
  30. #include "constants.h"
  31. #include "funcdefs.h"
  32.  
  33. /*
  34.  * Normalize a vector, return original length.
  35.  */
  36. double
  37. normalize(a)
  38. register Vector *a;
  39. {
  40.     double d;
  41.  
  42.     d = sqrt(a->x*a->x + a->y*a->y + a->z*a->z);
  43.     if(d == 0.)
  44.         return 0.;
  45.     a->x /= d;
  46.     a->y /= d;
  47.     a->z /= d;
  48.     return d;
  49. }
  50.  
  51. /*
  52.  * Compute cross-product of a and b, place normalized result in o.  Returns
  53.  * length of result before normalization.
  54.  */
  55. double
  56. crossp(o, a, b)
  57. Vector *o, *a, *b;
  58. {
  59.     rawcrossp(o, a, b);
  60.     return normalize(o);
  61. }
  62.  
  63. /*
  64.  * Compute cross-product of a and b, place result in o.
  65.  */
  66. rawcrossp(o, a, b)
  67. Vector *o, *a, *b;
  68. {
  69.     o->x = (a->y * b->z) - (a->z * b->y);
  70.     o->y = (a->z * b->x) - (a->x * b->z);
  71.     o->z = (a->x * b->y) - (a->y * b->x);
  72. }
  73.  
  74. /*
  75.  * Calculate direction of refracted ray using Heckbert's formula.  Returns TRUE
  76.  * if a total internal reflection occurs.
  77.  */
  78. refract(dir, from_index, to_index, I, N, cos1)
  79. double from_index, to_index, cos1;
  80. Vector *dir, I, N;
  81. {
  82.     double kn, cos2, k;
  83.  
  84.     if (cos1 < 0.) {
  85.         /*
  86.          * Hit the 'backside' of a surface -- flip the
  87.          * normal.
  88.          */
  89.         N.x = -N.x;
  90.         N.y = -N.y;
  91.         N.z = -N.z;
  92.         cos1 = -cos1;
  93.     }
  94.  
  95.     kn = from_index / to_index;
  96.     cos2 = 1. - kn*kn*(1. - cos1*cos1);
  97.     if (cos2 < 0.)
  98.         return TRUE;        /* Total internal reflection. */
  99.     k = kn * cos1 - sqrt(cos2);
  100.     veccomb(kn, I, k, N, dir);
  101.     return FALSE;
  102. }
  103.  
  104. #ifdef DUMB_CPP
  105. /*
  106.  * Return difference between two vectors.
  107.  */
  108. vecsub(a, b, res)
  109. Vector a, b, *res;
  110. {
  111.     res->x = a.x - b.x;
  112.     res->y = a.y - b.y;
  113.     res->z = a.z - b.z;
  114. }
  115.  
  116. /*
  117.  * Return sum of two vectors.
  118.  */
  119. vecadd(a, b, res)
  120. Vector a, b, *res;
  121. {
  122.     res->x = a.x + b.x;
  123.     res->y = a.y + b.y;
  124.     res->z = a.z + b.z;
  125. }
  126.  
  127. /*
  128.  * Compute scalar product of a vector.
  129.  */
  130. scalar_prod(scale, vec, res)
  131. double scale;
  132. Vector vec, *res;
  133. {
  134.     res->x = vec.x * scale;
  135.     res->y = vec.y * scale;
  136.     res->z = vec.z * scale;
  137. }
  138.  
  139. veccomb(s1, v1, s2, v2, res)
  140. double s1, s2;
  141. Vector v1, v2, *res;
  142. {
  143.     res->x = s1 * v1.x + s2 * v2.x;
  144.     res->y = s1 * v1.y + s2 * v2.y;
  145.     res->z = s1 * v1.z + s2 * v2.z;
  146. }
  147.  
  148. addscaledvec(v1, s, v2, res)
  149. Vector v1, v2, *res;
  150. double s;
  151. {
  152.     res->x = v1.x + s * v2.x;
  153.     res->y = v1.y + s * v2.y;
  154.     res->z = v1.z + s * v2.z;
  155. }
  156. #endif
  157.