-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ying Liu
committed
Jan 28, 2016
0 parents
commit 4fe1e6c
Showing
73 changed files
with
795 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>RayTracer</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package geometry; | ||
|
||
import main.Driver; | ||
import scene.Light; | ||
import utility.Color; | ||
import utility.LightingCoeff; | ||
import utility.Point3D; | ||
import utility.Ray; | ||
import utility.Vector3D; | ||
|
||
public abstract class GeometricObject { | ||
|
||
public LightingCoeff k_d; | ||
public LightingCoeff k_s; | ||
public LightingCoeff amb; | ||
|
||
public abstract double hit(Ray ray); | ||
|
||
public abstract Vector3D getNormal(Point3D point); | ||
|
||
public GeometricObject(LightingCoeff k_d, LightingCoeff k_s, LightingCoeff amb) { | ||
this.k_d = k_d; | ||
this.k_s = k_s; | ||
this.amb = amb; | ||
} | ||
|
||
public Color getColorNoLight() { | ||
return new Color(amb.r, amb.g, amb.b); | ||
} | ||
public Color getColor(Point3D point, Light light) { | ||
Vector3D n = getNormal(point); | ||
|
||
// Initialize with ambiance | ||
double new_r = amb.r; | ||
double new_g = amb.g; | ||
double new_b = amb.b; | ||
|
||
Vector3D l = light.location.sub_vec(point); | ||
l.normalize(); | ||
n.normalize(); | ||
|
||
// Perform Diffuse Reflection | ||
double diffuse = Math.max(0.0, n.dot(l)); | ||
new_r += k_d.r * light.I * diffuse; | ||
new_g += k_d.g * light.I * diffuse; | ||
new_b += k_d.b * light.I * diffuse; | ||
|
||
// Perform Spectral Reflection | ||
Vector3D v = Driver.eye.sub_vec(point); | ||
v.normalize(); | ||
Vector3D h = v.add(l); | ||
h.normalize(); | ||
|
||
double spectral = Math.max(0.0, Math.pow(n.dot(h), 3)); | ||
new_r += k_s.r * light.I * spectral; | ||
new_g += k_s.g * light.I * spectral; | ||
new_b += k_s.b * light.I * spectral; | ||
|
||
return new Color(new_r, new_g, new_b); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package geometry; | ||
|
||
import utility.Color; | ||
import utility.LightingCoeff; | ||
import utility.Point3D; | ||
import utility.Ray; | ||
import utility.Vector3D; | ||
|
||
public class Plane extends GeometricObject { | ||
Point3D point; | ||
Vector3D normal; | ||
|
||
/* | ||
* Given a point on the plane as well as the normal The locus of the plane | ||
* is the set of all points such that (p - o) dot n = 0 | ||
*/ | ||
|
||
public Plane(Point3D point, Vector3D normal, LightingCoeff k_d, LightingCoeff k_s, LightingCoeff amb) { | ||
super(k_d, k_s, amb); | ||
this.point = new Point3D(point); | ||
normal.normalize(); | ||
this.normal = new Vector3D(normal); | ||
} | ||
|
||
@Override | ||
public double hit(Ray ray) { | ||
/* | ||
* (o + td - a) * n = 0 o*n + t(d*n) - a*n = 0 t(d*n) + (o-a)*n = 0 | ||
* t(d*n) = (a-o)*n t = (a-o)*n/d*n | ||
*/ | ||
double t = point.sub(ray.origin).dot(normal) / (ray.direction.dot(normal)); | ||
|
||
if (t > 10E-10) | ||
return t; | ||
else | ||
return 0.0; | ||
} | ||
|
||
@Override | ||
public Vector3D getNormal(Point3D point) { | ||
return normal; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package geometry; | ||
|
||
import utility.Color; | ||
import utility.LightingCoeff; | ||
import utility.Point3D; | ||
import utility.Ray; | ||
import utility.Vector3D; | ||
|
||
public class Sphere extends GeometricObject { | ||
|
||
public Point3D center; | ||
public double radius; | ||
|
||
public Sphere(Point3D center, double radius, LightingCoeff k_d, LightingCoeff k_s, LightingCoeff amb) { | ||
super(k_d, k_s, amb); | ||
this.center = new Point3D(center); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double hit(Ray ray) { | ||
/* | ||
* (p-c)*(p-c) = r^2 (o+td-c)*(o+td-c) -r^2= 0 (td+o-c)(td+o-c) - r^2 = | ||
* 0 (d*d)t + 2((o-c)*d)t + (o-c)*(o-c)-r^2 = 0 | ||
*/ | ||
|
||
Point3D o = ray.origin; | ||
Vector3D d = ray.direction; | ||
d.normalize(); | ||
|
||
double a = d.dot(d); | ||
double b = 2 * ((o.sub(center)).dot(d)); | ||
double c = (o.sub(center)).dot(o.sub(center)) - radius * radius; | ||
|
||
double disc = b * b - 4 * a * c; | ||
if (disc < 0) { | ||
return 0.0; // did not hit | ||
} | ||
|
||
double t = ((-b) - Math.sqrt(disc)) / (2 * a); | ||
if (t > 10E-9) | ||
return t; | ||
else | ||
return 0.0; // No intersection | ||
|
||
} | ||
|
||
@Override | ||
public Vector3D getNormal(Point3D point) { | ||
Vector3D n = point.sub_vec(center); | ||
n.normalize(); | ||
return n; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main; | ||
|
||
import projection.Perspective; | ||
import projection.Projection; | ||
import sampling.JitteredSample; | ||
import sampling.RegularSample; | ||
import sampling.Sampler; | ||
import scene.World; | ||
import utility.Image; | ||
import utility.Point3D; | ||
|
||
/** | ||
* @author yingliu | ||
* | ||
*/ | ||
public class Driver { | ||
public static World world; | ||
public static Image image; | ||
public static Tracer tracer; | ||
public static Sampler sampler; | ||
public static Projection projection; | ||
public static Point3D eye; | ||
|
||
public static void main(String[] args) { | ||
for (int i = 1; i <= 27; i++) { | ||
long start_time = System.nanoTime(); | ||
world = new World(500, 500, 1.0, i); | ||
image = new Image("ShadowImage" + Integer.toString(i) + ".png"); | ||
tracer = new Tracer(); | ||
sampler = new JitteredSample(2); | ||
eye = new Point3D(-200.0, 200.0, 600.0); | ||
projection = new Perspective(eye, new Point3D(), 45.0); | ||
|
||
int height = world.viewplane.height; | ||
int width = world.viewplane.width; | ||
|
||
for (int y = 0; y < height; y++) { | ||
for (int x = 0; x < width; x++) { | ||
tracer.trace(x, y, true); | ||
} | ||
} | ||
image.write("PNG"); | ||
long end_time = System.nanoTime(); | ||
System.out.println((end_time - start_time) / 1E9F); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package main; | ||
|
||
import geometry.GeometricObject; | ||
import scene.Light; | ||
import utility.Color; | ||
import utility.Point2D; | ||
import utility.Point3D; | ||
import utility.Ray; | ||
|
||
public class Tracer { | ||
|
||
public void trace(int x, int y, boolean shadow) { | ||
int width = Driver.world.viewplane.width; | ||
int height = Driver.world.viewplane.height; | ||
int samples = Driver.sampler.samples; | ||
|
||
Color color = new Color(); | ||
|
||
for (int row = 0; row < samples; row++) { | ||
for (int col = 0; col < samples; col++) { | ||
Point2D point = Driver.sampler.sample(row, col, x, y); | ||
Ray ray = Driver.projection.createRay(point); | ||
ray.direction.normalize(); | ||
double min_T = Double.MAX_VALUE; | ||
boolean firstHit = false; | ||
Color tempColor = new Color(Driver.world.background); | ||
for (GeometricObject gObject : Driver.world.objects) { | ||
double t = gObject.hit(ray); | ||
if (t != 0.0) { | ||
firstHit = true; | ||
if (t < min_T) { | ||
min_T = t; | ||
Point3D hitPoint = ray.hitPoint(t); | ||
// shading effects | ||
boolean intersect = false; | ||
for (Light light : Driver.world.lights) { | ||
Ray ray_shad = new Ray(hitPoint, light.location); | ||
if (shadow) { | ||
for (GeometricObject gObject2 : Driver.world.objects) { | ||
if (gObject2 != gObject) { | ||
if (gObject2.hit(ray_shad) != 0.0) { | ||
intersect = true; | ||
break; | ||
} | ||
} | ||
} | ||
if (!intersect) | ||
tempColor = gObject.getColor(hitPoint, light); | ||
else { | ||
tempColor = gObject.getColorNoLight(); | ||
} | ||
} else { | ||
tempColor = gObject.getColor(hitPoint, light); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
if (firstHit) | ||
color.add(tempColor); | ||
else { | ||
color.add(Driver.world.background); | ||
} | ||
} | ||
} | ||
color.divide(samples * samples); | ||
Driver.image.buffer.setRGB(width - x - 1, height - y - 1, color.toInteger()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package projection; | ||
|
||
import main.Driver; | ||
import utility.Point2D; | ||
import utility.Point3D; | ||
import utility.Ray; | ||
import utility.Vector3D; | ||
|
||
public class Orthographic extends Projection { | ||
|
||
@Override | ||
public Ray createRay(Point2D point) { | ||
double pixelSize = Driver.world.viewplane.size; | ||
return new Ray(new Point3D(pixelSize * point.x, pixelSize * point.y, 100.0), new Vector3D(0.0, 0.0, -1.0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package projection; | ||
|
||
import main.Driver; | ||
import utility.Point2D; | ||
import utility.Point3D; | ||
import utility.Ray; | ||
|
||
public class Perspective extends Projection{ | ||
|
||
public Perspective(Point3D eye, Point3D lookat, double FOV) { | ||
this.eye = new Point3D(eye); | ||
this.lookat = new Point3D(lookat); | ||
this.distance = Driver.world.viewplane.height/2/Math.tan(Math.toRadians(FOV)); | ||
|
||
compute_uvw(); | ||
} | ||
|
||
@Override | ||
public Ray createRay(Point2D point) { | ||
Ray ray = new Ray(new Point3D(eye), u.mult(point.x).add(v.mult(point.y).sub(w.mult(distance)))); | ||
ray.direction.normalize(); | ||
|
||
return ray; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package projection; | ||
|
||
import utility.Point2D; | ||
import utility.Point3D; | ||
import utility.Ray; | ||
import utility.Vector3D; | ||
|
||
public abstract class Projection { | ||
|
||
public Point3D eye; | ||
// Shoot out to the look at | ||
public Point3D lookat; | ||
public double distance; | ||
public Vector3D u, v, w; | ||
|
||
public abstract Ray createRay(Point2D point); | ||
|
||
public void compute_uvw() { | ||
w = eye.sub_vec(lookat); | ||
w.normalize(); | ||
|
||
Vector3D up = new Vector3D(0.00424, 1.0, 0.00764); | ||
|
||
u = up.cross(w); | ||
up.normalize(); | ||
|
||
v = w.cross(u); | ||
v.normalize(); | ||
} | ||
|
||
} |
Oops, something went wrong.