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

Post on 19-Dec-2015

218 views 0 download

Tags:

transcript

Hidden Line Removal

Adopted from U Strathclyde’s course page

Introduction

• Depth cueing

• Surfaces

• Vectors/normals

• Hidden face culling

• Convex/concave solids

Perspective Confusion

Is this the back orthe front face ofthe cube?

Hidden Line Removal

(show demo – basic3d wireframe surface)

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

Based on surfaces not lines

• Need a Surface data structure

• WireframeSurface– made up of lines

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

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

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)

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

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

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

Determining visibility

• Alternatively:

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

cos. NL

How do we work out L.N?

cos. zzyyxx nlnlnlNL

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

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

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.

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)

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

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.

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

Calculating the normal

yzzyx baban

zxxzy baban

xyyxz baban

This is a definition of the vector product

Visibility

– At this point we know:

• we need to calculate cos

• Values for lx,ly,lz

• values for nx,ny,nz

cos. zzyyxx nlnlnlNL

Visibility

If

then draw the surface

else

don’t! (or draw dashes)

1cos0

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

An alternative approach

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

z

surface

extensionof surface

z=0

More complex shapes

Multiple objects Concave objects

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.

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

Ambiguities

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

z

x

surface 1

surface 2

<two books demo>

Ambiguities – front view

x

y

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

x - extents overlap?

z

x

P

Qno they don’t, test fails

y -extents overlap?

z

y

P

Q

no they don’t, test fails

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

z

x

P Q

yes it is, test fails

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

z

x

PQ

yes it is, test fails

Does the projection of the two surfaces overlap?

z

x

P

Q

y

x

PQ

hole in P

no they don’t, test fails

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

End up drawing Q2,P1,P2,Q1

z

x

P1Q2

P2

Q1

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

References

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