Title: Polygon Scan Conversion
1Polygon Scan Conversion
2Rasterization
- Rasterization takes shapes like triangles and
determines which pixels to fill.
3Filling Polygons
- First approach
- 1. Polygon Scan-Conversion
- Rasterize a polygon scan line by scan line,
determining which pixels to fill on each line.
4Filling Polygons
- Second Approach
- Polygon Fill
- Select a pixel inside the polygon. Grow outward
until the whole polygon is filled.
5Why Polygons?
- You can approximate practically any surface if
you have enough polygons. - Graphics hardware is optimized for polygons (and
especially triangles)
6Coherence
Scan-line
Edge
Span
7Polygon Scan Conversion
Intersection Points
Other points in the span
8Polygon Scan Conversion
- Process each scan line
- Find the intersections of the scan line with all
polygon edges. - Sort the intersections by x coordinate.
- Fill in pixels between pairs of intersections
using an odd-parity rule. - Set parity even initially.
- Each intersection flips the
- parity.
- Draw when parity is odd.
9Polygon Scan-Conversion
- Process for scan converting polygons
- Process one polygon at a time.
- Store information about every polygon edge.
- Compute spans for each scan line.
- Draw the pixels between the spans.
- This can be optimized using an table that stores
information about each edge. - Perform scan conversion one scan line at a time.
10Computing Intersections
- For each scan line, we need to know if it
intersects the polygon edges. - It is expensive to compute a complete line-line
intersection computation for each scan line. - After computing the intersection between a scan
line and an edge, we can use that information in
the next scan line.
11Scan Line Intersection
Polygon Edges
Intersection points needed
Previous scan line yi
Current scan line yi1
Intersection points from previous scan line
12Scan Line Intersection
- Use edge coherence to incrementally update the x
intersections. - We know
- Each new scan line is 1 greater in y
- We need to compute x for a given scan line,
(x_at_ymax,ymax)
(x_at_ymin,ymin)
13Scan Line Intersection
- So,
- and
- then
- This is a more efficient way to compute xi1.
14Edge Tables
- We will use two different edge tables
- Active Edge Table (AET)
- Stores all edges that intersect the current scan
line. - Global Edge Table (GET)
- Stores all polygon edges.
- Used to update the AET.
15Active Edge Table
- Table contains one entry per edge intersected by
the current scan line. - At each new scan line
- Compute new intersections for all edges using the
formula. - Add any new edges intersected.
- Remove any edges no longer intersected.
- To efficiently update the AET, we will can use a
global edge table (GET)
16Global Edge Table Example
Place entries into the GET based on the ymin
values.
GET
8
(ymax, x_at_ymin, 1/m)
7
8, 6, -3/2
6
CD
Indexed by scan line
6, 3, 3
5
BC
8, 0, ¾
4
DE
3
AB (2, 1), (3, 5) BC (3, 5), (6, 6) CD (6,
6), (3, 8) DE (3, 8), (0, 4) EA (0, 4), (2, 1)
2
AB
EA
5, 2, ¼
4, 2, -2/3
1
0
17Active Edge Table Example
- The active edge table stores information about
the edges that intersect the current scan line. - Entries are
- The ymax value of the edge
- The x value where the edge intersects the current
scan line. - The x increment value (1/m)
- Entries are sorted by x values.
18Active Edge Table Example
- The ymax value for that edge
- The x value where the scan line intersects the
edge. - The x increment value (1/m)
(ymax, x, 1/m)
AET
Scan Line 3
4, 2/3, -2/3
5, 5/2, 1/4
AB
EA
AB (2, 1), (3, 5) BC (3, 5), (6, 6) CD (6,
6), (3, 8) DE (3, 8), (0, 4) EA (0, 4), (2, 1)
19Active Edge Table Example
- The ymax value for that edge
- The x value where the scan line intersects the
edge. - The x increment value (1/m)
(ymax, x, 1/m)
AET
Scan Line 4
4, 2/3, -2/3
5, 5/2, 1/4
8, 0, 3/4
5, 11/4, 1/4
AB
EA
DE
Scan Line 4
In the GET, edge DE is stored in bucket 4, so it
gets added to the AET.
ymax 4 for EA, so it gets removed from the AET.
New x value for AB is 5/2 1/4 11/4
AB (2, 1), (3, 5) BC (3, 5), (6, 6) CD (6,
6), (3, 8) DE (3, 8), (0, 4) EA (0, 4), (2, 1)
20Active Edge Table Example
- The ymax value for that edge
- The x value where the scan line intersects the
edge. - The x increment value (1/m)
Scan Line 5
(ymax, x, 1/m)
AET
8, 0, 3/4
5, 11/4, 1/4
8, 3/4, 3/4
6, 3, 3
AB
DE
BC
Increment x 0 3/4
Remove AB, since ymax 5.
Add BC since it is in the GET at ymin 5.
AB (2, 1), (3, 5) BC (3, 5), (6, 6) CD (6,
6), (3, 8) DE (3, 8), (0, 4) EA (0, 4), (2, 1)
21Active Edge Table Example
D
8
7
C
Scan Line 6
6
5
B
E
4
3
2
1
A
0
0
1
2
3
4
5
6
7
8
AB (2, 1), (3, 5) BC (3, 5), (6, 6) CD (6,
6), (3, 8) DE (3, 8), (0, 4) EA (0, 4), (2, 1)
22Clipping
23Line Clipping
- What happens when one or both endpoints of a
line segment are not inside the specified drawing
area?
Drawing Area
24Line Clipping
- Strategies for clipping
- Check (in inner loop) if each point is inside ?
Works, but slow - Find intersection of line with boundary ?
Correct
if (x xmin and x xmax and y ymin and y
ymax) drawPoint(x,y,c)
Clip line to intersection
25Line Clipping Possible Configurations
- Both endpoints are inside the region (line AB)
- No clipping necessary
- One endpoint in, one
- out (line CD)
- Clip at intersection point
- Both endpoints outside
- the region
- No intersection (lines EF, GH)
- Line intersects the region (line IJ)
- Clip line at both intersection points
J
D
I
A
F
C
H
B
E
G
26Line Clipping Cohen-Sutherland
- Basic algorithm
- Accept lines that have both endpoints inside the
region.
- Reject lines that have both endpoints less than
xmin or ymin or greater than xmax or ymax.
- Clip the remaining lines at a region boundary and
repeat the previous steps on the clipped line
segments.
27Cohen-Sutherland Accept/Reject Tests
- Assign a 4-bit code to each
- endpoint c0, c1 based on its
- position
- 1st bit (1000) if y gt ymax
- 2nd bit (0100) if y lt ymin
- 3rd bit (0010) if x gt xmax
- 4th bit (0001) if x lt xmin
- Test using bitwise functions
- if c0 c1 0000
- accept (draw)
- else if c0 c1 ? 0000
- reject (dont draw)
- else clip and retest
1000
1001
1010
0001
0010
0000
0100
0101
0110
28Cohen-Sutherland Accept/Reject
- Accept/reject/redo all based on bit-wise Boolean
ops.
1000
1001
1010
0001
0010
0000
0100
0101
0110
29Polygon Clipping
30Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v0
ymax
Inside region No
v9
v5
v1
v2
Add p0 to output list
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
p0
31Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v1
ymax
inside region yes
v9
v5
v1
v2
add v1 to output list
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
, v1
p0
32Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v2
ymax
inside region yes
v9
v5
v1
v2
add v2, p1 to output list
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
, v2, p1
p0 , v1
33Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v3
ymax
inside region no
v9
v5
v1
v2
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
p0, v1, v2, p1
34Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v4
ymax
inside region no
v9
v5
v1
v2
add p2 to output list
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
p0, v1, v2, p1
, p2
35Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v5
ymax
inside region yes
v9
v5
v1
v2
add v5, p3 to output list
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
p0, v1, v2, p1, p2
, v5, p3
36Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v6
ymax
inside region no
v9
v5
v1
v2
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
p0, v1, v2, p1, p2, v5, p3
37Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v7
ymax
inside region no
v9
v5
v1
v2
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
p0, v1, v2, p1, p2, v5, p3
38Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v8
ymax
inside region no
v9
v5
v1
v2
add p4 to output list
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
p0, v1, v2, p1, p2, v5, p3
, p4
39Polygon Clipping Example
v6
v7
Clip first to ymin and ymax
v0
v8
vertex v9
ymax
inside region yes
v9
v5
v1
v2
add v9, p5 to output list
ymin
v3
v4
Input vertex list (v0, v1, v2, v3, v4, v5, v6,
v7, v8, v9)
Output vertex list
p0, v1, v2, p1, p2, v5, p3, p4
, v9, p5
40Polygon Clipping Example
This gives us a new polygon
ymax
v9
v5
v1
v2
ymin
with vertices
(p0, v1, v2, p1, p2, v5, p3, p4, v9, p5)
41Polygon Clipping Example (cont.)
Now clip to xmin and xmax
xmin
xmax
p9
v9
p6
v5
p7
v1
v2
p8
Input vertex list (p0, v1, v2, p1, p2, v5,
p3, p4, v9, p5) Output vertex list (p0, p6, p7,
v2, p1, p8, p9, p3, p4, v9, p5)
42Polygon Clipping Example (cont.)
Now post-process
xmin
xmax
p3
p4
p5
p0
p9
v9
p6
p7
v2
p1
v3
p8
Output vertex list (p0, p6, p7, v2,
p1, p8, p9, p3, p4, v9, p5) Post-process (p0,
p6, p9, p3,) and (p7, v2, p1, p8) and (v4, v9, p5)
43Transformations
44General Transformations
- Want to be able to manipulate graphical objects
- Translation move an object
- Rotation change orientation
- Scale change size
- Other transformations reflection, shear, etc.
- A general 2-D transformation has the form
- x fx(x, y)
- y fy(x, y)
- where x and y are the original coordinates and
x and y are the transformed coordinates. - In vector form
45General Transformations (cont.)
- General transforms may be non-linear
- Lines do not necessarily map to lines
- Every point (along lines, inside shapes, etc.)
needs to be transformed - Non-linear Transformations are harder to deal
with. - Fortunately, many important transformations are
linear. - Non-Linear Example
46Linear Transformations
- We will use linear transformations
- x fx(x, y) axx bxy cx
- y fy(x, y) ayx byy cy
- Linear Transforms can be written using matrix
operations - Advantages
- Lines transform to lines
- Only need to transform vertices.
- Computationally efficient
- Problem would like to simplify further ? get rid
of T
47Translation
- Move object from one place to another
- Forward transform
- x x tx
- y y ty
- or p p T where
- Inverse transform p p T
48Scale
- Change an objects size
- Forward transform
- x sx x
- y sy y
- or p Sp where
- Inverse transform p S-1p
- where
Why do we get an apparent translation?
49Scale
- Properties of the scale
- The scale is performed relative to the origin.
- A scale factor greater than one enlarges the
objects and moves it away from the origin. - A scale factor less than one shrinks the object
and moves it towards the origin. - Usually this isnt what we want
- Generally we would like to scale about the
objects center - not the coordinate origin.
50Scale Transforms
- Scale can accomplish the following transforms
- Uniform scale sx sy
- Preserves angles, but not lengths
- Non-uniform scale sx ? sy
- Doesnt preserve angles or
- lengths
- Reflection about the
- x-axis sx 1, sy 1
- y-axis sx 1, sy 1
- line y x sx 1, sy 1
- Reflection preserves angles
- and lengths
- What is the inverse matrix?
- A reflection matrix is its own
- inverse.
y x
51Rotation
- Given a point p on the plane, how do we rotate
that point about the origin?
1. Convert point p to polar coordinates
52Rotation (cont)
- Now, lets rotate that point by q about the
origin
2. To rotate q degrees, simply add q to f
3. Apply the sum of angles formula
4. Substitute x and y back in
53Rotation (cont)
- So, given a point p that we wish to rotate about
the origin, we simply multiply p by the rotation
matrix
54Rotating an Object
- Apply the rotation to each vertex of the object
- Forward transform
- x x cos(q) y sin(q)
- y x sin(q) y cos(q)
- or p Rp where
- Inverse rotation p R-1p
-
q 45
q
55Rotations
- Right-hand rule Stick your thumb towards the
z-axis. Your fingers rotate in the positive
direction as they curl. - If q is positive, the rotation is
counterclockwise about the origin.
r
- If q is negative, the rotation is clockwise about
the origin.
y
p (x, y)
r
x
56Composition
- How can we do more complex transformations?
- Scale an object in an arbitrary direction (other
than in x and/or y) - Rotate an object about a point (other than the
origin) - Etc.
- Composition combine basic transforms to achieve
a more complex one - Example Scale by ½ at a 30 angle
- Rotate clockwise 30 (q p / 6) p RTp
- Scale in x by ½ p Sp
- Rotate counter-clockwise 30 (q p / 6) p
Rp - Final result p Rp RSp RSRTp
Recall that a negative rotation matrix is the
transpose of its corresponding positive rotation
matrix.
57Composition Scale at Arbitrary Angle
- Scale object at an arbitrary angle q
- Rotate clockwise by q p RTp
- Scale about origin p SRTp
- Rotate back (counter-clockwise
- by q) p RSRTp
q 30
5
4
q 30
3
2
1
0
0
1
2
3
4
58Composition and Efficiency
- Multiple transformations Multiply each point
(i.e., vertex) in an object by each basic
transformation - p RSRTp
- Requires multiple matrix multiplies per vertex
- Lots of extra computation if transforming
thousands (or millions) of points. - Composition matrix multiply is associative
- Multiply matrices first to create a single matrix
- Multiply each point by a single matrix
- p RSRTp M p
- Much more efficient
59Rotation about an Arbitrary Point
- Our rotations (so far) have been about the origin
- This rotation moves the entire object as it
rotates - Usually when we want to rotate an object, we want
to rotate it about its center (or some other
fixed point) - How can we do this?
- Answer Apply a sequence of transformations
60Composition Rotation About a Point
- Rotate object about an arbitrary point T
- Translate T to origin p p T
- Rotate about origin p R(p T)
- Translate back p R(p T) T
q 45
61Rotation about an Arbitrary Point
- Standard format for rotating about an arbitrary
point - Translate point of rotation to the origin
- Rotate about the origin
- Translate the point back to its original position
- Compute the composite transformation that will
accomplish this, then apply that transformation
to all vertices in the object.
62Homogeneous Coordinates
- Problem
- Rotation, scale, and shear multiply a matrix with
p p Mp - Translation adds a vector to p p p T
- Would like to treat all transformations the same
- Optimize the hardware
- Compose transformations
- Solution homogeneous coordinates
- Increase points dimensionality ? add a third
coordinate w - Two homogeneous points (p1 and p2) specify
- the same 2-D Cartesian point if
- p1 cp2 for some real-valued scalar number c
63Homogeneous Coordinates (cont.)
- With homogeneous coordinates, the x-y plane is a
two-dimensional sub-space in 3-D - Although homogeneous points have three
coordinates, they correlate to positions on a 2-D
plane - Can use any 2-D plane that doesnt
- include the origin
- For simplicity, choose the plane
- w 1 ? x y 1T X Y 1T
2-D Cartesian coordinates
Homogeneous point
y
x
64Homogeneous Coordinates
- For 2D transformations, the points are now
3-vectors - And the transformation matrices are 3x3 matrices
65Homogeneous Coordinates Translation
- What does the translation matrix look like?
- To translate a point p to point p, we need
- with
- and
66Homogeneous Coordinates Scale
- What does the scale matrix look like?
- To scale a point p to point p, we need
- with
- and
67Homogeneous Coordinates Rotation
- Rotation has a similar derivation. Remember the
2D rotation matrix - When we move to homogeneous coordinates, we have
- We apply this matrix to each vertex of a polygon
to rotate the polygon as a whole
68Composition Rotation About a Point
- Now lets try that rotation about an arbitrary
point T - Translate to origin p T1p
- Rotate about origin p RT1p
- Translate back p T2RT1p
q 45
69Composition Rotation About a Point
- So
- Where M is the composite transformation matrix
703D Transformations
General Case
Translation
Scale
713D Rotation
Rotate about x-axis
Rotate about y-axis
Rotate about z-axis
These rotations can be combined to rotate about
an arbitrary axis.
72Viewing
73Graphics Pipeline
Model Space
Model Transformations
World Space
Viewing Transformation
Eye/Camera Space
Projection Window Transformation
Screen Space
74Viewing Transformations
- Projection take a point from m dimensions to n
dimensions where n lt m - There are essentially two types of viewing
transforms - Orthographic parallel projection
- Points project directly onto the view plane
- In eye/camera space (after viewing
- transformation) drop z
- Perspective convergent projection
- Points project through the origin
- onto the view plane
- In eye/camera space (after viewing
- transformation) divide by z
75Projection Environment
- We will use a right-handed view system
y
- The eye or camera position is on the z axis, a
distance d from the origin.
x
- The view direction is parallel to the z axis
z
- The view plane is in the xy plane and passes
through the origin
76Parallel Projection
(x, y, z)
(x, y, z)
x
z
77Parallel Projection
- Thus, for parallel, orthographic projections,
- x x, y y, z 0
- So, to perform a parallel projection on an
object, we can use matrix multiplication
What is M?
i.e., we simply drop the z coordinate
78Perspective Projection
- Points project through the focal point (e.g.,
eyepoint) onto the view plane - Projection lines converge
y
Virtual Image Plane
x
z
79Perspective Projection Computation
Looking down the y axis
By similar triangles
x
p (x, y, z)
x
x
view plane
p (x, y, z)
d-z
d
eye
z
80Perspective Projection Computation
Looking down the x axis
By similar triangles
y
p (x, y, z)
p (x, y, z)
y
y
z
view plane
eye
d
d-z
81Perspective Projection Computation
- So, we have
- what is z?
- can we put this into matrix form?
82Perspective Projection Computation
- So, the matrix that will give us the correct
perspective is
This works - what is the problem with it?
Answer The entries in the matrix are point
dependent! i.e., every point will have to have a
different matrix
83Perspective Projection Computation
- Solution use homogeneous points (remember them?)
- Our Cartesian point is
- A homogeneous point that is equivalent to our
desired Cartesian point is - can we come up with a matrix that gives us what
we need, but is point independent?
84Perspective Projection Computation
so,
85Perspective Projection Computation
- So, the new matrix we get is
This gives us correct results, and is point
independent