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 Makes fill algorithms more efficient 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) 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/articles-pages/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 Scalar = single value (or 1x1 vector) http://www.spudislunarresources.com/blog/wp-content/uploads/2013/03/Airplane.jpg
INTERPRETING VECTORS A vector can also be interpreted as: …in space Location (w = 1) Direction (w = 0) …in space
LENGTH OF A VECTOR We usually use Euclidean distance to get the length of a vector: The square of the distance (not surprisingly) is given by:
ZERO VECTOR A vector with a length of zero is called the zero vector:
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
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.
ADDING/SUBTRACTING VECTORS To add/subtract vectors, you add/subtract their respective components:
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: 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:
DOT PRODUCT: WHAT DOES IT MEAN? The dot product of two vectors A and B is in fact equivalent to: …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:
DOT PRODUCT: CHECKING ANGLES Remember: DOT PRODUCT: CHECKING ANGLES 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) = -1 vectors pointing in the exact opposite direction (θ = 180°) (We’re going to use this trick for lighting calculations later )
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 Example: (X x Y) = Z axis!
LENGTH OF CROSS PRODUCT The length of W (= U X V) is equivalent to: …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! Property of anti-commutativity REMEMBER THE RIGHT-HAND-RULE!!!
COMPUTING THE CROSS PRODUCT To compute the cross product:
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
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): 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!
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: 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: (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:
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: 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
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)
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: 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: A related, alternative equation for a plane is the point-normal form: …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