Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view.

Similar presentations


Presentation on theme: "2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view."— Presentation transcript:

1 2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view transformation matrices OpenGL transformations Basic perspective projection –Properties of perspective projection –The “eye at origin looking along –z” special case General Perspective –Specifying perspective view volumes (frustum)

2 2/26/04© University of Wisconsin, CS559 Spring 2004 Today More general perspective viewing Clipping

3 2/26/04© University of Wisconsin, CS559 Spring 2004 Basic Perspective Projection Eye at origin, looking along -z xvxv yvyv -z v d P(x v,y v,z v ) P(x s,y s )

4 2/26/04© University of Wisconsin, CS559 Spring 2004 Perspective View Volume Recall the orthographic view volume, defined by a near, far, left, right, top and bottom plane The perspective view volume is also defined by near, far, left, right, top and bottom planes – the clip planes –Near and far planes are parallel to the image plane: z v =n, z v =f –Other planes all pass through the center of projection (the origin of view space) –The left and right planes intersect the image plane in vertical lines –The top and bottom planes intersect in horizontal lines

5 2/26/04© University of Wisconsin, CS559 Spring 2004 Clipping Planes xvxv -z v Near Clip Plane Far Clip Plane View Volume Left Clip Plane Right Clip Plane f n l r

6 2/26/04© University of Wisconsin, CS559 Spring 2004 Where is the Image Plane? Notice that it doesn’t really matter where the image plane is located, once you define the view volume –You can move it forward and backward along the z axis and still get the same image, only scaled But we need to know where it is to define the clipping planes –Assume the left/right/top/bottom planes are defined according to where they cut the near clip plane Or, define the left/right and top/bottom clip planes by the field of view

7 2/26/04© University of Wisconsin, CS559 Spring 2004 Clipping Planes xvxv -z v Near Clip Plane Far Clip Plane View Volume Left Clip Plane Right Clip Plane f FOV Assumes a symmetric view volume

8 2/26/04© University of Wisconsin, CS559 Spring 2004 OpenGL gluPerspective(…) –Field of view in the y direction, FOV, (vertical field-of-view) –Aspect ratio, a, should match window aspect ratio –Near and far clipping planes, n and f –Defines a symmetric view volume glFrustum(…) –Give the near and far clip plane, and places where the other clip planes cross the near plane –Defines the general case –Used for stereo viewing, mostly

9 2/26/04© University of Wisconsin, CS559 Spring 2004 gluPerspective to glFrustum As noted previously, glu functions don’t add basic functionality, they are just more convenient –So how does gluPerspective convert to glFrustum ? –Symmetric, so only need t and l y z FOV / 2 n t ?

10 2/26/04© University of Wisconsin, CS559 Spring 2004 gluPerspective to glFrustum As noted previously, glu functions don’t add basic functionality, they are just more convenient –So how does gluPerspective convert to glFrustum ? –Symmetric, so only need t and l y z FOV / 2 n t

11 2/26/04© University of Wisconsin, CS559 Spring 2004 Perspective Projection Matrices We want a matrix that will take points in our perspective view volume and transform them into the orthographic view volume –This matrix will go in our pipeline just before the orthographic projection matrix (l,b,n) (r,t,n) (l,b,n) (r,t,n)

12 2/26/04© University of Wisconsin, CS559 Spring 2004 Mapping Lines We want to map all the lines through the center of projection to parallel lines –Points on lines through the center of projection map to the same point on the image –Points on parallel lines map orthographically to the same point on the image –If we convert the perspective case to the orthographic case, we can use all our existing methods The intersection points of lines with the near clip plane should not change The matrix that does this, not surprisingly, looks like the matrix for our simple perspective case

13 2/26/04© University of Wisconsin, CS559 Spring 2004 General Perspective This matrix leaves points with z=n unchanged It is just like the simple projection matrix, but it does some extra things to z to map the depth properly We can multiply a homogenous matrix by any number without changing the final point, so the two matrices above have the same effect

