hidden line removal adopted from u strathclyde’s course page

41
Hidden Line Removal Adopted from U Strathclyde’s course page

Post on 19-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hidden Line Removal Adopted from U Strathclyde’s course page

Hidden Line Removal

Adopted from U Strathclyde’s course page

Page 2: Hidden Line Removal Adopted from U Strathclyde’s course page

Introduction

• Depth cueing

• Surfaces

• Vectors/normals

• Hidden face culling

• Convex/concave solids

Page 3: Hidden Line Removal Adopted from U Strathclyde’s course page

Perspective Confusion

Is this the back orthe front face ofthe cube?

Page 4: Hidden Line Removal Adopted from U Strathclyde’s course page

Hidden Line Removal

(show demo – basic3d wireframe surface)

Page 5: Hidden Line Removal Adopted from U Strathclyde’s course page

Hidden Line Removal

• No one best algorithm• Look at a simple approach for convex solids• based upon working out which way a surface is

pointing relative to the viewer.

convex concave

Page 6: Hidden Line Removal Adopted from U Strathclyde’s course page

Based on surfaces not lines

• Need a Surface data structure

• WireframeSurface– made up of lines

Page 7: Hidden Line Removal Adopted from U Strathclyde’s course page

Drawing3D

Shape3d Line3d Point3d

JFrame

GappGcanvas

JPanel

initComponentsthisWindowclosing

paintComponent

Transformation3d

Matrix

Bezier3d

GItem3d

1 M

1 1 11

1

M

1

1 1 1

Surface3dFlat

M1

Page 8: Hidden Line Removal Adopted from U Strathclyde’s course page

Flat surfaces

• Key requirement of our surfaces is that they are FLAT.

• Easiest way to ensure this is by using only three points to define the surface….

– (any triangle MUST be flat - think about it)

• …but as long as you promise not to do anything that will bend a flat surface, we can allow them to be defined by as many points as you like.

• Single sided

Page 9: Hidden Line Removal Adopted from U Strathclyde’s course page

Which way does a surface point?

– Vector mathematics defines the concept of a surface’s normal vector.

– A surface’s normal vector is simply an arrow that is perpendicular to that surface (i.e. it sticks straight out)

Page 10: Hidden Line Removal Adopted from U Strathclyde’s course page

Determining visibility

Consider the six faces of a cube and their normal vectors

Vectors N1 and N2 are are the normals to surfaces 1 and 2 respectively.

Vector L points from surface 1 to the viewpoint.

It can be seen that surface 1 is visible to the viewer whilst surface 2 cannot be seen from that position.

N2

N1Lsurface1 su

rface2

Page 11: Hidden Line Removal Adopted from U Strathclyde’s course page

Determining visibility

• Mathematically, a surface is visible from the position given by L if:

• Where is the angle between L and N.

• Equivalently,

900

1cos0

Page 12: Hidden Line Removal Adopted from U Strathclyde’s course page

Determining visibility

• Fortunately we can calculate cos from the direction of L (lx,ly,lz) and N (nx,ny,nz)

• This is due to the well known result in vector mathematics - the dot product (or the scalar product) whereby:

cos... NLNL

Page 13: Hidden Line Removal Adopted from U Strathclyde’s course page

Determining visibility

• Alternatively:

• Where L and N are unit vectors (i.e of length 1)

cos. NL

Page 14: Hidden Line Removal Adopted from U Strathclyde’s course page

How do we work out L.N?

cos. zzyyxx nlnlnlNL

Page 15: Hidden Line Removal Adopted from U Strathclyde’s course page

How do we work out L.N?

– At this point we know:

• we need to calculate cos

• Values for lx,ly,lz

• The only things we are missing are nx,ny,nz

Page 16: Hidden Line Removal Adopted from U Strathclyde’s course page

Calculating the normal vector

– If you multiply any two vectors using the vector product, the result is another vector that is perpendicular to the plane (i.e normal) which contained the two original vectors.

a

b

n = a ̂b

Page 17: Hidden Line Removal Adopted from U Strathclyde’s course page

IMPORTANT

• We need to adopt the convention that the calculated normal vector points away from the observer when the angle between the two initial vectors is measured in an clockwise direction.

• Failure to do this will lead to MAJOR confusion when you try and implement this

• Believe me, I know.

Page 18: Hidden Line Removal Adopted from U Strathclyde’s course page

Calculating the normal

– Where to find two vectors that we can multiply?

– Answer: we can manufacture them artificially from the points that define the plane we want the normal of

a

b

P0

P1

P2

P3

P4

n

(x0,y

0,z

0)

(x1,y

1,z

1)

(x4,y

4,z

4)

Page 19: Hidden Line Removal Adopted from U Strathclyde’s course page

Calculating the normal

