Informationsteknologi Todays class Clipping n Parametric and pointnormal

  • Slides: 42
Download presentation
Informationsteknologi Today’s class Clipping n Parametric and point-normal form of lines n Intersecting lines

Informationsteknologi Today’s class Clipping n Parametric and point-normal form of lines n Intersecting lines and planes n Thursday, November 22, 2007 Computer Graphics - Class 11 1

Informationsteknologi Normalized device coordinates n n n Graphics imaging is device dependent Image development

Informationsteknologi Normalized device coordinates n n n Graphics imaging is device dependent Image development is device independent To simplify the process images are frequently generated using normalized device coordinates (NDC), where x and y values are between -1. 0 and 1. 0 (for Open. GL; other systems may use 0. 0 to 1. 0) Thursday, November 22, 2007 Computer Graphics - Class 11 2

Informationsteknologi Converting NDC to DC To display an image a transformation from NDC to

Informationsteknologi Converting NDC to DC To display an image a transformation from NDC to device coordinates (DC) needs to take place n To convert NDC to DC: n ® multiply by ½ the number of pixels in the dimension ® add ½ the number of pixels in the dimension Thursday, November 22, 2007 Computer Graphics - Class 11 3

Informationsteknologi Viewing transformation It is usually inconvenient to represent an image in DC or

Informationsteknologi Viewing transformation It is usually inconvenient to represent an image in DC or NDC n Want to do it in world coordinates (WC) n Setting the window and the viewport accomplishes the transformation from WC to DC, thus providing the viewing transformation n Thursday, November 22, 2007 Computer Graphics - Class 11 4

Informationsteknologi Clipping volume The combination of setting the window and the viewport defines the

Informationsteknologi Clipping volume The combination of setting the window and the viewport defines the clipping volume n It is possible some primitives lie wholly or partially outside this volume n Need to insure these items are not displayed n This process is known as clipping n Thursday, November 22, 2007 Computer Graphics - Class 11 5

Informationsteknologi Clipping points is trivial n A point is visible if n x xmax

Informationsteknologi Clipping points is trivial n A point is visible if n x xmax ® ymin y ymax ® xmin Thursday, November 22, 2007 Computer Graphics - Class 11 6

Informationsteknologi Clipping lines n Lines lie: wholly inside window ® wholly outside window ®

Informationsteknologi Clipping lines n Lines lie: wholly inside window ® wholly outside window ® partially inside and partially outside window ® n Define a 4 -bit outcode (b 0 b 1 b 2 b 3) describing relationship of line segment to window b 0 = 1 if y > ymax (point above window) ® b 1 = 1 if y < ymin (point below window) ® b 2 = 1 if x > xmax (point to right of window) ® b 3 = 1 if x < xmin (point to left of window) ® Thursday, November 22, 2007 Computer Graphics - Class 11 7

Informationsteknologi Cohen-Sutherland Outcode Algorithm n n code 1 outcode (p 1); code 2 outcode

Informationsteknologi Cohen-Sutherland Outcode Algorithm n n code 1 outcode (p 1); code 2 outcode (p 2); loop ® if code 1 = 0 and code 2 = 0 then § display line segment; exit loop; ® if code 1 & code 2 0 then § reject line segment; exit loop; ® if code 1 = 0 then § swap (p 1, p 2); swap (code 1, code 2); ® ® ® find a nonzero bit in code 1 find intersection of line with corresponding window boundary; p 1 intersection point; code 1 outcode (p 1); Thursday, November 22, 2007 Computer Graphics - Class 11 8

Informationsteknologi Observations Clipping of one line segment may occur more than once n Don’t

Informationsteknologi Observations Clipping of one line segment may occur more than once n Don’t want to change the slope of the line n Therefore, do the work in floating point numbers n Works well when explicit form of line equation is used n Thursday, November 22, 2007 Computer Graphics - Class 11 9

Informationsteknologi Vertical lines have an infinite slope n Will this cause a problem for

Informationsteknologi Vertical lines have an infinite slope n Will this cause a problem for the Cohen. Sutherland algorithm? n Pair off and determine the answer to the above question. n ® If no, great! ® If yes, how can you overcome the problem? Thursday, November 22, 2007 Computer Graphics - Class 11 10

Informationsteknologi Vertical lines and Cohen. Sutherland clipping p 2 y p 1 top x

