CS 536 Overview Computer Graphics Backface Culling, Z ...david/Classes/CS536/Lectures/L... · 1 2...
Transcript of CS 536 Overview Computer Graphics Backface Culling, Z ...david/Classes/CS536/Lectures/L... · 1 2...
1
2
CS 536Computer Graphics
Backface Culling,Z-buffering,
Ray Casting, Ray TracingWeek 8, Lecture 17
David Breen, William Regli and Maxim PeysakhovDepartment of Computer Science
Drexel University
4
Overview
• Rendering topics– Z-buffering– Back-Face Culling– Ray Tracing (Ray Casting)
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
5
Mesh/Faceted Model
6
Back-Face Culling• Assumptions:
– Object approximated as closed polyhedron– Polyhedron interior is not exposed by the front
cutting plane– Eye-point not inside object– Right-hand vertex ordering defines outward normal– Polygons not facing the viewer called Back-Facing
• Back-Face Culling is a technique for eliminating polygons for these kinds of models
• On average eliminates half of the polygons– Could be done for performance reasons
7
Back-Face Culling
A
B
C
D
F
E
G
H
X
Z
• After canonical transformation, examine normal Nk (xk , yk, zk)to the face.
• If zk < 0, face is a Back-Face - don’t draw it
© More general test looks at Nk•V
© V - View vector
• The only test necessary for a single convex polyhedron 1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
8
Normal for Triangle
p0p1
p2
nplane n · (p - p0 ) = 0
n = (p1 - p0 ) × (p2 - p0 )
normalize n ¬ n / |n|
p
Note that right-hand rule determines outward face
2
9
Back-Face Culled Wire-Frame
10
Z-buffering• Z-buffering (depth-buffering) is a visible surface
detection algorithm• Implementable in hardware and software• Requires data structure (z-buffer) in addition to
frame buffer.• Z-buffer stores values [0 .. ZMAX] corresponding
to depth of each point.• If the point is closer than one in the buffers, it will
replace the buffered values
11
Z-buffering0 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 0
5 5 5 5 5 5 55 5 5 5 5 55 5 5 5 55 5 5 55 5 55 55
5 5 5 5 5 5 5 05 5 5 5 5 5 0 05 5 5 5 5 0 0 05 5 5 5 0 0 0 05 5 5 0 0 0 0 05 5 0 0 0 0 0 05 0 0 0 0 0 0 00 0 0 0 0 0 0 0
5 5 5 5 5 5 5 05 5 5 5 5 5 0 05 5 5 5 5 0 0 05 5 5 5 0 0 0 05 5 5 0 0 0 0 05 5 0 0 0 0 0 05 0 0 0 0 0 0 00 0 0 0 0 0 0 0
5 5 5 5 5 5 5 05 5 5 5 5 5 0 05 5 5 5 5 0 0 05 5 5 5 0 0 0 06 5 5 3 0 0 0 07 6 5 4 3 0 0 08 7 6 5 4 3 0 00 0 0 0 0 0 0 0
34 35 4 36 5 4 37 6 5 4 38 7 6 5 4 3
+
+
=
=
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
12
Z-bufferingfor (y = 0; y < YMAX; y++)
for (x = 0; x < XMAX; x++) {F[x][y] = BACKGROUND_COLOR;Z[x][y] = ZMIN;
}for (each polygon)
for (each pixel in polygon’s projection) {pz = polygon’s z-value at pixel coordinates (x,y)if (pz > Z[x][y]) { /* New point is closer */
Z[x][y] = pz;F[x][y] = polygon’s color at pixel coordinates
(x,y)}
} 1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
13
Z-buffering w/ front/back clippingfor (y = 0; y < YMAX; y++)
for (x = 0; x < XMAX; x++) {F[x][y] = BACKGROUND_VALUE;Z[x][y] = -1; /* Back value in NPC */
}for (each polygon)
for (each pixel in polygon’s projection) {pz = polygon’s z-value at pixel coordinates (x,y)if (pz < FRONT && pz > Z[x][y]) { /* New point is behind front plane
& closer than previous point */ Z[x][y] = pz;F[x][y] = polygon’s color at pixel coordinates (x,y)
}}
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
14
• We can simplify the calculation of z by exploiting the fact that triangle is planar.– Interpolate z values along the edges– Interpolate z values along scan line– Special cases: horizontal edge, degenerate
triangle & single vertex
Z Interpolation
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
3
15
• za = z1 + (|Pa - P1|/|P2 - P1|)(z2 - z1)• zb = z1 + (|Pb - P1|/|P3 - P1|)(z3 - z1)• zp = za + (|Pp - Pa|/|Pb - Pa|)(zb - za)
• P1 = (x1, y1) • P2 = (x2, y2)• P3 = (x3, y3)
16
Back-Face Culled & Z-Buffered Wire-Frame
See the Difference
18
See the Difference
19
See the Difference
20 21
Depth Cueing• Objects that are closer are brighter• Objects farther away are darker• Color = BaseColor*(z - far)/(near - far)
www.dm.unibo.it/~casciola www.siggraph.org
4
23
Ray Casting (Ray Tracing )
• Determines visible surfaces by tracing rays of light from the viewers eye to the objects in the world.
24
Ray Casting • Determines visible surfaces by tracing rays of
light from the viewers eye to the objects• View plane is divided on the pixel grid• The eye ray is fired from the center of
projection through each pixel
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
25
Select the center of projection and viewplane windowfor (each scan line) {
for (each pixel on the scan line) {Find ray from the center of projections through the pixelfor (each object in the scene) {
if (object is intersected and intersection is closest considered so far) {
Record the intersection and the object name}
}Set pixel color to that of the closest intersected object
}}
Ray Casting
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
26
Computing Intersections• Using parametric equation of the line:
• Simplifying for speed:
• Resulting ray:
(x0,y0,z0)
(x1,y1,z1)
)(),(),( 010010010 zztzzyytyyxxtxx −+=−+=−+=
)(),(),( 010101 zzzyyyxxx −=Δ−=Δ−=Δ
ztzzytyyxtxx Δ+=Δ+=Δ+= 000 ,,
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
27
• Sphere with radius r and center (a,b,c) is:
• Intersection is found by substituting values for (x,y,z)
• With some straightforward algebraic transformations:
• Equation is quadratic in terms of t• Solve with quadratic formula
– No real roots (square root is negative). No intersection– One real root. Ray grazes the sphere– Two real roots. There are two points of intersection
Computing Intersections with Sphere
2222 )()()( rczbyax =−+−+−
220
20
20 )()()( rcztzbytyaxtx =−Δ++−Δ++−Δ+
0)()()(
)]()()([2)(22
02
02
0
0002222
=−−+−+−+
+−Δ+−Δ+−Δ+Δ+Δ+Δ
rczbyax
czzbyyaxxttxyx
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
Relation of t to IntersectionWe want the smallest positive t - call it ti
t0
t1
t1
t0Discriminant = 0
Discriminant < 0
t1
t0
t0 =−B− B2 − 4AC
2A
"
#$$
%
&''
t1 =−B+ B2 − 4AC
2A
"
#$$
%
&''
t0
5
29
Ray-triangle Intersection
30
Computing Intersections with Polygon
• First intersect ray with plane
• The substitution results in:• If denominator is 0,
ray is parallel to the plane• Project polygon and point
orthographically on the coordinate plane
• Polygon containment test can be performed in 2D
0=+++ DCzByAx
)(000
zCyBxADCzByAx
tΔ+Δ+Δ
+++−=
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
31
Polygon Containment Test
• Jordan Curve Theorem:– Point is inside if, for any
ray, there is an odd number of crossings
– Otherwise it is outside• Be careful with all the
special cases• Wide variety of other
techniques exist
2
3
32
Why Trace Rays?• More elegant • Testbed for techniques:
– modeling (reflectance, transport)
– rendering (e.g. Monte Carlo)
– texturing (e.g. hypertexture)
• Easiest photorealistic renderer to implement
Boat re flec ted in wavy water rendered in openGL us ing an env i ronment map
Compi led from: Lec ture notes of Dr. J ohn C. Hart @ Univers i ty o f Il l inois
33
Ray Tracing• Extension of ray casting• Idea: Continue to bounce
the ray in the scene• Shoot rays to light sources• Simple and powerful• Reflections, shadows,
transparency and multiple light sources
• Can be used to produce highly realistic images
34
Ray Traced Image
Bles s ed State , by Ken Mus grave
6
35
• Snell’s Law: hi sin qi = ht sin qt• Let h =hi /ht = sin qt / sin qi• Let m = (cos qi n - i) / sin qi• Then…t = sin qt m - cos qt n
= (sin qt / sin qi) (cos qi n - i) - cos qt n= (h cos qi - cos qt )n - h i
To Ray Trace, We Need Refraction
cos qi n - i
i
-n
qi
qt t = ?
m
i
tt
θη
θθ
22
2
sin1
sin1cos
−=
−=
€
t = η(n ⋅ i) − 1−η2(1− (n ⋅ i)2)( ) n−ηi
Can be negative for grazing angles when h >1, say when going from glass to air, resulting in total internal reflection(no refraction).
Compi led from: Lec ture notes of Dr. J ohn C. Hart @ Univers i ty o f Il l ino is
36
Refraction in Action
Turner Whitted, Bell Labs, 1980
37
More Refractions
Henrik Wann JensenUC, San Diego
Photon Mapping!
Justin HansenUniversity of Utah 38
Efficiency Considerations• Partition the bounding box on a regular grid with
equal sized extents• Associate each partition with the list of objects
contained in it
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
39
• Ray only intersected with the objects contained in the partitions it passes through
• If partitions are examined in the order ray travels, we can stop after first intersection is found
• Need to check if intersection itself is in the partition
1994 Foley /VanDam/Finer/Huges/Phi ll ips ICG
Efficiency Considerations
40Image produced by Horace Ip
Advanced Rendering Techniques
7
41Image produced by Dmitriy Bespalov
Advanced Rendering Techniques