Sunteți pe pagina 1din 38

Lines of various thickness

Colored geometric shapes


Facet of 3D objects
Three main properties are
Mask
Shading Rule
Priority

Mask

Defines which pixel lies within solid area


Matrix of binary values
1 (within) 0 (outside)
Computing areas mask from the shapes is known
as solid area scan conversion

Shading Rule
Compute the intensity of each pixel inside
Color variation

Priority
If more than one solid area overlaps, the priority
defines which area should be on top

How the solid areas stored ?


Matrix of binary values (Eg. Mask Raster)
Line represented by its end points
Rectangle aligned with coordinate axes.

Polygons

Boundaries / Outlines
Ordered list of vertices
(1,7)(9,7)(9,2)(5,5)(1,1)
Start from any vertices
Adjacent vertices represent edges
Last vertex ?
Might be represented by list of edges

Finding all pixels inside the polygon


From the outline, draw an imaginary line
If odd number of intersections
the pixel is inside,

If even the pixel is outside

This algorithm is simple, but slow

How to speed up ?

Coherence

If a given pixel is inside,


Immediate adjacent pixels are likely to be inside
Same concept for outside pixels
Pixels must be tested together/entire scan line

For y=6.5, x=1,9 => (1,6) to (9,6)


For more than one intersections
For y=3.5, x=1,3.5,7,9
=> (1,3.5) & (7,9)

x<1

1x 9

x>9

YX Algorithm
Build a list of all (x,y) intersections
Sort the list & Group together, as x value increases
For (x1,y1) and (x2,y2)
(x1,y1) should precede (x2,y2)

if (x1<x2) and (y1<y2 or y1=y2)

For every pairs in the list

If (xa,ya) and (xb,yb) are inside and on the same scan


line (ya=yb) and (xa xb),
Call SetPixel(FrameBuffer, x, ya-, intensity)
For xa x + xb

YX Algorithm
The order of x, y can be changed
Pixels are grouped based on
Topological properties ( in out )
Geometrical properties ( adj on the same scan line )

Sorting order may be changed


Another approach for sorting

Polygon with Holes ???

When a vertex of the polygon lies exactly on a


scan line center ???

Solution
Adjust the vertex move up / down
Use only integer values for vertex representation

Singularities
Intersection must be recorded differently
Based on direction of successive edges
Up / down = only one intersection
Up down (or) down up = intersection must be
repeated.
Singularity Algorithm

Limitations of our scan-conversion algorithms

The edges wont be smooth (aliasing) !!!

Solution

Sampling
More precise calculations are needed at edges
To know what fraction of the pixel is covered
Based on the fraction, the pixels can be toned.
Where we can avoid/no need sampling ???
Four methods

Steep edge
Shallow angle Edge
Increased Resolution
Polygon Clipping

Sampling
Steep edge, |dx| < 1
If the edge is at the left of the polygon
= Floor(x) + 1 x
Here = ?

Right edge

= x - Floor(x)

Sampling
Shallow angle edge
If the edge passes at a shallow angle, |dx| > 1
Several pixels require fractional intensities
Fractional area

0 at xleft = x - |dx|/2
1 at xright = x + |dx|/2
= ((2i + 1) (2x dx)) / 2|dx|
If i = 3, = ?

Sampling Method 3
Consider a polygon with narrow edges

Increase the resolution

Sampling Method 4

Again for the narrow edges


Clip the polygon against pixel edges
Increase the resolution
= area of the polygon

Fraction Algorithm
1. Assume an array fraction(x) to hold fractional coverage for each
pixel x.
2. Set fraction (x) = 0 for all the pixels on the scan line
3. Set s = 1
4. Remove an interval [xl,xr] from intersection list
If Floor (xl) = Floor (xr), use method 1, x= floor(xl)
Fraction (x) = fraction (x) + s
Else
Fraction (x) = fraction (x) + s , Floor(xl) x Floor(xr)
For all x > floor(xr), update the fraction
Set s = -1

5. Goto Step 4
6. Stop when no intervals remain

Scan Converting Polygons

The order in which the polygons has to be


displayed
Painters Approach
Each polygon assigned with Priority
Number
Drawing starts with Low -> high priority
3

Scan Converting Polygons

Scan Converting Polygons

Painters Algorithm P-(YX)


Two sorting steps
polygon, scan conversion

Disadvantages
The object with highest priority comes last
Appear one-by-one in random order
Scan conversion must be row-by-row, not
polygon-by-polygon

Scan Converting Polygons

Painters Algorithm P-(YX)


For each polygon,
Compute all intersections of edges, (x,y,p)
Sort the list by y, p and x.
(x1,y1,p1) precedes (x2,y2,p2) if
y1 y2 and p1 p2 and x1 < x2

Remove pairs of triples (as in YX algorithm)


Whenever a new y value is encountered
Clear the current list, and start computing the new
intersections

Scan Converting Polygons

Difficulties
Large list of triples
Causes the sorting slow down

The performance of scan-conversion


algorithms is mostly determined by sorting
steps.
Need to use an efficient sorting algorithms
Though we are using y-bucket sort, the
storage remain same.
Solution : Y-X Algorithm

Scan Converting Polygons

Bucket sort
Elements are distributed among bins

Then, elements are sorted within each bin

Scan Converting Polygons

Generates intersections only when needed


One scan line at a time
For each scan line
Compute all the intersections
Sort them by x coordinate
Y sort followed by x sort (Y-then-X Alg.)

Scan Converting Polygons

bucket sorting for each line

Scan Converting Polygons

The principal data structure is

List of polygon edges, sorted by y maximum


Called as y-bucket list

Edge record contains information needed to generate


intersections
x the topmost location
dx amount of changes in x @ every scan line
dy number of intersections

Active edge list, sorted by x


After a scan line is completed

update the edge record; x=x+dx, dy=dy-1


Also update the active edge list

Scan Converting Polygons

How to adopt Y-X algorithm for polygon


scan conversion with priority decisions.
Y-(PX)
Sort the active edge-list based on priority and then
x value

P-Y-X
Polygons are sorted by priority first, then Y-X

Scan Converting Polygons

Scan Converting Polygons

Scan conversion of single polygons


(YX)
Y-X

simple
efficient, adv. Edge coherence

Scan conversion and priority resolution

P-(YX) painters alg. - simple


(YPX)
flow down, inefficient sorting
P-Y-X painters alg. + faster sorting
Y-(PX) flow down, efficient.
Choice : sorting, storage and graphics system,
must be able to make small changes rapidly.

Scan Converting Polygons

36

Scan Converting Polygons

37

Scan Converting Polygons

Scan Converting Polygons

Solve the following problems.

16.9
16.12
16.13
16.14
16.21
Principles of Interactive Computer Graphics
William M. Newman, Robert F. Sproull
McGraw-Hill, 1979

38

S-ar putea să vă placă și