Informationsteknologi Vertical lines and Cohen. Sutherland clipping p 2 y p 1 top x x For a vertical line x=0, but you’ll never divide by it! Thursday, November 22, 2007 Computer Graphics - Class 11 11

Informationsteknologi Parametric form of a line Describes a “travelling” motion along a line or

Informationsteknologi Parametric form of a line Describes a “travelling” motion along a line or line segment n The x and y coordinates of points on the line are described by linear equations involving a parameter variable (frequently t, u, or v) n Example: n x=t y = 2 t + 1 Thursday, November 22, 2007 Computer Graphics - Class 11 12

Informationsteknologi General form n In general, the parametric form for the equation of a

Informationsteknologi General form n In general, the parametric form for the equation of a line is x = at + b y = ct + d Thursday, November 22, 2007 Computer Graphics - Class 11 13

Informationsteknologi Line segments Frequently, in computer graphics we restrict t by 0 t 1

Informationsteknologi Line segments Frequently, in computer graphics we restrict t by 0 t 1 n This generates a line segment n Thursday, November 22, 2007 Computer Graphics - Class 11 14

Informationsteknologi Finding the parametric form of a line segment Given two endpoints, (x 1,

Informationsteknologi Finding the parametric form of a line segment Given two endpoints, (x 1, y 1) and (x 2, y 2) n Let t = 0 be at (x 1, y 1) and t = 1 be at (x 2, y 2) n Then: x = x 1 + (x 2 - x 1)t y = y 1 + (y 2 - y 1)t n Thursday, November 22, 2007 Computer Graphics - Class 11 15

Informationsteknologi Vector form of parametric equation Let the two endpoints of a line segment

Informationsteknologi Vector form of parametric equation Let the two endpoints of a line segment be denoted A and B n The line between them is described by P(t) = A + (B - A)t 0 t 1 n Thursday, November 22, 2007 Computer Graphics - Class 11 16

Informationsteknologi Slope vector The slope of a line is (y 2 -y 1)/(x 2

Informationsteknologi Slope vector The slope of a line is (y 2 -y 1)/(x 2 -x 1) n The slope vector is defined as the vector (x 2 -x 1, y 2 -y 1) n Note that the slope vector’s components are the coefficients of the linear terms of the parametric equations n Thursday, November 22, 2007 Computer Graphics - Class 11 17

Informationsteknologi Perpendicular lines n n Slopes are negative reciprocals of each other Given a

Informationsteknologi Perpendicular lines n n Slopes are negative reciprocals of each other Given a line with a slope of (y 2 -y 1)/(x 2 -x 1), which implies a slope vector of (x 2 -x 1, y 2 -y 1), a perpendicular line will have slope -(x 2 -x 1)/(y 2 -y 1) and a slope vector (-(y 2 -y 1), (x 2 -x 1)) To get the equation of the perpendicular line choose the midpoint (mx, my) of the line to base it from Parametric form is now x(t) = mx - (y 2 - y 1)t y(t) = my + (x 2 - x 1)t Thursday, November 22, 2007 Computer Graphics - Class 11 18

Informationsteknologi Point normal form n n • r = D, where n is the

Informationsteknologi Point normal form n n • r = D, where n is the normal to the line, r represents all points on the line, and D = n • A, where A is any point on the line n c A Thursday, November 22, 2007 R n • (R - A) = 0 Computer Graphics - Class 11 19

Informationsteknologi Half spaces A line divides space into two halves, an outside half space

Informationsteknologi Half spaces A line divides space into two halves, an outside half space and an inside half space n Given a line through point A and outward normal n, then any point Q lies n in the outside half space if (Q-A) • n > 0 ® on the line if (Q-A) • n = 0 ® in the inside half space if (Q-A) • n < 0 ® Thursday, November 22, 2007 Computer Graphics - Class 11 20

Informationsteknologi Intersections of lines with lines and planes n n Let a ray be

Informationsteknologi Intersections of lines with lines and planes n n Let a ray be represented by B+ct Both lines and planes are represented in point normal form by n ∙ (R – A) = 0 or n ∙ R = D To find the intersection, substitute the ray equation into the point normal form for R: n ∙ (B+ct) = D Solve for t, which we will designate thit, the time at which the ray hits the line or plane: thit = (D - n ∙ B) / (n ∙ C) Thursday, November 22, 2007 Computer Graphics - Class 11 21

Informationsteknologi Intersecting a ray with a polygon n We frequently need to answer the

Informationsteknologi Intersecting a ray with a polygon n We frequently need to answer the questions ® Where does a ray hit an object? ® What part of a line is inside an object? n We can solve these problems by determining the intersections of a ray with a polygon Thursday, November 22, 2007 Computer Graphics - Class 11 22

Informationsteknologi Two intersections n n Let the ray in question be denoted B +

Informationsteknologi Two intersections n n Let the ray in question be denoted B + ct It will hit the boundary of a convex polygon at most twice, once upon entering the polygon and once upon leaving Call these times tin and tout Need to find intersection of ray with each edge and determine portion within polygon Thursday, November 22, 2007 Computer Graphics - Class 11 23

Informationsteknologi Entering or leaving? When the ray intersects an edge, we need to decide

Informationsteknologi Entering or leaving? When the ray intersects an edge, we need to decide if it is entering or leaving the polygon n Let n represent the normal to an edge n The following conditions hold: n ® If n ∙ c < 0 the ray is entering ® If n ∙ c = 0 the ray is parallel ® If n ∙ c > 0 the ray is leaving Thursday, November 22, 2007 Computer Graphics - Class 11 24

Informationsteknologi Times to keep For entering times, want to keep the largest intersection time

Informationsteknologi Times to keep For entering times, want to keep the largest intersection time (or 0 if the ray starts inside the polygon) n For leaving times, want to keep the smallest intersection time n How would this be modified if we consider a line segment intersecting the polygon rather than a ray? n Thursday, November 22, 2007 Computer Graphics - Class 11 25

Informationsteknologi What’s left? The ray between tin and tout is the part inside the

Informationsteknologi What’s left? The ray between tin and tout is the part inside the polygon n The rest is clipped n Thursday, November 22, 2007 Computer Graphics - Class 11 26

Informationsteknologi Liang-Barksy clipping n n Useful when clipping against a rectangular window Uses parametric

Informationsteknologi Liang-Barksy clipping n n Useful when clipping against a rectangular window Uses parametric form of line equation x(t) = x 1 + t(x 2 -x 1) = x 1 + t x ® y(t) = y 1 + t(y 2 -y 1) = y 1 + t y ® 0 t 1 ® n Clipping conditions become xmin x 1 + t x xmax ® ymin y 1 + t y ymax ® Thursday, November 22, 2007 Computer Graphics - Class 11 27

Informationsteknologi Inequality form n Each of the four inequalities is of the form tpk

Informationsteknologi Inequality form n Each of the four inequalities is of the form tpk qk, where ® p 1 = - x, q 1 = x 1 -xmin ® p 2 = x, q 2 = xmax-x 1 ® p 3 = - y, q 3 = y 1 -ymin ® p 4 = y, q 4 = ymax-y 1 Thursday, November 22, 2007 Computer Graphics - Class 11 28

Informationsteknologi Lines parallel to window boundary For lines that are parallel to a window

Informationsteknologi Lines parallel to window boundary For lines that are parallel to a window boundary, the corresponding pk will be 0 n If the corresponding qk < 0 the line is outside the boundary and can be ignored n Thursday, November 22, 2007 Computer Graphics - Class 11 29

Informationsteknologi Other lines If pk < 0 the infinite extension of the line enters

Informationsteknologi Other lines If pk < 0 the infinite extension of the line enters the clipping region n If pk > 0 the infinite extension of the line leaves the clipping region n In either case, the value of t for which the line crosses the boundary is qk / pk n Thursday, November 22, 2007 Computer Graphics - Class 11 30

Informationsteknologi Liang-Barksy algorithm n n Let t 1 and t 2 define the ends

Informationsteknologi Liang-Barksy algorithm n n Let t 1 and t 2 define the ends of the clipped line that will be visible t 1 is the entering time, t 2 the leaving time Initialize t 1 to 0 and t 2 to 1 For each of the four boundaries: ® if pk = 0 then § if qk < 0 then reject line ® else if pk < 0 then § compute entering time Thursday, November 22, 2007 Computer Graphics - Class 11 31

Informationsteknologi Liang-Barksy algorithm (cont. ) § if entering time > t 1 then •

Informationsteknologi Liang-Barksy algorithm (cont. ) § if entering time > t 1 then • change t 1 to entering time • if t 1 > t 2 then reject line ® else // pk > 0 § compute leaving time § if leaving time < t 2 then • change t 2 to leaving time • if t 1 > t 2 then reject line n If line has not been rejected then draw line between t 1 and t 2 Thursday, November 22, 2007 Computer Graphics - Class 11 32

Informationsteknologi Problems with polygons n n n Edges of polygons are made of line

Informationsteknologi Problems with polygons n n n Edges of polygons are made of line segments Recognize that when we clip a polygon’s edges, some of the edges may be completely removed and others may be partially removed Thus, we may change the shape of the polygon during clipping Thursday, November 22, 2007 Computer Graphics - Class 11 33

Informationsteknologi Clipping polygons to a rectangular window Number vertices of polygon and window in

Informationsteknologi Clipping polygons to a rectangular window Number vertices of polygon and window in a clockwise manner n Clip against one window edge at a time n Input is a set of polygon vertices n Output is an updated set of polygon vertices n Thursday, November 22, 2007 Computer Graphics - Class 11 34

Informationsteknologi Sutherland-Hodgman algorithm n Start with edge from last polygon vertex to first one,

Informationsteknologi Sutherland-Hodgman algorithm n Start with edge from last polygon vertex to first one, and then repeat for each successive edge: ® Assume beginning vertex has been handled ® If both vertices are inside window with respect to clipping edge output second vertex Thursday, November 22, 2007 Computer Graphics - Class 11 35

Informationsteknologi Sutherland-Hodgman algorithm (cont. ) ® If first vertex is inside and second vertex

Informationsteknologi Sutherland-Hodgman algorithm (cont. ) ® If first vertex is inside and second vertex is outside, find intersection of polygon edge with clipping edge and output intersection ® If both vertices are outside window output nothing ® If first vertex is outside and second vertex is inside, find intersection of polygon edge with clipping edge and output intersection and then second vertex Thursday, November 22, 2007 Computer Graphics - Class 11 36

Informationsteknologi Bounding box Clipping polygons can be time consuming, especially if it is many

Informationsteknologi Bounding box Clipping polygons can be time consuming, especially if it is many sided and lies completely outside the window n Use a bounding box (extent) to eliminate these quickly n Thursday, November 22, 2007 Computer Graphics - Class 11 37

Informationsteknologi What’s visible? Once clipped, we now have a list of polygons to display

Informationsteknologi What’s visible? Once clipped, we now have a list of polygons to display n We must determine which surfaces are visible and which are hidden n Thursday, November 22, 2007 Computer Graphics - Class 11 38

Informationsteknologi Object-space approaches n n n Given a list of n polygons Take one

Informationsteknologi Object-space approaches n n n Given a list of n polygons Take one polygon and compare it with the remaining ones to determine the part that is visible; render it The above polygon no longer needs to be considered Take one of the remaining polygons and repeat the process This is O(n 2) and thus should only be used for scenes with few polygons Thursday, November 22, 2007 Computer Graphics - Class 11 39

Informationsteknologi Image-space approaches n n n n Follow ray tracing paradigm Consider a ray

Informationsteknologi Image-space approaches n n n n Follow ray tracing paradigm Consider a ray from viewpoint that passes through a pixel Find the closest intersection of the ray with all polygons Color pixel by the shade of the face closest to viewpoint Repeat for all pixels This is O(n) if there are n polygons However, they tend to be more jagged since they work at the pixel level Thursday, November 22, 2007 Computer Graphics - Class 11 40

Informationsteknologi Back face removal n n n A back face is one that is

Informationsteknologi Back face removal n n n A back face is one that is turned away from the eye of the camera The camera will not see it if the faces in front are opaque For a convex polyhedron: every face is either wholly visible or wholly invisible ® eliminating back faces is equivalent to hidden surface removal ® Thursday, November 22, 2007 Computer Graphics - Class 11 41

Informationsteknologi Culling The process of removing something is known as culling n In Open.

Informationsteknologi Culling The process of removing something is known as culling n In Open. GL, turn on culling of back faces with gl. Enable (GL_CULL_FACE); and then gl. Cull. Face (GL_BACK); n Thursday, November 22, 2007 Computer Graphics - Class 11 42