-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersection.cpp
executable file
·46 lines (44 loc) · 1.07 KB
/
Intersection.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
#include "Ray.h"
#include "Intersection.h"
#include "Color.h"
#include "Sphere.h"
Intersection::Intersection(Object *o1, Object *o2, double r) : Object(o1->color, r), object1(o1), object2(o2)
{
}
MyVector Intersection::NormalVector(MyVector position)
{
return MyVector(0, 0, 0);
}
double Intersection::distance(MyVector p)
{
double d1 = object1->distance(p);
double d2 = object2->distance(p);
return std::max(d1, d2);
}
double Intersection::hit(Ray *ray)
{
MyVector position = ray->position;
double d = this->distance(position);
double total = d;
for (int i = 0; i < 100; i++)
{
if (d < 0.05)
{
return total;
}
position += d * ray->direction;
d = distance(position);
total += d;
}
return -1;
}
void Intersection::Rebound(Ray *ray, MyVector hitPosition)
{
MyVector N1 = object1->NormalVector(hitPosition);
if (dotProduct(ray->direction, N1) > 0) // ray inside object 1
{
object2->Rebound(ray, hitPosition);
return;
}
object1->Rebound(ray, hitPosition);
}