-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlane.h
51 lines (47 loc) · 1.51 KB
/
Plane.h
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
#ifndef PLANE_H
#define PLANE_H
#include "Object3D.h"
#include <vecmath.h>
#include <cmath>
using namespace std;
///TODO: Implement Plane representing an infinite plane
///choose your representation , add more fields and fill in the functions
class Plane : public Object3D
{
public:
Vector3f normal;
float d;
Plane(){
}
/**
d is the offset from the origin, meaning that the plane equation is P * n = d.
You can also implement other constructors (e.g. using 3 points).
*/
Plane( const Vector3f& normal, float d, Material* m) : Object3D(m){
this->normal = normal;
this->d = d;
}
~Plane(){
}
/*
Implement intersect, and remember that you also need to update the normal stored by Hit,
in addition to the intersection distance t and color.
*/
virtual bool intersect( const Ray& r, Hit& h, float tmin) {
// Ray: e+t*dr = r.getOrigin() + t * r.getDirection()
// Plane: P * n = d
// Intersection: (ray)*n=d = (e+t*dr)*n=d = n*e+n*t*dr=d
// t = (d-n*e)/(n*dr)
float t = (d - Vector3f::dot(normal, r.getOrigin()) ) / (Vector3f::dot(normal,r.getDirection()));
if (t > 0.0f // Positive, has intersection
&& t > tmin // Greater than tmin
&& t < h.getT() // Closest than previous hit.t
) {
h.set(t, material, normal);
return true;
}
return false;
}
protected:
};
#endif //PLANE_H