cs 376 introduction to computer graphics 02 / 12 / 2007 instructor: michael eckmann
TRANSCRIPT
CS 376Introduction to Computer Graphics
02 / 12 / 2007
Instructor: Michael Eckmann
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Today’s Topics• Questions?
• World & Screen coordinates, Windows & Viewports
• Clipping– points– lines– polygons
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Windows and Viewports
• Recall that we define our objects in World coordinates and our screen
shows (some part of) the world in Screen coordinates.
• Now that we know about transformations like translation and scaling,
we can talk about how the world coordinates are transformed into screen
coordinates.
• In the world, we define a clipping window which contains that part of
the world we wish to be displayed.
• On the screen we create and position a display window and within that
display window we create and position some viewport which can be all
or part of the display window. Diagram on the board.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Windows and Viewports
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Windows and Viewports
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Windows and Viewports• clipping window in world
– specified in openGL in the call to • glu.gluOrtho2D(MIN_X, MAX_X, MIN_Y, MAX_Y);
• display window on the screen– specified in openGL in the calls
• testFrame = new Frame("TestFrame");• testFrame.setLocation(10, 10); // positions it on screen at 10,10• testFrame.setSize( 410, 452 ); // size in pixels
• by default the viewport is the entire display window to make a smaller
viewport (part of that display window) use• gl.glViewport(lowLeftX, lowLeftY, width, height);
• openGL still automatically performs the viewing transform from the
clipping window in the world to the viewport in the display window.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Windows and Viewports
• The transformation that converts the clipping window to the viewport is
called the 2d viewing transformation, (aka window-to-viewport
transformation, aka windowing transformation).
• So a way to do this is:– define a window in your world coordinates– clip the scene to that window (that is, “clip” or ignore everything
outside the window)– translate this clipping window to the origin– scale it to the size of the viewport– translate from the origin to correct position within the display
window.
• Diagram on board.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Windows and Viewports• Different effects can be generated when you change viewport / clipping
window.– 1) if we just change the viewport's position
• same exact objects will be shown but in a different position within the display window.
– 2) if we change size of viewport• changes the size of the objects shown within that viewport
– 3) if we keep the viewport constant but change the clipping window• larger clipping window will cause more of the scene to be shown
but these objects will be smaller• smaller clipping window will cause less of the scene to be shown
but the objects will be larger• if you continually make the clipping window smaller this has the
effect of zooming in• making the clipping window larger --- zooms out.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Windows and Viewports• Different effects can be generated when you change viewport / clipping
window.– 4) if we keep the viewport constant, keep the clipping window the
same size but change it's position in the world• what kind of effect will be displayed?
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Windows and Viewports• Different effects can be generated when you change viewport / clipping
window.– 4) if we keep the viewport constant, keep the clipping window the
same size but change it's position in the world• what kind of effect will be displayed?
– panning
• Aspect ratio: if the clipping window and viewport have the same ratio
of x to y, then proportions will be correct (no stretching etc.)– but if they have different aspect ratios, for example a square in the
world may not be a square in the viewport
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Windows and Viewports
• Example on the board of a viewing transformation– define a clipping window in our world coordinates– define a viewport in screen coordinates
• translate clipping window to origin
• scale
• translate to correct screen coords
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Clipping
• To clip our world into a window we need to determine what in the world
is inside our clipping window and what is outside it.
• we'll talk about clipping points, lines and polygons
• let's say our clipping window has the following vertices in the world– (xL, yB), (xR, yB), (xR, yT), (xL, yT) – L = left, R = right, T = top, B = bottom
• A point (x,y) is visible ifxL <= x <= xRyB <= y <= yT
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Clipping
• let's say our clipping window has the following vertices in the world– (xL, yB), (xR, yB), (xR, yT), (xL, yT) – L = left, R = right, T = top, B = bottom– draw clipping window on board
• A line segment is trickier– a line is visible if both its endpoints are in the clipping window – could solve simultaneous equations for the intersections of the
window edge with the lines, but this is expensive
• The Cohen-Sutherland algorithm for line clipping is a more efficient
way.– each vertex (endpoint) is assigned a region code (aka outcode) that
defines that vertex to be in one of 9 regions
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Clipping
this region code is 4 bits1st bit is the sign of yT-y2nd bit is the sign of y-yB3rd bit is the sign of xR-x4th bit is the sign of x-XL0 = positive1 = negative
Clipping window is the center
area in the right diagram
with the 0000
| |
1001 | 1000 | 1010
--------------|-----------------------|--------------
| |
0001 | 0000 | 0010
--------------|-----------------------|--------------
| |
0101 | 0100 | 0110
| |
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Clipping
Trivially accept if both
endpoints have 0000 region
codes
Trivially reject if any
corresponding bits in the two
region codes are both 1(and them together and if
result != 0000, then reject)
Example on board of trivial
accepts and rejects.
| |
1001 | 1000 | 1010
--------------|-----------------------|--------------
| |
0001 | 0000 | 0010
--------------|-----------------------|--------------
| |
0101 | 0100 | 0110
| |
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Clipping
• Example on board of several non-trivial rejects / accepts.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Clipping
If can't trivially accept or reject
a line then we compute some
intersections with the line
boundaries
Text book figure 6-14 has a
mistake that shows a line
that would have been
trivially rejected (P3 to P4)
but they consider it as not
rejected until intersections
are computed ...
| |
1001 | 1000 | 1010
--------------|-----------------------|--------------
| |
0001 | 0000 | 0010
--------------|-----------------------|--------------
| |
0101 | 0100 | 0110
| |
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Clipping
• Besides Cohen-Sutherland line clipping, there are several faster line
clipping algorithms.
• Our text talks about two more: Liang-Barsky line clipping and Nicholl-
Lee-Nicholl line clipping.
• Let's discuss how Liang-Barsky Line Clipping works.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Clipping• Liang-Barsky line clipping
– based on parametric equation of a line• given end points (x
0 , y
0) and (x
end , y
end) the parametric equations
of a line are:• x = x
0 + u (x
end – x
0)
• y = y0 + u (y
end – y
0)
• 0 <= u <= 1– recall that our clipping window has the following vertices in the
world (xL, yB), (xR, yB), (xR, yT), (xL, yT) – and a point (x,y) is visible if
xL <= x <= xRyB <= y <= yT
– replace the parametric equations for x and y in those inequalitesxL <= x
0 + u (x
end – x
0) <= xR
yB <= y0 + u (y
end – y
0) <= yT
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Liang-Barsky Line ClippingxL <= x
0 + u (x
end – x
0) <= xR
yB <= y0 + u (y
end – y
0) <= yT
– which can be expressed as 4 inequalities of the form• u p
k <= q
k, where k = 1, 2, 3, or 4 and
– p1= (x
0 – x
end) p
2= (x
end – x
0) p
3= (y
0 – y
end) p
4= (y
end – y
0)
– q1= (x
0 – xL) q
2= (xR – x
0) q
3= (y
0 – yB)
q
4= (yT – y
0)
– a line parallel to a clipping edge has pk= 0, where k represents the
edge to which it is parallel: 1=Left, 2=Right, 3=Bottom, 4=Top– if (p
k== 0 and q
k< 0) then line completely outside boundary, done
– if (pk== 0 and q
k>= 0) then the line is inside the boundary
– if (pk< 0) then the line goes from outside to inside of the infinite
extension of a clipping window edge– if (p
k> 0) then the line goes from inside to outside of the infinite
extension of a clipping window edge
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Liang-Barsky Line Clipping– Recall what k's mean: 1=Left, 2=Right, 3=Bottom, 4=Top– when p
k is non-zero, we find the intersection of the line with the infinite
extension of the clipping window edge k• recall that u p
k <= q
k so this intersection is simply when u = q
k/p
k
– initialize u1 to be 0 and u
2 to be 1
– For each of the four infinite extensions of a clipping window edge we calculate either r
1 or r
2
• r1 is the u parameter for the intersection of the line with the edge when
coming from the outside of the clipping window to the inside (p < 0)• r
2 is the u parameter for the intersection of the line with the edge when
coming from the inside of the clipping window to the outside (p > 0)• if p<0, update u1 to be r1 if it results in a shorter line (i.e. if r1 > u1)• if p>0, update u2 to be r2 if it results in a shorter line (i.e. if r2 < u2)• if u1 > u2 we reject the line, and we're done.
– if it makes it through all four boundary checks without rejection, then the line to be drawn is between u1 and u2.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Liang-Barsky Line Clipping• Example of Liang-Barsky line clipping algorithm in action
– (x0,y
0) = (10,5)
– (xend
,yend
) = (30,30)– Clipping window: xL = 20, xR = 35, yB = 10, yT = 20.– p
1= (x
0 – x
end) = -20
– p2= (x
end – x
0) = 20
– p3= (y
0 – y
end) = -25
– p4= (y
end – y
0) = 25
– q1= (x
0 – xL) = -10
– q2= (xR – x
0) = 25
– q3= (y
0 – yB) = -5
– q4= (yT – y
0) = 15
– u1= 0 and u
2= 1 to start, let's run the algorithm on the board
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Liang-Barsky Line Clipping• Another example
– (x0,y
0) = (10,18)
– (xend
,yend
) = (25,25)– Clipping window: xL = 20, xR = 35, yB = 10, yT = 20.– p
1= (x
0 – x
end) = -15
– p2= (x
end – x
0) = 15
– p3= (y
0 – y
end) = -17
– p4= (y
end – y
0) = 17
– q1= (x
0 – xL) = -10
– q2= (xR – x
0) = 25
– q3= (y
0 – yB) = 8
– q4= (yT – y
0) = 2
– u1= 0 and u
2= 1 to start, let's run the algorithm on the board
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Line Clipping• Liang-Barsky (L-B) vs. Cohen-Sutherland (C-S)
– L-B in general more efficient• updates of u1 & u2 require only one divide• window edge intersections are computed only once when u1 & u2 are
final– C-S
• repeatedly calcs intersections along a line path even though line may be completely outside clipping window
• each intersection calc requires a divide and a multiply
• Both line clipping algorithms can be extended to 3-d
• A third line clipping algorithm, Nicholl-Lee-Nicholl (N-L-N), which we will
not go into is faster than both L-B & C-S but it cannot be extended to 3-d.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Polygon Clipping• Polygon clipping
– Polygons can be clipped against successive infinite extensions of the clipping window edges
– Keep track of new vertices (intersections with edge of clipping window) at each stage.
– Notice the number of vertices increased in the example below.
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Polygon Clipping
Michael Eckmann - Skidmore College - CS 376 - Spring 2007
Polygon Clipping• The text describes two polygon clipping algorithms.
• The Sutherland-Hodgman algorithm and the Weiler-Atherton algorithm.
• A major difference between the two is that the Sutherland-Hodgman algorithm
produces as output one polygon.
• Think about a concave polygon that when clipped, really should result in two
polygons. Example on board.