-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathray.java
More file actions
87 lines (72 loc) · 2.32 KB
/
ray.java
File metadata and controls
87 lines (72 loc) · 2.32 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
/*
* ray.java
* Created by: William Tyas
* Date: 8/9/17
* Description: A ray used for raytracing.
*/
public class ray {
private nTuple camera; // terminal
private nTuple vector; // direction
private int reflectDepth; // Reflection depth
public nTuple getCamera() { return this.camera; }
public nTuple getVector() { return this.vector; }
public int getDepth() { return this.reflectDepth; }
public ray(nTuple p, nTuple v, int depth) {
this.camera = p;
this.vector = v.normalize();
this.reflectDepth = depth;
}
public void setDepth(int newDepth) {
this.reflectDepth = newDepth;
}
public float intersectSphere(Sphere s) {
// Move sphere and ray by same amount to simplify math
nTuple q = this.camera.subtract(s.getCenter());
float a = 1.0f; // dot product of normalized vector with itself
float b = 2.0f * (q.dot(this.vector));
float c = q.dot(q) - (s.getRadius() * s.getRadius());
float discriminant = (b*b) - 4*a*c;
if (discriminant < 0.0f) { // no intersection
return -1.0f;
} else { // no need to check both solutions, this is always largest
return (0.5f * (-b - (float) Math.sqrt(discriminant)));
}
}
public float intersectShadowSphere(Sphere s) {
nTuple q = this.camera.subtract(s.getCenter());
float a = 1.0f;
float b = 2.0f * (q.dot(this.vector));
float c = q.dot(q) - (s.getRadius() * s.getRadius());
float discriminant = (b*b) - 4*a*c;
if (discriminant < 0.0f) {
return 1.0f;
} else {
float value1 = 0.5f * (-b - (float) Math.sqrt(discriminant));
float value2 = 0.5f * (-b + (float) Math.sqrt(discriminant));
if (value2 < value1) {
return value2;
}
return value1;
}
}
public float reflectIntersect(Sphere s) {
nTuple q = this.camera.subtract(s.getCenter());
float a = 1.0f;
float b = 2.0f * (q.dot(this.vector));
float c = q.dot(q) - (s.getRadius() * s.getRadius());
float discriminant = (b*b) - 4*a*c;
if (discriminant < 0.0f) {
return 1.0f;
} else {
float value1 = 0.5f * (-b - (float) Math.sqrt(discriminant));
float value2 = 0.5f * (-b + (float) Math.sqrt(discriminant));
if (value2 < value1) {
return value2;
}
return value1;
}
}
public nTuple pointAlongRay(float t) {
return this.camera.add(this.vector.scale(t));
}
}