home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / Light / raysp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  4.1 KB  |  115 lines

  1. /**********************************************************************/
  2. /* raysp.c                                                            */
  3. /*                                                                    */
  4. /* Routines to reduce the number of rays to shoot                     */
  5. /* - Culling of planes against planes                                 */
  6. /* - Culling of bounding volumes against planes                       */
  7. /*                                                                    */
  8. /* Copyright (C) 1992, Bernard Kwok                                   */
  9. /* All rights reserved.                                               */
  10. /* Revision 1.0                                                       */
  11. /* May, 1992                                                          */
  12. /**********************************************************************/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include "misc.h"
  17. #include "geo.h"
  18. #include "struct.h"
  19.  
  20. /**********************************************************************/
  21. /* Test if polygon vertices are completely, partially, or totally     */ 
  22. /* behind the plane of another poly. Includes test if two polys are   */
  23. /* coplanar, and face the same direction (normals point in same       */
  24. /* direction).                                                        */
  25. /**********************************************************************/
  26. int Poly_Behind_Poly(testp, againstp)
  27.      Polygon *testp, *againstp;
  28. {
  29.   int i, behind, front, on, is_behind;
  30.   double plane_val;
  31.  
  32.   /* If 2 polys belong to same patch then by definition of a patch
  33.      cannot "see" each other */
  34.   if ((testp->Pfather == againstp->Pfather) && (testp->Pfather != 0))
  35.     is_behind = 1;
  36.     
  37.   /* Count number of vertices in front, behind plane, and on the plane */
  38.   else {
  39.     behind = front = on = 0;
  40.     for(i=0;i<testp->numVert;i++) {
  41.       plane_val = dot(&testp->vert[i]->pos, &againstp->normal[0]) +
  42.     againstp->d;
  43.       if (plane_val < 0.0)
  44.     behind++;
  45.       else if (plane_val > 0.0)
  46.     front++;
  47.       else
  48.     on++;
  49.     }
  50.     if (front == testp->numVert)
  51.       is_behind = 0;
  52.     else if ((behind == testp->numVert) || (on == testp->numVert))
  53.       is_behind = 1;
  54.     else
  55.       is_behind = 0;
  56.   }
  57.   return is_behind;
  58. }
  59.  
  60. /**********************************************************************/
  61. /* Backface culling of a vector */
  62. /**********************************************************************/
  63. int Vertex_Behind_Plane(P, N, d) 
  64.      Vector P, N;
  65.      double d;
  66. { return ((dot(&P,&N) + d) <= 0.0 ? 1 : 0); }
  67.  
  68. /**********************************************************************/
  69. /* Find nearest point on box from plane */
  70. /**********************************************************************/
  71. Vector Nearest_Point(box,N,d)
  72.      BoundingBoxType *box;        /* Bounding box to test */
  73.      Vector N;                    /* Plane normal */
  74.      double d;                    /* Plane d */
  75. {
  76.   Vector P;
  77.   
  78.   P.x = (N.x < 0.0 ? box->max.x : box->min.x);
  79.   P.y = (N.y < 0.0 ? box->max.y : box->min.y);
  80.   P.z = (N.z < 0.0 ? box->max.z : box->min.z);
  81.  
  82.   return P;
  83. }
  84.  
  85. /**********************************************************************/
  86. /* Find farthest point on box from plane */
  87. /**********************************************************************/
  88. Vector Farthest_Point(box,N,d)
  89.      BoundingBoxType *box;        /* Bounding box to test */
  90.      Vector N;                    /* Plane normal */
  91.      double d;                    /* Plane d */
  92. {
  93.   Vector P;
  94.   
  95.   P.x = (N.x > 0.0 ? box->max.x : box->min.x);
  96.   P.y = (N.y > 0.0 ? box->max.y : box->min.y);
  97.   P.z = (N.z > 0.0 ? box->max.z : box->min.z);
  98.  
  99.   return P;
  100. }
  101.  
  102. /**********************************************************************/
  103. /* Backface culling of axis-aligned bounding box */
  104. /**********************************************************************/
  105. int Bounds_Behind_Plane(box, N, d)
  106.      BoundingBoxType *box;        /* Bounding box to test */
  107.      Vector N;                    /* Plane normal */
  108.      double d;                    /* Plane d */
  109. {
  110.   Vector P; /* Farthest point on box from plane */
  111.  
  112.   P = Farthest_Point(box,N,d);
  113.   return(Vertex_Behind_Plane(P, N, d));
  114. }
  115.