simple mesh format (smf) 3d clipping
TRANSCRIPT
1
1
CS 430/585Computer Graphics I
3D ClippingBackface Culling,
Z-buffering,Ray Casting, Ray Tracing
Week 7, Lecture 14
David Breen, William Regli and Maxim PeysakhovGeometric and Intelligent Computing Laboratory
Department of Computer ScienceDrexel University
http://gicl.cs.drexel.edu 2
Overview
• 3D Clipping• Advanced topics
– Z-buffering– Back-Face Culling– Ray Tracing (Ray Casting)
1994 Foley/VanDam/Finer/Huges/Phillips ICG
3
Simple Mesh Format (SMF)
• Michael Garlandhttp://graphics.cs.uiuc.edu/~garland/
• Triangle data
• Vertex indices begin at 1
4
3D Clipping
• Cohen-Sutherland and Cyrus-Beck canbe trivially extended to 3D
• We will cover:– Cohen-Sutherland for 3D, (parallel
projection)– Cohen-Sutherland for 3D, (perspective
projection)
5
Recall: Cohen-Sutherland
• Line is completelyvisible iff both codevalues of endpointsare 0, i.e.
• If line segments arecompletely outsidethe window, then
Pics/Math courtesy of Dave Mount @ UMD-CP
6
Cohen-Sutherland for 3D,Parallel Projection
• Use 6 bits• Trivially accept if both end-codes are 0• Trivially reject if bit-by-bit AND of end-codes is not 0• Up to 6 intersections may have to be computed
bit 1 point ABOVE the view volume y > 1bit 2 point BELOW the view volume y < -1bit 3 point RIGHT OF the view volume x > 1bit 4 point LEFT OF the view volume x < -1bit 5 point BEHIND the view volume z < -1bit 6 point IN FRONT the view volume z > 0
2
7
Cohen-Sutherland for 3Dcomputing intersection points.• Use parametric representation of the line to
compute intersections• So for y=1 replace y with 1 and solve for t• If 1 ≥ t ≥ 0 use it to find x and z• Test if x and z are in valid range• Repeat for planes y=-1, x=1, x=-1, z=-1, z=0
)()()(
010
010
010
zztzzyytyyxxtxx
-+=
-+=
-+=
)()1(
01
0
yyyt
-
-=
8
Cohen-Sutherland for 3D, Perspective Projection
• Use 6 bits identical to parallel view volume clipping• Conditions on the codes are different• Trivially accept/reject lines using same roles• Intersection points computed differently
bit 1 point ABOVE the view volume y > -zbit 2 point BELOW the view volume y < zbit 3 point RIGHT OF the view volume x > -zbit 4 point LEFT OF the view volume x < zbit 5 point BEHIND the view volume z < -1bit 6 point IN FRONT the view volume z > zmin
9
Cohen-Sutherland for 3Dcomputing intersection points.
• Intersections with planes z=-1, z=zmin is the same.• Calculating intersections with a sloping plane …• For plane y=z these two equations are equal• Repeat for planes y=-z, x=z, x=-z
)()()(
010
010
010
zztzzyytyyxxtxx
-+=
-+=
-+=
)()()(
0101
00
zzyyyzt
---
-=
14
More Efficient Alternative?
• Use Cohen-Sutherland to do trivialaccept/reject
• Project remaining edges onto view plane• Clip lines in 2D
15
end clipping
16
Mesh/Faceted Model
3
17
Back-Face Culling
• Assumptions:– Object approximated as closed polyhedron– Polyhedron interior is not exposed by the front
cutting plane– Eye-point not inside object– Polygons not facing the viewer called Back-Facing– Right-hand vertex ordering defines outward normal
• Back-Face Culling is a technique foreliminating these polygons
• On average eliminates half of the polygons18
Back-Face Culling
A
B
C
D
F
E
G
H
X
Z
• After projection, examinenormal Nk (xk , yk, zk)to the face.
• If zk < 0, face is aBack-Face - don’t draw it
©More general testlooks at Nk•V
© V - View vector
• The only test necessaryfor a single convexpolyhedron
1994 Foley/VanDam/Finer/Huges/Phillips ICG
19
Back-Face Culled Wire-Frame
20
Z-buffering
• Z-buffering (depth-buffering) is a visible surfacedetection 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
21
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/Phillips ICG
22
Z-bufferingfor (y = 0; y < YMAX; y++)
for (x = 0; x < XMAX; x++) {F[x][y] = BACKGROUND_VALUE;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/Phillips ICG
4
23
• We can simplify the calculation of z byexploiting the fact that triangle is planar.– Interpolate z values along the edges– Interpolate z values along scan line– Special cases: vertices & horizontal edge
Z-buffering
1994 Foley/VanDam/Finer/Huges/Phillips ICG
24
Back-Face Culled & Z-BufferedWire-Frame
25
See the Difference
From HW of Andrew Mroczkowski
26
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
27
Important Reminder• Recall: near and far planes are
transformed by Nparand Nper
• Parallel projection– Near: z = 0– Far: z = -1
• Perspective projection– Near: z =– Far: z = -1
†
- vrpz '+Fvrpz '+B
28
Ray Casting (Ray Tracing )
• Determinesvisible surfacesby tracing rays oflight from theviewers eye to theobjects in theworld.
5
29
Ray Casting
• Determines visible surfaces by tracing rays oflight 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/Phillips ICG
30
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/Phillips ICG
31
Computing Intersections• Using parametric equation of the line:
• Simplifying for speed:
• Resulting ray:
(x0,y0,z0)
(x1,y1,z1)
)(),(),( 010010010 zztzzyytyyxxtxx -+=-+=-+=
)(),(),( 010101 zzzyyyxxx -=D-=D-=D
ztzzytyyxtxx D+=D+=D+= 000 ,,
1994 Foley/VanDam/Finer/Huges/Phillips ICG
32
Computing Intersections withSphere
• 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– No real roots, No intersection– One real root, Ray grazes the sphere– Two roots, There are two points of intersection
2222 )()()( rczbyax =-+-+-
220
20
20 )()()( rcztzbytyaxtx =-D++-D++-D+
0)()()(
)]()()([2)(22
02
02
0
0002222
=--+-+-+
+-D+-D+-D+D+D+D
rczbyaxczzbyyaxxttxyx
1994 Foley/VanDam/Finer/Huges/Phillips ICG
33
Computing Intersections withPolygon
• 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 thecoordinate plane
• Polygon containment test canbe performed in 2D
0=+++ DCzByAx
)(000
zCyBxADCzByAxt
D+D+D
+++-=
1994 Foley/VanDam/Finer/Huges/Phillips ICG
34
Polygon Containment Test
• Jordan Curve Theorem:– Point is inside if, for any
ray, there is an oddnumber of crossings
– Otherwise it is outside• Be careful with all the
special cases• Wide variety of other
techniques exist
2
3
6
35
Why Trace Rays?• More elegant• Testbed for techniques:
– modeling (reflectance,transport)
– rendering (e.g. MonteCarlo)
– texturing (e.g.hypertexture)
• Easiest photorealisticrenderer to implement Boat reflected in wavy water rendered in openGLusing an
environment map
Compiled from: Lecture notes of Dr. John C. Hart @ University ofIllinois
36
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 multiplelight sources
• Can be used to producehighly realistic images
37
Ray Traced Image
Blessed State, by Ken Musgrave
38
• 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
qh
22
2
sin1
sin1cos
-=
-=
†
t = h(n ⋅ i) - 1-h2(1- (n ⋅ i)2)( ) n -hi
Can be negative for grazing angles when h >1, say whengoing from glass to air, resulting in total internal reflection(no refraction).
Can be negative for grazing angles when h >1, say whengoing from glass to air, resulting in total internal reflection(no refraction).
Compiled from: Lecture notes of Dr. John C. Hart @ University of Illinois
39
Refraction in Action
40
More Refractions
Henrik Wann JensenUC, San Diego
Photon Mapping!
Justin HansenUniversity of Utah
7
41
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/Phillips ICG
42
• Ray only intersected with the objects contained in thepartitions it passes through
• If partitions are examined in the order ray travels, wecan stop after first intersection is found
• Need to check if intersection itself is in the partition
1994 Foley/VanDam/Finer/Huges/Phillips ICG
Efficiency Considerations
43
Computer Graphics II
Image produced by Horace Ip