3 INTRODUCTIONSo far, we’ve talked mostly about points, lines, and curvesFill area (or filled area) = some area that needs to be filled in with a solid color or patternUsually used to describe the surface of an object (but not always)Usually polygons (but not always)
4 WHY POLYGONS?Most graphics libraries force you to use polygons for filled areasWhy?Polygon boundaries can be described with linear equationsMakes fill algorithms more efficientCan approximate most curved surfaces with polygonsSimilar to approximating curve with line segmentsMakes 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 mappingSurface tessellation = approximating a curved surface with polygonsMore polygons better detail, but more vertices/polygons to processAlso called fitting the surface with a polygon mesh
5 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 boundaryMore finicky definitions contained in single plane, edges have no common points other than their endpoints, no three successive points collinearStandard polygon or simple polygon = closed-polyline with no crossing edges
6 DOES A POLYGON LIE IN A SINGLE PLANE? In computer graphics, polygon not always in same plane:Round-off errorI.e., points originally in single plane, but, after transformation, the points may be slightly offFitting to surface makes non-planar polygonsE.g., if using quads, approximation may “bend” the quad in halfThus, we usually use triangles to avoid this problem
7 DEGENERATE POLYGONSDegenerate polygons = often used to describe polygon with:3 or more collinear vertices generates a line segmentE.g., in extreme case, triangle with no areaRepeated vertex positions generates shape with:Extraneous linesOverlapping edgesEdges with length 0
8 CONVEX VS. CONCAVE: BY INTERIOR ANGLE Interior angle = angle inside polygon boundary formed by two adjacent edgesConvex = all interior angles less than 180°Concave = one or more interior angles greater than or equal to 180°
9 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 notConvex = for all edge lines, all other vertices are on one sideConcave = for one or more edge lines, some of the vertices are on one side and some are on the otherAlso, one or more edge lines will intersect another edge
11 DEALING WITH DEGENERATES AND CONCAVE POLYGONS Most graphics software packages (i.e., OpenGL) can’t really deal with the degenerate polygonsTo avoid extra processing, however, they leave it as the programmer’s problemConcave polygons are more complicated to deal with when it comes to filling algorithmsSolution: split concave polygon into convex polygons (e.g., triangles)Again, often have to do this yourselfOpenGL “requires” that all polygons be convex (or at least, there’s no guarantee the filling will work if not convex)
13 INTRODUCTIONBefore we go any further, we’ll need to quickly review some concepts from linear algebraWe will revisit these concepts in a future, more thorough reviewFor now, let’s talk about:VectorsVector lengthScaling vectorsNormalized vectorsAdding/subtracting vectorsDot productCross product
14 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 values3D x, y, and z values4D x, y, z, and w valuesScalar = single value (or 1x1 vector)
15 INTERPRETING VECTORS A vector can also be interpreted as: …in space Location (w = 1)Direction (w = 0)…in space
16 LENGTH OF A VECTORWe usually use Euclidean distance to get the length of a vector:The square of the distance (not surprisingly) is given by:
17 ZERO VECTORA vector with a length of zero is called the zero vector:
18 SCALING VECTORSTo multiply a scalar by a vector, you multiple that scalar by the components of the vectorExample: multiply 5 by a 3D vector A
19 NORMALIZED VECTORSNormalize a vector = divide vector by its length ║v║ makes length equal to 1Equivalent to multiplying vector by 1/║v║Resulting vector is called a normalized vectorWARNING: This is NOT the same as a NORMAL vector!Although normal vectors are often normalized.
20 ADDING/SUBTRACTING VECTORS To add/subtract vectors, you add/subtract their respective components:
21 GEOMETRIC INTERPRETATION OF ADDING/SUBTRACTING (DIRECTION) VECTORS Adding putting head of one vector on tail of the otherSubtracting gives direction from one endpoint to the other
22 DOT PRODUCTGiven two 3D vectors A and B, the dot product of A and B is given by:Basically:Multiple corresponding componentsAdd them togetherNOTE: Result is a single number (i.e., scalar) another name for the dot product is the scalar product
23 DOT PRODUCT WITH YOURSELF The dot product of a vector A with itself is equal to the square of its length:
24 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:
25 DOT PRODUCT: CHECKING ANGLES Remember:DOT PRODUCT: CHECKING ANGLESWhat 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 )
26 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 VU, V, and W form right-handed system!I.e., can use right-hand-rule on U and V (IN THAT ORDER) to get WExample: (X x Y) = Z axis!
27 LENGTH OF CROSS PRODUCT The length of W (= U X V) is equivalent to:…where again θ = smallest angle between U and VIf U and V are parallel θ = 0° sin θ = 0 get zero vector for W!
28 CROSS PRODUCT: ORDER MATTERS! WARNING! ORDER MATTERS with the cross product!Property of anti-commutativityREMEMBER THE RIGHT-HAND-RULE!!!
29 COMPUTING THE CROSS PRODUCT To compute the cross product:
30 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 topex = x axis, ey = y axis, ez = z axis
32 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
33 CHECKING INTERIOR ANGLES Treat each edge as a vector (go counterclockwise):For each pair of consecutive edge vectors, get cross productIf convex signs of all cross products will be the sameIn 2D cross product will always be +Z or –ZIn 3D transform polygon to XY plane (as best we can)If concave one or more will be differentNOTE: This breaks down if we have 3 successive collinear points anywhere cross product becomes zero vector!
34 SPLITTING A CONCAVE POLYGON There are 2 ways we can do this:Vector methodRotational method
35 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 productIf a cross product has a negative z component split polygon along first vector in cross product pairHave to intersect this line with other edges
36 SPLITTING BY ROTATIONAL METHOD Transform to XY plane (if necessary)For each vertex Vk:Move polygon so that Vk is at the originRotate polygon so that Vk+1 is on the X axisIf Vk+2 is below X axis polygon is concave split polygon along x axisRepeat concave test for each of the two new polygonsStop when we’ve checked all vertices
37 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 triangleRemove middle vertexKeep going until down to last 3 vertices
39 INTRODUCTIONWe’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 polygonNeed this for:Lighting/shadingBackface 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 inUseful for collision detection and/or raytracing
40 PLANE EQUATIONEach polygon in our 3D scene is contained within a plane of infinite extentThe general equation of a plane is as follows:(x,y,z) = any point on the planeA,B,C,D = plane parameters
41 CALCULATING THE PLANE EQUATION To get the plane parameters:Divide the formula by DPick any 3 noncollinear points in the polygonSolve set of 3 simultaneous linear plane equations to get A/D, B/D, and C/D:
42 USING CRAMER’S RULEIt turns out you can use something called Cramer’s Rule to solve thisBasically, the solutions for A, B, C, and D are given by the following determinants:More information on Cramer’s Rule:
44 GETTING THE DETERMINANTS Expanding the determinants gives us the answers for A,B,C, and DThis 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)
45 If THE POLYGON IS NOT CONTAINED IN A PLANE Either:Divide into trianglesOR:Find approximating planeDivide vertices into subsets of 3Get plane parameters for each subsetGet average plane parameters
46 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 != 0BEHIND the plane Ax + By + Cz + D < 0IN FRONT OF the plane Ax + By + Cz + D > 0NOTE: This is valid for a right-handed coordinate system, so we should also make sure our vertices are ordered counterclockwise!
47 NORMAL VECTOR Normal vector = gives us orientation of plane/polygon Points towards OUTSIDE of planeFrom back face to front facePerpendicular to surface of planeNormal 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)
48 GETTING THE NORMAL VECTOR FROM EDGES You can also compute the normal vector directly from the edges using the cross-productAssuming 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
49 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 pointBasically, 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).