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

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

Embed Size (px)

Citation preview

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

Vertices and Fragments III

Mohan SridharanBased on slides created by Edward Angel

1CS4395: Computer Graphics

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

Objectives

• This lecture:

– Survey hidden-surface algorithms.

• More about these algorithms later

CS4395: Computer Graphics 2

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

Hidden Surface Removal• Object-space approach: use pair-wise testing between

polygons (objects).

• Worst case complexity O(n2) for n polygons.• Best-case sorting algorithms: O(n log(n)).

CS4395: Computer Graphics 3

partially obscuring can draw independently

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

Painter’s Algorithm

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

CS4395: Computer Graphics 4

B behind A as seen by viewer Fill B then A

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

Depth Sort

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

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

CS4395: Computer Graphics 5

Polygons sorted by distance from COP

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

Easy Cases

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

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

CS4395: Computer Graphics 6

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

Hard Cases

CS4395: Computer Graphics 7

Overlap in all directions but can render because all vertices of one polygon on one side of the plane of the other polygon.

cyclic overlap

penetration

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

Back-Face Removal (Culling)

CS4395: Computer Graphics 8

Face is visible iff 90 -90equivalently cos 0or 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 9: Vertices and Fragments III Mohan Sridharan Based on slides created by Edward Angel 1 CS4395: Computer Graphics

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.

CS4395: Computer Graphics 9

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

z-Buffer Algorithm

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

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

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

CS4395: Computer Graphics 10

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

Efficiency

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

CS4395: Computer Graphics 11

Along scan line y = 0z = - x

c

a

In screen space x = 1

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

Scan-Line Algorithm

• Can combine shading and HSR through scan line algorithm.

CS4395: Computer Graphics 12

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 13: Vertices and Fragments III Mohan Sridharan Based on slides created by Edward Angel 1 CS4395: Computer Graphics

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.

CS4395: Computer Graphics 13

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

Visibility Testing

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

• Partition with Binary Spatial Partition (BSP) Tree.

CS4395: Computer Graphics 14

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

Simple Example

CS4395: Computer Graphics 15

Consider 6 parallel polygons.

top view

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

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

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 .

CS4395: Computer Graphics 16

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

Summary

• Looked at hidden-surface removal algorithms.

• OpenGL includes Z-buffering.

• Better organization of objects required to properly and efficiently render complex scenes.

CS4395: Computer Graphics 17