09_Overview_Polygons

advertisement
CS 450: COMPUTER GRAPHICS
OVERVIEW OF POLYGONS
SPRING 2015
DR. MICHAEL J. REALE
INTRODUCTION TO POLYGONS
INTRODUCTION
• So far, we’ve talked mostly about points, lines, and curves
• Fill area (or filled area) = some area that needs to be filled in with a solid color or pattern
•
Usually used to describe the surface of an object (but not always)
•
Usually polygons (but not always)
WHY POLYGONS?
•
Most graphics libraries force you to use polygons for filled areas
•
Why?
•
Polygon boundaries can be described with linear equations
•
•
Can approximate most curved surfaces with polygons
•
•
Similar to approximating curve with line segments
Makes shading easier (especially if polygons are triangles  single plane per triangle)
•
•
Makes fill algorithms more efficient
Can still create the illusion of a curved/complicated surface with tricks like phong shading and normal mapping
Surface tessellation = approximating a curved surface with polygons
•
More polygons  better detail, but more vertices/polygons to process
•
Also called fitting the surface with a polygon mesh
http://www.guru3d.com/articlespages/radeon-hd-5870-review-test,7.html
WHAT IS A POLYGON, EXACTLY?
•
The official definition varies, but at bare minimum:
•
Polygon = a figure with 3 or more vertices connected in sequence by straight-line segments (edges or sides of the polygon)
•
•
Most loose definition  any closed-polyline boundary
•
More finicky definitions  contained in single plane, edges have no common points other than their endpoints, no three
successive points collinear
Standard polygon or simple polygon = closed-polyline with no crossing edges
DOES A POLYGON LIE IN A SINGLE PLANE?
• In computer graphics, polygon not always in same plane:
•
Round-off error
•
•
I.e., points originally in single plane, but, after transformation,
the points may be slightly off
Fitting to surface makes non-planar polygons
•
E.g., if using quads, approximation may “bend” the quad in half
• Thus, we usually use triangles to avoid this problem
DEGENERATE POLYGONS
• Degenerate polygons = often used to describe polygon with:
•
3 or more collinear vertices  generates a line segment
•
•
E.g., in extreme case, triangle with no area
Repeated vertex positions  generates shape with:
•
Extraneous lines
•
Overlapping edges
•
Edges with length 0
CONVEX VS. CONCAVE: BY INTERIOR ANGLE
• Interior angle = angle inside polygon boundary formed by two adjacent edges
• Convex = all interior angles less than 180°
• Concave = one or more interior angles greater than or equal to 180°
CONVEX VS. CONCAVE: BY PICKING SIDES
• Another way to define convex vs. concave is to look at each line formed by each edge and see if all other vertices
on one side or not
• Convex = for all edge lines, all other vertices are on one side
• Concave = for one or more edge lines, some of the vertices are on one side and some are on the other
•
Also, one or more edge lines will intersect another edge
CONVEX VS. CONCAVE: CORNY MEMORY HOOK
DEALING WITH DEGENERATES AND CONCAVE
POLYGONS
• Most graphics software packages (i.e., OpenGL) can’t really deal with the degenerate polygons
•
To avoid extra processing, however, they leave it as the programmer’s problem
• Concave polygons are more complicated to deal with when it comes to filling algorithms
•
Solution: split concave polygon into convex polygons (e.g., triangles)
•
•
Again, often have to do this yourself
OpenGL “requires” that all polygons be convex (or at least, there’s no guarantee the filling will work if not
convex)
BRIEF VECTOR MATH REVIEW
INTRODUCTION
• Before we go any further, we’ll need to quickly review some concepts from linear algebra
•
We will revisit these concepts in a future, more thorough review
• For now, let’s talk about:
•
Vectors
•
Vector length
•
Scaling vectors
•
Normalized vectors
•
Adding/subtracting vectors
•
Dot product
•
Cross product
WHAT’S YOUR VECTOR, VICTOR?
• A vector is basically a (N x 1) or (1 x N) matrix
•
(# of rows X # of columns)
• In computer graphics:
•
N is usually 2, 3, or 4 (homogeneous coordinates)
•
Usually use column vectors (N x 1)
• Components of the vector:
•
2D  x and y values
•
3D  x, y, and z values
•
4D  x, y, z, and w values
 x
v 
 y
 x
v   y 
 z 
x
 y
v 
z
 
 w
• Scalar = single value (or 1x1 vector)
http://www.spudislunarresources.com/blog/wpcontent/uploads/2013/03/Airplane.jpg
INTERPRETING VECTORS
• A vector can also be
interpreted as:
•
Location (w = 1)
•
Direction (w = 0)
• …in space
LENGTH OF A VECTOR
• We usually use Euclidean distance to get the length of a vector:
v  x y z
2
2
2
• The square of the distance (not surprisingly) is given by:
v x y z
2
2
2
2
ZERO VECTOR
• A vector with a length of zero is called the zero vector:
0 
v 
0 
0 
v  0
0
0 
0 
v 
0 
 
0 
SCALING VECTORS
• To multiply a scalar by a vector, you multiple that scalar by the components of the vector
• Example: multiply 5 by a 3D vector A
 x A  5 * x A 




5  A  5 *  y A   5 * y A 
 z A   5 * z A 
NORMALIZED VECTORS
• Normalize a vector = divide vector by its length ║v║ 
makes length equal to 1
•
Equivalent to multiplying vector by 1/║v║
•
Resulting vector is called a normalized vector
• WARNING: This is NOT the same as a NORMAL vector!
•
Although normal vectors are often normalized.
v

v


x


2
2
2
 x y z 


v
y


x2  y2  z 2  x2  y2  z 2 


z
 2

2
2
 x  y  z 
ADDING/SUBTRACTING VECTORS
• To add/subtract vectors, you add/subtract
their respective components:
 x A   xB   ( x A  xB ) 






A  B   y A    yB   ( y A  yB )
 z A   z B   ( z A  z B ) 
 x A   xB   ( x A  xB ) 
A  B   y A    yB   ( y A  yB )
 z A   z B   ( z A  z B ) 
GEOMETRIC INTERPRETATION OF
ADDING/SUBTRACTING (DIRECTION) VECTORS
• Adding  putting head of one vector on tail of the other
• Subtracting  gives direction from one endpoint to the other
DOT PRODUCT
• Given two 3D vectors A and B, the dot product of A and B is given by:
A  B  xA xB  y A yB  z A zB
• Basically:
•
Multiple corresponding components
•
Add them together
• NOTE: Result is a single number (i.e., scalar)  another name for the dot product is the scalar product
DOT PRODUCT WITH YOURSELF
• The dot product of a vector A with itself is equal to the square of its length:
A A  x  y  z  A
2
2
2
2
DOT PRODUCT: WHAT DOES IT MEAN?
• The dot product of two vectors A and B is in fact equivalent to:
A  B  A B cos 
• …where θ = smallest angle between the two vectors
•
(Basically it has to do with projecting one vector on another; we’ll discuss this later)
• If the vectors are normalized, then:
A 1
B 1
A  B  cos 
Remember:
DOT PRODUCT: CHECKING ANGLES
A  B  A B cos 
cos(0 )  1
• What this means:
•
(A · B) > 0  vectors pointing in similar directions (0 <= θ < 90°)
•
(A · B) = 0  vectors are orthogonal (i.e., perpendicular to each other) (θ = 90°)
•
(A · B) < 0  vectors pointing away from each other (90° < θ <= 180°)
• For normalized vectors, dot product ranges from [-1, 1]:
•
(A · B) = 1  vectors pointing in the exact same direction (θ = 0°)
•
(A · B) = 0  vectors are orthogonal (i.e., perpendicular to each other) (θ = 90°)
•
(A · B) = -1  vectors pointing in the exact opposite direction (θ = 180°)
• (We’re going to use this trick for lighting calculations later  )
cos(90 )  0
cos(180 )  1
CROSS PRODUCT
• Cross product
•
Also called vector product (results is a vector)
•
Given two vectors U and V  gives vector W that is orthogonal (perpendicular) to both U and V
•
U, V, and W form right-handed system!
•
I.e., can use right-hand-rule on U and V (IN THAT ORDER) to get W
U V  W
•
Example: (X x Y) = Z axis!
LENGTH OF CROSS PRODUCT
• The length of W (= U X V) is equivalent to:
W  U V  U V sin 
• …where again θ = smallest angle between U and V
• If U and V are parallel  θ = 0°  sin θ = 0  get zero vector for W!
CROSS PRODUCT: ORDER MATTERS!
• WARNING! ORDER MATTERS with the cross product!
U V  V U
• Property of anti-commutativity
• REMEMBER THE RIGHT-HAND-RULE!!!
COMPUTING THE CROSS PRODUCT
• To compute the cross product:
u y v z  u z v y 
 wx 




W   wy   U  V   u z v x  u x v z 
u x v y  u y v x 
 wz 


COMPUTING THE CROSS PRODUCT: SARRUS’S
SCHEME
• An easier way to remember this is with a method called Sarrus’s scheme:
•
Follow diagonal arrows  for each arrow  multiply elements along arrow times sign at top
•
ex = x axis, ey = y axis, ez = z axis
U V  ex (u y vz )  e y (u z vx )  ez (u x v y )
 ex (u z v y )  e y (u x vz )  ez (u y vx )
IDENTIFYING AND SPLITTING CONCAVE
POLYGONS
DETECTING CONCAVE POLYGONS
• We can do this one of two ways:
• Check interior angles for one greater than 180°
• For each edge line, check if vertices are all on the same side  if not,
concave
CHECKING INTERIOR ANGLES
• Treat each edge as a vector (go counterclockwise):
Ek  Vk 1  Vk
• For each pair of consecutive edge vectors, get cross product
• If convex  signs of all cross products will be the same
•
In 2D  cross product will always be +Z or –Z
•
In 3D  transform polygon to XY plane (as best we can)
• If concave  one or more will be different
• NOTE: This breaks down if we have 3 successive collinear points anywhere 
cross product becomes zero vector!
( E1  E 2 ) z  0
( E2  E 3 ) z  0
( E3  E 4 ) z  0
( E4  E 5 ) z  0
( E5  E 1 ) z  0
SPLITTING A CONCAVE POLYGON
• There are 2 ways we can do this:
• Vector method
• Rotational method
SPLITTING BY VECTOR METHOD
• Transform to XY plane (if necessary)
• Get edge vectors in counterclockwise order:
Ek  Vk 1  Vk
• For each pair of consecutive edge vectors, get cross product
• If a cross product has a negative z component  split polygon along first vector
in cross product pair
•
Have to intersect this line with other edges
SPLITTING BY ROTATIONAL METHOD
• Transform to XY plane (if necessary)
• For each vertex Vk:
•
Move polygon so that Vk is at the origin
•
Rotate polygon so that Vk+1 is on the X axis
•
If Vk+2 is below X axis  polygon is concave  split polygon along x axis
•
Repeat concave test for each of the two new polygons
• Stop when we’ve checked all vertices
SPLITTING A CONVEX POLYGON
• Once we have a convex polygon(s), it is
usually beneficial to split it into
triangles:
•
Every 3 consecutive vertices 
make triangle
•
Remove middle vertex
•
Keep going until down to last 3
vertices
POLYGONS, PLANES, AND NORMALS
INTRODUCTION
• We’re going to assume at this point that each polygon we have fits into a single plane (triangle or not)
• One very important piece of information we need to know is what is the front (and back) of our polygon
•
Need this for:
•
Lighting/shading
•
Backface culling (i.e., not drawing polygons that face away from the camera)
• More generally, we also would like to know the equation of the plane the polygon rests in
•
Useful for collision detection and/or raytracing
PLANE EQUATION
• Each polygon in our 3D scene is contained within a plane of infinite extent
• The general equation of a plane is as follows:
Ax  By  Cz  D  0
• (x,y,z) = any point on the plane
• A,B,C,D = plane parameters
•
CALCULATING THE PLANE EQUATION
• To get the plane parameters:
•
Divide the formula by D
•
Pick any 3 noncollinear points in the polygon
•
Solve set of 3 simultaneous linear plane equations to get A/D, B/D, and C/D:
( A / D) xk  ( B / D) yk  (C / D) zk  1
k  1,2,3
USING CRAMER’S RULE
• It turns out you can use something called Cramer’s Rule to solve this
• Basically, the solutions for A, B, C, and D are given by the following determinants:
1 y1
z1
x1 1 z1
x1
y1 1
x1
y1
z1
A  1 y2
1 y3
z2
z3
B  x2 1 z2
x3 1 z3
C  x2
x3
y2 1
y3 1
D   x2
x3
y2
y3
z2
z3
More information on Cramer’s Rule:
http://www.purplemath.com/modules/cramers.htm
http://2000clicks.com/MathHelp/AlgebraOnline/a000926PointsLines3d.htm
QUICK REVIEW: DETERMINANT OF A MATRIX
M
1 2
3 4
 1* 4  2 * 3
i j k
M  1 2 3  i (2 * 6  3 * 5)  j (3 * 4  1* 6)  k (1* 5  2 * 4)
4 5 6
GETTING THE DETERMINANTS
• Expanding the determinants gives us the answers for A,B,C, and D
•
This will actually work even when D = 0 (technically you can’t use Cramer’s Rule under those circumstances, but
the workaround actually gives you the same answer)
A  y1 ( z 2  z3 )  y2 ( z3  z1 )  y3 ( z1  z2 )
B  z1 ( x2  x3 )  z 2 ( x3  x1 )  z3 ( x1  x2 )
C  x1 ( y2  y3 )  x2 ( y3  y1 )  x3 ( y1  y2 )
D   x1 ( y2 z3  y3 z 2 )  x2 ( y3 z1  y1 z3 )  x3 ( y1 z 2  y2 z1 )
IF THE POLYGON IS NOT CONTAINED IN A PLANE
• Either:
•
Divide into triangles
• OR:
•
Find approximating plane
•
Divide vertices into subsets of 3
•
Get plane parameters for each subset
•
Get average plane parameters
FRONT AND BACK
• Each polygon has a front face and a back face
• We can use the plane equation to determine if a given point (x,y,z) is:
•
ON the plane  Ax + By + Cz + D != 0
•
BEHIND the plane  Ax + By + Cz + D < 0
•
IN FRONT OF the plane  Ax + By + Cz + D > 0
• NOTE: This is valid for a right-handed coordinate system, so we should also make sure our vertices are
ordered counterclockwise!
NORMAL VECTOR
• Normal vector = gives us orientation of plane/polygon
•
Points towards OUTSIDE of plane
•
From back face to front face
•
Perpendicular to surface of plane
•
Normal N = (A,B,C)  parameters from plane equation!
•
Although it doesn’t have to be, the normal vector is often
normalized (i.e., length = 1)
GETTING THE NORMAL VECTOR FROM EDGES
• You can also compute the normal vector directly from the edges using the cross-product
• Assuming V1, V2, and V3 are consecutive vertices of a convex polygon in counterclockwise order:
N  (V2  V1 )  (V3  V2 )
• Can also use this approach to get A, B, and C, and then solve for D.
• NOTE: Counterclockwise looking from outside the polygon towards inside
POINT-NORMAL PLANE EQUATION
• Given the normal N and any point on the plane, the following holds true:
N  P  D
• A related, alternative equation for a plane is the point-normal form:
N  (V  P)  0
• …where V is any 3D point
• Basically, think of a vector going from P (a point on the plane) to some other point V. If that vector is on
the plane, it should be orthogonal to the normal (hence, the dot product with be 0).
EXAMPLE
Download