home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libobj / plane.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  3.1 KB  |  167 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.  * plane.c,v 4.1 1994/08/09 07:59:46 explorer Exp
  17.  *
  18.  * plane.c,v
  19.  * Revision 4.1  1994/08/09  07:59:46  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:10  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:38:51  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "geom.h"
  30. #include "plane.h"
  31.  
  32. static Methods *iPlaneMethods = NULL;
  33. static char planeName[] = "plane";
  34.  
  35. unsigned long PlaneTests, PlaneHits;
  36.  
  37. /*
  38.  * create plane primitive
  39.  */
  40. Plane *
  41. PlaneCreate(pos, norm)
  42. Vector *pos, *norm;
  43. {
  44.     Plane *plane;
  45.     Vector tmpnrm;
  46.  
  47.     tmpnrm = *norm;
  48.     if (VecNormalize(&tmpnrm) == 0.) {
  49.         RLerror(RL_WARN, "Degenerate plane normal.\n");
  50.         return (Plane *)NULL;
  51.     }
  52.     plane = (Plane *)share_malloc(sizeof(Plane));
  53.     plane->norm = tmpnrm;
  54.     plane->pos = *pos;
  55.     plane->d = dotp(&plane->norm, pos);
  56.  
  57.     return plane;
  58. }
  59.  
  60. Methods *
  61. PlaneMethods()
  62. {
  63.     if (iPlaneMethods == (Methods *)NULL) {
  64.         iPlaneMethods = MethodsCreate();
  65.         iPlaneMethods->name = PlaneName;
  66.         iPlaneMethods->create = (GeomCreateFunc *)PlaneCreate;
  67.         iPlaneMethods->methods = PlaneMethods;
  68.         iPlaneMethods->intersect = PlaneIntersect;
  69.         iPlaneMethods->normal = PlaneNormal;
  70.         iPlaneMethods->uv = PlaneUV;
  71.         iPlaneMethods->bounds = PlaneBounds;
  72.         iPlaneMethods->stats = PlaneStats;
  73.         iPlaneMethods->checkbounds = FALSE;
  74.         iPlaneMethods->closed = FALSE;
  75.     }
  76.     return iPlaneMethods;
  77. }
  78.  
  79. int
  80. PlaneIntersect(plane, ray, mindist, maxdist)
  81. Plane *plane;
  82. Ray *ray;
  83. Float mindist, *maxdist;
  84. {
  85.     Float d;
  86.  
  87.     PlaneTests++;
  88.  
  89.     d = dotp(&plane->norm, &ray->dir);
  90.     if (fabs(d) < EPSILON)
  91.         return FALSE;
  92.     d = (plane->d - dotp(&plane->norm, &ray->pos)) / d;
  93.  
  94.     if (d > mindist && d < *maxdist) {
  95.         *maxdist = d;
  96.         PlaneHits++;
  97.         return TRUE;
  98.     }
  99.     return FALSE;
  100. }
  101.  
  102. /*ARGSUSED*/
  103. int
  104. PlaneNormal(plane, pos, nrm, gnrm)
  105. Plane *plane;
  106. Vector *pos, *nrm, *gnrm;
  107. {
  108.     *gnrm = *nrm = plane->norm;
  109.     return FALSE;
  110. }
  111.  
  112. void
  113. PlaneUV(plane, pos, norm, uv, dpdu, dpdv)
  114. Plane *plane;
  115. Vector *pos, *norm, *dpdu, *dpdv;
  116. Vec2d *uv;
  117. {
  118.     Vector vec, du, dv;
  119.  
  120.     VecCoordSys(norm, &du, &dv);
  121.     VecSub(*pos, plane->pos, &vec);
  122.  
  123.     uv->u = dotp(&vec, &du);
  124.     uv->v = dotp(&vec, &dv);
  125.  
  126.     if (dpdu)
  127.         *dpdu = du;
  128.     if (dpdv)
  129.         *dpdv = dv;
  130. }
  131.     
  132. /*ARGSUSED*/
  133. void
  134. PlaneBounds(plane, bounds)
  135. Plane *plane;
  136. Float bounds[2][3];
  137. {
  138.     /*
  139.      * Planes are unbounded by nature.  minx > maxx signifies
  140.      * this.
  141.      */
  142.     bounds[LOW][X] = 1.0;
  143.     bounds[HIGH][X] = -1.0;
  144. }
  145.  
  146. char *
  147. PlaneName()
  148. {
  149.     return planeName;
  150. }
  151.  
  152. void
  153. PlaneStats(tests, hits)
  154. unsigned long *tests, *hits;
  155. {
  156.     *tests = PlaneTests;
  157.     *hits = PlaneHits;
  158. }
  159.  
  160. void
  161. PlaneMethodRegister(meth)
  162. UserMethodType meth;
  163. {
  164.     if (iPlaneMethods)
  165.         iPlaneMethods->user = meth;
  166. }
  167.