






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Lecture 17
Slide 1
6.837 Fall 2001
Lecture 17
Slide 2
6.837 Fall 2001
Lecture 17
Slide 3
6.837 Fall 2001
Lecture 17
Slide 4
6.837 Fall 2001
t^0
Lecture 17
Slide 5
6.837 Fall 2001
Lecture 17
Slide 6
6.837 Fall 2001
Lecture 17
Slide 7
6.837 Fall 2001
Lecture 17
Slide 8
6.837 Fall 2001
Lecture 17
Slide 13
6.837 Fall 2001
d
transmitte
reflected
direct
d
transmitte
t
d
transmitte
nshiny
s
d
light
ambient a
direct
E
P
d
d
ˆ N L^
reflected
direct
reflected
r
reflected
Lecture 17
Slide 14
6.837 Fall 2001
Eye
Lecture 17
Slide 15
6.837 Fall 2001
r i t i t
sin sin
N
M
T^
t
t^
ˆ
cos ˆ
sin ˆ
θ
θ^
−
=
i^ i
I
N
M
sin
ˆ)
ˆcos ( ˆ^
−
=
N
I
N
T^
t
i
t^ i
ˆ
cos ˆ)
ˆcos ( sinsin ˆ
− −
=
I
N
T^
r
t
i r^
ˆ ˆ)
cos
cos ( ˆ
−
−
=
) ˆ) ˆ( (^1) (
1
sin 1
sin 1
cos
ˆ ˆ
cos
2
2
2 2
2
I N
I N
r
i
r
t
i t
⋅ − − = − = − =
I N I N I N
T^
r
r
r^
ˆ ˆ ) ˆ) ˆ( (^1) (
1 ˆ) ˆ(
ˆ^
2
2
−
^
⋅ − − − ⋅ =
ˆ N ˆ N −
ˆ M ˆ T
ˆ^ I
θ i
θ^ t I
N^
i^
ˆ
ˆ^ cos
−
i
N
ˆcos
the incoming ray
Lecture 17
Slide 16
6.837 Fall 2001
S
Lecture 17
Slide 17
6.837 Fall 2001
class Vector3D {public float x, y, z;// constructorspublic Vector3D( ) { }public Vector3D(float x, float y, float z);public Vector3D(Vector3D v);// methodspublic Vector3D to(Vector3D B)
// B - this
public float dot(Vector3D B);
// this with B
public float dot(float Bx, float By, float Bz);
// B spelled out
public static float dot(Vector3D A, Vector3D B);
// A dot B
public Vector3D cross(Vector3D B);
// this with B
public Vector3D cross(float Bx, float By, float Bz);
// B spelled out
public static Vector3D cross(Vector3D A, Vector3D B); // A cross Bpublic float length( );
// of this
public static float length(Vector3D A);
// of A
public void normalize( );
// makes this unit length
public static Vector3D normalize(Vector3D A);
// makes A unit length
public String toString();
// convert to a string
Lecture 17
Slide 18
6.837 Fall 2001
class Ray {
public static final float MAX_T = Float.MAX_VALUE;Vector3D origin, direction;float t;Renderable object;public Ray(Vector3D eye, Vector3D dir) {
origin = new Vector3D(eye);direction = Vector3D.normalize(dir); }public boolean trace(Vector objects) {
Enumeration objList = objects.elements();t = MAX_T;object = null;while (objList.hasMoreElements()) {
Renderable object = (Renderable) objList.nextElement();object.intersect(this); }return (object != null); }// ray.Shade(...) is nicer than ray.object.Shade(ray, ...)public final Color Shade(Vector lights, Vector objects, Color bgnd) {
return object.Shade(this, lights, objects, bgnd); } }
Lecture 17
Slide 19
6.837 Fall 2001
// All the public variables here are ugly, but I// wanted Lights and Surfaces to be "friends"class Light {
public static final int AMBIENT = 0;public static final int DIRECTIONAL = 1;public static final int POINT = 2;public int lightType;public Vector3D lvec;
// the position of a point light or// the direction to a directional light
public float ir, ig, ib;
// color of the light source
public Light(int type, Vector3D v, float r, float g, float b) {
lightType = type;ir = r;ig = g;ib = b;if (type != AMBIENT) {
lvec = v;if (type == DIRECTIONAL) {
lvec.normalize(); } } } }
Lecture 17
Slide 20
6.837 Fall 2001
// An object must implement a Renderable interface in order to// be ray traced. Using this interface it is straightforward// to add new objectsabstract interface Renderable {
public abstract boolean intersect(Ray r);public abstract Color Shade(Ray r, Vector lights, Vector objects, Color bgnd);
public String toString();
Lecture 17
Slide 25
6.837 Fall 2001
public Color Shade(Vector3D p, Vector3D n, Vector3D v, Vector lights,
Vector objects, Color bgnd) {
Enumeration lightSources = lights.elements();float r = 0, g = 0, b = 0;while (lightSources.hasMoreElements()) {Light light = (Light) lightSources.nextElement();if (light.lightType == Light.AMBIENT) {
// inc r,g,b by ambient contribution } else {
Vector3D l;if (light.lightType == Light.POINT) {
// Set l vector to point to light source } else {
// Set l vector to -light direction } // Check if the surface point is in shadow, if it is, go to next light.float lambert = Vector3D.dot(n,l);if (lambert > 0) {
if (kd > 0) {
// add in diffuse component
}if (ks > 0) {
// add in specular component
if (kr > 0) {
// Compute illumination due to reflection
}if (kt > 0) {
// Compute illumination due to reflection
}// make sure color components are within range.return new Color(r, g, b); }
Lecture 17
Slide 26
6.837 Fall 2001
while (lightSources.hasMoreElements()) {
Light light = (Light) lightSources.nextElement();if (light.lightType == Light.AMBIENT) {r += kairlight.ir;g += kaiglight.ig;b += kaiblight.ib;} else {Vector3D l;if (light.lightType == Light.POINT) {**
l = new Vector3D(light.lvec.x - p.x,
light.lvec.y - p.y,light.lvec.z - p.z);
l.normalize(); } else {
l = new Vector3D(-light.lvec.x,
-light.lvec.y,
-light.lvec.z);
Lecture 17
Slide 27
6.837 Fall 2001
// Check if the surface point is in shadowVector3D poffset;poffset = new Vector3D(p.x+TINYl.x, p.y+TINYl.y, p.z+TINYl.z);Ray shadowRay = new Ray(poffset, l);if (shadowRay.trace(objects))*
break;
// go to next light source
Lecture 17
Slide 28
6.837 Fall 2001
float lambert = Vector3D.dot(n,l);
// cos(theta)
if (lambert > 0) {
if (kd > 0) {
float diffuse = kdlambert;r += diffuseirlight.ir;g += diffuseiglight.ig;b += diffuseiblight.ib; }if (ks > 0) {*
lambert = 2;float spec = v.dot(lambertn.x - l.x,
lambertn.y - l.y,lambertn.z - l.z);**
if (spec > 0) {
spec = ks((float) Math.pow((double) spec, (double) ns));r += speclight.ir;g += speclight.ig;b += speclight.ib; } } } }**
Lecture 17
Slide 29
6.837 Fall 2001
// Compute illumination due to reflection
if (kr > 0) {float t = v.dot(n);if (t > 0) {
t = 2;Vector3D reflect = new Vector3D(tn.x - v.x,
tn.y - v.y,tn.z - v.z);**
Vector3D poffset = new Vector3D(p.x + TINYreflect.x,*
p.y + TINYreflect.y,p.z + TINYreflect.z);**
Ray reflectedRay = new Ray(poffset, reflect);if (reflectedRay.trace(objects)) {Color rcolor = reflectedRay.Shade(lights, objects, bgnd);r += krrcolor.getRed();g += krrcolor.getGreen();b += krrcolor.getBlue();} else {r += krbgnd.getRed();g += krbgnd.getGreen();b += krbgnd.getBlue();} }}**
Lecture 17
Slide 30
6.837 Fall 2001
if (kt > 0) {// Add refraction code here// Project 5!} r = (r > 1f)? 1f : r;g = (g > 1f)? 1f : g;b = (b > 1f)? 1f : b;return new Color(r, g, b);
Lecture 17
Slide 31
6.837 Fall 2001
public class RayTrace {Vector objectList, lightList;Color background = new Color(0,0,0);Image screen;Graphics gc;int height, width;boolean viewSet = false;Vector3D Eye, Du, Dv, Vp;public RayTrace(Vector objects, Vector lights, Image scr);public void setBackground(Color bgnd);public Image getScreen();public void setView(Vector3D eye, Vector3D lookat, Vector3D up, float fov);public void renderPixel(int i, int j) {
Vector3D dir = new Vector3D(iDu.x + jDv.x + Vp.x,**
iDu.y + jDv.y + Vp.y,iDu.z + jDv.z + Vp.z);**
Ray ray = new Ray(Eye, dir);if (ray.trace(objectList)) {gc.setColor(ray.Shade(lightList, objectList, background));} else {gc.setColor(background);}gc.drawLine(i, j, i, j);
// oh well, it works.
} public void renderScanLine(int j);public void renderScreen();}
That's basically all we need towrite a ray tracer. Comparedto a graphics pipeline the codeis very simple and easy tounderstand.
Lecture 17
Slide 32
6.837 Fall 2001
eye 0 3 10lookat 0 -1 0up 0 1 0fov 30background 0.2 0.8 0.9light 1 1 1 ambientlight 1 1 1 directional -1 -2 -1light 0.5 0.5 0.5 point -1 2 -1surface 0.7 0.2 0.
0.5 0.4 0.2 10.00.0 0.0 1. sphere -2 -3 -2 1.5sphere 0 -3 -2 1.5sphere 2 -3 -2 1.5sphere -1 -3 -1 1.5sphere 1 -3 -1 1.5sphere -2 -3 0 1.5sphere 0 -3 0 1.5sphere 2 -3 0 1.5sphere -1 -3 1 1.5sphere 1 -3 1 1.5sphere -2 -3 2 1.5sphere 0 -3 2 1.5sphere 2 -3 2 1.
surface 0.7 0.2 0.
0.5 0.4 0.2 3.00.0 0.0 1. sphere -1 -3 -2 1.5sphere 1 -3 -2 1.5sphere -2 -3 -1 1.5sphere 0 -3 -1 1.5sphere 2 -3 -1 1.5sphere -1 -3 0 1.5sphere 1 -3 0 1.5sphere -2 -3 1 1.5sphere 0 -3 1 1.5sphere 2 -3 1 1.5sphere -1 -3 2 1.5sphere 1 -3 2 1.50.0 0.0 1.0surface 0.4 0.4 0.
0.1 0.1 0.6 100.00.8 0.0 1. sphere 0 0 0 1
Lecture 17
Slide 37
6.837 Fall 2001
Lecture 17
Slide 38
6.837 Fall 2001
Another modeling technique is to combine the volumes occupied by overlapping3D shapes using set operations.
This creates a new volume by applying the
union, intersection, or difference operation to two volumes.
Lecture 17
Slide 39
6.837 Fall 2001
Lecture 17
Slide 40
6.837 Fall 2001
A^
B
t^0
t^1
t^2
t^3
Need to flip normals for B intersections
Lecture 17
Slide 41
6.837 Fall 2001
^
-^.
^
Lecture 17
Slide 42
6.837 Fall 2001
^
^
^
^
^
Lecture 17
Slide 43
6.837 Fall 2001
Lecture 17
Slide 44
6.837 Fall 2001