-
Notifications
You must be signed in to change notification settings - Fork 0
/
plane.cpp
69 lines (54 loc) · 1.63 KB
/
plane.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*************************************************
**
** Plane.cpp: implementation of the Plane class.
** created: Tue Sep 12 2000
**
*************************************************/
#include "plane.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Plane::Plane(Point3d* thruPoint, Point3d* normal, double diff_ref, double spec_ref)
{
diffuse = diff_ref;
specular = spec_ref;
thru_point = new Point3d;
thru_point->x = thruPoint->x;
thru_point->y = thruPoint->y;
thru_point->z = thruPoint->z;
a = normal->x;
b = normal->y;
c = normal->z;
d = (normal->x * (0-thruPoint->x)) +
(normal->y * (0-thruPoint->y)) +
(normal->z * (0-thruPoint->z));
// normalize
double length = sqrt( pow(a, 2) +
pow(b, 2) +
pow(c, 2) );
a = a / length;
b = b / length;
c = c / length;
d = d / length;
}
Plane::~Plane()
{
delete thru_point;
}
bool Plane::intersects(Ray* r, Point3d* cam)
{
double top_half = (a*cam->x) + (b*cam->y) + (c*cam->z) + d;
double denominator = (a*r->delta_x) + (b*r->delta_y) + (c*r->delta_z);
if ((denominator == 0) || (denominator > 0)) // do not intersect
return false;
else {
r->t = 0 - ( top_half / denominator);
return true;
}
}
void Plane::calcNormal(Ray* norm, Point3d* inters)
{
norm->delta_x = (inters->x - thru_point->x) + a;
norm->delta_y = (inters->y - thru_point->y) + b;
norm->delta_z = (inters->z - thru_point->z) + c;
}