-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayTraceShadows.java
More file actions
173 lines (156 loc) · 5.5 KB
/
RayTraceShadows.java
File metadata and controls
173 lines (156 loc) · 5.5 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* RayTraceShadows illustrates some basics of Java 2D.
* This version is compliant with Java 1.2 Beta 3, Jun 1998.
* Please refer to: <BR>
* http://www.javaworld.com/javaworld/jw-07-1998/jw-07-media.html
* <P>
* @author Bill Day <bill.day@javaworld.com>
* @version 1.0
* @see java.awt.Graphics2D
**/
/**
Geoffrey Matthews modified this code to show how to make
an image pixel by pixel.
13 April 2017
**/
/**
William Tyas modified this code to implement a ray tracer.
9 August 2017
**/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class RayTraceShadows extends Frame {
public static final int WIDTH = 512;
public static final int HEIGHT = 512;
public static final Color BACKGROUND = new Color(0.4f, 0.6f, 0.8f);
public static final nTuple LIGHT = new nTuple(1.0f, 1.0f, 1.0f).normalize();
public static final nTuple LIGHT_BASIS_2 = new nTuple(5.0f, -3.0f, -2.0f).normalize();
public static final nTuple LIGHT_BASIS_3 = new nTuple(1.0f, 7.0f, -8.0f).normalize();
public static final float IMG_PLANE_SZ = 10.0f;
public static final float CAM_Z = 20.0f;
public static Quadtree tree;
public static Quadtree shadowTree;
/*
* Main entry point
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numSpheres = howManySpheres(input);
int treeDepth = howDeep(input);
tree = new Quadtree(-IMG_PLANE_SZ,
-IMG_PLANE_SZ,
IMG_PLANE_SZ,
IMG_PLANE_SZ,
treeDepth,
CAM_Z);
shadowTree = new Quadtree(-IMG_PLANE_SZ * 5,
-IMG_PLANE_SZ * 5,
IMG_PLANE_SZ * 5,
IMG_PLANE_SZ * 5,
treeDepth,
CAM_Z);
ArrayList<Sphere> spheres = new ArrayList<Sphere>();
for (int i = 0; i < numSpheres; i++) {
Sphere s = randSphere(LIGHT, LIGHT_BASIS_2, LIGHT_BASIS_3);
tree.addSphere(s);
shadowTree.addShadowSphere(s);
spheres.add(s);
}
Statistics stats = new Statistics(spheres);
stats.generateUsefulInfo();
new RayTraceShadows();
}
public static int howManySpheres(Scanner input) {
System.out.print("How many spheres do you want drawn? ");
return input.nextInt();
}
public static int howDeep(Scanner input) {
System.out.print("How deep do you want the quadtree to be? (Less than 10 recommended)? ");
return input.nextInt();
}
public static Sphere randSphere(nTuple u1, nTuple u2, nTuple u3) {
float x = (float) Math.random() * 16.0f - 8.0f;
float y = (float) Math.random() * 16.0f - 8.0f;
float z = (float) Math.random() * 16.0f - 8.0f;
float radius = (float) Math.random() * 0.1f + 0.05f;
float r = (float) Math.random();
float g = (float) Math.random();
float b = (float) Math.random();
return new Sphere(x, y, z, radius, r, g, b, u1, u2, u3);
}
public Color getColor(int x, int y) {
nTuple p = new nTuple(0.0f, 0.0f, CAM_Z); // camera point
nTuple q = imagePlaneCoord(x, y); // point on image plane
ray ray = new ray(p, q.subtract(p));
double closestHit = Double.POSITIVE_INFINITY;
float intersection = 0.0f; // t-value for ray to intersect sphere
Sphere closestSphere = null;
SphereList intersectSpheres = tree.getSpheres(q.getX(), q.getY());
while (intersectSpheres != null) { // Find closest sphere
Sphere next = intersectSpheres.getSphere();
intersection = ray.intersectSphere(next);
if (intersection > 0.01f && intersection < closestHit) {
closestHit = intersection;
closestSphere = next;
}
intersectSpheres = intersectSpheres.getNext();
}
if (closestSphere != null) {
nTuple IntPt = ray.pointAlongRay((float) closestHit);
boolean inShadow = inShadow(IntPt);
return closestSphere.shadeSphere(IntPt, LIGHT, inShadow);
} else {
return BACKGROUND;
}
}
// Check if a point on a sphere is in shadow
public boolean inShadow(nTuple point) {
nTuple coords = new nTuple(point.coordChange(LIGHT,
LIGHT_BASIS_2,
LIGHT_BASIS_3,
point));
SphereList shadowIntersect = shadowTree.getSpheres(coords.getY(), coords.getZ());
ray shadowRay = new ray(new nTuple(), LIGHT); // terminal at origin makes math easier
boolean inShadow = false;
while (!inShadow && (shadowIntersect != null)) {
Sphere next = new Sphere(shadowIntersect.getSphere());
next.setCenter(next.getCenter().subtract(point));
float intersection = shadowRay.intersectSphere(next);
if (intersection > 0.0f) {
inShadow = true;
}
shadowIntersect = shadowIntersect.getNext();
}
return inShadow;
}
public nTuple imagePlaneCoord(float u, float v) {
return new nTuple(this.IMG_PLANE_SZ * (2*u/(float)WIDTH - 1),
-1.0f * this.IMG_PLANE_SZ * (2*v/(float)HEIGHT - 1),
0.0f);
}
/**
* Our RayTraceShadows constructor sets the frame's size, adds the
* visual components, and then makes them visible to the user.
* It uses an adapter class to deal with the user closing
* the frame.
**/
public RayTraceShadows() {
super("RayTracer");
setSize(WIDTH, HEIGHT);
setVisible(true);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{dispose(); System.exit(0);}
}
);
}
public void paint(Graphics g) {
for (int u = 0; u < WIDTH; u++) {
for (int v = 0; v < HEIGHT; v++) {
g.setColor(getColor(u, v));
g.drawLine(u, v, u, v);
}
}
}
}