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

  1. /****************************************************************************
  2. *                point.c
  3. *
  4. *  This module implements the point & spot light source primitive.
  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 Point_Methods =
  27. { Object_Intersect, All_Point_Intersections,
  28.    Inside_Point, NULL,
  29.    Copy_Point,
  30.    Translate_Point, Rotate_Point,
  31.    Scale_Point, Invert_Point};
  32.  
  33. static DBL cubic_spline PARAMS(( DBL low,DBL high,DBL pos));
  34. extern LIGHT_SHAPE *Get_Light_Source_Shape();
  35.  
  36. int All_Point_Intersections (Object, Ray, Depth_Queue)
  37. OBJECT *Object;
  38. RAY *Ray;
  39. PRIOQ *Depth_Queue;
  40. {
  41.  
  42.    return(FALSE);
  43. }
  44.  
  45.  
  46. int Inside_Point (Test_Point, Object)
  47. VECTOR *Test_Point;
  48. OBJECT *Object;
  49. {
  50.    return(FALSE);
  51. }
  52.  
  53.  
  54. void *Copy_Point (Object)
  55. OBJECT *Object;
  56. {
  57.    LIGHT_SHAPE *New_Shape;
  58.  
  59.    New_Shape = Get_Light_Source_Shape ();
  60.    *New_Shape = *((LIGHT_SHAPE *) Object);
  61.    New_Shape -> Next_Object = NULL;
  62.  
  63.    if (New_Shape->Shape_Texture != NULL)
  64.       New_Shape->Shape_Texture = Copy_Texture (New_Shape->Shape_Texture);
  65.  
  66.    return (New_Shape);
  67. }
  68.  
  69. void Translate_Point (Object, Vector)
  70. OBJECT *Object;
  71. VECTOR *Vector;
  72. {
  73.    VAdd (((LIGHT_SHAPE *) Object)->Center, ((LIGHT_SHAPE *) Object)->Center, *Vector);
  74.    VAdd (((LIGHT_SHAPE *) Object)->Points_At,
  75.       ((LIGHT_SHAPE *) Object)->Points_At, *Vector);
  76. }
  77.  
  78. void Rotate_Point (Object, Vector)
  79. OBJECT *Object;
  80. VECTOR *Vector;
  81. {
  82.    TRANSFORMATION Transformation;
  83.    Get_Rotation_Transformation (&Transformation, Vector);
  84.    MTransformVector (&((LIGHT_SHAPE *) Object)->Center,
  85.       &((LIGHT_SHAPE *) Object)->Center, &Transformation);
  86.    MTransformVector (&((LIGHT_SHAPE *) Object)->Points_At,
  87.       &((LIGHT_SHAPE *) Object)->Points_At, &Transformation);
  88. }
  89.  
  90. void Scale_Point (Object, Vector)
  91. OBJECT *Object;
  92. VECTOR *Vector;
  93. {
  94.    TRANSFORMATION Transformation;
  95.    Get_Scaling_Transformation (&Transformation, Vector);
  96.    MTransformVector (&((LIGHT_SHAPE *) Object)->Center,
  97.       &((LIGHT_SHAPE *) Object)->Center, &Transformation);
  98.    MTransformVector (&((LIGHT_SHAPE *) Object)->Points_At,
  99.       &((LIGHT_SHAPE *) Object)->Points_At, &Transformation);
  100.    Scale_Texture (&((LIGHT_SHAPE *) Object)->Shape_Texture, Vector);
  101. }
  102.  
  103. void Invert_Point (Object)
  104. OBJECT *Object;
  105. {
  106.    ((LIGHT_SHAPE *) Object)->Inverted ^= TRUE;
  107. }
  108.  
  109.  
  110. /* Cubic spline that has tangents of slope 0 at x == low and at x == high.
  111.    For a given value "pos" between low and high the spline value is returned */
  112. static DBL cubic_spline(low, high, pos)
  113. DBL low, high, pos;
  114. {
  115.    /* Check to see if the position is within the proper boundaries */
  116.    if (pos < low)
  117.       return 0.0;
  118.    else if (pos > high)
  119.       return 1.0;
  120.    if (high == low)
  121.       return 0.0;
  122.  
  123.    /* Normalize to the interval 0->1 */
  124.    pos = (pos - low) / (high - low);
  125.  
  126.    /* See where it is on the cubic curve */
  127.    return (3 - 2 * pos) * pos * pos;
  128. }
  129.  
  130. DBL Attenuate_Light (Light_Source, Light_Source_Ray)
  131. LIGHT_SHAPE *Light_Source;
  132. RAY *Light_Source_Ray;
  133. {
  134.    DBL Len,costheta;
  135.    DBL Attenuation = 1.0;
  136.    VECTOR Spot_Direction;
  137.  
  138.    /* If this is a spotlight then attenuate based on the incidence angle */
  139.    if (Light_Source->Type == SPOT_LIGHT_TYPE) {
  140.       VSub(Spot_Direction, Light_Source->Points_At, Light_Source->Center);
  141.       VLength(Len, Spot_Direction);
  142.       if (Len > 0.0) {
  143.          VInverseScale(Spot_Direction, Spot_Direction, Len);
  144.          VDot(costheta, Light_Source_Ray->Direction, Spot_Direction);
  145.          costheta *= -1.0;
  146.          if (costheta > 0.0) {
  147.             Attenuation = pow(costheta, Light_Source->Coeff);
  148.             /* If there is a soft falloff region associated with the light then
  149.                do an interpolation of values between the hot center and the
  150.                direction at which light falls to nothing. */
  151.             if (Light_Source->Radius > 0.0)
  152.                Attenuation *= cubic_spline(Light_Source->Falloff,
  153.                   Light_Source->Radius,
  154.                   costheta);
  155.             /* printf("Atten: %lg\n", Attenuation); */
  156.          }
  157.          else
  158.             Attenuation = 0.0;
  159.       }
  160.       else
  161.          Attenuation = 0.0;
  162.    }    
  163.    return(Attenuation);
  164. }    
  165.