32
Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Embed Size (px)

Citation preview

Page 1: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Vertices and Fragments I

CS4395: Computer Graphics 1

Mohan SridharanBased on slides created by Edward Angel

Page 2: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Objectives

• Chapter 7.

• Implementation of rendering pipeline.– Clipping: viewing volume.– Rasterization: fragments from valid objects.– Hidden-surface removal: occlusions, blocked surfaces.

• This lecture:– Clipping.– Scan conversion.

CS4395: Computer Graphics 2

Page 3: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Overview

• At end of the geometric pipeline, vertices have been assembled into primitives.

• Must clip out primitives that are outside the view frustum:– Algorithms represent primitives by lists of vertices.

• Must compute pixels that can be affected by each primitive:– Fragment generation.– Rasterization or scan conversion.

CS4395: Computer Graphics 3

Page 4: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Required Tasks

• Clipping.• Rasterization or scan conversion.• Transformations.

• Some tasks deferred until fragment processing:– Hidden surface removal .– Anti-aliasing.

CS4395: Computer Graphics 4

Page 5: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Rasterization Meta Algorithms

• Consider two approaches to render a scene with opaque objects.

• For every pixel, determine object that projects on the pixel and is closest to the viewer. Compute the shade of this pixel.– Ray tracing paradigm.

• For every object, determine pixels it covers and shade them:– Pipeline approach.– Must keep track of depths.

CS4395: Computer Graphics 5

Page 6: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Clipping• 2D against clipping window.• 3D against clipping volume.• Easy for line segments and polygons.• Hard for curves and text:

– Convert to lines and polygons first

CS4395: Computer Graphics 6

Page 7: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Clipping 2D Line Segments

• Brute force approach: compute intersections with all sides of clipping window.– Inefficient: one division per intersection

CS4395: Computer Graphics 7

Page 8: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Cohen-Sutherland Algorithm

• Idea: eliminate as many cases as possible without computing intersections.

• Start with four lines that determine the sides of the clipping window.

CS4395: Computer Graphics 8

x = xmaxx = xmin

y = ymax

y = ymin

Page 9: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

The Cases

• Case 1: endpoints of line segment inside all four lines.– Draw (accept) the line segment.

• Case 2: endpoints outside all lines and on same side of a line.– Discard (reject) the line segment.

CS4395: Computer Graphics 9

x = xmaxx = xmin

y = ymax

y = ymin

Page 10: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

The Cases

• Case 3: one endpoint inside, one outside.– Must do at least one intersection.

• Case 4: both outside.– May have part inside.– Must do at least one intersection.

CS4395: Computer Graphics 10

x = xmaxx = xmin

y = ymax

Page 11: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Defining Outcodes• For each endpoint, define an outcode:

• Outcodes divide space into 9 regions.• Computation of outcode requires at most 8 subtractions per

line segment.

CS4395: Computer Graphics 11

b0b1b2b3

b0 = 1 if y > ymax, 0 otherwiseb1 = 1 if y < ymin, 0 otherwiseb2 = 1 if x > xmax, 0 otherwiseb3 = 1 if x < xmin, 0 otherwise

Page 12: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Using Outcodes

• Consider the 5 cases below.

• AB: outcode(A) = outcode(B) = 0000.– Accept line segment.

CS4395: Computer Graphics 12

Page 13: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Using Outcodes

• CD: outcode (C) = 0000, outcode(D) 0 (0010).– Compute intersection.– Location of 1 in outcode(D) determines which edge to intersect with.– Note: if there were a segment from A to a point in a region with 2

ones in outcode, we might have to do two intersections.

CS4395: Computer Graphics 13

Page 14: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Using Outcodes

• EF: outcode(E) logically ANDed with outcode(F) (bitwise) 0.– Both outcodes have a 1 bit in the same place (0010, 0010).– Line segment is outside of corresponding side of clipping window.– Reject.

CS4395: Computer Graphics 14

Page 15: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Using Outcodes

• GH, IJ have similar outcodes: (1001, 1000) and (0001, 1000).– Reject GH but what about IJ (logical AND returns zero) ?

