Three-Dimensional Graphics IV - University of Oulujiechen/Course/Lecture9/CG_3d_4-C8.pdf ·...

Preview:

Citation preview

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Computer GraphicsComputer Graphics

Three-Dimensional Graphics IV

Guoying Zhao 1 / 61Guoying Zhao 1 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

• Hidden Surface Removal• Hidden Surface Removal

• Polygon Clipping

Guoying Zhao 2 / 61Guoying Zhao 2 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

S fHidden Surface Removal

Guoying Zhao 3 / 61Guoying Zhao 3 / 61

C O M P U T E R G R A P H I C S

Vi ibl S f D t i tiVisible Surface Determination

• DefinitionDefinition– Given a set of 3-D objects and a view

specification (camera) determine whichspecification (camera), determine which lines or surfaces of the object are visible

’ l d VSD t ti• you’ve already seen a VSD step…computing smallest non-negative depth value along a ray

• why might objects not be visible? occlusion vs• why might objects not be visible? occlusion vs. clipping

• clipping is one object at a time while occlusion• clipping is one object at a time while occlusion is global

– Also called Hidden Surface Removal (HSR)Guoying Zhao 4 / 61

– Also called Hidden Surface Removal (HSR)

C O M P U T E R G R A P H I C S

Photorealistic Image

Guoying Zhao 5 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

GraphicsGraphics Pipeline

3D d l i ld di t

Visible surface detection and rendering process also take place in the pipeline

Geometric transformation in 3D space

3D model in world coordinates

Rasterization

P j i

3D model in world coordinates

2D i iProjection

2D graphics in projection coordinates

2D image in viewport coordinates

Window clipping

Clipped 2D graphics in window coordinates

Transformation from window to viewport

pp g p

Guoying Zhao 6 / 61Guoying Zhao 6 / 612D graphics in viewport coordinates

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Visible Surface Determination

• Why to do it?• Why to do it?– Avoiding ambiguity– Improving rendering rates

Guoying Zhao 7 / 61Guoying Zhao 7 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

A idi bi itAvoiding ambiguity

Guoying Zhao 8 / 61Guoying Zhao 8 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

I i d i tImproving rendering rates

Guoying Zhao 9 / 61Guoying Zhao 9 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Hidden Surface Removal Methods• Object space algorithms• Object-space algorithms

Area-Subdivision MethodDepth Sorting Method

• Image-space algorithmsRay Casting MethodZ-buffer

Guoying Zhao 10 / 61Guoying Zhao 10 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Hidden Surface Removal

Obj t h i i t ti• Object-space approach: use pairwise testing between polygons (objects)

partially obscuring can draw independently

• Worst case complexity O(n2) for n polygons

p y obscu g c d w depe de y

Guoying Zhao 11 / 61Guoying Zhao 11 / 6111

Worst case complexity O(n ) for n polygons

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Area-Subdivision Method

• It’s applied by successively dividing the total viewing area into smaller and smaller rectangles until each small area is the projection of part of a single visible

Guoying Zhao 12 / 61Guoying Zhao 12 / 61

small area is the projection of part of a single visible surface or no surface at all.

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Area-Subdivision Method

Guoying Zhao 13 / 61Guoying Zhao 13 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Possible relationships between polygon surfaces and a rectangular

areaarea– surrounding– overlapping– inside

t id– outside2

4

1

3

1

Guoying Zhao 14 / 61Guoying Zhao 14 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Area-Subdivision Method

• No further subdivisions until:• No further subdivisions until:– All polygons are outside the area– Only one inside, overlapping, or

surrounding polygon is in the area– A surrounding polygon obscures all other

surfaces within the area boundariessu aces t t e a ea bou da es

Guoying Zhao 15 / 61Guoying Zhao 15 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Area-Subdivision Method

F i d ith l ti 2N*2N ft N• For a window with resolution 2N*2N,after N divisions, the obtained sub-window is same to one pixel

Guoying Zhao 16 / 61Guoying Zhao 16 / 61

one pixel.

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Depth Sorting Method

Guoying Zhao 17 / 61Guoying Zhao 17 / 61

