cap4730: computational structures in computer graphics visible surface determination

40
CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Upload: roy-hart

Post on 17-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

CAP4730: Computational Structures in Computer Graphics

Visible Surface Determination

Page 2: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Outline

•The goal of visible surface determination•Normals•Backface Culling•Depth Buffer•BSP Trees•Determining if something isn’t in the view frustum (research topics)

Page 3: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Goal of Visible Surface Determination

To draw only the surfaces (triangles) that are visible, given a view point and a view direction

Page 4: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Three reasons to not draw something

• 1. It isn’t in the view frustum

• 2. It is “back facing”

• 3. Something is in front of it (occlusion)

• We need to do this computation quickly. How quickly?

Page 5: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Surface Normal

• Surface Normal - vector perpendicular to the surface

• Three non-collinear points (that make up a triangle), also describes a plane. The normal is the vector perpendicular to this plane.

Page 6: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Normals

Page 7: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

How do we compute a normal?

• Q: Given a Triangle, how do we compute a normal?

A: Normal = V0V1 X V0V2

But…. we know V0V1 X V0V2 != V0V2 X V0V1

Page 8: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Vertex Order

• Vertex order matters. We usually agree that counterclockwise determines which “side” or a triangle is labelled the “front”. Think: Right handed coordinate system.

Page 9: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

What do the normals tell us?

Q: How can we use normals to tell us which “face” of a triangle we see?

Page 10: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Examine the angle between the normal and the view direction

V

N

Front if V . N <0

Page 11: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Viewing Coordinates

If we are in viewing coordinates, how can we simplify our comparison?

Think about the different components of the normals you want and don’t want.

Page 12: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Backface Culling

•Before scan converting a triangle, determine if it is facing you

•Compute the dot product between the view vector (V) and triangle normal (N)

•Simplify this to examining only the z component of the normal

•If Nz<0 then it is a front facing triangle, and you should scan convert it

•What surface visibility problems does this solve? Not solve?

•Review OpenGL code

Page 13: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Multiple Objects

• If we want to draw: We can sort in z. What are the advantages? Disadvantages?

Called Painter’s Algorithm or splatting.

Page 14: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Painter’s Algorithm Subtleties

• What do we mean sort in z? That is for a triangle, what is its representative z value?– Minimum z– Maximum z– Polygon’s centroid

• Work cost = sort + draw• We still use Painter’s Algorithms for blended objects

(discussed in the Blending Lesson)• An object space visibility algorithm

Page 15: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Side View

Page 16: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Side View - What is a solution?

Page 17: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Even Worse… Why?

Page 18: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Painter’s Algorithm

• Pros:– No extra memory

– Relatively fast

– Easy to understand and implement

• Cons:– Precision issues (and

additional work to handle them)

– Sort stage

– Intersecting objects

Page 19: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Depth Buffers

Goal: We want to only draw something if it appears in front of what is already drawn.

What does this require? Can we do this on a per object basis?

Page 20: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Depth Buffers

We can’t do it object based, it must be image based.

What do we know about the x,y,z points where the objects overlap?

Remember our “eye” or “camera” is at the origin of our view coordinates.

What does that mean need to store?

Page 21: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Side View

Page 22: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Algorithm

• We need to have an additional value for each pixel that stores the depth value.

• What is the data type for the depth value?• How much memory does this require?• Playstation 1 had 2 MB.• The first 512 x 512 framebuffer cost $50,000• Called Depth Buffering or Z buffering

Page 23: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Depth Buffer Algorithm

• Begin frame– Clear color

– Clear depth to z = zmax

• Draw Triangles– When scan converting znew pixel < zvalue at the pixel, set color and

zvalue at the pixel = znew pixel

– What does it mean if znew pixel > zvalue at the pixel?

– Why do we clear the depth buffer?– Now we see why it is sometimes called the z buffer

Page 24: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Computing the znew pixel

• Q: We can compute the znsc at the vertices, but what is the znsc as we scan convert?

• A: We interpolate znsc while we scan convert too!

Page 25: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Metrics for Visibility Algorithms

• Running Time - 1 extra compare

• Storage Overhead - 1 extra field per pixel

• Overdraw - we have to scan convert all triangles

• Memory Bandwidth - how much we have to retrieve from memory. Increased

• Precision - Let’s examine the possible values of z and what that means

Page 26: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Z Buffer Precision

• What does the # of bits for a depth buffer element mean?

• The z from eye space to normalized screen space is not linear. That is we do not have the same precision across z. (we divided by z).

• In fact, half of our precision is in z=0 and z=0.5. What does this mean? What happens if we do NOT have enough precision?

Page 27: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Z Fighting

• If we do not have enough precision in the depth buffer, we can not determine which fragment should be “in front”.

• What does this mean for the near and far plane?

We want them to as closely approximate our volume

Page 28: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Z Fighting Zoomed In

Run Demo

Page 29: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Don’t forget

• Even in 1994, memory wasn’t cheap. If we wanted 1024x768x16bit = 1.6 MB additional memory.

• Depth Buffers weren’t common till recently because of this.

• Since we have to draw every triangle -> fill rate goes UP. Currently graphics cards approach the many billions of pixels per second.

• An image space algorithm• Let’s review OpenGL code

Page 30: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Depth Buffer Algorithm

• Pros:– Easy to understand and

implement

– per pixel “correct” answer

– no preprocess

– draw objects in any order

– no need to redivide objects

• Cons:– Z precision

– additional memory

– Z fighting

Page 31: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

BSP Trees (Fuchs, et. al 1980)

• Binary Space Partitioning– Doom and most games before depthbuffers

(circa 1994-95)– Given a world, we want to build a data structure

that given any point, it can return a sorted list of objects

– What assumptions are we making?– Note, what happens in those “old” games like

Doom?

Page 32: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

BSP Trees

• Two stages:– preprocess - we do this at the “offline”– runtime - what we do per frame

• Draw parallels to Doom– Since this is easier in 2D, note all “old” FPS are

really 3D.

Page 33: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

BSP Algorithm

• For a viewpoint, determine where it sits on the tree.

• Now draw objects on the “other half of the tree” – farside.draw(viewpoint)– nearside.draw(viewpoint)

• Intuition - we draw things farther away first• Is this an image space or object space algorithm?

Page 34: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination
Page 35: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination
Page 36: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

BSP Trees

• Pros– Preprocess step means

fast determination of what we can see and can’t

– Works in 3D -> Quake1

– Painter’s algorithm Pros

• Cons– Still has intersecting

object problems

– Static scene

Page 37: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination

Determining if something is viewable

• Viewfrustum Culling (football example)

• Cells and Portals– definitions

• cell

• portal

– preprocess step– runtime computation– where do we see it?

• Quake3

Page 38: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination
Page 39: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination
Page 40: CAP4730: Computational Structures in Computer Graphics Visible Surface Determination