14 2/26/04© University of Wisconsin, CS559 Spring 2004 Complete Perspective Projection After applying the perspective matrix, we still have to map the orthographic view volume to the canonical view volume:

15 2/26/04© University of Wisconsin, CS559 Spring 2004 OpenGL Perspective Projection For OpenGL you give the distance to the near and far clipping planes The total perspective projection matrix resulting from a glFrustum call is:

16 2/26/04© University of Wisconsin, CS559 Spring 2004 Near/Far and Depth Resolution It may seem sensible to specify a very near clipping plane and a very far clipping plane –Sure to contain entire scene But, a bad idea: –OpenGL only has a finite number of bits to store screen depth –Too large a range reduces resolution in depth - wrong thing may be considered “in front” –See Shirley for a more complete explanation Always place the near plane as far from the viewer as possible, and the far plane as close as possible

17 2/26/04© University of Wisconsin, CS559 Spring 2004 Clipping Parts of the geometry to be rendered may lie outside the view volume –View volume maps to memory addresses –Out-of-view geometry generates invalid addresses –Geometry outside the view volume also behaves very strangely under perspective projection Triangles can be split into two pieces, for instance Clipping removes parts of the geometry that are outside the view Best done in canonical space before perspective divide –Before dividing out the homogeneous coordinate

18 2/26/04© University of Wisconsin, CS559 Spring 2004 Clipping Points are trivial to clip - just check which side of the clip planes they are on Many algorithms for clipping lines exist –You need one for the 2 nd project Two main algorithms for clipping polygons exist –Sutherland-Hodgman –Weiler

19 2/26/04© University of Wisconsin, CS559 Spring 2004 Clipping Points A point is inside the view volume if it is on the “inside” of all the clipping planes –The normals to the clip planes are considered to point inward, toward the visible region Now we see why clipping is done in canonical view space –For instance, to check against the left plane: –X coordinate in 3D must be > -1 –In homogeneous screen space, same as: x screen > -w screen In general, a point, p, is “inside” a plane if: –You represent the plane as n x x+n y y+n z z+d=0, with (n x,n y,n z ) pointing inward –And n x p x +n y p y +n z p z +d>0

20 2/26/04© University of Wisconsin, CS559 Spring 2004 Sutherland-Hodgman Clip Clip the polygon against each edge of the clip region in turn –Clip polygon each time to line containing edge –Only works for convex clip regions (Why?)

21 2/26/04© University of Wisconsin, CS559 Spring 2004 Sutherland-Hodgman Clip To clip a polygon to a line/plane: –Consider the polygon as a list of vertices –One side of the line/plane is considered inside the clip region, the other side is outside –We are going to rewrite the polygon one vertex at a time – the rewritten polygon will be the polygon clipped to the line/plane –Check start vertex: if “inside”, emit it, otherwise ignore it –Continue processing vertices as follows… p0p0 p4p4 p3p3 p2p2 p1p1 p0p0 p4p4 p3p3 p2p2 p1p1 Clipper points in points out

22 2/26/04© University of Wisconsin, CS559 Spring 2004 Sutherland-Hodgman (3) InsideOutside s p Output p InsideOutside s p Output i InsideOutside s p No output InsideOutside s p Output i and p i i

23 2/26/04© University of Wisconsin, CS559 Spring 2004 Sutherland-Hodgman (4) Look at the next vertex in the list, p, and the edge from the last vertex, s, to p. If the… –polygon edge crosses the clip line/plane going from out to in: emit crossing point, i, next vertex, p –polygon edge crosses clip line/plane going from in to out: emit crossing, i –polygon edge goes from out to out: emit nothing –polygon edge goes from in to in: emit next vertex, p

24 2/26/04© University of Wisconsin, CS559 Spring 2004 Inside-Outside Testing Lines/planes store a vector pointing toward the outside of the clip region – the outward pointing normal –Could re-define for inward pointing Dot products give inside/outside information Note that x (a vector) is any point on the clip line/plane OutsideInside n s f i x