Painter’s Method

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Painter’s Algorithm

• Render polygons a back to front order so that• Render polygons a back to front order so that polygons behind others are simply painted overover

B behind A as seen by viewer Fill B then A

Guoying Zhao 18 / 61Guoying Zhao 18 / 6118

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Depth Sort

• Requires ordering of polygons first• 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 withOrder polygons and deal with easy cases first, harder later

Polygons sorted by distance from COP

Guoying Zhao 19 / 61Guoying Zhao 19 / 6119

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Easy Cases

• A lies behind all other polygons– Can render

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

Guoying Zhao 20 / 61Guoying Zhao 20 / 6120

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Hard Cases

Overlap in all directionscyclic overlap

But one is fully on one side of the other

penetration

Guoying Zhao 21 / 61Guoying Zhao 21 / 6121

p

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Back-Face Removal (Culling)Back Face Removal (Culling)

θ•face is visible iff 90 ≥ θ ≥ -90equivalently cos θ ≥ 0equivalently cos θ ≥ 0or v • n ≥ 0

I O GL i l bl lli•In OpenGL we can simply enable cullingbut may not work correctly if we have nonconvex objects

Guoying Zhao 22 / 61Guoying Zhao 22 / 6122

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Image Space Approach

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

lpolygons• Complexity O(nmk)Complexity O(nmk)• Ray tracing • z-buffer

Guoying Zhao 23 / 61Guoying Zhao 23 / 6123

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Ray Casting Method

• Cast a ray along the line of sight from a y g gpixel position through a scene

PixelPixel

Guoying Zhao 24 / 61Guoying Zhao 24 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

z-Buffer Algorithm

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

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

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

Guoying Zhao 25 / 61Guoying Zhao 25 / 6125

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Frame Buffer and Depth Buffer

Screen Frame buffer Depth Buffer

Color value Depth valueGuoying Zhao 26 / 61Guoying Zhao 26 / 61

Color value Depth value

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z-buffer Method

y

x

Guoying Zhao 27 / 61Guoying Zhao 27 / 61Z

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z-buffer Method

yy

x

x ZGuoying Zhao 28 / 61Guoying Zhao 28 / 61

x Z

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z-buffer Method

y

x

Guoying Zhao 29 / 61Guoying Zhao 29 / 61Z

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Framebuffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 5 5 5 5 5 5-5 -5 -5 -5 -5 -5 -5 -5

-5 -5 -5 -5 -5 -5 -5 -5-5 -5 -5 -5 -5 -5 -5 -5-5 -5 -5 -5 -5 -5 -5 -55 5 5 5 5 5 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 30 / 61Guoying Zhao 30 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Framebuffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 5 5 5 5 5 5-5 -5 -5 -5 -5 -5 -5 -5

-5 -5 -5 -5 -5 -5 -5 -5-5 -5 -5 -5 -5 -5 -5 -5-5 -5 -5 -5 -5 -5 -5 -55 5 5 5 5 5 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 31 / 61Guoying Zhao 31 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Framebuffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 32 / 61Guoying Zhao 32 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z-buffer Method

y

x

Guoying Zhao 33 / 61Guoying Zhao 33 / 61Z

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z-buffer Method

yy

x

x ZGuoying Zhao 34 / 61Guoying Zhao 34 / 61

x Zv

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 35 / 61Guoying Zhao 35 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 36 / 61Guoying Zhao 36 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 37 / 61Guoying Zhao 37 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 38 / 61Guoying Zhao 38 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 39 / 61Guoying Zhao 39 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 1 0 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 40 / 61Guoying Zhao 40 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 1 0 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 41 / 61Guoying Zhao 41 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 1 0 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 42 / 61Guoying Zhao 42 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 1 0 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 43 / 61Guoying Zhao 43 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 1 0 -1 -2 -5-5 -5 -1 -1 -1 -1 -5 -5-5 -5 -1 -1 -1 -1 -5 -55 5 1 1 1 1 5 5-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 44 / 61Guoying Zhao 44 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

Frame buffer Depth Buffer

-5 -5 -5 -5 -5 -5 -5 -55 5 11 1 1 5 5-5 -5 -1-1 -1 -1 -5 -5

