Tachyon (current)  Current Main Branch
plane.c
Go to the documentation of this file.
1 /*
2  * plane.c - This file contains the functions for dealing with planes.
3  *
4  * (C) Copyright 1994-2022 John E. Stone
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * $Id: plane.c,v 1.26 2022/02/18 17:55:28 johns Exp $
8  *
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <math.h>
15 
16 #define TACHYON_INTERNAL 1
17 #include "tachyon.h"
18 #include "macros.h"
19 #include "vector.h"
20 #include "intersect.h"
21 #include "util.h"
22 
23 #define PLANE_PRIVATE
24 #include "plane.h"
25 
26 static object_methods plane_methods = {
27  (void (*)(const void *, void *))(plane_intersect),
28  (void (*)(const void *, const void *, const void *, void *))(plane_normal),
29  plane_bbox,
30  free
31 };
32 
33 object * newplane(void * tex, vector ctr, vector norm) {
34  plane * p;
35 
36  p=(plane *) malloc(sizeof(plane));
37  memset(p, 0, sizeof(plane));
38  p->methods = &plane_methods;
39 
40  p->tex = tex;
41  p->norm = norm;
42  VNorm(&p->norm);
43  p->d = -VDot(&ctr, &p->norm);
44 
45  return (object *) p;
46 }
47 
48 static int plane_bbox(void * obj, vector * min, vector * max) {
49  return 0;
50 }
51 
52 static void plane_intersect(const plane * pln, ray * ry) {
53  flt t,td;
54 
55  /* may wish to reorder these computations... */
56 
57  t = -(pln->d + (pln->norm.x * ry->o.x +
58  pln->norm.y * ry->o.y +
59  pln->norm.z * ry->o.z));
60 
61  td = pln->norm.x * ry->d.x + pln->norm.y * ry->d.y + pln->norm.z * ry->d.z;
62 
63  if (td != 0.0) {
64  t /= td;
65  if (t > 0.0)
66  ry->add_intersection(t,(object *) pln, ry);
67  }
68 }
69 
70 static void plane_normal(const plane * pln, const vector * pnt, const ray * incident, vector * N) {
71  *N=pln->norm;
72 
73  /* Flip surface normal to point toward the viewer if necessary */
74  if (VDot(N, &(incident->d)) > 0.0) {
75  N->x=-N->x;
76  N->y=-N->y;
77  N->z=-N->z;
78  }
79 }
80 
static object_methods plane_methods
Definition: plane.c:26
static int plane_bbox(void *obj, vector *min, vector *max)
Definition: plane.c:48
void VNorm(apivector *)
static void plane_normal(const plane *pln, const vector *pnt, const ray *incident, vector *N)
Definition: plane.c:70
flt VDot(apivector *a, apivector *b)
double flt
generic floating point number, using double
Definition: tachyon.h:47
object * newplane(void *tex, vector ctr, vector norm)
Definition: plane.c:33
Tachyon cross-platform timers, special math function wrappers, and RNGs.
static void plane_intersect(const plane *pln, ray *ry)
Definition: plane.c:52
Tachyon public API function prototypes and declarations used to drive the ray tracing engine...