home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / src / plane.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  3KB  |  161 lines

  1. /*
  2.  * plane.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: plane.c,v 4.0 91/07/17 14:38:51 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    plane.c,v $
  19.  * Revision 4.0  91/07/17  14:38:51  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "geom.h"
  24. #include "plane.h"
  25.  
  26. static Methods *iPlaneMethods = NULL;
  27. static char planeName[] = "plane";
  28.  
  29. unsigned long PlaneTests, PlaneHits;
  30.  
  31. /*
  32.  * create plane primitive
  33.  */
  34. Plane *
  35. PlaneCreate(pos, norm)
  36. Vector *pos, *norm;
  37. {
  38.     Plane *plane;
  39.     Vector tmpnrm;
  40.  
  41.     tmpnrm = *norm;
  42.     if (VecNormalize(&tmpnrm) == 0.) {
  43.         RLerror(RL_WARN, "Degenerate plane normal.\n");
  44.         return (Plane *)NULL;
  45.     }
  46.     plane = (Plane *)share_malloc(sizeof(Plane));
  47.     plane->norm = tmpnrm;
  48.     plane->pos = *pos;
  49.     plane->d = dotp(&plane->norm, pos);
  50.  
  51.     return plane;
  52. }
  53.  
  54. Methods *
  55. PlaneMethods()
  56. {
  57.     if (iPlaneMethods == (Methods *)NULL) {
  58.         iPlaneMethods = MethodsCreate();
  59.         iPlaneMethods->name = PlaneName;
  60.         iPlaneMethods->create = (GeomCreateFunc *)PlaneCreate;
  61.         iPlaneMethods->methods = PlaneMethods;
  62.         iPlaneMethods->intersect = PlaneIntersect;
  63.         iPlaneMethods->normal = PlaneNormal;
  64.         iPlaneMethods->uv = PlaneUV;
  65.         iPlaneMethods->bounds = PlaneBounds;
  66.         iPlaneMethods->stats = PlaneStats;
  67.         iPlaneMethods->checkbounds = FALSE;
  68.         iPlaneMethods->closed = FALSE;
  69.     }
  70.     return iPlaneMethods;
  71. }
  72.  
  73. int
  74. PlaneIntersect(plane, ray, mindist, maxdist)
  75. Plane *plane;
  76. Ray *ray;
  77. Float mindist, *maxdist;
  78. {
  79.     Float d;
  80.  
  81.     PlaneTests++;
  82.  
  83.     d = dotp(&plane->norm, &ray->dir);
  84.     if (fabs(d) < EPSILON)
  85.         return FALSE;
  86.     d = (plane->d - dotp(&plane->norm, &ray->pos)) / d;
  87.  
  88.     if (d > mindist && d < *maxdist) {
  89.         *maxdist = d;
  90.         PlaneHits++;
  91.         return TRUE;
  92.     }
  93.     return FALSE;
  94. }
  95.  
  96. /*ARGSUSED*/
  97. int
  98. PlaneNormal(plane, pos, nrm, gnrm)
  99. Plane *plane;
  100. Vector *pos, *nrm, *gnrm;
  101. {
  102.     *gnrm = *nrm = plane->norm;
  103.     return FALSE;
  104. }
  105.  
  106. void
  107. PlaneUV(plane, pos, norm, uv, dpdu, dpdv)
  108. Plane *plane;
  109. Vector *pos, *norm, *dpdu, *dpdv;
  110. Vec2d *uv;
  111. {
  112.     Vector vec, du, dv;
  113.  
  114.     VecCoordSys(norm, &du, &dv);
  115.     VecSub(*pos, plane->pos, &vec);
  116.  
  117.     uv->u = dotp(&vec, &du);
  118.     uv->v = dotp(&vec, &dv);
  119.  
  120.     if (dpdu)
  121.         *dpdu = du;
  122.     if (dpdv)
  123.         *dpdv = dv;
  124. }
  125.     
  126. /*ARGSUSED*/
  127. void
  128. PlaneBounds(plane, bounds)
  129. Plane *plane;
  130. Float bounds[2][3];
  131. {
  132.     /*
  133.      * Planes are unbounded by nature.  minx > maxx signifies
  134.      * this.
  135.      */
  136.     bounds[LOW][X] = 1.0;
  137.     bounds[HIGH][X] = -1.0;
  138. }
  139.  
  140. char *
  141. PlaneName()
  142. {
  143.     return planeName;
  144. }
  145.  
  146. void
  147. PlaneStats(tests, hits)
  148. unsigned long *tests, *hits;
  149. {
  150.     *tests = PlaneTests;
  151.     *hits = PlaneHits;
  152. }
  153.  
  154. void
  155. PlaneMethodRegister(meth)
  156. UserMethodType meth;
  157. {
  158.     if (iPlaneMethods)
  159.         iPlaneMethods->user = meth;
  160. }
  161.