Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ying Liu committed Jan 28, 2016
0 parents commit 4fe1e6c
Show file tree
Hide file tree
Showing 73 changed files with 795 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
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>
17 changes: 17 additions & 0 deletions .project
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>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
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
Binary file added Image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage26.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage27.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ShadowImage7.png
Binary file added ShadowImage8.png
Binary file added ShadowImage9.png
Binary file added bin/geometry/GeometricObject.class
Binary file not shown.
Binary file added bin/geometry/Plane.class
Binary file not shown.
Binary file added bin/geometry/Sphere.class
Binary file not shown.
Binary file added bin/main/Driver.class
Binary file not shown.
Binary file added bin/main/Tracer.class
Binary file not shown.
Binary file added bin/projection/Orthographic.class
Binary file not shown.
Binary file added bin/projection/Perspective.class
Binary file not shown.
Binary file added bin/projection/Projection.class
Binary file not shown.
Binary file added bin/sampling/JitteredSample.class
Binary file not shown.
Binary file added bin/sampling/RegularSample.class
Binary file not shown.
Binary file added bin/sampling/Sampler.class
Binary file not shown.
Binary file added bin/scene/Light.class
Binary file not shown.
Binary file added bin/scene/ViewPlane.class
Binary file not shown.
Binary file added bin/scene/World.class
Binary file not shown.
Binary file added bin/utility/Color.class
Binary file not shown.
Binary file added bin/utility/Image.class
Binary file not shown.
Binary file added bin/utility/LightingCoeff.class
Binary file not shown.
Binary file added bin/utility/Point2D.class
Binary file not shown.
Binary file added bin/utility/Point3D.class
Binary file not shown.
Binary file added bin/utility/Ray.class
Binary file not shown.
Binary file added bin/utility/Vector3D.class
Binary file not shown.
62 changes: 62 additions & 0 deletions src/geometry/GeometricObject.java
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);
}

}
44 changes: 44 additions & 0 deletions src/geometry/Plane.java
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;
}

}
55 changes: 55 additions & 0 deletions src/geometry/Sphere.java
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;
}

}
47 changes: 47 additions & 0 deletions src/main/Driver.java
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);
}
}
}
71 changes: 71 additions & 0 deletions src/main/Tracer.java
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());
}
}
16 changes: 16 additions & 0 deletions src/projection/Orthographic.java
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));
}
}
27 changes: 27 additions & 0 deletions src/projection/Perspective.java
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;

}

}
31 changes: 31 additions & 0 deletions src/projection/Projection.java
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();
}

}
Loading

0 comments on commit 4fe1e6c

Please sign in to comment.