Lecture 8: Clipping - Uppsala University

61
-Removing what is not seen on the screen Lecture 8: Clipping

Transcript of Lecture 8: Clipping - Uppsala University

Page 1: Lecture 8: Clipping - Uppsala University

-Removing what is not seen on the screen

Lecture 8:

Clipping

Page 2: Lecture 8: Clipping - Uppsala University

The Rendering Pipeline

The Graphics pipeline includes one stage for clipping

Modeling

Transformation

Viewing

Transformation

Projection

Transformation Clipping Rasterization

Page 3: Lecture 8: Clipping - Uppsala University

The view frustum is defined by six clipping planes

(Frustum = clipped pyramid)

View Frustum

COP

Projection plane

Near plane

Far plane

View frustum

Viewport

Bottom plane

Top plane

Page 4: Lecture 8: Clipping - Uppsala University

The view frustum is defined by six clipping planes

(Frustum = clipped pyramid)

View Frustum

COP

Projection plane Viewport

3D Object

Page 5: Lecture 8: Clipping - Uppsala University

The view frustum is defined by six clipping planes

(Frustum = clipped pyramid)

View Frustum

COP

Projection plane Viewport

Object completely outside frustum. Should not be rendered!

Page 6: Lecture 8: Clipping - Uppsala University

The view frustum is defined by six clipping planes

(Frustum = clipped pyramid)

View Frustum

COP

Projection plane Viewport

Object completely outside frustum. Should not be rendered!

Page 7: Lecture 8: Clipping - Uppsala University

The view frustum is defined by six clipping planes

(Frustum = clipped pyramid)

View Frustum

COP

Projection plane Viewport

3D object

Part of object outside the view frustum -> clipping is needed!.

Page 8: Lecture 8: Clipping - Uppsala University

The clipped 3D object seen through the viewport.

View Frustum

Viewport

Clipped 3D object

Page 9: Lecture 8: Clipping - Uppsala University

Remember that the objects are represented by a set

of polygons…

View Frustum

Viewport

Clipped 3D Object

Page 10: Lecture 8: Clipping - Uppsala University

Examples

Types of operations

– Accept – Reject – Clip

Viewport

Polygon

Page 11: Lecture 8: Clipping - Uppsala University

Normalization

We map the view frustum into a cube

(Normalized device coordinates) Clipping in a cube is easier! Observe the x-coordinate of the points!

COP

2/2/

2/2/

2/2/

wzw

wyw

wxw

Range

w = side of the cube COP

Viewport

Page 12: Lecture 8: Clipping - Uppsala University

How it is done

Transfer all vertices into normalized device

coordinates by perspective division (Remember viewing lecture) Scale the coordinates in the range (x,z)

(x´,z´)

(x´,z)

Viewport

COP

Page 13: Lecture 8: Clipping - Uppsala University

Clipping in 2D

Clipping can also be performed in 2D

But it is usually less effective

– Discard all polygons behind the camera

– Project on the viewport plane

– Clip polygons in 2D (on the projection plane)

Most approaches for 2D clipping can be extended to 3D

Page 14: Lecture 8: Clipping - Uppsala University

The Rendering Pipeline

Modeling

Transformation

Viewing

Transformation

Projection

Transformation Clipping Rasterization

Modeling

Transformation

Viewing

Transformation

Projection

Transformation Clipping Rasterization

Modeling

Transformation

Viewing

Transformation

Projection

Transformation Clipping Rasterization

3D Clipping

2D Clipping

Scissoring

Page 15: Lecture 8: Clipping - Uppsala University

Algorithms

Some well known clipping algorithms

– Cohen-Sutherland

– Liang-Barsky

– Sutherland-Hodgeman

– Weiler-Atherton

– Cyrus-Beck

We will look at the 2D version for Line clipping and discuss extensions to polygon clipping in 3D

Page 16: Lecture 8: Clipping - Uppsala University

Cohen-Sutherland

Divide space in 9 regions And assign codes to them (4 bits)

1001 1000 1010

0001

0101 0100

0010

0110

The viewport

0000

Page 17: Lecture 8: Clipping - Uppsala University

Pattern

Each side corresponds to one bit in the

codes (outcode)

0000 0010 0001

1001 1000 1010

0101 0100 0110

Page 18: Lecture 8: Clipping - Uppsala University

Example

The endpoints are assigned an outcode

– 1000 and 0101 in this case

1001 1000 1010

0001

0101

0000

0100

0010

0110