25 2/26/04© University of Wisconsin, CS559 Spring 2004 Finding Intersection Pts Use the parametric form for the edge between two points, x 1 and x 2 : For planes of the form x=a: Similar forms for y=a, z=a Solution for general plane can also be found

26 2/26/04© University of Wisconsin, CS559 Spring 2004 Inside/Outside in Screen Space In canonical screen space, clip planes are x s =±1, y s =±1, z s =±1 Inside/Outside reduces to comparisons before perspective divide

27 2/26/04© University of Wisconsin, CS559 Spring 2004 Hardware Sutherland-Hodgman Suitable for hardware implementation –Only need the clip edge, the endpoints of the current edge, and the last output point –Polygon edges are output as they are found, and passed right on to the next clip region edge Clip Top Vertices in Clip Right Clip Bottom Clip Left Clipped vertices out Clip Far Clip Near

28 2/26/04© University of Wisconsin, CS559 Spring 2004 Other Ways to Reject If a polygonal object is closed, then no back-facing face is visible –Front-facing faces must occlude all back-facing ones –Reject back-facing polygons in view space Transform face normal and check –OpenGL supports optional back-face culling (and front-face culling too) Bounding volumes enclosing many polygons can be checked against the view volume –Done in software in world or view space Visibility can reject whole chunks of geometry without even looking at them

29 2/26/04© University of Wisconsin, CS559 Spring 2004 Clipping In General Apart from clipping to the view volume, clipping is a basic operation in many other algorithms –Special purpose rendering might use different clipping (Project 2) –Breaking space up into chunks –2D drawing and windowing –Modeling May require more complex geometry than rectangular boxes

30 2/26/04© University of Wisconsin, CS559 Spring 2004 Additional Clipping Planes Useful for doing things like cut-away views –Use a clip plane to cut off part of the object –Only works if piece to be left behind is convex OpenGL allows you to do it Also one way to use OpenGL to identify objects in a region of space (uses the selection mechanism)

31 2/26/04© University of Wisconsin, CS559 Spring 2004 Clipping Lines Lines can also be clipped by Sutherland-Hodgman –Slower than necessary, unless you already have hardware Better algorithms exist –Cohen-Sutherland –Liang-Barsky –Nicholl-Lee-Nicholl (we won’t cover this one – only good for 2D)

32 2/26/04© University of Wisconsin, CS559 Spring 2004 Cohen-Sutherland (1) Works basically the same as Sutherland-Hodgman –Was developed earlier Clip line against each edge of clip region in turn –If both endpoints outside, discard line and stop –If both endpoints in, continue to next edge (or finish) –If one in, one out, chop line at crossing pt and continue Works in both 2D and 3D for convex clipping regions

33 2/26/04© University of Wisconsin, CS559 Spring 2004 Cohen-Sutherland (2) 12 3 4 12 3 4 3 4 12 3 4 12

34 2/26/04© University of Wisconsin, CS559 Spring 2004 Cohen-Sutherland (3) Some cases lead to premature acceptance or rejection –If both endpoints are inside all edges –If both endpoints are outside one edge General rule of clipping – if a fast test can cover many cases, do it first

35 2/26/04© University of Wisconsin, CS559 Spring 2004 Cohen-Sutherland - Details Only need to clip line against edges where one endpoint is out Use outcode to record endpoint in/out wrt each edge. One bit per edge, 1 if out, 0 if in. Trivial reject: –outcode(x1) & outcode(x2)!=0 Trivial accept: –outcode(x1) | outcode(x2)==0 Which edges to clip against? –outcode(x1) ^ outcode(x2) 12 3 4 0010 0101


Download ppt "2/26/04© University of Wisconsin, CS559 Spring 2004 Last Time General Orthographic Viewing –Specifying cameras in world coordinates –Building world  view."

Similar presentations


Ads by Google