-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlane.cpp
More file actions
110 lines (106 loc) · 2.21 KB
/
Copy pathPlane.cpp
File metadata and controls
110 lines (106 loc) · 2.21 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "precomp.h"
#include "Plane.h"
// Defult constructor, gives the x-z plane with gray color
Plane::Plane()
: GeoPrimitive()
{
p = Point3D(0.0f);
n = Normal(0.0f, 1.0f, 0.0f);
color = RGBColor(0.5f);
d = -1.0f * p * n;
}
// Construct from a Point3D, a Normal and a RGBColor
Plane::Plane(const Point3D& p3d, const Normal& nrml, const RGBColor& colorInput)
: GeoPrimitive(), p(p3d), n(nrml), color(colorInput)
{
d = -1.0f * p * n;
}
// Copy Constructor
Plane::Plane(const Plane& planeInput)
: GeoPrimitive(), p(planeInput.p), n(planeInput.n), color(planeInput.color), d(planeInput.d)
{
}
// Destructor
Plane::~Plane()
{}
// Assignment operator
Plane& Plane::operator= (const Plane& planeInput)
{
if (this == &planeInput)
return *this;
p = planeInput.p;
n = planeInput.n;
color = planeInput.color;
d = planeInput.d;
return *this;
}
// Color setter
void Plane::set_color(const RGBColor& colorInput)
{
color = colorInput;
}
// Color getter
RGBColor Plane::get_color()
{
return color;
}
// Get AABB, return a INFINITE AABB
AABB Plane::get_AABB() const
{
return AABB(-1.0f*VERYBIG, -1.0f*VERYBIG, -1.0f*VERYBIG,
VERYBIG, VERYBIG, VERYBIG);
}
// Generate a random Point3D on surface
Point3D Plane::rand_pnt(const Point3D& p3d)
{
Point3D rand_p3d;
do
{
rand_p3d.x = drand48();
rand_p3d.y = drand48();
rand_p3d.z = drand48();
} while ((rand_p3d-p) * n != 0.0f);
return rand_p3d;
}
// Ray-Plane hit function
bool Plane::hit(Ray& ray) const
{
ray.counter++;
float t_tmp;
t_tmp = float(-1.0f * (ray.o * n + d) / (ray.d * n));
if (t_tmp > EPSILON && t_tmp < ray.t)
{
ray.t = t_tmp;
//ray.counter++;
return true;
}
return false;
}
// Ray-Plane hit function, with color
bool Plane::hit(Ray& ray, RGBColor& colorOut) const
{
if (this->hit(ray))
{
colorOut = this->color;
return true;
}
return false;
}
// Ray-Plane hit function, with hit point
bool Plane::hit(Ray& ray, HitPoint& hitPoint) const
{
bool hit = this->hit(ray);
hitPoint.ray_counter = ray.counter;
if (hit)
{
hitPoint.ray_counter++;
hitPoint.hit = true;
hitPoint.color = this->color;
hitPoint.point = ray.o + ray.t * ray.d;
hitPoint.ray_t = ray.t;
hitPoint.normal = this->n;
hitPoint.material = this->material;
return hit;
}
return hit;
}