-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene_sceneobject_Plane.pde
63 lines (55 loc) · 2.08 KB
/
scene_sceneobject_Plane.pde
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
/**
* Represents a one-sided infinite plane. The plane is only visible from the side that the normal vector points toward.
*/
class Plane extends SceneObject {
/** A point which, by definition, the plane passes through. */
public Vector3 origin;
/** The normal vector of the plane. The plane is only visible from the side that this vector points toward. */
public Vector3 normal;
/**
* Constructs a Plane.
* @param origin a point on the plane.
* @param normal the normal vector of the plane.
* @param albedo the albedo of the surface (passed on to {@link SceneObject(int)}).
*/
Plane(Vector3 origin, Vector3 normal, color albedo) {
super(albedo);
this.origin = origin;
this.normal = normal.normalize();
}
/**
* Constructs a Plane object from the values in the JSONObject (JSON deserialization).
* @param j the JSONObject containing the values for this object.
*/
public Plane(JSONObject j) {
super(j);
this.origin = new Vector3(j.getJSONObject("origin"));
this.normal = new Vector3(j.getJSONObject("normal")).normalize();
}
JSONObject toJSONObject() {
JSONObject j = super.toJSONObject();
j.setJSONObject("origin", this.origin.toJSONObject());
j.setJSONObject("normal", this.normal.toJSONObject());
return j;
}
float rayIntersect(Ray ray) {
float cosTheta = -this.normal.dot(ray.direction);
// If viewing from the wrong side of the plane, return zero, representing no ray intersection.
if (cosTheta < 1e-6)
return 0;
// Computes the distance between the ray origin the hit point.
Vector3 co = ray.origin.minus(this.origin);
float t = co.dot(this.normal) / cosTheta;
return t;
}
Vector3 getNormal(Vector3 origin) {
return this.normal;
}
// implements Tweakable
ArrayList<ParameterControl> getParameters() {
ArrayList<ParameterControl> parameters = super.getParameters();
parameters.add(new Vector3Parameter(this, "origin", "Origin", this.origin, -5, 5));
parameters.add(new Vector3Parameter(this, "normal", "Normal", this.normal, -1, 1));
return parameters;
}
}