-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRay.java
More file actions
142 lines (117 loc) · 4.61 KB
/
Copy pathRay.java
File metadata and controls
142 lines (117 loc) · 4.61 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* The MIT License
*
* Copyright 2023 akava.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package ptjava;
import java.util.concurrent.ThreadLocalRandom;
import ptjava.Hit.HitInfo;
public class Ray {
public Vector Origin, Direction;
public boolean reflected;
boolean reflect;
public Ray(Vector Origin, Vector Direction) {
this.Origin = Origin;
this.Direction = Direction;
}
public Vector Position(double t) {
return Origin.Add(Direction.MulScalar(t));
}
public Ray Reflect(Ray i) {
return new Ray(Origin, Direction.Reflect(i.Direction));
}
public Ray Refract(Ray i, double n1, double n2) {
return new Ray(Origin, Direction.Refract(i.Direction, n1, n2));
}
public double Reflectance(Ray i, double n1, double n2) {
return this.Direction.Reflectance(i.Direction, n1, n2);
}
public Ray WeightedBounce(double u, double v, ThreadLocalRandom rand) {
var radius = Math.sqrt(u);
var theta = 2 * Math.PI * v;
var s = Direction.Cross(Vector.RandomUnitVector(rand)).Normalize();
var t = Direction.Cross(s);
var d = s.MulScalar(radius * Math.cos(theta))
.Add(t.MulScalar(radius * Math.sin(theta)))
.Add(Direction.MulScalar(Math.sqrt(1 - u)));
return new Ray(Origin, d);
}
public Ray ConeBounce(double theta, double u, double v, ThreadLocalRandom rand) {
return new Ray(this.Origin, Util.Cone(Direction, theta, u, v, rand));
}
public BounceResult Bounce(HitInfo info, double u, double v, BounceType bounceType, ThreadLocalRandom rand) {
Ray n = info.Ray;
Material material = info.material;
double n1 = 1.0;
double n2 = material.Index;
if (info.Inside) {
double swap = n1;
n1 = n2;
n2 = swap;
}
double p;
if(material.Reflectivity >= 0) {
p = material.Reflectivity;
} else {
p = n.Reflectance(this, n1, n2);
}
boolean reflect = false;
switch (bounceType) {
case BounceTypeAny:
reflect = rand.nextDouble() < p;
break;
case BounceTypeDiffuse:
reflect = false;
break;
case BounceTypeSpecular:
reflect = true;
break;
}
if (reflect) {
var reflected = n.Reflect(this);
return new BounceResult(reflected.ConeBounce(material.Gloss, u, v, rand), true, p);
} else if (material.Transparent) {
var refracted = n.Refract(this, n1, n2);
return new BounceResult(refracted.ConeBounce(material.Gloss, u, v, rand), true, 1-p);
} else {
return new BounceResult(n.WeightedBounce(u, v, rand), false, 1 - p);
}
}
public static class BounceResult {
private Ray ray;
private boolean reflected;
private double probability;
public BounceResult(Ray ray, boolean reflected, double probability) {
this.ray = ray;
this.reflected = reflected;
this.probability = probability;
}
public Ray getRay() {
return ray;
}
public boolean isReflected() {
return reflected;
}
public double getProbability() {
return probability;
}
}
}