CS G140 Graduate Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 2 - January 14, 2009 November 7, 2015 ©College of Computer and Information Science, Northeastern.

Download Report

Transcript CS G140 Graduate Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 2 - January 14, 2009 November 7, 2015 ©College of Computer and Information Science, Northeastern.

CS G140
Graduate Computer Graphics
Prof. Harriet Fell
Spring 2009
Lecture 2 - January 14, 2009
November 7, 2015
©College of Computer and Information Science, Northeastern University
1
Today’s Topics
• Ray Tracing




Ray-Sphere Intersection
Light: Diffuse Reflection
Shadows
Phong Shading
• More Math
 Matrices
 Transformations
 Homogeneous Coordinates
November 7, 2015
©College of Computer and Information Science, Northeastern University
2
Ray Tracing
a World of Spheres
November 7, 2015
©College of Computer and Information Science, Northeastern University
3
What is a Sphere
Vector3D
double
double
double
double
int
(double
double
double
int
November 7, 2015
center; // 3 doubles
radius;
R, G, B; // for RGB colors between 0 and 1
kd;
// diffuse coeficient
ks;
// specular coefficient
specExp; // specular exponent 0 if ks = 0
ka;
// ambient light coefficient)
kgr;
// global reflection coefficient
kt;
// transmitting coefficient
pic;
// > 0 if picture texture is used
©College of Computer and Information Science, Northeastern University
4
-.01 .01
500 800 //
1 // antialias
1 // numlights
100 500 800 // Lx, Ly,
9 // numspheres
//cx
cy cz radius R
-100 -100 0 40
.9
-100
0 0 40
.9
-100 100 0 40
.9
0 -100 0 40
.9
0
0 0 40
.9
0 100 0 40
.9
100 -100 0 40
.9
100
0 0 40
.9
100 100 0 40
.9
November 7, 2015
transform theta phi mu distance
Lz
G
0
0
0
0
0
0
0
0
0
B
0
0
0
0
0
0
0
0
0
ka
.2
.2
.2
.2
.2
.2
.2
.2
.2
kd
.9
.8
.7
.6
.5
.4
.3
.2
.1
ks specExp kgr kt pic
.0
4
0
0 0
.1
8
.1
0 0
.2
12
.2
0 0
.3
16
.3
0 0
.4
20
.4
0 0
.5
24
.5
0 0
.6
28
.6
0 0
.7
32
.7
0 0
.8
36
.8
0 0
©College of Computer and Information Science, Northeastern University
5
World of Spheres
Vector3D VP;
int numLights;
Vector3D theLights[5];
double ka;
int numSpheres;
Sphere theSpheres[20];
// the viewpoint
int ppmT[3];
View sceneView;
double distance;
bool antialias;
// ppm texture files
// transform data
// view plane to VP
// if true antialias
November 7, 2015
// up to 5 white lights
// ambient light coefficient
// 20 sphere max
©College of Computer and Information Science, Northeastern University
6
Simple Ray Casting for
Detecting Visible Surfaces
select window on viewplane and center of projection
for (each scanline in image) {
for (each pixel in the scanline) {
determine ray from center of projection
through pixel;
for (each object in scene) {
if (object is intersected and
is closest considered thus far)
record intersection and object name;
}
set pixel’s color to that of closest object intersected;
}
}
November 7, 2015
©College of Computer and Information Science, Northeastern University
7
Ray Trace 1
Finding Visible Surfaces
November 7, 2015
©College of Computer and Information Science, Northeastern University
8
Ray-Sphere Intersection
• Given
 Sphere
• Center (cx, cy, cz)
• Radius, R
 Ray from P0 to P1
• P0 = (x0, y0, z0) and P1 = (x1, y1, z1)
 View Point
• (Vx, Vy, Vz)
• Project to window from (0,0,0) to (w,h,0)
November 7, 2015
©College of Computer and Information Science, Northeastern University
9
Sphere Equation
y
Center C = (cx, cy, cz)
(x, y, z)
Radius R
R
x
(cx, cy, cz)
 x - cx    z - cz 
2
2
 x - cx 
2
  y - c y    z - c z   R2
2
2
z
November 7, 2015
©College of Computer and Information Science, Northeastern University
10
Ray Equation
P0 = (x0, y0, z0) and P1 = (x1, y1, z1)
P1
P0
The ray from P0 to P1 is given by:
P(t) = (1 - t)P0 + tP1
0 <= t <= 1
= P0 + t(P1 - P0)
November 7, 2015
©College of Computer and Information Science, Northeastern University
11
Intersection Equation
P(t) = P0 + t(P1 - P0)
0 <= t <= 1
is really three equations
x(t) = x0 + t(x1 - x0)
y(t) = y0 + t(y1 - y0)
z(t) = z0 + t(z1 - z0)
0 <= t <= 1
Substitute x(t), y(t), and z(t) for x, y, z, respectively in
 x - cx 
 x
0
 t  x1  x 0   - c x
November 7, 2015
2
  y - c y    z - c z   R2
   y
2
2
0
2
 t  y1  y0 1  - c y
  z
2
0
 t  z1  z0   - c z
©College of Computer and Information Science, Northeastern University

2
 R2
12
Solving the Intersection
Equation

 x0  t  x1  x0   - cx
 
2
  y0  t  y1  y0 1  - c y
 
2
  z0  t  z1  z0   - c z

2
 R2
is a quadratic equation in variable t.
For a fixed pixel, VP, and sphere,
x0, y0, z0, x1, y1, z1, cx, cy, cz, and R
eye
pixel
sphere
are all constants.
We solve for t using the quadratic formula.
November 7, 2015
©College of Computer and Information Science, Northeastern University
13
The Quadratic Coefficients
 x
0
 t  x1  x 0   - c x
   y
2
0
 t  y1  y0 1  - c y
  z
2
0
 t  z1  z0   - c z

2
 R2
Set
dx = x1 - x0
dy = y1 - y0
dz = z1 - z0
Now find the the coefficients:
At  Bt  C  0
2
November 7, 2015
©College of Computer and Information Science, Northeastern University
14
Computing Coefficients
 x
 x
   y
0
 t  x1  x 0   - c x
0
 tdx  - c x    y0  tdy  - c y
2

2
0
 t  y1  y0   - c y
  (z
2
 x0
 tdx   2c x  x 0  tdx   c x 2 
y
 tdy   2c y  y0  tdy   c y 2 
  z
 t  z1  z0   - c z
2
0

2
 R2
 tdz  - c z   R 2
2
0
2
2
0
 z0  tdz   2c z  z0  tdz   c z 2  R 2  0
2
x 0 2  2x 0tdx  t 2dx 2  2c x x 0  2c x tdx  c x 2 
y0 2  2y0tdy  t 2dy 2  2c y y0  2c y tdy  c y 2 
z0 2  2z0tdz  t 2dz 2  2c zz0  2c ztdz  c z 2  R 2  0
November 7, 2015
©College of Computer and Information Science, Northeastern University
15
The Coefficients
x 0 2  2x 0tdx  t 2dx 2  2c x x 0  2c x tdx  c x 2 
y0 2  2y0tdy  t 2dy 2  2c y y0  2c y tdy  c y 2 
z0 2  2z0tdz  t 2dz 2  2c zz0  2c ztdz  c z 2  R 2  0
A  dx 2  d y 2  d z 2
B  2 dx ( x 0 - c x )  2 dy ( y 0 - c y )  2 d z ( z 0 - c z )
C  c x 2  c y 2  c z 2  x 0 2  y 0 2  z0 2 
-2(c x x 0  c y y 0  c z z0 ) - R
November 7, 2015
©College of Computer and Information Science, Northeastern University
2
16
Solving the Equation
At 2 + Bt + C = 0
discriminant = D  A,B,C  = B2 - 4AC
< 0 no intersection

D  A,B,C  = 0 ray is tangent to the sphere
> 0 ray intersects sphere in two points

November 7, 2015
©College of Computer and Information Science, Northeastern University
17
The intersection nearest P0 is given by:
2 - 4AC
B
-B t=
2A
To find the coordinates of the intersection
x = x 0 + tdx
point:
y = y0 + tdy
z = z0 + tdz
November 7, 2015
©College of Computer and Information Science, Northeastern University
18
First Lighting Model
• Ambient light is a global constant.
Ambient Light = ka (AR, AG, AB)
ka is in the “World of Spheres”
0  ka  1
(AR, AG, AB) = average of the light sources
(AR, AG, AB) = (1, 1, 1) for white light
• Color of object S = (SR, SG, SB)
• Visible Color of an object S with only ambient light
CS= ka (AR SR, AG SG, AB SB)
• For white light
CS= ka (SR, SG, SB)
November 7, 2015
©College of Computer and Information Science, Northeastern University
19
Visible Surfaces
Ambient Light
November 7, 2015
©College of Computer and Information Science, Northeastern University
20
Second Lighting Model
• Point source light L = (LR, LG, LB) at (Lx, Ly, Lz)
• Ambient light is also present.
• Color at point p on an object S with ambient & diffuse
reflection
Cp= ka (AR SR, AG SG, AB SB)+ kd kp(LR SR, LG SG, LB SB)
• For white light, L = (1, 1, 1)
Cp= ka (SR, SG, SB) + kd kp(SR, SG, SB)
•
•
•
•
kp depends on the point p on the object and (Lx, Ly, Lz)
kd depends on the object (sphere)
ka is global
ka + kd <= 1
November 7, 2015
©College of Computer and Information Science, Northeastern University
21
Diffuse Light
November 7, 2015
©College of Computer and Information Science, Northeastern University
22
Lambertian Reflection Model
Diffuse Shading
• For matte (non-shiny) objects
• Examples
 Matte paper, newsprint
 Unpolished wood
 Unpolished stones
• Color at a point on a matte object does not
change with viewpoint.
November 7, 2015
©College of Computer and Information Science, Northeastern University
23
Physics of
Lambertian Reflection
• Incoming light is partially absorbed and partially
transmitted equally in all directions
November 7, 2015
©College of Computer and Information Science, Northeastern University
24
Geometry of Lambert’s Law
N
L
L

dA
90 - 
 dAcos()
90 - 
Surface 1
November 7, 2015
Surface 2
©College of Computer and Information Science, Northeastern University
25
cos()=NL
L

90 - 
 dAcos()
90 - 
Surface 2
Cp= ka (SR, SG, SB) + kd NL (SR, SG, SB)
November 7, 2015
©College of Computer and Information Science, Northeastern University
26
Finding N
N = (x-cx, y-cy, z-cz)
|(x-cx, y-cy, z-cz)|
normal
(x, y, z)
radius
(cx, cy, cz)
November 7, 2015
©College of Computer and Information Science, Northeastern University
27
Diffuse Light 2
November 7, 2015
©College of Computer and Information Science, Northeastern University
28
Shadows on Spheres
November 7, 2015
©College of Computer and Information Science, Northeastern University
29
More Shadows
November 7, 2015
©College of Computer and Information Science, Northeastern University
30
Finding Shadows
P
Pixel gets
shadow color
November 7, 2015
©College of Computer and Information Science, Northeastern University
31
Shadow Color
• Given
Ray from P (point on sphere S) to L (light)
P= P0 = (x0, y0, z0) and L = P1 = (x1, y1, z1)
• Find out whether the ray intersects
any other object (sphere).
 If it does, P is in shadow.
 Use only ambient light for pixel.
November 7, 2015
©College of Computer and Information Science, Northeastern University
32
Shape of Shadows
November 7, 2015
©College of Computer and Information Science, Northeastern University
33
Different Views
November 7, 2015
©College of Computer and Information Science, Northeastern University
34
Planets
November 7, 2015
©College of Computer and Information Science, Northeastern University
35
Starry Skies
November 7, 2015
©College of Computer and Information Science, Northeastern University
36
Shadows on the Plane
November 7, 2015
©College of Computer and Information Science, Northeastern University
37
Finding Shadows
on the Back Plane
Pixel in Shadow
November 7, 2015
P
©College of Computer and Information Science, Northeastern University
38
Close up
November 7, 2015
©College of Computer and Information Science, Northeastern University
39
On the Table
November 7, 2015
©College of Computer and Information Science, Northeastern University
40
Phong Highlight
November 7, 2015
©College of Computer and Information Science, Northeastern University
41
Phong Lighting Model
Light
The viewer only sees
the light when  is 0.
Normal
Reflected
View
N
L
R
V
  
Surface
November 7, 2015
We make the highlight
maximal when  is 0,
but have it fade off
gradually.
©College of Computer and Information Science, Northeastern University
42
Phong Lighting Model
cos() = RV
We use cosn().
The higher n is, the
faster the drop off.
N
L
R
V
  
For white light
Surface
Cp= ka (SR, SG, SB) + kd NL (SR, SG, SB) + ks (RV)n(1, 1, 1)
November 7, 2015
©College of Computer and Information Science, Northeastern University
43
Powers of cos()
cos10()
cos20()
cos40()
cos80()
November 7, 2015
©College of Computer and Information Science, Northeastern University
44
Computing R
L + R = (2 LN) N
R = (2 LN) N - L
R
LN
L
N
L+R
LN
November 7, 2015
L
R
 
©College of Computer and Information Science, Northeastern University
45
The Halfway Vector
H = L+ V
|L+ V|
Use HN
instead of RV.
H is less
expensive to
compute than
R.
From the picture
+φ=θ-φ+α
So φ = α/2.
N
L
H
φ
R
V
θ θ α
This is not
generally true.
Why?
Surface
Cp= ka (SR, SG, SB) + kd NL (SR, SG, SB) + ks (HN)n (1, 1, 1)
November 7, 2015
©College of Computer and Information Science, Northeastern University
46
Varied Phong Highlights
November 7, 2015
©College of Computer and Information Science, Northeastern University
47
Varying Reflectivity
November 7, 2015
©College of Computer and Information Science, Northeastern University
48
Time for a Break
November 7, 2015
©College of Computer and Information Science, Northeastern University
49
More Math
• Matrices
• Transformations
• Homogeneous Coordinates
November 7, 2015
©College of Computer and Information Science, Northeastern University
50
Matrices
 a11 a12 
A 

a
a
22 
 21
 b11 b12
B  b21 b22

 b31 b32
b13 
b23 

b33 
 c11 c12
c
c22
21
C
 c31 c32

 c41 c42
c13
c23
c33
c43
c14 
c24 

c34 

c44 
• We use 2x2, 3x3, and 4x4 matrices in computer
graphics.
• We’ll start with a review of 2D matrices and
transformations.
November 7, 2015
©College of Computer and Information Science, Northeastern University
51
Basic 2D Linear Transforms
 a11
a
 21
a12   x   a11 x  a12 y 





a22   y   a21 x  a22 y 
 a11 a12  1  a11 
 
a



 21 a22  0 a21 
November 7, 2015
 a11 a12  0  a12 
 
a



 21 a22  1  a22 
©College of Computer and Information Science, Northeastern University
52
Scale by .5
scale .5,.5 
.5 0 
 0 .5


(1, 0)
(0.5, 0)
(0, 1)
(0, 0.5)
November 7, 2015
©College of Computer and Information Science, Northeastern University
53
Scaling by .5
y
y
0.5 0 
 0 0.5


x
November 7, 2015
©College of Computer and Information Science, Northeastern University
x
54
General Scaling
y
y
 s xs 0 0 1   s x0
 0 x s   0    
  0 ysy  01   0sy 


 
x
November 7, 2015
©College of Computer and Information Science, Northeastern University
x
55
General Scaling
y
y
scale  sx , s y  
1
 sx
0

x
1
November 7, 2015
sy
0
s y 
x
sx
©College of Computer and Information Science, Northeastern University
56
Rotation
rot   
cos    sin   


 sin   cos   
 sin()
cos()
-sin()
 cos()
November 7, 2015
©College of Computer and Information Science, Northeastern University
57
Rotation
y
rot   
y
cos    sin   


 sin   cos   

x
November 7, 2015
©College of Computer and Information Science, Northeastern University
x
58
Reflection in y-axis
reflect-y 
 1 0
 0 1


November 7, 2015
©College of Computer and Information Science, Northeastern University
59
Reflection in y-axis
y
reflect-y 
y
 1 0
 0 1


x
November 7, 2015
©College of Computer and Information Science, Northeastern University
x
60
Reflection in x-axis
reflect-x 
1 0 
0 1


November 7, 2015
©College of Computer and Information Science, Northeastern University
61
Reflection in x-axis
y
y
reflect-x 
1 0 
0 1


x
November 7, 2015
©College of Computer and Information Science, Northeastern University
x
62
Shear-x
shear-x  s  
1 s 
 0 1


s
November 7, 2015
©College of Computer and Information Science, Northeastern University
63
Shear x
y
y
1 s 
 0 1


x
November 7, 2015
©College of Computer and Information Science, Northeastern University
x
64
Shear-y
shear-y  s  
1 0 
 s 1


s
November 7, 2015
©College of Computer and Information Science, Northeastern University
65
Shear y
y
y
1 0 
 s 1


x
November 7, 2015
©College of Computer and Information Science, Northeastern University
x
66
Linear Transformations
• Scale, Reflection, Rotation, and Shear are
all linear transformations
• They satisfy: T(au + bv) = aT(u) + bT(v)
 u and v are vectors
 a and b are scalars
• If T is a linear transformation
 T((0, 0)) = (0, 0)
November 7, 2015
©College of Computer and Information Science, Northeastern University
67
Composing Linear
Transformations
• If T1 and T2 are transformations
 T2 T1(v) =def T2( T1(v))
• If T1 and T2 are linear and are represented
by matrices M1 and M2
 T2 T1 is represented by M2 M1
 T2 T1(v) = T2( T1(v)) = (M2 M1)(v)
November 7, 2015
©College of Computer and Information Science, Northeastern University
68
Reflection About an
Arbitrary Line (through the origin)
y
y
x
November 7, 2015
©College of Computer and Information Science, Northeastern University
x
69
Reflection as a Composition
y
x
November 7, 2015
©College of Computer and Information Science, Northeastern University
70
Decomposing
Linear Transformations
• Any 2D Linear Transformation can be
decomposed into the product of a rotation,
a scale, and a rotation if the scale can
have negative numbers.
• M = R1SR2
November 7, 2015
©College of Computer and Information Science, Northeastern University
71
Rotation about
an Arbitrary Point
y
y


x
x
This is not a linear transformation. The origin moves.
November 7, 2015
©College of Computer and Information Science, Northeastern University
72
Translation
(x, y)(x+a,y+b)
y
y
(a, b)
x
x
This is not a linear transformation. The origin moves.
November 7, 2015
©College of Computer and Information Science, Northeastern University
73
Homogeneous Coordinates
y
y
Embed the xy-plane in R3 at z = 1.
(x, y)  (x, y, 1)
x
x
z
November 7, 2015
©College of Computer and Information Science, Northeastern University
74
2D Linear Transformations
as 3D Matrices
Any 2D linear transformation can be
represented by a 2x2 matrix
 a11 a12   x   a11 x  a12 y 

a




 21 a22   y   a21 x  a22 y 
or a 3x3 matrix
 a11
a
 21
 0
November 7, 2015
a12
a22
0
0  x   a11 x  a12 y 
0  y    a21 x  a22 y 
  

1   1  
1

©College of Computer and Information Science, Northeastern University
75
2D Linear Translations
as 3D Matrices
Any 2D translation can be represented by
a 3x3 matrix.
1 0 a   x   x  a 
0 1 b   y    y  b

  

 0 0 1   1   1 
This is a 3D shear that acts as a
translation on the plane z = 1.
November 7, 2015
©College of Computer and Information Science, Northeastern University
76
Translation as a Shear
y
y
x
x
z
November 7, 2015
©College of Computer and Information Science, Northeastern University
77
2D Affine Transformations
• An affine transformation is any transformation
that preserves co-linearity (i.e., all points lying on
a line initially still lie on a line after
transformation) and ratios of distances (e.g., the
midpoint of a line segment remains the midpoint
after transformation).
• With homogeneous coordinates, we can
represent all 2D affine transformations as 3D
linear transformations.
• We can then use matrix multiplication to
transform objects.
November 7, 2015
©College of Computer and Information Science, Northeastern University
78
Rotation about
an Arbitrary Point
y
y


x
November 7, 2015
©College of Computer and Information Science, Northeastern University
x
79
Rotation about
an Arbitrary Point
y
T(cx, cy) R()T(-cx, -cy)


November 7, 2015
x
©College of Computer and Information Science, Northeastern University
80
Windowing Transforms
(A,B)
(a,b)
translate
(A-a,B-b)
scale
(C,D)
(C-c,D-d)
translate (c,d)
November 7, 2015
©College of Computer and Information Science, Northeastern University
81
3D Transformations
Remember:
 x
 x
 y
 y   
 
z
 z 
 
1
A 3D linear transformation can be represented by a
3x3 matrix.
 a11 a12
a
a
 21 22
 a31 a32
November 7, 2015
 a11 a12
a13 
a
a22
21


a23 

 a31 a32
a33 

0
0
a13
a22
a33
0
0
0

0

1
©College of Computer and Information Science, Northeastern University
82
3D Affine Transformations
 sx
0
scale  sx , s y , sz   
0

0
0
sy
0
0
0
0
sz
0
0
0

0

1
1
0
translate  t x , t y , tz   
0

0
November 7, 2015
0
1
0
0
0 tx 
0 ty 

1 tz 

0 1
©College of Computer and Information Science, Northeastern University
83
3D Rotations
0
0
1
0 cos    sin  
rotate x    
0 sin   cos  

0
0
0
rotate z   
November 7, 2015
0
0

0

1
 cos  

0

rotate y   
  sin  

cos    sin   0 0
0



sin

cos

0
0









0
0
0
0
0 sin  
1
0
0 cos  
0
0
0

0
0

1
1 0

0 1
©College of Computer and Information Science, Northeastern University
84