home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / povsrc.sit / SOURCE / PLANES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-03  |  5.4 KB  |  201 lines

  1. /****************************************************************************
  2. *                planes.c
  3. *
  4. *  This module implements functions that manipulate planes.
  5. *
  6. *  from Persistence of Vision Raytracer 
  7. *  Copyright 1992 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  Copying, distribution and legal info is in the file povlegal.doc which
  10. *  should be distributed with this file. If povlegal.doc is not available
  11. *  or for more info please contact:
  12. *
  13. *       Drew Wells [POV-Team Leader] 
  14. *       CIS: 73767,1244  Internet: 73767.1244@compuserve.com
  15. *       Phone: (213) 254-4041
  16. * This program is based on the popular DKB raytracer version 2.12.
  17. * DKBTrace was originally written by David K. Buck.
  18. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  19. *
  20. *****************************************************************************/
  21.  
  22. #include "frame.h"
  23. #include "vector.h"
  24. #include "povproto.h"
  25.  
  26. METHODS Plane_Methods =
  27. { Object_Intersect, All_Plane_Intersections,
  28.    Inside_Plane, Plane_Normal,
  29.    Copy_Plane,
  30.    Translate_Plane, Rotate_Plane,
  31.    Scale_Plane, Invert_Plane};
  32.  
  33. extern PLANE *Get_Plane_Shape();
  34.  
  35. extern RAY *VP_Ray;
  36. extern long Ray_Plane_Tests, Ray_Plane_Tests_Succeeded;
  37.  
  38. int All_Plane_Intersections (Object, Ray, Depth_Queue)
  39. OBJECT *Object;
  40. RAY *Ray;
  41. PRIOQ *Depth_Queue;
  42. {
  43.    PLANE *Shape = (PLANE *) Object;
  44.    DBL Depth;
  45.    VECTOR Intersection_Point;
  46.    INTERSECTION Local_Element;
  47.  
  48.    if (Intersect_Plane (Ray, Shape, &Depth))
  49.       if (Depth > Small_Tolerance)
  50.       {
  51.          Local_Element.Depth = Depth;
  52.          Local_Element.Object = Shape -> Parent_Object;
  53.          VScale (Intersection_Point, Ray -> Direction, Depth);
  54.          VAdd (Intersection_Point, Intersection_Point, Ray -> Initial);
  55.          Local_Element.Point = Intersection_Point;
  56.          Local_Element.Shape = (SHAPE *)Shape;
  57.          pq_add (Depth_Queue, &Local_Element);
  58.          return (TRUE);
  59.       }
  60.  
  61.    return (FALSE);
  62. }
  63.  
  64. int Intersect_Plane (Ray, Plane, Depth)
  65. RAY *Ray;
  66. PLANE *Plane;
  67. DBL *Depth;
  68. {
  69.    DBL NormalDotOrigin, NormalDotDirection;
  70.  
  71.    Ray_Plane_Tests++;
  72.    if (Ray == VP_Ray) {
  73.       if (!Plane->VPCached) {
  74.          VDot (Plane->VPNormDotOrigin, Plane->Normal_Vector, Ray->Initial);
  75.          Plane->VPNormDotOrigin += Plane->Distance;
  76.          Plane->VPNormDotOrigin *= -1.0;
  77.          Plane->VPCached = TRUE;
  78.       }
  79.  
  80.       VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  81.       if ((NormalDotDirection < Small_Tolerance) &&
  82.          (NormalDotDirection > -Small_Tolerance))
  83.          return (FALSE);
  84.  
  85.       *Depth = Plane->VPNormDotOrigin / NormalDotDirection;
  86.       if ((*Depth >= Small_Tolerance) && (*Depth <= Max_Distance)) {
  87.          Ray_Plane_Tests_Succeeded++;
  88.          return (TRUE);
  89.       }
  90.       else
  91.          return (FALSE);
  92.    }
  93.    else {
  94.       VDot (NormalDotOrigin, Plane->Normal_Vector, Ray->Initial);
  95.       NormalDotOrigin += Plane->Distance;
  96.       NormalDotOrigin *= -1.0;
  97.  
  98.          VDot (NormalDotDirection, Plane->Normal_Vector, Ray->Direction);
  99.       if ((NormalDotDirection < Small_Tolerance) &&
  100.          (NormalDotDirection > -Small_Tolerance))
  101.          return (FALSE);
  102.  
  103.       *Depth = NormalDotOrigin / NormalDotDirection;
  104.       if ((*Depth >= Small_Tolerance) && (*Depth <= Max_Distance)) {
  105.          Ray_Plane_Tests_Succeeded++;
  106.          return (TRUE);
  107.       }
  108.       else
  109.          return (FALSE);
  110.    }
  111. }
  112.  
  113. int Inside_Plane (Test_Point, Object)
  114. VECTOR *Test_Point;
  115. OBJECT *Object;
  116. {
  117.    PLANE *Plane = (PLANE *) Object;
  118.    DBL Temp;
  119.  
  120.    VDot (Temp, *Test_Point, Plane->Normal_Vector);
  121.    return ((Temp + Plane->Distance) <= Small_Tolerance);
  122. }
  123.  
  124. void Plane_Normal (Result, Object, Intersection_Point)
  125. OBJECT *Object;
  126. VECTOR *Result, *Intersection_Point;
  127. {
  128.    PLANE *Plane = (PLANE *) Object;
  129.  
  130.    *Result = Plane->Normal_Vector;
  131. }
  132.  
  133. void *Copy_Plane (Object)
  134. OBJECT *Object;
  135. {
  136.    PLANE *New_Shape;
  137.  
  138.    New_Shape = Get_Plane_Shape ();
  139.    *New_Shape = * ((PLANE *)Object);
  140.    New_Shape -> Next_Object = NULL;
  141.  
  142.    if (New_Shape->Shape_Texture != NULL)
  143.       New_Shape->Shape_Texture = Copy_Texture (New_Shape->Shape_Texture);
  144.  
  145.    return (New_Shape);
  146. }
  147.  
  148. void Translate_Plane (Object, Vector)
  149. OBJECT *Object;
  150. VECTOR *Vector;
  151. {
  152.    PLANE *Plane = (PLANE *) Object;
  153.    VECTOR Translation;
  154.  
  155.    VEvaluate (Translation, Plane->Normal_Vector, *Vector);
  156.    Plane->Distance -= Translation.x + Translation.y + Translation.z;
  157.  
  158.    Translate_Texture (&Plane->Shape_Texture, Vector);
  159. }
  160.  
  161. void Rotate_Plane (Object, Vector)
  162. OBJECT *Object;
  163. VECTOR *Vector;
  164. {
  165.    TRANSFORMATION Transformation;
  166.  
  167.    Get_Rotation_Transformation (&Transformation, Vector);
  168.    MTransformVector (&((PLANE *) Object)->Normal_Vector,
  169.       &((PLANE *) Object)->Normal_Vector, &Transformation);
  170.  
  171.    Rotate_Texture (&((PLANE *) Object)->Shape_Texture, Vector);
  172. }
  173.  
  174. void Scale_Plane (Object, Vector)
  175. OBJECT *Object;
  176. VECTOR *Vector;
  177. {
  178.    DBL Length;
  179.    PLANE *Plane = (PLANE  *) Object;
  180.  
  181.    Plane->Normal_Vector.x = Plane->Normal_Vector.x / Vector->x;
  182.    Plane->Normal_Vector.y = Plane->Normal_Vector.y / Vector->y;
  183.    Plane->Normal_Vector.z = Plane->Normal_Vector.z / Vector->z;
  184.  
  185.    VLength(Length, Plane->Normal_Vector);
  186.    VScale (Plane->Normal_Vector, Plane->Normal_Vector, 1.0 / Length);
  187.    Plane->Distance /= Length;
  188.  
  189.    Scale_Texture (&((PLANE *) Object)->Shape_Texture, Vector);
  190. }
  191.  
  192. void Invert_Plane (Object)
  193. OBJECT *Object;
  194. {
  195.    PLANE *Plane = (PLANE  *) Object;
  196.  
  197.    VScale (Plane->Normal_Vector, Plane->Normal_Vector, -1.0);
  198.    Plane->Distance *= -1.0;
  199. }
  200.