26
Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides by Edward Angel and Dave Shreiner

Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Embed Size (px)

Citation preview

Page 1: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Chapter 6: Vertices to FragmentsPart 2

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

20121

Mohan SridharanBased on Slides by Edward Angel and Dave Shreiner

Page 2: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Objectives

• Introduce clipping algorithms for polygons.

• Discuss hidden-surface removal algorithms.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

20122

Page 3: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

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.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

20123

Page 4: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Tessellation and Convexity

• One strategy is to replace non-convex (concave) polygons with a set of triangular polygons (a tessellation).

• Makes fill easier.

• Tessellation code in GLU library.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

20124

Page 5: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Clipping as a Black Box

• Can 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.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

20125

Page 6: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Pipeline Clipping of Line Segments

• Clipping against any side of viewing window is independent of all other sides:– Can use four independent clippers in a pipeline.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

20126

Page 7: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Pipeline Clipping of Polygons

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

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

20127

Page 8: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

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.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

20128

Page 9: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Bounding boxes

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

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

20129

reject

accept

requires detailed clipping

Page 10: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

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.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201210

Page 11: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Hidden Surface Removal

• Object-space approach: pair-wise testing of objects to remove hidden surfaces.– High computational complexity.– Suitable for scenes with relatively few polygons.

• Image-space approach: follows the viewing and ray-casting approach.– Even worst case complexity much better than object-space approach.– Suitable for complex scenes.– Accuracy limited by resolution of frame buffer.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201211

Page 12: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Hidden Surface Removal

• Object-space approach: use pair-wise testing between polygons (objects).

• Worst case complexity O(n2) for n polygons.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201212

partially obscuring can draw independently

Page 13: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Painter’s Algorithm

• Render polygons from back to front so that polygons behind others are simply painted over.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201213

B behind A as seen by viewer Fill B then A

Page 14: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Depth Sort

• Requires ordering of polygons first:– O(n log n) calculation for ordering.– Not every polygon is either in front or behind all other polygons.

• Order polygons and deal with easy cases first, harder later.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201214

Polygons sorted by distance from COP

Page 15: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Easy Cases

• A lies behind all other polygons:– Can render.

• Polygons overlap in z but not in either x or y:– Can render independently.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201215

Page 16: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Hard Cases

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201216

Overlap in all directions but one object is fully on one side of the other

cyclic overlap

penetration

Page 17: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Back-Face Removal (Culling)

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201217

Face is visible iff 90 -90 equivalently cos 0 or v • n 0

Plane of face has form ax + by +cz +d =0but after normalization n = ( 0 0 1 0)T

Need only test the sign of c.

In OpenGL we can simply enable culling but may not work correctly if we have non-convex objects .

Page 18: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Image Space Approach

• Look at each projector (nm for an n x m frame buffer) and find closest of k polygons.

• Complexity: O(nmk).

• Ray tracing.

• z-buffer.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201218

Page 19: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

z-Buffer Algorithm

• Use a buffer called the depth or z-buffer to store the depth of the closest object at each pixel found so far.

• As we render each polygon, compare depth of each pixel to z-buffer depth.

• If less, place shade of pixel in color buffer and update z-buffer.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201219

Page 20: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Scan-Line Algorithm

• Can combine shading and hidden surface removal through scan line algorithm.

• Generate pixels as they are displayed.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201220

scan line i: no need for depth information, can only be in noor one polygon.

scan line j: need depth information only when inmore than one polygon.

Page 21: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Efficiency

• If we work scan line by scan line as we move across a scan line, the depth changes satisfy ax+by+cz=0.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201221

Along scan line y = 0z = - x

c

a

In screen space x = 1

Page 22: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Implementation

• Need a data structure to store:

– Flag for each polygon (inside/outside).

– Incremental structure for scan lines that stores which edges are encountered.

– Parameters for planes.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201222

Page 23: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Visibility Testing

• In many real-time applications, such as games, we want to eliminate as many objects as possible within the application:– Reduce burden on pipeline.– Reduce traffic on bus.

• Partition space with Binary Spatial Partition (BSP) Tree.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201223

Page 24: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

Simple Example

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201224

Consider 6 parallel polygons

top view

The plane of A separates B and C from D, E and F

Page 25: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

BSP Tree

• Can continue recursively:– Plane of C separates B from A.– Plane of D separates E and F.

• Can put this information in a BSP tree:– Use for visibility and occlusion testing.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201225

Page 26: Chapter 6: Vertices to Fragments Part 2 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 1 Mohan Sridharan Based on Slides

What Next?

• Line drawing algorithms.

• Aliasing and anti-aliasing.

• Assignment-3.

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley

201226