1 /*
2  * plane.c - This file contains the functions for dealing with planes.
3  *
4  *  $Id: plane.c,v 1.23 2007/02/04 04:42:00 johns Exp $
5  */
6 
7 #include "machine.h"
8 #include "types.h"
9 #include "macros.h"
10 #include "vector.h"
11 #include "intersect.h"
12 #include "util.h"
13 
14 #define PLANE_PRIVATE
15 #include "plane.h"
16 
17 static object_methods plane_methods = {
18   (void (*)(const void *, void *))(plane_intersect),
19   (void (*)(const void *, const void *, const void *, void *))(plane_normal),
20   plane_bbox,
21   free
22 };
23 
newplane(void * tex,vector ctr,vector norm)24 object * newplane(void * tex, vector ctr, vector norm) {
25   plane * p;
26 
27   p=(plane *) malloc(sizeof(plane));
28   memset(p, 0, sizeof(plane));
29   p->methods = &plane_methods;
30 
31   p->tex = tex;
32   p->norm = norm;
33   VNorm(&p->norm);
34   p->d = -VDot(&ctr, &p->norm);
35 
36   return (object *) p;
37 }
38 
plane_bbox(void * obj,vector * min,vector * max)39 static int plane_bbox(void * obj, vector * min, vector * max) {
40   return 0;
41 }
42 
plane_intersect(const plane * pln,ray * ry)43 static void plane_intersect(const plane * pln, ray * ry) {
44   flt t,td;
45 
46   /* may wish to reorder these computations... */
47 
48   t = -(pln->d + (pln->norm.x * ry->o.x +
49                   pln->norm.y * ry->o.y +
50                   pln->norm.z * ry->o.z));
51 
52   td = pln->norm.x * ry->d.x + pln->norm.y * ry->d.y + pln->norm.z * ry->d.z;
53 
54   if (td != 0.0) {
55     t /= td;
56     if (t > 0.0)
57       ry->add_intersection(t,(object *) pln, ry);
58   }
59 }
60 
plane_normal(const plane * pln,const vector * pnt,const ray * incident,vector * N)61 static void plane_normal(const plane * pln, const vector * pnt, const ray * incident, vector * N) {
62   *N=pln->norm;
63 
64   /* Flip surface normal to point toward the viewer if necessary */
65   if (VDot(N, &(incident->d)) > 0.0)  {
66     N->x=-N->x;
67     N->y=-N->y;
68     N->z=-N->z;
69   }
70 }
71 
72