Top Banner
26

computer graphics algorithms

Nov 18, 2014

Download

Documents

Sahil Sholla

it contains cohen-sutherland , warnock, painter sutherland hodgeman algorithms. good illustrations
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: computer graphics algorithms
Page 2: computer graphics algorithms

10/29/02 (c) 2002 University of Wisconsin, CS559

Depth Sorting or painters algo (Object Precision, in view space)

• An example of a list-priority algorithm

• Sort polygons on depth of some point

• Render from back to front (modifying order on the fly)

• Rendering: For surface S with greatest depth– If no overlap in depth with other polygons, scan convert

– Else, for overlaps in depth, test for overlaps in the image plane• If none, scan convert and go to next polygon

– If S, S’ overlap in depth and in image plane, swap order and try again

– If S, S’ have been swapped already, split and reinsert

Page 3: computer graphics algorithms

10/29/02 (c) 2002 University of Wisconsin, CS559

Depth Sorting (2)

• Testing for overlaps: Start drawing when first condition is met:– x-extents or y-extents do not overlap

– S is behind the plane of S’

– S’ is in front of the plane of S

– S and S’ do not intersect in the image plane

SS’

S

S’or

z

x

SS’

z

x

SS’

SS’

Page 4: computer graphics algorithms

10/29/02 (c) 2002 University of Wisconsin, CS559

Depth sorting(3)

• Advantages:– Filter anti-aliasing works fine

• Composite in back to front order with a sequence of over operations

– No depth quantization error• Depth comparisons carried out in high-precision view space

• Disadvantages:– Over-rendering– Potentially very large number of splits - (n2) fragments from n

polygons

Page 5: computer graphics algorithms

5

• An area-subdivision technique• Idea:

• Divide an area into four equal sub-areas• At each stage, the projection of each polygon will

do one of four things:1. Completely surround a particular area

2. Intersect the area

3. Be completely contained in the area

4. Be disjoint to the area

Warnock’s Algorithm

Page 6: computer graphics algorithms

6

Warnock’s Algorithm

• Disjoint polygons do not influence an area.

• Parts of an intersecting polygon that lie outside the area do not influence that area

• At each step, we determine the areas we can color and color them, then subdivide the areas that are ambiguous.

Page 7: computer graphics algorithms

7

Warnock’s Algorithm

• At each stage of the algorithm, examine the areas:

1. If no polygons lie within an area, the area is filled with the background color

2. If only one polygon is in part of the area, the area is first filled with the background color and then the polygon is scan converted within the area.

3. If one polygon surrounds the area and it is in front of any other polygons, the entire area is filled with the color of the surrounding polygon.

4. Otherwise, subdivide the area and repeat the above 4 tests.

Page 8: computer graphics algorithms

8

Warnock’s Algorithm

Initial scene

Page 9: computer graphics algorithms

9

Warnock’s Algorithm

First subdivision

Page 10: computer graphics algorithms

10

Warnock’s Algorithm

Second subdivision

Page 11: computer graphics algorithms

11

Warnock’s Algorithm

Third subdivision

Page 12: computer graphics algorithms

12

Warnock’s Algorithm

Fourth subdivision

Page 13: computer graphics algorithms

13

Warnock’s Algorithm

• Subdivision continues until:• All areas meet one of the four criteria

• An area is pixel size• in this case, the polygon with the closest point at that pixel

determines the pixel color

Page 14: computer graphics algorithms

• Region and outcodes

The Cohen-Sutherland Line-Clipping Algorithm

First bit: above top edge y > ymax

Second bit: below bottom edge y < ymin

Third bit: to right of right edge x > xmax Fourth bit: to left of left edge x < xmin

Page 15: computer graphics algorithms

• Checking for trivial acceptance or rejection using outcodes

1). Each endpoint of a line segment is assigned an outcode;

2). If both 4-bit codes are zero, the line can be trivially accepted;

3). A logical and is performed on both outcodes;

4). If the result is nonzero, the line can be trivially rejected.

The C-S Line-Clipping Algorithm (cont.)

Page 16: computer graphics algorithms

Steps for Cohen-Sutherland Algorithm

1. End-points pairs are checked for trivial acceptance or rejection using outcode;

2. If not trivially accepted or rejected, divide the line segment into two at a clip edge;

3. Iteratively clipped by test trivial-acceptance or trivial-rejection, and divided into two segments until completely inside or trivial-rejection.

A

B

C

D

E

F

G

H

I1001

0001

0101 0100

0000

1000 1010

0010

0110

Page 17: computer graphics algorithms

Polygon Clipping

• Sutherland-Hodgeman algorithm (A divide-and-conquer strategy)

• Polygons can be clipped against each edge of the window one at a time. Edge intersections, if any, are easy to find since the X or Y coordinates are already known.

• Vertices which are kept after clipping against one window edge are saved for clipping against the remaining edges.

• Note that the number of vertices usually changes and often increases.

Page 18: computer graphics algorithms

Top Clip Boundary

Clipping A Polygon Step by Step

Right Clip Boundary

Bottom Clip Boundary

Left Clip Boundary

Page 19: computer graphics algorithms

Sutherland-Hodgeman Algorithm

Note the difference between this strategy and the Cohen-Sutherland algorithm for clipping a line: the polygon clipper clips against each window edge in succession, whereas the line clipper is a recursive algorithm.

Given a polygon with n vertices, v1, v2,…, vn, the algorithm clips the polygon against a single, infinite clip edge and outputs another series of vertices defining the clipped polygon. In the next pass, the partially clipped polygon is then clipped against the second clip edge, and so on. Let’s considering the polygon edge from vertex vi to vertex vi+1. Assume that start point vi has been dealt with in the previous iteration, four cases will appear.

Page 20: computer graphics algorithms

Sutherland-Hodgeman Algo (cont.) refer p.239

Inside Outside

Clip Boundary

Polygon been clipped

vi

vi+1: output

Case 1

Inside Outside

Polygon been clipped

vi

vi+1

Case 2

i: output

Inside Outside

Polygon been clipped

vi

vi+1

Case 3

(no output)

Inside Outside

vi

Case 4

vi+1: second output

i: first output

Page 21: computer graphics algorithms

An Example for the Polygon Clipping

v1

v5

v2 v3

v4

Page 22: computer graphics algorithms

Solution:

v1

v5

v2 v3

v4

As we said, the Sutherland-Hodgeman algorithm clip the polygon against one at a time. We start with the right edge of the clip rectangle. In order to clip the polygon against the line, each edge of the polygon have to be considered. Starting with the edge, represented by a pair of vertices, v5v1:

v1

v5

v1

Clipping edge Clipping edge Clipping edge

Page 23: computer graphics algorithms

Solution (cont.):

v1

v5

v2 v3

v4

Now v1v2:

Clipping edge Clipping edge Clipping edge

v1

v2

v1

v2

Page 24: computer graphics algorithms

Solution (cont.):

v1

v5

v2 v3

v4

Now v2v3:

Clipping edge Clipping edge Clipping edge

v1

v2v2 v3v2 v3

Page 25: computer graphics algorithms

Solution (cont.):

v1

v5

v2 v3

v4

Now v3v4:

Clipping edge Clipping edge Clipping edge

v1

v2v2 v3v3

v4

i1

Page 26: computer graphics algorithms

Solution (cont.):

v1

v5

v2 v3

v4

Now v4v5:

Clipping edge Clipping edge Clipping edge

v1

v2v2 v3

i1

v5

v4 i2

v5

After these, we have to clip the polygon against the other three edges of the window in a similar way.