Page 19: Lecture 8: Clipping - Uppsala University

Assignment

The outcode o1=outcode(x1, y1)=(b0 b1 b2 b3) is easily assigned:

otherwise.0

,if1 max

0

yyb

otherwise.0

,if1 min

1

yyb

otherwise.0

,if1 max

2

xxb

otherwise.0

,if1 min

3

xxb

Page 20: Lecture 8: Clipping - Uppsala University

Decision based on the outcode

o1 =o2=0000 Both endpoints are inside the clipping window (Accept, no clipping needed)

Page 21: Lecture 8: Clipping - Uppsala University

Decision based on the outcode

o1!=0000, o2=0000; or vice versa One endpoint is inside and the other is outside

– The line segment must be shortened (clipped)

Page 22: Lecture 8: Clipping - Uppsala University

Decision based on the outcode

o1&o2!=0000 (Bitwise and operator) Both endpoints are on the same side of the clipping window

– Trivial Reject

Page 23: Lecture 8: Clipping - Uppsala University

Decision based on the outcode

1001 1000 1010

0001

0101

0000

0100

0010

0110

O1= 1001, O2= 0101 bitwise and operator gives: 1001 & 0101 = 0001 != 0000, both ends are on the same side of the clipping window ---> reject!

Page 24: Lecture 8: Clipping - Uppsala University

Decision based on the outcode

1001 1000 1010

0001

0101

0000

0100

0010

0110

O1= 0101, O2= 0110 bitwise and operator gives: 0101 & 0110 = 0100 != 0000, both ends are on the same side of the clipping window ---> reject!

Page 25: Lecture 8: Clipping - Uppsala University

Decision based on the outcode

o1&o2=0000 (Given o1!=0000 and o2!=0000) Both endpoint are outside but outside different edges

– The line segment must be investigated further

Page 26: Lecture 8: Clipping - Uppsala University

Decision based on the outcode

1001 1000 1010

0001

0101

0000

0100

0010

0110

O1= 0101, O2= 1000 bitwise and operator gives:

0101 & 1000 = 0000, part of the line could be inside

the viewport ---> further investigation needed!

Page 27: Lecture 8: Clipping - Uppsala University

Parametric Lines

Intersections with border can easily be computed by regarding the line as a parametric line. This is a linear interpolation with:

