Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Ray Polygon Intersection. Lecture #15 Thursday, October 17th, 2013
Review - Ray Casting
§ Goal: throw rays through pixels, intersect them with surfaces
§ Compute surface reflectance at points of intersection
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 2
PRP (fp)
Review - Cameras & Ray Casting
Given PRP, then where is pixel Px,y? § Remember the camera’s rotation matrix:
§ N is the vector in the direction of the VPN § U is the camera’s x axis in world coordinates § V is the camera’s y axis in world coordinates § N, U, V are all unit length
§ Focal length d is measured is from PRP to image plane
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 3
Px,y = PRP − dVPN +Ux +Vy
Review - Cameras (II)
§ What is a ray? A parametric line for t > 0. § What ray associated with pixel Px,y? § Move away from FP (same as PRP). § … in direction of vector with base at FP § … and tip at Px,y
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 4
Rx,y t( ) = FP + t(Px,y −FP)Rx,y t( ) = FP + t Ux +Vy− dVPN( )
Review Ray Casting (main loop)
§ A ray casting program throws a ray from the focal point through every pixel § This is the main loop of your ray tracer
§ For each ray: § Intersect the ray with every polygonal surface
§ Find the first point of intersection.
§ Compute the illumination at that point.
§ Use this value to fill the pixel
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 5
Ray/Surface Intersection
§ Implicit surfaces are defined by
§ Given a ray
§ The intersection is solved by
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 6
f (p) = 0
L + tU
f L + tU( ) = 0
Intersect a Polygon Face
§ Find intersection point P on infinite plane. § Test if point P is inside polygon. § General equation for a plane in 3D: § Recall an (x,y,z) point P is on a plane iff:
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 7
ax + by+ cz+ d = 0
N ⋅P = −d where N = a b c
Here is a worked Example
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 8
Planar Intersection
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 9
N ⋅ L + tU( ) = −dtN ⋅U = − N ⋅L + d( )
t =− N ⋅L + d( )
N ⋅U
Be careful. What if this is zero?
Cost: 6 mults, 1 div, 5 adds, 1 neg
Inside/Outside
§ Finding point of intersection arguably easy. § Is the point inside the bounded polygon? § Multiple ways to approach this question
§ Odd/even parity for general polygons § Divide convex polygons into triangles, perform
triangle inside/outside test
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 10
Arbitrary polygons
Step 1: project from 3D to 2D
§ Polygon edges are vectors in 3D § Point of ray/plane intersection is in 3D
§ … but they all reside on the same plane
§ … test inside/outside in 2D, not 3D
§ Not just faster; fewer stability issues § How do we project onto a 2D plane?
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 11
But, there is a faster way.
§ Orthographic projection onto either: § XY, YZ or XZ plane. § How? Just drop one dimension (set it to zero).
§ But, be careful § If your polygon is in the XY plane (z=const) § … and you drop X or Y, § … your polygon collapses to a line.
§ Close to parallel is essentially just as bad. § If your polygon is almost in the XY plane
(Δz≈0), then round-off can create problems 10/22/13 © Bruce Draper & J. Ross Beveridge 2012 12
Illustration – favored choice
§ Six sided polygon nearly parallel with ? § See the trap.
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 13
XY Plane View (red,green)
XZ Plane View (red,blue)
YZ Plane View (green,blue)
Projections for Intersection (II)
§ To be safe, drop the dimension with the largest value in N (the plane normal) § This is the coordinate most orthogonal to the
plane, and therefore the safest to drop § Alternative: rotate the coordinate system to
make N the Z axis § Rotation matrix is easy to compute § More multiplies, but no round-off issues § Fewer cases in your code
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 14
Rotate – align N with Z
§ Equation of the plane.
§ Rotation
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 15
N̂ ⋅P = −d where N̂ = a b c and N̂ ⋅ N̂ =1
R =? ? ?? ? ?a b c
There are a variety of ways to selection the other to orthogonal basis vectors.
2D Polygon Membership
§ Either way,
§ We now have a 2D problem: § Polygon specified with 2D vertices
§ Point P (of planar intersection) is a 2D point
§ Task: § is P inside or outside of the polygon?
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 16
Odd/Even Parity Rule
§ Tests whether a 2D point P is inside or outside of a polygon
§ Step 1: draw a ray from P in any direction in the plane. (we overwork word ‘ray’ here)
§ Step 2: count how many boundaries are crossed § Odd # of intersections ⇒ inside § Even # of intersections ⇒ outside
10/22/13 17 © Bruce Draper & J. Ross Beveridge 2012
Odd/Even Illustrated
Direction doesn’t matter!
10/22/13 18 © Bruce Draper & J. Ross Beveridge 2012
Odd/Even Intersections
§ Represent boundaries as rays § Bi = Vi+1 + tb(Vi+1 – Vi)
§ Start ray at intersection P in any direction § R = P + tp(1,0) works nicely…
§ To intersect ray R with polygon P § Intersect R with every boundary § An intersection is valid iff
§ tp ≥ 0 and 0 ≤ tb < 1.0
§ Odd # intersections => inside, even outside.
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 19
Is this efficient?
§ No! § But it’s easy J § … and anyone in graphics ought to know it. § … because it is general, works for § … non-convex polygons, § … self-intersection polygons.
§ And efficiency often matters § There are better methods for special cases…
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 20
Ray/Triangle Intersections
§ Ray/Triangle intersections are efficient and can be computed directly in 3D
§ They rely on the following implicit definition of a triangle:
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 21
P = A+β B− A( )+γ C − A( )β > 0,γ > 0,β +γ <1
Implicit Triangles
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 22
A C-A
β = 0.5 γ = 0.48
Solve for implicit intersections
§ To find intersection, f(L+tU) = 0 (slide #4) § This is a set of 3 linear equations with 3
unknowns:
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 23
L + tU = A+β B− A( )+γ (C − A)
Lx + tUx = Ax +β Bx − Ax( )+γ Cx − Ax( )Ly + tUy = Ay +β By − Ay( )+γ Cy − Ay( )Lz + tUz = Az +β Bz − Az( )+γ Cz − Az( )
unkowns t, β, γ
Push it through …
§ Constants on one side. § Now in Matrix Form
10/22/13 © Bruce Draper & J. Ross Beveridge 2012 24
Lx − Ax = β Bx − Ax( )+γ Cx − Ax( )− tUx
Ly − Ay = β By − Ay( )+γ Cy − Ay( )− tUy
Lz − Az = β Bz − Az( )+γ Cz − Az( )− tUz
Bx − Ax( ) Cx − Ax( ) −Ux
By − Ay( ) Cy − Ay( ) −Uy
Bz − Az( ) Cz − Az( ) −Uz
β
γ
t=
Lx − Ax
Ly − Ay
Lz − Az