View
219
Download
0
Embed Size (px)
Citation preview
2/135
OpenGL Geometric Primitives
All geometric primitives are specified by vertices
GL_QUAD_STRIPGL_QUAD_STRIP
GL_POLYGONGL_POLYGON
GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP
GL_TRIANGLE_FANGL_TRIANGLE_FAN
GL_POINTSGL_POINTSGL_LINESGL_LINES
GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP
GL_TRIANGLESGL_TRIANGLES
GL_QUADSGL_QUADS
3/135
Review: Drawing General Polygons
How to draw every interior pixels?
?
Scanline conversion of polygons!
6/135
Why Clip?
We do not want to waste time drawing objects that are outside of viewing window (or clipping window)
7/135
Clipping Points
Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn
(xmin,ymin)
(xmax,ymax)
(x,y)
8/135
Clipping Points
Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn
(xmin,ymin)
(xmax,ymax)
(x,y)
xmin=<x<=xmax?ymin=<y<=ymax?
9/135
Clipping Points
Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn
(x1,y1)
(x2,y2)
(xmin,ymin)
(xmax,ymax)
xmin=<x<=xmax?ymin=<y<=ymax?
10/135
Clipping Points
Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn
(x1,y1)
(x2,y2)
(xmin,ymin)
(xmax,ymax)
xmin=<x1<=xmax Yes
ymin=<y1<=ymax Yes
11/135
Clipping Points
Given a point (x, y) and clipping window (xmin, ymin), (xmax, ymax), determine if the point should be drawn
(x1,y1)
(x2,y2)
(xmin,ymin)
(xmax,ymax)
xmin=<x2<=xmax Noymin=<y2<=ymax No
14/135
Clipping Lines
Given a line with end-points (x0, y0), (x1, y1)
and clipping window (xmin, ymin), (xmax, ymax),
determine if line should be drawn and clipped end-points of line to draw.
(x0, y0)
(x1, y1)
(xmin,ymin)
(xmax,ymax)
18/135
Clipping Lines – Simple Algorithm
If both end-points inside rectangle, draw line If one end-point outside,
intersect line with all edges of rectangle
clip that point and repeat test
22/135
Intersecting Two Lines
),( 00 yx
),( 22 yx
),( 11 yxtxxxtx )()( 010 ),( 33 yx
tyyyty )()( 010
10 t
23/135
Intersecting Two Lines
),( 00 yx
),( 22 yx
),( 11 yxtxxxtx )()( 010 ),( 33 yx
tyyyty )()( 010
0)0( xx 0)0( yy
1)1( xx 1)1( yy
24/135
Intersecting Two Lines
),( 00 yx
),( 22 yx
),( 11 yx
sxxxtxxx )()( 232010
),( 33 yx
syyytyyy )()( 232010
25/135
Intersecting Two Lines
),( 00 yx
),( 22 yx
),( 11 yx
02
02
3201
3201
yy
xx
s
t
yyyy
xxxx
),( 33 yx
26/135
Intersecting Two Lines
),( 00 yx
),( 22 yx
),( 11 yx
02
02
3201
3201
yy
xx
s
t
yyyy
xxxx
),( 33 yx
Substitute t or s back into equation to find intersection
42/135
Clipping Lines – Simple Algorithm
Lots of intersection tests makes algorithm expensive
Complicated tests to determine if intersecting rectangle
Is there a better way?
43/135
Trivial Accepts
Big Optimization: trivial accepts/rejects How can we quickly decide whether line
segment is entirely inside window Answer: test both endpoints
44/135
Trivial Accepts
Big Optimization: trivial accepts/rejects How can we quickly decide whether line
segment is entirely inside window Answer: test both endpoints
47/135
Trivial Rejects
How can we know a line is outside of the window
Answer: both endpoints on wrong side of same edge, can trivially reject the line
48/135
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting
into two segments
010 cc
010 cc
49/135
Cohen-Sutherland Algorithm
Every end point is assigned to a four-digit binary value, i.e. Region code
Each bit position indicates whether the point is inside or outside of a specific window edges
bit 4 bit 3 bit 2 bit 1
leftrightbottomtop
50/135
Cohen-Sutherland Algorithm
bit 4 bit 3 bit 2 bit 1
leftrightbottomtop
top
rightleft
bottom
Region code?
51/135
Cohen-Sutherland Algorithm
bit 4 bit 3 bit 2 bit 1
leftrightbottomtop
top
rightleft
bottom
0010
?
52/135
Cohen-Sutherland Algorithm
bit 4 bit 3 bit 2 bit 1
leftrightbottomtop
top
rightleft
bottom
0010
0100
55/135
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting
into two segments
010 cc
010 cc
56/135
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting
into two segments
010 cc
010 cc
57/135
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting
into two segments
010 cc
010 cc
00000001
0101
1001 1000 1010
0010
01100100
Line is outside the window! reject
58/135
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting
into two segments
010 cc
010 cc
00000001
0101
1001 1000 1010
0010
01100100
Line is outside the window! reject
59/135
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting
into two segments
010 cc
010 cc
00000001
0101
1001 1000 1010
0010
01100100
Line is inside the window! draw
60/135
Cohen-Sutherland Algorithm
Classify p0, p1 using region codes c0, c1
If , trivially reject If , trivially accept Otherwise reduce to trivial cases by splitting
into two segments
010 cc
010 cc
00000001
0101
1001 1000 1010
0010
01100100
61/135
Window Intersection
(x1, y1), (x2, y2) intersect with vertical edge at xright
yintersect = y1 + m(xright – x1)
• where m=(y2-y1)/(x2-x1)
(x1, y1), (x2, y2) intersect with horizontal edge at ybottom
xintersect = x1 + (ybottom – y1)/m
• where m=(y2-y1)/(x2-x1)
77/135
Cohen Sutherland
Java applet: click here
79/135
Cohen-Sutherland Algorithm
Use region codes to quickly eliminate/include lines
Best algorithm when trivial accepts/rejects are common
Must compute viewing window clipping of remaining lines
Non-trivial clipping cost More efficient algorithms exist
80/135
Liang-Barsky Algorithm
Parametric definition of a line: x = x1 + uΔx
y = y1 + uΔy
Δx = (x2-x1), Δy = (y2-y1), 0<=u<=1
Lines are oriented: classify lines as moving inside to out or outside to in
Goal: find range of u for which x and y both inside the viewing window
83/135
Liang-Barsky Algorithm
For lines starting outside of boundary, update its starting point (u1)
For lines starting inside of boundary, update end point (u2)
For lines paralleling the boundaries and outside window,
reject it.
84/135
Liang-Barsky Algorithm
For lines starting outside of boundary, update its starting point (u1)
For lines starting inside of boundary, update end point (u2)
For lines paralleling the boundaries and outside window,
reject it.
85/135
Liang-Barsky Algorithm
Mathematically: xmin <= x1 + uΔx <= xmax
ymin <= y1 + uΔy <= ymax
Rearranged 1: u*(-Δx) <= (x1 – xmin) 2: u*(Δx) <= (xmax – x1) 3: u*(-Δy) <= (y1 – ymin) 4: u*(Δy) <= (ymax – y1) gen: u*(pk) <= (qk), k=1,2,3,4
86/135
Liang-Barsky Algorithm
Rules:
1) pk = 0: the line is parallel to boundaries If for that same k, qk < 0, it’s outside Otherwise it’s inside
2) pk < 0: the line starts outside this boundary rk = qk/pk
u1 = max(0, rk, u1) /**update starting point**/ pk > 0: the line starts inside the boundary
rk = qk/pk
u2 = min(1, rk, u2) /** update end point**/ If u1 > u2, the line is completely outside
88/135
Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p1 = -Δx =-2.5<0
q1 = (x1-xmin)=-.5
r1 = q1/p1=0.2
89/135
Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p1 = -Δx =-2.5<0
q1 = (x1-xmin)=-.5
r1 = q1/p1=0.2line starts outside this boundary
90/135
Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p1 = -Δx =-2.5<0
q1 = (x1-xmin)=-.5
r1 = q1/p1=0.2
91/135
Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p1 = -Δx =-2.5<0
q1 = (x1-xmin)=-.5
r1 = q1/p1=0.2
92/135
Liang-Barsky Algorithm
Current clipped line (u1=0,u2=1)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p1 = -Δx =-2.5<0
q1 = (x1-xmin)=-.5
r1 = q1/p1=0.2u1 = max(0, r1, u1)
93/135
Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=1)
(-.5,-.5)
(2,1)
u1<u2?
- yes: continue
- no: the line is completely outside window
95/135
Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=1)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p2 = Δx =2.5>0
q2 = (xmax-x1)=1.5
r2 = q2/p2=0.6
96/135
Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=1)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p2 = Δx =2.5>0
q2 = (xmax-x1)=1.5
r2 = q2/p2=0.6u2 = min(1, r2, u2)
97/135
Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=0.6)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p2 = Δx =2.5>0
q2 = (xmax-x1)=1.5
r2 = q2/p2=0.6
98/135
Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=0.6)
(-.5,-.5)
(2,1)
u1<u2?
- yes: continue
- no: the line is completely outside window
99/135
Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=0.6)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p3 = -Δy =-1.5<0
q3 = (y1-ymin)=0.5
r3 = q3/p3=0.333
100/135
Liang-Barsky Algorithm
Current clipped line (u1=0.2,u2=0.6)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p3 = -Δy =-1.5<0
q3 = (y1-ymin)=0.5
r3 = q3/p3=0.333u1 = max(0, r3, u1)
101/135
Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p3 = -Δy =-1.5<0
q3 = (y1-ymin)=0.5
r3 = q3/p3=0.333
103/135
Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p3 = -Δy =-1.5<0
q3 = (y1-ymin)=0.5
r3 = q3/p3=0.333
104/135
Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p4 = Δy =1.5>0
q4 = (ymax-y1)=1.5
r4 = q4/p4=1
105/135
Liang-Barsky Algorithm
Current clipped line (u1=0.333,u2=0.6)
(-.5,-.5)
(2,1)
Δx = (x2-x1)=2.5
Δy = (y2-y1)=1.5
p4 = Δy =1.5>0
q4 = (ymax-y1)=1.5
r4 = q4/p4=1u2 = min(1, r4, u2)
106/135
Liang-Barsky Algorithm
Check the left edge (u1=0.333,u2=0.6)
(-.5,-.5)
(2,1)
u1<u2
xnew1=x1+Δx*u1
ynew1=y1+Δy*u1
xnew2=x2+Δx*u2
ynew2=y2+Δy*u2
107/135
Liang-Barsky Algorithm
Faster than Cohen-Sutherland
Extension to 3D is easy
- Parametric representation for 3D lines
- Compute u1,u2 based on the intersection between line and plane
108/135
Comparison
Cohen-Sutherland Repeated clipping is expensive Best used when trivial acceptance and rejection is
possible for most lines Liang-Barsky
Computation of t-intersections is cheap (only one division)
Computation of (x,y) clip points is only done once
Algorithm doesn’t consider trivial accepts/rejects Best when many lines must be clipped
111/135
Curve Clipping
Approximate a curve using a set of straight-line segments
Apply line clipping for curve clipping