21)1()( ppp

10

p1

p2

Page 28: Lecture 8: Clipping - Uppsala University

Intersection computation

Example: Intersection with right border xmax The parameter α can easily be computed.

12

1max

121max

121max

21max

)(

)(

)1(

xx

xx

xxxx

xxxx

xxx

p1=(x1, y1)

p2=(x2, y2)

xmax

Page 29: Lecture 8: Clipping - Uppsala University

Intersection computation

Finally we compute the y-coordinate by putting α into the line equation.

)(

)(

)(

)1(

1max

12

121

12

12

1max1

121

21

xxxx

yyyy

yyxx

xxyy

yyyy

yyy

xmax

y

The two point formula! p1=(x1, y1)

p2=(x2, y2)

12

1max

xx

xx

Page 30: Lecture 8: Clipping - Uppsala University

Intersections

We can therefore compute intersections with the border using the two point formula. We will obtain similar equations for the other borders. What happens if we have no intersection with the border?

– The parameter α is out of range [0, 1].

– α is ∞ if the line is parallel with the border we

check for intersections with.

Page 31: Lecture 8: Clipping - Uppsala University

Intersections

Parameter α out of [0, 1] range:

p1

p2

xmax

12

1max

xx

xx

max12 xxx 0

Viewport

Page 32: Lecture 8: Clipping - Uppsala University

Intersections

Parameter α out of [0, 1] range:

p1

p2

xmax

12

1max

xx

xx

12 xx

Viewport

Page 33: Lecture 8: Clipping - Uppsala University

Cohen-Sutherland in 3D

A little bit more complicated…. 27 regions with a 6 bit code

Page 34: Lecture 8: Clipping - Uppsala University

Intersections in 3D

Demo time!

Page 35: Lecture 8: Clipping - Uppsala University

Intersections in 3D

The first equation into the second equation

Page 36: Lecture 8: Clipping - Uppsala University

A hybrid approach

Use 3D Cohen Sutherland for trivial Reject and trivial Accept Then project onto viewport And finally do final clipping in 2D

– Trivial cases need not to be handled!

Page 37: Lecture 8: Clipping - Uppsala University

Liang Barsky

Uses the parametric line! Compute α for each (extended) border in a clockwise order

α1

α3

α2

α4

01 1234

p1

p2

Viewport

Page 38: Lecture 8: Clipping - Uppsala University

Liang Barsky

Note the changed order!

α1

α3

α2

α4

01 1324

p1

p2

Page 39: Lecture 8: Clipping - Uppsala University

Liang Barsky

Similar equations can be derived for all possible cases Clip using the computed α’s 3D: just add one dimension in the parametric line

21)1()( zzz

Page 40: Lecture 8: Clipping - Uppsala University

Sutherland-Hodgeman

Is a ’pipeline’ clipper…

Page 41: Lecture 8: Clipping - Uppsala University

Sutherland-Hodgeman

Is a ’pipeline’ clipper…

Original polygon

Page 42: Lecture 8: Clipping - Uppsala University

Sutherland-Hodgeman

Is a ’pipeline’ clipper…

Original polygon Clip top…

Page 43: Lecture 8: Clipping - Uppsala University

Sutherland-Hodgeman

Is a ’pipeline’ clipper…

Clip bottom… Original polygon Clip top…

Page 44: Lecture 8: Clipping - Uppsala University

Sutherland-Hodgeman

Is a ’pipeline’ clipper…

Clip bottom…

Clip left…

Original polygon Clip top…

Page 45: Lecture 8: Clipping - Uppsala University

Sutherland-Hodgeman

Is a ’pipeline’ clipper…

Clip bottom…

Clip left… Clip right

Original polygon Clip top…

Page 46: Lecture 8: Clipping - Uppsala University

Sutherland-Hodgeman

Use the two-point formula for intersection computations

(Top clipping)

Page 47: Lecture 8: Clipping - Uppsala University

Polygon clipping

The previous explained approaches can be

used for clipping polygons with some

modifications.

Note that a triangle/polygon can have more

vertices after clipping!

Page 48: Lecture 8: Clipping - Uppsala University

Polygon clipping

Convex polygon… 4 vertices.

Page 49: Lecture 8: Clipping - Uppsala University

Polygon clipping

Convex polygon… 6 vertices after clipping.

Page 50: Lecture 8: Clipping - Uppsala University

Polygon clipping

Concave polygon… 4 vertices.

Page 51: Lecture 8: Clipping - Uppsala University

Polygon clipping

Concave polygon… 7 vertices after clipping.

- Vertices inside the clip window are retained. - Vertices outside are removed. - New vertices are added at the clip window boundary.

Page 52: Lecture 8: Clipping - Uppsala University

Polygon clipping

Concave polygon example 2…

Page 53: Lecture 8: Clipping - Uppsala University

Polygon clipping

Concave polygons can split into multiple polygons after clipping!

Page 54: Lecture 8: Clipping - Uppsala University

Acceleration techniques

Imagine an object with many polygons It is not efficient to check thousands of polygons for intersections! We need some acceleration technique

69451 polygons

Page 55: Lecture 8: Clipping - Uppsala University

Bounding Volumes

Create the smallest box that contains the Bunny

Check eight sides for intersection instead!

We can constrain the bounding box to be

aligned with the axes (simpler calculations),

or we can allow arbitrary orientation of the box.

(possible to achieve better fit)

Axis Aligned Bounding Box

Page 56: Lecture 8: Clipping - Uppsala University

Intersection test

Trivial cases are easy! Non trivial cases still need extensive Computations

– Unless you use a hierarchy of cubes

Page 57: Lecture 8: Clipping - Uppsala University

Bounding Spheres

Only one center and a radius have to be checked! But not all objects are suitable for spheres…

Page 58: Lecture 8: Clipping - Uppsala University

Bounding Spheres

Make a hierarchy of spheres for elongated objects!

Page 59: Lecture 8: Clipping - Uppsala University

OpenGL

gluPerspective or glFrustum easily sets up the clipping frustrum in OpenGL. gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);

glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,

GLdouble zNear, GLdouble zFar);

Demo Time!

http://www.codesampler.com/oglsrc/oglsrc_2.htm

Page 60: Lecture 8: Clipping - Uppsala University

Some final words…

Not only 3D Objects need to be clipped

– Also splines, text etc…

A mirror or a portal in a game can have non rectangular shape

– Clipping is needed

Even though clipping is implemented in hardware, it is essential to understand the basics of it!

Page 61: Lecture 8: Clipping - Uppsala University

Lab 4

- Volume rendering of CT data.

- OpenGL Shading Language.