-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayTracer.java
More file actions
140 lines (120 loc) · 4.36 KB
/
RayTracer.java
File metadata and controls
140 lines (120 loc) · 4.36 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
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.Math;
/** Main class to do ray tracing
* @author Carol Chen
*/
public class RayTracer {
private static int nx, ny;
public static void main(String args[]) {
System.out.println("Starting tracer");
long startTime = System.nanoTime();
long lastTime = startTime;
long timeOnWriting = 0;
long timeOnTracing = 0;
nx = 800;
ny = 800;
int ns = 1200;
BufferedWriter out;
try {
File imageFile = new File("test.ppm");
if (!imageFile.exists()) {
imageFile.createNewFile();
}
FileWriter fw = new FileWriter(imageFile);
out = new BufferedWriter(fw);
out.write("P3\n");
out.write(tos(nx));
out.write(" ");
out.write(tos(ny));
out.write("\n225\n");
timeOnWriting += System.nanoTime() - lastTime;
lastTime = System.nanoTime();
Camera camera = planetarySceneCam();
Scene world = Scene.planetaryScene();
timeOnTracing += System.nanoTime() - lastTime;
lastTime = System.nanoTime();
for (int j = ny - 1; j >= 0; j--) {
for (int i = 0; i < nx; i++) {
Vector colour = new Vector(0, 0, 0);
for (int s = 0; s < ns; s++) {
double u = (i + Math.random()) / (nx * 1.0);
double v = (j + Math.random()) / (ny * 1.0);
Ray r = camera.getRay(u, v);
Vector p = r.pointAt(2);
colour.addEquals(colour(r, world, 0));
}
colour.divideEquals(ns);
colour = new Vector(Math.min(1, Math.sqrt(colour.r())), Math.min(1, Math.sqrt(colour.g())), Math.min(1, Math.sqrt(colour.b())));
int ir = (int)(255.99 * colour.x());
int ig = (int)(255.99 * colour.y());
int ib = (int)(255.99 * colour.z());
timeOnTracing += System.nanoTime() - lastTime;
lastTime = System.nanoTime();
out.write(tos(ir));
out.write(" ");
out.write(tos(ig));
out.write(" ");
out.write(tos(ib));
out.write("\n");
timeOnWriting += System.nanoTime() - lastTime;
lastTime = System.nanoTime();
}
System.out.println(1.0*(ny - j) / ny * 100 + " % done");
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Finished at " + (System.nanoTime() - startTime) / 1000000000.0);
}
private static String tos(int n) {
return Integer.toString(n);
}
private static Vector colour(Ray r, Scene world, int depth) {
FormHit hit = new FormHit();
if (world.hit(r, 0.001, Double.MAX_VALUE, hit)) {
Ray scattered = new Ray();
Vector attenuation = new Vector();
Vector emitted = hit.mat.emitted(hit.u, hit.v, hit.p);
if (depth < 1000 && hit.mat.scatter(r, hit, attenuation, scattered)) {
return colour(scattered, world, depth + 1).multiply(attenuation).add(emitted);
} else {
return emitted;
}
} else {
return new Vector(0, 0, 0);
// Vector unit = r.direction().unitVector();
// double t = 0.5 * (unit.y() + 1.0);
// return (new Vector(1, 1, 1).multiply(1 - t)).add(new Vector(0.5, 0.7, 1).multiply(t));
}
}
private static Camera getCornellCam() {
Vector lookFrom = new Vector(278, 278, -800);
Vector lookAt = new Vector(278, 278, 0);
double focusDist = lookFrom.subtract(lookAt).length();
double aperture = 0;
Camera camera = new Camera(lookFrom, lookAt, new Vector(0, 1, 0), 40, Double.valueOf(nx) / ny, aperture, focusDist, 0, 1);
return camera;
}
private static Camera getRandomSceneCam() {
Vector lookFrom = new Vector(13,4,3);
Vector lookAt = new Vector(0, 0, 0);
double focusDist = 10;
double aperture = 0;
Camera camera = new Camera(lookFrom, lookAt, new Vector(0, 1, 0), 40, Double.valueOf(nx) / ny, aperture, focusDist, 0, 1);
return camera;
}
private static Camera planetarySceneCam() {
Vector lookFrom = new Vector(10, 14, 20);
Vector lookAt = new Vector(0, 0, 0);
double focusDist = 10;
double aperture = 0;
Camera camera = new Camera(lookFrom, lookAt, new Vector(0, 1, 0), 50, Double.valueOf(nx) / ny, aperture, focusDist, 0, 1);
return camera;
}
}