CS 450: COMPUTER GRAPHICS REVIEW: OVERVIEW OF POLYGONS SPRING 2015 DR. MICHAEL J. REALE NOTE: COUNTERCLOCKWISE ORDER • Assuming: • Right-handed system • Vertices in counterclockwise order (looking at front of polygon) FILL AREAS • Fill area (or filled area) = area to be filled with color or pattern (or both) • Usually surface of object • Usually polygons WHY POLYGONS? • Why? • Boundaries = linear equations (efficient fill algorithms) • Can approximate most curved surfaces with polygons • Shading easier (especially with triangles single plane per triangle) • 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 POLYGON DEFINITIONS • 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 • • E.g., after transformations Fitting to surface makes non-planar polygons • E.g., quad “bent” in half • Thus, use triangles single plane per triangle 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 some edges have length 0 CONVEX VS. CONCAVE • Interior angle = angle inside polygon boundary formed by two adjacent edges • By interior angle: • Convex = all interior angles less than 180° • Concave = one or more interior angles greater than or equal to 180° • By looking at vertices compared to edge lines: • 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 • OpenGL cannot deal with degenerate polygons or concave polygons programmer must detect/preprocess them • Degenerate polygons detect and remove • Concave polygons detect and split into convex polygons http://www.spudislunarresources.co m/blog/wpcontent/uploads/2013/03/Airplane.j pg WHAT’S YOUR VECTOR, VICTOR? • A vector = (N x 1) or (1 x N) matrix • (# of rows X # of columns) • In computer graphics: • N usually 2, 3, or 4 (homogeneous coordinates) • 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 • Vector interpreted as: • Location (w = 1) • Direction (w = 0) • Scalar = single value (or 1x1 vector) x v y x v y z x y v z w LENGTH OF A VECTOR AND THE ZERO VECTOR • Use Euclidean distance for length: v x y z 2 2 • A vector with a length of zero is called the zero vector: 0 v 0 0 v 0 0 0 0 v 0 0 2 SCALING VECTORS • Scalar times a vector = scalar times 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 • Add/subtract vectors add/subtract components • Geometric interpretation: • Adding putting head of one vector on tail of the other • Subtracting gives direction from one endpoint to the other 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 ) DOT PRODUCT A B xA xB y A yB z A zB • Result is a single number (i.e., scalar) another name for the dot product is the scalar product • Dot product of vector with itself = square of length of vector: • Equivalent to: A B A B cos • …where θ = smallest angle between the two vectors • If the vectors are normalized, then: A 1 B 1 A B cos A A x y z A 2 2 2 2 Remember: DOT PRODUCT: CHECKING ANGLES A B A B cos cos(0 ) 1 • Look at sign of dot product to check angle: • (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 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 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! • 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: 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 ) DETECTING AND SPLITTING CONCAVE POLYGONS • There are 2 ways we can do this: • Vector method • Check interior angles using cross product • Rotational method • Rotate each edge in line with X axis check if vertex below X axis ( E1 E 2 ) z 0 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 concave negative Z component split polygon along first vector in cross product pair • Have to intersect this line with other edges • Repeat process with two new polygons • NOTE: 3 successive collinear points anywhere cross product becomes zero vector! ( E2 E 3 ) z 0 ( E3 E 4 ) z 0 ( E4 E 5 ) z 0 ( E5 E 1 ) z 0 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 • Every 3 consecutive vertices make triangle • Remove middle vertex • Keep going until down to last 3 vertices PLANE EQUATION • Need plane equation: • Collision detection, raytracing/raycasting, etc. • Need normal of polygon: • Lighting/shading • Backface culling don’t draw polygon facing away from camera • General equation of a plane: Ax By Cz D 0 • (x,y,z) = any point on the plane • A,B,C,D = plane parameters • 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 CALCULATING THE PLANE EQUATION • Divide the formula by D • Pick any 3 noncollinear points in the polygon ( A / D) xk ( B / D) yk (C / D) zk 1 k 1,2,3 • Solve set of 3 simultaneous linear plane equations to get A/D, B/D, and C/D use Cramer’s Rule 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 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 ) 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 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 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 NORMAL AND PLANE EQUATION • Solve for plane equation normal = (A,B,C) • OR • Get normal from edges using cross-product solve for D in plane equation • V1, V2, and V3 = consecutive vertices in counterclockwise order: N (V2 V1 ) (V3 V2 ) • 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 EXAMPLE