-5 -5 2 1 0 -1 -2 -5-5 -5 -1 -5-5 -5 -1 -1 -1 -1 -5 -5

2 1 0 -25 5 1 1 1 1 5 5

-5 -5 -5 -5 -5 -5 -5 -5

Guoying Zhao 45 / 61Guoying Zhao 45 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Z b ff M h dZ-buffer Method

y

x

Guoying Zhao 46 / 61Guoying Zhao 46 / 61Z

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Efficiency

• If we work scan line by scan line as we• If we work scan line by scan line as we move across a scan line, the depth h i fchanges satisfy aΔx+bΔy+cΔz=0

Along scan line Δy = 0Δz = - Δxa

c

In screen space Δx = 1Guoying Zhao 47 / 61Guoying Zhao 47 / 61

47

p

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Scan-Line Algorithm

• Can combine shading and hsr through• Can combine shading and hsr through scan line algorithm

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

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

Guoying Zhao 48 / 61Guoying Zhao 48 / 6148

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Implementation

• Need a data structure to store• 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

Guoying Zhao 49 / 61Guoying Zhao 49 / 6149

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Visibility Testing

• In many realtime applications such as games we• In many realtime applications, such as games, we want to eliminate as many objects as possible within the applicationpp– Reduce burden on pipeline– Reduce traffic on bus

• Partition space with Binary Spatial Partition (BSP) Tree

Guoying Zhao 50 / 61Guoying Zhao 50 / 6150

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Simple Example

consider 6 parallel polygons

top view

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

Guoying Zhao 51 / 61Guoying Zhao 51 / 6151

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

BSP Tree

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

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

Guoying Zhao 52 / 61Guoying Zhao 52 / 6152

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Clipping

Guoying Zhao 53 / 61Guoying Zhao 53 / 61

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Polygon Clipping

• Not as simple as line segment clipping• Not as simple as line segment clipping– Clipping a line segment yields at most one

li tline segment– Clipping a polygon can yield multiple

polygons

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

Guoying Zhao 54 / 61Guoying Zhao 54 / 6154

at most one other polygon

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Tessellation and ConvexityTessellation and Convexity

• One strategy is to replace nonconvex• One strategy is to replace nonconvex (concave) polygons with a set of triangular polygons (a tessellation)polygons (a tessellation)

• Also makes fill easierT ll ti d i GLU lib• Tessellation code in GLU library

Guoying Zhao 55 / 61Guoying Zhao 55 / 6155

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Clipping as a Black Box

• Can consider line segment clipping as a• Can consider line segment clipping as a process that takes in two vertices and

d i h i hproduces either no vertices or the vertices of a clipped line segmentg

Guoying Zhao 56 / 61Guoying Zhao 56 / 6156

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Pipeline Clipping of Line SegmentsPipeline Clipping of Line Segments

• Clipping against each side of window is• Clipping against each side of window is independent of other sides– Can use four independent clippers in a

pipeline

Guoying Zhao 57 / 61Guoying Zhao 57 / 6157

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Pipeline Clipping of PolygonsPipeline Clipping of Polygons

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

Guoying Zhao 58 / 61Guoying Zhao 58 / 6158

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Bounding Boxes

• Rather than doing clipping on a complex• Rather than doing clipping on a complex polygon, we can use an axis-aligned bounding box or extentbox or extent– Smallest rectangle aligned with axes that

encloses the polygonencloses the polygon– Simple to compute: max and min of x and y

Guoying Zhao 59 / 61Guoying Zhao 59 / 6159

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Bounding boxes

Can usually determine accept/rejectCan usually determine accept/reject based only on bounding box

reject

accept

requires detailedclipping

Guoying Zhao 60 / 61Guoying Zhao 60 / 6160

C O M P U T E R G R A P H I C SC O M P U T E R G R A P H I C S

Clipping and Visibility

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

• In both cases, we are trying to remove objects that are not visible to theobjects that are not visible to the cameraOft i ibilit l i• 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

Guoying Zhao 61 / 61Guoying Zhao 61 / 6161

g g g p p