-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayTrace.java
More file actions
127 lines (107 loc) · 5.59 KB
/
RayTrace.java
File metadata and controls
127 lines (107 loc) · 5.59 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
/* File: RayTrace.java
* Date: 4/20/17
* Student: William Tyas
* ID: W01203451
* Class: CSCI 241, Spring 2017
* Description: Draws spheres on the screen using ray tracing.
*/
import java.awt.*;
import java.util.*;
import java.io.*;
public class RayTrace {
public static void main(String args[]) throws FileNotFoundException {
// Set up world
int imagePlaneSize = 10;
int s = imagePlaneSize;
World world = readWrlFile(s, args);
nTuple backgroundColor = new nTuple(0.5f, 0.6f, 0.8f);
// Create an image
Image image = new Image();
// Insert spheres into quadtree prior to ray tracing
int quadtreeLevels = 3; // levels in the quadtree
Quadtree quadtree = world.setUpQuadtree(quadtreeLevels);
// Draw pixel
drawPixel(world, image, backgroundColor, quadtree);
}
// Reads a world file from the command line and sets up the world
public static World readWrlFile(int imagePlaneSize, String[] args) throws FileNotFoundException{
// Read from world file specified by command line argument
Scanner input = new Scanner(new File(args[0]));
nTuple lightDirection = new nTuple();
ArrayList<Sphere> objectList = new ArrayList<Sphere>();
//first line is camera
String camera = input.next();
double cameraDistance = input.nextDouble();
//second line is light
String light = input.next();
lightDirection.setX((float) input.nextDouble());
lightDirection.setY((float) input.nextDouble());
lightDirection.setZ((float) input.nextDouble());
// Read spheres from file
while (input.hasNextLine()) {
//rest of the lines are spheres
nTuple sphereCenter = new nTuple();
String sphere = input.next();
sphereCenter.setX((float) input.nextDouble());
sphereCenter.setY((float) input.nextDouble());
sphereCenter.setZ((float) input.nextDouble());
float sphereRadius = (float) input.nextDouble();
nTuple sphereColor = new nTuple();
sphereColor.setX((float) input.nextDouble());
sphereColor.setY((float) input.nextDouble());
sphereColor.setZ((float) input.nextDouble());
Sphere nextSphere = new Sphere(sphereCenter, sphereRadius, sphereColor);
objectList.add(nextSphere);
}
return new World((float) cameraDistance, imagePlaneSize, lightDirection, objectList);
}
// Determine what color the pixel should be and draw it
public static void drawPixel(World world, Image image, nTuple backgroundColor, Quadtree quadtree) {
int w = image.getWIDTH();
int h = image.getHEIGHT();
nTuple p = world.camera();
int s = world.imagePlaneSize();
nTuple lightDirection = world.lightDirection();
ArrayList<Sphere> objectList = world.getObjectList();
nTuple v = new nTuple();
Sphere closestSphere = new Sphere();
float closestZ = (float) Double.NEGATIVE_INFINITY;
// Iterate over pixels in image
for (int u = 0; u < w; u++) {
for (int d = 0; d < h; d++) {
// Keep track of intersections
boolean intersection = false;
boolean currentIntersection = false;
ray ray = new ray();
ray.imagePlaneCoords(world, s, u, d, w, h); // get image plane coordinates
if (!world.sphereIntersects(quadtree)) { // no intersection with sphere, paint background color
Color pixelColor = new Color(backgroundColor.x(), backgroundColor.y(), backgroundColor.z());
image.paint(image.getGraphics(), pixelColor, u, d, u, d);
} else {
// Shoot ray towards image plane
ray = ray.shootRay(world, v, p, s, u, d, w, h);
for (int i = 0; i < objectList.size(); i++) {
//Move sphere to origin and camera by same translation vector to simplify math
nTuple q = new nTuple(ray.camera().x() - objectList.get(i).center().x(), ray.camera().y() - objectList.get(i).center().y(), ray.camera().z() - objectList.get(i).center().z());
Sphere translatedSphere = new Sphere(new nTuple(0.0f, 0.0f, 0.0f), objectList.get(i).r(), objectList.get(i).color());
// Check if ray intersects sphere
intersection = ray.isIntersect(q, v, translatedSphere, intersection);
if (intersection) {
currentIntersection = true;
}
// If current sphere intersected, check if closest sphere
if (currentIntersection && ray.direction().z() > closestZ) {
closestZ = ray.direction().z();
closestSphere = world.getObject(i);
}
currentIntersection = false;
}
closestZ = (float) Double.NEGATIVE_INFINITY;
// Determine pixel color and draw pixel
Color pixelColor = world.getColor(intersection, image, backgroundColor, closestSphere);
image.paint(image.getGraphics(), pixelColor, u, d, u, d);
}
}
}
}
}