faintBlack = new Color(0,0,0,64);
abs(t) { return Math.abs(t); }
cos(t) { return Math.cos(t); }
sin(t) { return Math.sin(t); }
pi = Math.PI;
px(v) { return px(v[0],v[1],v[2]); }
py(v) { return py(v[0],v[1],v[2]); }
px(x,y,z) { return (7 * x - 3.4 * z) / (10 - .1*z); }
py(x,y,z) { return (7 * y - 3.4 * z - .3*x) / (10 - .1*z); }
set(v, x, y, z) { v[0] = x; v[1] = y; v[2] = z; }
dot(v, w) { return v[0] * w[0] + v[1] * w[1] + v[2] * w[2]; }
diff(a, b, c) {
for (j = 0 ; j < 3 ; j++)
c[j] = a[j] - b[j];
}
Color gridColor = new Color(0, 0, 0, 32);
normalize(vec) {
norm = Math.sqrt(dot(vec, vec));
vec[0] /= norm;
vec[1] /= norm;
vec[2] /= norm;
}
drawEye(x, y) {
draw.setColor(Color.red);
draw.drawDisk(x,y-.01,.13,.07);
draw.fillDisk(x,y-.01,.13,.07);
draw.setColor(Color.white);
draw.drawText("eye",x,y);
draw.setColor(Color.black);
}
drawSphere(cx,cy,r,color) {
draw.setColor(color);
draw.fillDisk(cx,cy,r);
draw.setColor(Color.black);
draw.drawDisk(cx,cy,r);
}
drawArrow(ax,ay,bx,by) {
drawArrow(ax,ay,bx,by,0.02);
}
drawArrow(ax,ay,bx,by,double r) {
drawArrow(ax,ay,bx,by,r,Color.black);
}
drawArrow(ax,ay,bx,by,Color color) {
drawArrow(ax,ay,bx,by,0.02,color);
}
drawArrow(ax,ay,bx,by,Color color,bgColor) {
drawArrow(ax,ay,bx,by,0.02,color,bgColor);
}
drawArrow(ax,ay,bx,by,r,color) {
drawArrow(ax,ay,bx,by,0.02,color,Color.black);
}
drawArrow(ax,ay,bx,by,r,color,outlineColor) {
draw.setColor(color);
draw.fillArrow(ax,ay,bx,by,r);
draw.setColor(outlineColor);
draw.drawArrow(ax,ay,bx,by,r);
}
Ray tracing, part two
Ray tracing different shapes
Ray tracing to a plane
Equation of a ray and equation of a plane:
Equation of a ray: P = v + tw, for t 0
Equation of a plane: L = (a,b,c,d)
Point P is on plane L when PL = 0.
L(x,y,z,1) = ax + by + cz + d = 0
Solving for t where the ray intersects the plane:
ax + by + cz + d = 0
a(vx+twx) + b(vy+twy) + c(vz+twz) + d = 0
(awx + bwy + cwz) t + (avx + bvy + cvz + d) = 0
(wL) t + (vL) = 0;
t = -(vL) / (wL)
Finding the intersection point P:
P = v + tw
There are a number of cases:
(1) The ray intersects the plane at t 0 (one root).
(2) The line containing the ray intersects the plane at t 0 (no root).
(3) The ray is parallel to the plane (zero roots).
(4) The ray is within the plane (infinitely many roots).
Transforming a plane equation.
Consider a plane L containing a point P. In other words, LP = 0.
When we transform P by matrix M to get MP, we need the transformed plane to also contain the transformed point.
As we learned in a previous lecture, L is transformed to LM-1, because:
(LM-1) (MP) =
L (M-1 M) P =
LP
Also, remember that LM-1 is the same as (M-1 )TL
Ray tracing to a cylinder
x2 + y2 - 1 = 0
(vx + t wx)2 + (vy + t wy)2 - 1 = 0
(wx2 + wy2) t2 + (2 vx wx + 2 vy wy) t + (vx2 + vy2 - 1) = 0
(ww) t2 + 2(vw) t + (vv - 1) = 0
So to solve the quadratic equation in t:
A = ww
B = 2 (vw)
C = vv - 1
Ray tracing to a general quadratic
(1) Describe quadratic in its untransformed form.
ax2 + by2 + cz2 + dyz + ezx + fxy + gx + hy + iz + j = 0
Unit sphere: x2 + y2 + z2 - 1 = 0
[ 1 1 1 0 0 0 0 0 0 -1 ]
Cylindrical tube along z axis: x2 + y2 - 1 = 0
[ 1 1 0 0 0 0 0 0 0 -1 ]
Cone along z axis: x2 + y2 - z2 = 0
[ 1 1 -1 0 0 0 0 0 0 0 ]
(2) Apply matrix transformation.
Plane equation ax + by + cz + d, which describes the plane L containing points x, can be defined by the following dot product:
(a b c d)(x y z 1)T = 0
In other words, LxT = 0.
So to find the plane that contains transformed points Mx, L L M-1.
Similarly, we can define quadratic equation
ax2 + by2 + cz2 + dyz + ezx + fxy + gx + hy + iz + j = 0
by the following double dot product:
In other words, x Q xT = 0, where Q denotes the 10 quadratic coefficients arranged into the above 44 matrix.
This means that if you want to find the three-variable quadratic equation that contains transformed points MxT, you need to replace Q by (M-1)T Q M-1, because that will give you:
(MxT)T (M-1)T Q M-1 (MxT) =
xT (MT(M-1)T) Q (M-1 M) xT =
x Q xT = 0
Note that when you create the product of:
(M-1)T Q M-1 you can end up with non-zero values in all sixteen slots of your 44 matrix. This is because in addition to x*y, x*z, x*1, y*z, y*1, z*1 terms, you also end up with y*x, z*y, 1*z, z*y, 1*z, 1*z terms, as follows:
Since multiplication of numbers is commutative, you can just add the six numbers in the lower left (shown in magenta above) into their mirror locations on the upper right, as follows:
This will leave you with just the ten unique coefficients of the quadratic in three variables, in the upper right of your transformed matrix.
(3) Substitute the ray equation into the surface equation.
Replace (x,y,z) by ( vx+twx , vy+twy , vz+twz ).
a(vx+twx)2 + b(vy+twy)2 + c(vz+twz)2 +
d(vy+twy)(vz+twz) + e(vz+twz)(vx+twx) + f(vx+twx)(vy+twy) +
g(vx+twx) + h(vy+twy) + i(vz+twz) + j = 0
Multiply everything out:
a(vx+twx)(vx+twx) + b(vy+twy)(vy+twy) + c(vz+twz)(vz+twz) +
d(vy+twy)(vz+twz) + e(vz+twz)(vx+twx) + f(vx+twx)(vy+twy) +
g(vx+twx) + h(vy+twy) + i(vz+twz) + j = 0
(awx2 + bwy2 + cwz2 + dwywz + ewzwx + fwxwy) t2 +
(2avxwx + 2bvywy + 2cvzwz + d(vywz+vzwy) + e(vzwx+vxwz) + f(vxwy+vywx) + gwx + hwy + iwz) t +
(avx2 + bvy2 + cvz2 + dvyvz + evzvx + fvxvy + gvx + hvy + ivz + j) = 0
To solve the quadratic equation:
A = awx2 + bwy2 + cwz2 + dwywz + ewzwx + fwxwy
B = 2(avxwx + bvywy + cvzwz) + d(vywz+vzwy) + e(vzwx+vxwz) + f(vxwy+vywx) + gwx + hwy + iwz
C = avx2 + bvy2 + cvz2 + dvyvz + evzvx + fvxvy + gvx + hvy + ivz + j
(4) Solve for t and find point.
P = v + tw
x = vx + t wx
y = vy + t wy
z = vz + t wz
(5) Compute surface normal.
The normal direction aligns with the derivative of:
ax2 + by2 + cz2 + dyz + ezx + fxy + gx + hy + iz + j = 0
N = ( 2ax+ez+fy+g, 2by+dz+fx+h, 2cz+dy+ex+i )
normalize N to unit length
Combining shapes
Boolean combinatorial geometry
Union
Difference
Intersection
Making cool shapes using intersection
Intersection from the perspective of the ray
AB = [ max(A0,B0) ... min(A1,B1) ]
Union from the perspective of the ray
AB = [ min(A0,B0) ... max(A1,B1) ]
Difference from the perspective of the ray
Concave shapes
Generalized boolean
Algorithm:
Intersection: two input sequences output sequence
Difference: take negative of input sequence
Union: AB = - ((-A)(-B))
Shading, lighting, materials for ray tracing
Light sources not at infinity
A point source drops off in intensity with distance D from the surface:
Illuminance *= 1 / D2
An extended light source drops off more slowly
Illuminance *= 1 / Dpower, where power 2
Sky and fog
Rays that don't hit anything can return a "sky gradient":
Map the y component of ray direction w to different (r,g,b) values.
You can also add fog.
In fog visibility decreases exponentially with distance
Fog can greatly increase the drama of a scene
Measure distance D ray travels (before it hits something)
Compute visibility v = 2-kD Mix: result = v*rayColor + (1-v)*fogColor
Reflection
We need to create a secondary ray in R direction, and keep going, recursively.
v S + w
w R
where S is the surface point.
This ray will return a color.
Then we need to mix that color into the result.
Refraction
For glass materials, apply Snell's law, according to index of refraction.
Index of refraction = 1/velocity
sin 1 / sin 2 = n2 / n1
Create new ray:
v S + Q
w Q
Mix in resulting color
Texturing
Procedural texture
noise-based (more on this later)
Texture mapping
We will learn about this at the next lecture.
Applying texture
modifying reflectance values (ambient,diffuse,specular+power)
"bump mapping": changing surface normal
Rendering issues
Ray tracing can be slow.
To speed it up:
Start by looking at only every 44 pixels.
See where there are big changes in depth or color.
In those regions, shoot a ray at every pixel.
To create antialiased edges:
Look for pixels with color very different from neighbors
Shoot 44 rays within that pixel
Sub-pixel offsets: [x + (double)i/4, y + (double)j/4] Average all 16 results to get final color.
HOMEWORK
Your next homework is due on Wednesday May 8 (three weeks from this past lecture). Implement as many feature of ray tracing as you would like. Obviously those of you who are extremely motivated to learn, and consequently implement many features, will receive extra credit.
Among the features you can implement are: light sources not at infinity, transformation, quadratic surfaces (and matrix transformations of them), boolean shape modeling (intersection, union, difference), shadows, reflection, refraction, antialiasing, subsampling for efficiency, procedural texturing.
You can also feel free to surprise me by implementing other features as well.
Because the next assignment is not due until May 8, it would be a very bad idea to wait until very near to that deadline. You should get to work soon, so that you will be able to get a jump on things. If you have any questions, feel free to send me email.
Also, because of a problem with the grading of the "zbuffering with normals rendering" assignment, I am also giving you until May 8 to hand in the "zbuffering with Phong reflectance" assignment.
Notes about the noise function
Java implementation of noise