• Shorten line segment by intersecting with sides of window.• Compute outcode of intersection (new endpoint of shortened

line segment).• Re-execute algorithm.

CS4395: Computer Graphics 15

Page 16: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Efficiency

• In many applications, the clipping window is small relative to the size of the entire data base:– Most line segments are outside one or more side of the window and

can be eliminated based on their outcodes.

• Inefficiency when code has to be re-executed for line segments that must be shortened in more than one step.

CS4395: Computer Graphics 16

Page 17: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Cohen Sutherland in 3D

• Use 6-bit outcodes.• When needed, clip line segment against planes.

CS4395: Computer Graphics 17

Page 18: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Liang-Barsky Clipping

• Consider the parametric form of a line segment:

• As parameter changes from 0 to 1, move from p1 to p2.

• We can distinguish between the cases by looking at the values of where the extended line segment crosses the lines that determine the window.

CS4395: Computer Graphics 18

p1

p2

21

21

21

)1()(

)1()(

10)1()(

yyy

xxx

ppp

Page 19: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Liang-Barsky Clipping

• In (a): 4 > 3 > 2 > 1

– Intersect right, top, left, bottom: shorten.• In (b): 4 > 2 > 3 > 1

– Intersect right, left, top, bottom: reject.

CS4395: Computer Graphics 19

Page 20: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Advantages

• Can accept or reject as easily as with Cohen-Sutherland.

• Using values of , we do not have to use algorithm recursively as with C-S.

• Extends to 3D.

CS4395: Computer Graphics 20

Page 21: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Clipping and Normalization

• General clipping in 3D requires intersection of line segments against arbitrary plane.

• Example: oblique view.

CS4395: Computer Graphics 21

Page 22: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Plane-Line Intersections

CS4395: Computer Graphics 22

)(

)(

12

1

ppn

ppna o

Page 23: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Normalized Form

CS4395: Computer Graphics 23

before normalization after normalization

Normalization is part of viewing (pre-clipping), but after normalization clip against sides of right parallelepiped.

Typical intersection calculation now requires only a floating point subtraction, e.g. is x > xmax ?

top view

Page 24: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Polygon Clipping

• Not as simple as line segment clipping:– Clipping a line segment yields at most one line segment.– Clipping a polygon can yield multiple polygons.

• However, clipping a convex polygon can yield at most one other polygon.

CS4395: Computer Graphics 24

Page 25: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Tessellation and Convexity

• Replace non-convex (concave) polygons with a set of triangular polygons (a tessellation).

• Also makes fill easier.• Tessellation code in GLU library.

CS4395: Computer Graphics 25

Page 26: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Line Clipping as a Black Box

• Consider line segment clipping as a process that takes in two vertices and produces either no vertices or the vertices of a clipped line segment.

CS4395: Computer Graphics 26

Page 27: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Pipeline Clipping of Line Segments

• Clipping against each side of window is independent of other sides.

• Can use four independent clippers in a pipeline.

CS4395: Computer Graphics 27

Page 28: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Pipeline Clipping of Polygons

• Three dimensions: add front and back clippers!• Strategy used in SGI Geometry Engine.• Small increase in latency.

CS4395: Computer Graphics 28

Page 29: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Bounding Boxes

• Rather than doing clipping on a complex polygon, we can use an axis-aligned bounding box or extent.– Smallest rectangle aligned with axes that encloses the polygon.– Simple to compute: max and min of x and y.

CS4395: Computer Graphics 29

Page 30: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Bounding Boxes

• Can usually determine accept or reject decisions based only on bounding box.

CS4395: Computer Graphics 30

reject

accept

requires detailed clipping

Page 31: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Clipping and Visibility

• Clipping has much in common with hidden-surface removal.

• In both cases, we are trying to remove objects that are not visible to the camera.

• Often we can use visibility or occlusion testing early in the process to eliminate as many polygons as possible before going through the entire pipeline.

CS4395: Computer Graphics 31

Page 32: Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel

Summary

• More information: Sections 7.4-7.7.

• What next:– Rasterization.– Hidden surface removal.

CS4395: Computer Graphics 32