home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / nextrad.lha / NeXtRad / clip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-22  |  6.0 KB  |  258 lines

  1. /* clip.c */
  2. /* written by Jason R. Wilson   2/21/93 */
  3. /* this package provides a 3-D clip routine */
  4.  
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include "datastruct.h"
  8.  
  9. /***********************************************************************/
  10.  
  11.  
  12. Boolean inside (VisVertexCell *A,int Case,double bound)
  13.  
  14. {
  15.    Boolean temp;
  16.    temp = false;
  17.  
  18.    switch (Case) {
  19.     case 1: if (A->ViewPosition.hom[3] >= bound) temp = true;
  20.             break;
  21.     case 2: if (A->ViewPosition.hom[0] <= bound) temp = true;
  22.             break;
  23.     case 3: if (A->ViewPosition.hom[1] <= bound) temp = true;
  24.             break;
  25.     case 4: if (A->ViewPosition.hom[0] >= bound) temp = true;
  26.             break;
  27.     case 5: if (A->ViewPosition.hom[1] >= bound) temp = true;
  28.             break;
  29.    }
  30.    return temp;
  31. }
  32. /***********************************************************************/
  33.  
  34. Boolean outside (VisVertexCell *A,int Case,double bound)
  35.  
  36. {
  37.    if (inside (A,Case,bound) == true)
  38.       return false;
  39.    else
  40.       return true;
  41. }
  42.  
  43. /***********************************************************************/
  44.  
  45.  
  46. void ClipTo (VisVertexCell *OldVerts,PolygonCell *Poly,Boolean *Gone,
  47.           int Case,double bound)
  48.  
  49. /* Clips against one plane */
  50.  
  51. {
  52. VisVertexCell *New;
  53. VisVertexCell *NewB;
  54. VisVertexCell *A;
  55. VisVertexCell *B;
  56. VisVertexCell *Traverse;
  57. Boolean Output;
  58. Boolean Intersect;
  59. double t;
  60. VisVertexCell *traverse,*tempfree;
  61.  
  62. Traverse = OldVerts;
  63.  
  64. Poly->VisVertices = NULL;
  65.  
  66. while (!(Traverse == NULL))
  67. {
  68.    A = Traverse; /* set up A */
  69.  
  70.    if (Traverse->Next == NULL) /* set up B */
  71.       B = OldVerts;
  72.    else
  73.       B = Traverse->Next;
  74.  
  75.    Intersect = false;
  76.  
  77.  
  78.    if (inside (A,Case,bound) && inside (B,Case,bound))
  79.       Output = true;
  80.    else
  81.       if (outside (A,Case,bound) && outside (B,Case,bound))
  82.      Output = false;
  83.       else
  84.       {
  85.      switch (Case) {
  86.       case 1:
  87.         t = (bound - A->ViewPosition.hom[3])/
  88.            (B->ViewPosition.hom[3] - A->ViewPosition.hom[3]);
  89.         break;
  90.       case 2:
  91.         t = (bound - A->ViewPosition.hom[0])/
  92.            (B->ViewPosition.hom[0] - A->ViewPosition.hom[0]);
  93.         break;
  94.       case 3:
  95.         t = (bound - A->ViewPosition.hom[1])/
  96.            (B->ViewPosition.hom[1] - A->ViewPosition.hom[1]);
  97.         break;
  98.       case 4:
  99.         t = (bound - A->ViewPosition.hom[0])/
  100.            (B->ViewPosition.hom[0] - A->ViewPosition.hom[0]);
  101.         break;
  102.       case 5:
  103.         t = (bound - A->ViewPosition.hom[1])/
  104.            (B->ViewPosition.hom[1] - A->ViewPosition.hom[1]);
  105.         break;
  106.      }
  107.      Intersect = true;
  108.      New = (VisVertexCell *)
  109.         (malloc (sizeof (VisVertexCell))); /* allocate for new*/
  110.      InitVisVertexCell (New);
  111.      New->ViewPosition.hom[0] = A->ViewPosition.hom[0] + 
  112.         (B->ViewPosition.hom[0] - A->ViewPosition.hom[0])*t;
  113.      New->ViewPosition.hom[1] = A->ViewPosition.hom[1] + 
  114.         (B->ViewPosition.hom[1] - A->ViewPosition.hom[1])*t;
  115.      New->ViewPosition.hom[2] = A->ViewPosition.hom[2] + 
  116.         (B->ViewPosition.hom[2] - A->ViewPosition.hom[2])*t;
  117.      New->ViewPosition.hom[3] = A->ViewPosition.hom[3] + 
  118.         (B->ViewPosition.hom[3] - A->ViewPosition.hom[3])*t;
  119.      New->B.r = A->B.r + (B->B.r - A->B.r)*t;
  120.      New->B.g = A->B.g + (B->B.g - A->B.g)*t;
  121.      New->B.b = A->B.b + (B->B.b - A->B.b)*t;
  122.  
  123.          /* compute the new intensity value for the new point */
  124.      if (outside (A,Case,bound))
  125.         Output = true;
  126.      else Output = false;
  127.       }
  128.    if (Intersect == true)
  129.    {
  130.       New->Next = Poly->VisVertices;
  131.       Poly->VisVertices = New;
  132.    }
  133.    if (Output == true)
  134.    {
  135.       NewB = (VisVertexCell *)(malloc(sizeof (VisVertexCell)));
  136.       InitVisVertexCell (NewB);
  137.       NewB->ViewPosition.hom[0] = B->ViewPosition.hom[0];
  138.       NewB->ViewPosition.hom[1] = B->ViewPosition.hom[1];
  139.       NewB->ViewPosition.hom[2] = B->ViewPosition.hom[2];
  140.       NewB->ViewPosition.hom[3] = B->ViewPosition.hom[3];
  141.       NewB->B.r = B->B.r;
  142.       NewB->B.g = B->B.g;
  143.       NewB->B.b = B->B.b;
  144.       NewB->Next = Poly->VisVertices;
  145.       Poly->VisVertices = NewB;
  146.    }
  147.  
  148.    Traverse = Traverse->Next;
  149. }
  150.  
  151.  
  152. /* free up memory */
  153. traverse = OldVerts;
  154. while (!(traverse == NULL))
  155. {
  156.    tempfree = traverse;
  157.    traverse = traverse->Next;
  158.    free (tempfree);
  159. }
  160.  
  161.  
  162. if (Poly->VisVertices == NULL) /* is any of the polygon left */
  163.      *Gone = true;
  164. else *Gone = false;
  165.  
  166. }
  167.  
  168.  
  169.  
  170. /***********************************************************************/     
  171.             
  172.      
  173.  
  174. void ClipPolygon (PolygonCell *Poly,Window ViewWindow,
  175.           Boolean *Gone)
  176.  
  177. /* clips to Wtol */
  178. /* does perspective divisions */
  179. /* clips a polygon to the view volume defined by Wl, Wr, Wt, and Wb */
  180. /* returns the new list of vertices */
  181.  
  182. {
  183. VertexListCell *traverselist;
  184. VisVertexCell  *newvertex;
  185. VisVertexCell  *traverse,*tempfree;
  186.  
  187.  
  188. /* free up memory */
  189. traverse = Poly->VisVertices;
  190.  
  191. while (!(traverse == NULL))
  192. {
  193.    tempfree = traverse;
  194.    traverse = traverse->Next;
  195.    free (tempfree);
  196. }
  197.  
  198. Poly->VisVertices = NULL;
  199.  
  200.  
  201. traverselist = Poly->Vertices;
  202.  
  203. while (!(traverselist == NULL))
  204.  
  205. {
  206.  newvertex = (VisVertexCell *)(malloc (sizeof(VisVertexCell)));
  207.  InitVisVertexCell (newvertex);
  208.  newvertex->ViewPosition = traverselist->Vertex->ViewPosition;
  209.  newvertex->B.r = traverselist->Vertex->B.r;
  210.  newvertex->B.g = traverselist->Vertex->B.g;
  211.  newvertex->B.b = traverselist->Vertex->B.b;
  212.  newvertex->Next = Poly->VisVertices;
  213.  Poly->VisVertices = newvertex;
  214.  traverselist = traverselist->Rest;
  215. }
  216.  
  217.  
  218. ClipTo (Poly->VisVertices,Poly,Gone,1,Wtol);
  219.  
  220. /* do the perspective divisions */
  221.  
  222. traverse = Poly->VisVertices;
  223.  
  224.  
  225. while (!(traverse == NULL))
  226. {
  227.    traverse->ViewPosition.hom[0] =
  228.    traverse->ViewPosition.hom[0]/traverse->ViewPosition.hom[3];
  229.    traverse->ViewPosition.hom[1] =
  230.    traverse->ViewPosition.hom[1]/traverse->ViewPosition.hom[3];
  231.    traverse->ViewPosition.hom[2] =
  232.    traverse->ViewPosition.hom[2]/traverse->ViewPosition.hom[3];
  233.    traverse = traverse->Next;
  234. }
  235.  
  236.  
  237. if (!(*Gone == true))
  238.    ClipTo (Poly->VisVertices,
  239.        Poly,Gone,2,ViewWindow.Wr);
  240.  
  241. if (!(*Gone == true))
  242.    ClipTo (Poly->VisVertices,
  243.        Poly,Gone,3,ViewWindow.Wt);
  244.  
  245. if (!(*Gone == true))
  246.    ClipTo (Poly->VisVertices,
  247.        Poly,Gone,4,ViewWindow.Wl);
  248.  
  249. if (!(*Gone == true))
  250.    ClipTo (Poly->VisVertices,
  251.        Poly,Gone,5,ViewWindow.Wb);
  252.  
  253.  
  254.   }
  255.  
  256.  
  257.  
  258.