|
CS 371 Computer Graphics Spring 2005
|
|
Lecture 2: A Simple Ray Tracer
There is some basic, easy-to-digest information on graphics hardware that you
should know. It is in Chapter 1 of your text. Please read it. These notes
are on a completely different topic! Also, read Sections 4.1-4.3 on basic
properties of vectors.
Given a scene composed of geometric objects, produce an image (2D array of pixel values) of (part of) the scene.
- Keep it simple:
- Ambient light only (for today)
- No inter-object reflection
Project scene onto a rectangular region of a viewing plane
Alternately: Sample the scene by casting rays from the eye,
through the view plane, into the scene.
- Scene objects: Keep it simple: spheres
- Location of eye
- View plane (VP) and rectangular view region in VP
- Samples: One per pixel (for now).
- Eye: Point in R3
- VP: Keep it simple: a fixed plane, say x-y plane (z=0), with
left, right, bottom, top parameters. Later on, we'll make more flexible
- Ray: source & direction (points in R3)
- Sphere: center (point in R3), radius r>0, and material properties
- Material: ambient lighting co-efficients (point in R3).
Given a camera, some ambient light, and a scene (list of spheres),
we compute sampling rays from eye and determine closest (first)
point on list of spheres hit by ray.
Ambient lighting co-efficients are used for color of pixel
Pseudo-code
- Read in scene description:
- Location of eye
- ambient lighting Intensity Ia
- List of spheres with ambient lighting coefficients (see below)
- For each sample (i,j)
- Construct sampling ray (i,j)
- Intersect ray with scene to get closest point hit P (if any)
- If P is null (no intersection) set pixel (i,j) to background color
- Else use ambient lighting co-efficients of P's sphere to set
pixel (i,j)
- How do we describe a ray?
- How do we construct sampling rays?
- How do we intersect a ray with a sphere?
- How do we compute lighting value?
Given a point p=(px,py,pz) and a vector d=(dx,dy,dz),
the ray R through p in direction d is given by the function
R(t)=p + t d, where t>0 and t (x,y,z)=(t x,t y,t z).
- Note: when t=0, R(t)=p and when t=1, R(t)=p+d.
- Note: We can write R(t) as (x(t),y(t),z(t)), where
x(t)=px+t dx, y(t)=py+t dy, and z(t)=pz+t dz.
Given a point s=(sx,sy,sz) on the view plane, and the position of the
eye e=(ex,ey,ez), the sampling ray uses e as its point and s-e as
its direction.
Note: R(0) = e and R(1) = s.
If we want a rows by cols grid of samples using the VP with corners
(-1,1,0), (1,1,0),(1,-1,0) and (-1,-1,0), we can divide the VP into
rows ·cols rectangles, and use the center of rectangle (i,j) as s.
See Section 4.3 of your text.
The dot product of two vectors v = (x,y,z) and v' = (x',y',z') is
defined by v ·v' = x x' + y y' + z z'.
Note: Dot products have many nice properties, including
- (u + v) ·w = u ·w + v ·w
- ||v||2 = v ·v, where ||v|| is the length of v.
- u ·v = ||u|| ||v|| cos(theta), where theta is the angle
between u and v.
- Some implications:
- Angle between vectors can be computed from lengths and dot product
- Angle between u and v is acute iff u ·v > 0
- Angle between u and v is obtuse iff u ·v < 0
- u and v are orthogonal (perpendicular) iff u ·v = 0
Read all about dot product properties in your text!
A sphere S with center q = (a,b,c) and radius r>0 has it's surface
given by all points (x,y,z) having
(x-a)2 + (y-b)2 + (z-c)2 = r2,
or, equivalently, by all points (x,y,z) having F(x,y,z) = 0 where
F(x,y,z) = (x-a)2 + (y-b)2 + (z-c)2 - r2.
For ray R to intersect sphere S, we need that, for some t>0
F(R(t)) = (px+t dx - a)2 + (py+t dy - b)2 + (pz+t dz - c)2 - r2 = 0.
Note: We usually write F(t) instead of F(R(t)).
This can be simplified using dot product notation:
We can write
F(t) = (p + t d - q) ·(p + t d - q) - r2 = 0.
Or:
F(t) = (p - q) ·(p - q) + 2 t d ·(p - q) + t2 d ·d
- r2 = 0.
We can re-write this as F(t) = A t2 + 2 B t + C = 0, where
- C=(p-q) ·(p-q) - r2,
- B=(p-q) ·d, and
- A=d ·d.
Note: By the quadratic formula
t= (-2 B ±sqrt((2 B)2-4AC))/(2A).
Thus R intersects S iff B2-AC >= 0.
(The 4 was factored out and
cancelled with the 2 A in the denominator.).
This quantity B2-AC is called the discriminant.
If R does intersect S, we then choose the smaller positive
of the (usually) 2 values of t corresponding to intersections of R & S.
Each object in our scene will store a description of how it interacts with
light. This interaction can get a bit complex, so for now we assume that
the object interacts as a diffuse reflector of ambient light.
In theory, there should be an ambient light coefficient Ia,lambda
for each wavelength
lambda, but, in practice a small number of wavelengths are used (such as
RGB or YCM).
We define, for each of the three primary colors red, green and blue, an
ambient light intensity for the scene. It is a vector
Ia = (Ia,r, Ia,g, Ia,b) of values in the interval [0,1].
Each object has ambient lighting co-efficients
rhoa = (rhoa,r, rhoa,g, rhoa,b) in the same interval.
If there were no light source, the pixel color of an object with ambient
lighting co-efficients rhoa would get color
(Ia,r rhoa,r, Ia,g rhoa,g, Ia,b rhoa,b).
lenhart@cs.williams.edu