• By subtracting the coordinates of consecutive points we can form vectors which a guaranteed to lie in the plane of the surface under consideration.

),,(),,( 010101 zzyyxxaaaa zyx

),,(),,( 040404 zzyyxxbbbb zyx

Page 20: Hidden Line Removal Adopted from U Strathclyde’s course page

Calculating the normal

• We define the vectors to be anti-clockwise, when viewing the surface from the interior

• (imagine the surface is part of a cube and your looking at it from INSIDE the cube).

• Following the anticlockwise convention mentioned above we have produced what is known as an outward normal.

Page 21: Hidden Line Removal Adopted from U Strathclyde’s course page

IMPORTANT

• An important consequence of this is that when you define the points that define a surface in a program, you MUST add them in anti-clockwise order

normal vector is INTOthe page

Page 22: Hidden Line Removal Adopted from U Strathclyde’s course page

Calculating the normal

yzzyx baban

zxxzy baban

xyyxz baban

This is a definition of the vector product

Page 23: Hidden Line Removal Adopted from U Strathclyde’s course page

Visibility

– At this point we know:

• we need to calculate cos

• Values for lx,ly,lz

• values for nx,ny,nz

cos. zzyyxx nlnlnlNL

Page 24: Hidden Line Removal Adopted from U Strathclyde’s course page

Visibility

If

then draw the surface

else

don’t! (or draw dashes)

1cos0

Page 25: Hidden Line Removal Adopted from U Strathclyde’s course page

Making it easier

• Actually, in certain cases we can simplify things a little. If the viewpoint lies somewhere on the negative side of z-axis, as it did when we first set up the projection transformations (i.e without any viewpoint transformations) we can forget about L and cos

• All we really need to know is whether or not the normal points into the screen or out of it, i.e. is nz positive or negative? In that case, all we need to do is calculate nz and test it

Page 26: Hidden Line Removal Adopted from U Strathclyde’s course page

An alternative approach

• Consider where the extension of the surface cuts the z-axis

z

surface

extensionof surface

z=0

Page 27: Hidden Line Removal Adopted from U Strathclyde’s course page

More complex shapes

Multiple objects Concave objects

Page 28: Hidden Line Removal Adopted from U Strathclyde’s course page

More complex shapes

• In these cases, each surface must be considered individually. Two different types of approach are possible:

– Object space algorithms - examine each face in space to determine it visibility

– Image space algorithms - at each screen pixel position, determine which face element is visible.

• Approximately, the relative efficiency of an image space algorithm increases with the complexity of the scene being represented, but often the drawing can be simplified for convex objects by removing surfaces which are invisible even for a single object.

Page 29: Hidden Line Removal Adopted from U Strathclyde’s course page

The z-buffer (or painter’s) algorithm

– based upon sorting the surfaces by their z-coordinates. The algorithm can be summarised thus:

– Sort the surfaces into order of increasing depth. Define the maximum z value of the surface and the z-extent.

– resolve any depth ambiguities– draw all the surfaces starting with the largest z-value

z-extent

zmax

z

x

Page 30: Hidden Line Removal Adopted from U Strathclyde’s course page

Ambiguities

• Ambiguities arise when the z-extents of two surfaces overlap.

z

x

surface 1

surface 2

<two books demo>

Page 31: Hidden Line Removal Adopted from U Strathclyde’s course page

Ambiguities – front view

x

y

Page 32: Hidden Line Removal Adopted from U Strathclyde’s course page

Resolving Ambiguities

– An algorithm exists for ambiguity resolution

– Where two shapes P and Q have overlapping z-extents, perform the following 5 tests (in sequence of increasing complexity).

– If any test fails, draw P first.

z

x

P

Q

Page 33: Hidden Line Removal Adopted from U Strathclyde’s course page

x - extents overlap?

z

x

P

Qno they don’t, test fails

Page 34: Hidden Line Removal Adopted from U Strathclyde’s course page

y -extents overlap?

z

y

P

Q

no they don’t, test fails

Page 35: Hidden Line Removal Adopted from U Strathclyde’s course page

Is Q not completely on the side of P nearest the viewer?

z

x

P Q

yes it is, test fails

Page 36: Hidden Line Removal Adopted from U Strathclyde’s course page

Is P not completely on the side of Q further from the viewer?

z

x

PQ

yes it is, test fails

Page 37: Hidden Line Removal Adopted from U Strathclyde’s course page

Does the projection of the two surfaces overlap?

z

x

P

Q

y

x

PQ

hole in P

no they don’t, test fails

Page 38: Hidden Line Removal Adopted from U Strathclyde’s course page

If all tests are passed

• … then reverse P and Q in the list of surfaces sorted by Zmax

• set a flag to say that the test has been perfomed once.

• The flag is necessary for the case of intersecting planes.

• If the tests are all passed a second time, then it is necessary to split the surfaces and repeat the algorithm on the 4 surfaces

Page 39: Hidden Line Removal Adopted from U Strathclyde’s course page

End up drawing Q2,P1,P2,Q1

z

x

P1Q2

P2

Q1

Page 40: Hidden Line Removal Adopted from U Strathclyde’s course page

Summary

• Need for depth cues and hidden line removal

• Using surfaces rather than lines

• Calculating which way a surface is facing (surface normal)

• Base a decision on visibility on normal vector and observer vector

Page 41: Hidden Line Removal Adopted from U Strathclyde’s course page

References

• http://local.cis.strath.ac.uk/teaching/ug/classes/52.359if/