Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13 Clipping & Scan Conversion

Similar presentations


Presentation on theme: "Lecture 13 Clipping & Scan Conversion"— Presentation transcript:

1 Lecture 13 Clipping & Scan Conversion
CSc4820/6820 Computer Graphics Algorithms Ying Zhu Georgia State University Lecture 13 Clipping & Scan Conversion

2 Outline Clipping The rasterizer stage Line clipping Polygon clipping
Triangle setup Back-face culling Scan conversion

3 Clipping

4 Clipping Must eliminate objects outside viewing frustum
Only the primitives wholly or partially inside the viewing frustum need to be passed on to the rasterizer stage. Primitives (line or polygon) that are partially inside the view volume require clipping.

5 Location of clipping in the pipeline
Clipping operation can be placed in various places in the graphics pipeline Before perspective projection After perspective project but before perspective division After perspective division The second option is one usually implemented

6 Clipping in OpenGL pipeline

7 Clipping Against a Frustum
General case of frustum (perspective projection) Clipping is tricky because of frustum shape (truncated pyramid)

8 Clipping Against Canonical View Volume
Solution: Perspective projection transforms the perspective view volume (truncated pyramid) to a unit cube (often called the clip space) The 6 faces of the cube are called clipping planes These clipping planes are now orthogonal (perpendicular) to the X, Y, or Z axes of the view space Clipping against such a cube is more efficient than other frustum shapes.

9 Line-Segment Clipping
General case: 3D object against cube Simpler case: 2D line against square or rectangle Several practical algorithms: Avoid expensive line-rectangle intersection Cohen-Sutherland Clipping Liang-Barsky Clipping, etc.

10 Clipping Against Rectangle
Line-segment clipping: modify endpoints of lines to lie within clipping rectangle We can calculate intersections of line segments with clipping rectangle. But this is quite expensive.

11 Cohen-Sutherland Clipping
Clipping rectangle as intersection of 4 half-planes. Every line endpoint is assigned a 4 bit Region code. The appropriate bit is set depending on the location of the endpoint with respect to that clipping rectangle as shown below: Endpoint Left of rectangle then set bit 1 Endpoint Right of rectangle then set bit 2 Endpoint Below rectangle then set bit 3 Endpoint Above rectangle then set bit 4

12 Cohen-Sutherland Method
Can determine the bit code by testing the endpoints with clipping rectangle as follows: If x is less than Xrect_min then set bit 1 If x is greater than Xrect_max then set bit 2 If y is less than Yrect_min then set bit 3 If y is greater than Yrect_max then set bit 4

13 Cohen-Sutherland Method Example

14 Cohen-Sutherland Method
Trivial accept or reject: If both endpoints = 0000 (in window) then display line. If both endpoints have a bit set in same position (P7, P8) then the line is completely outside the window and is rejected. For the rest of the lines we must check for intersection with window. If point is to the left of window then compute intersection with Left window boundary. Do the same for Right, Bottom, and Top. Then re-compute region code and retest.

15 Cohen-Sutherland Method
The algorithm is as follows: Compute region code for endpoints Check for trivial accept or reject If step 2 unsuccessful then compute window intersections in order: Left, Right, Bottom, Top (only do 1) Repeat steps 1, 2,3 until all lines segments are within the rectangle.

16 Polygon Clipping: Sutherland-Hodgeman Method
Sub-problem: Input: polygon (vertex list) and single clip plane Output: new (clipped) polygon (vertex list) Compute each of the vertices against window edges. The inside vertices are saved for clipping against next boundary; outside vertices are discarded. Will output clipped polygon as vertex list

17 Sutherland-Hodgeman Method
There are four possible cases as we loop through the vertex list. Let s be the previous point and p be the next point. Check the transition of s-to-p In-to-in: output p In-to-out: output intersection i Out-to-in: output intersection I and vertex p Out-to-out: no output

18 Sutherland-Hodgeman Example
Input: p1, p2, p3 Output: i1, p2, i2 Clipping planes

19 Clipping Against Cube Can be derived from the 2D cases

20 Cohen-Sutherland Method in 3D
Use 6 bits in outcode Other calculations same as before

21 Clipping Once a triangle has been clipped, it must be re-tessellated
made into a new set of triangles.

22 Clipping Summary Clipping line segments against a rectangle
Avoid expensive multiplication and divisions Cohen-Sutherland or Liang-Barsky Clipping polygons against a rectangle Sutherland-Hodgeman method Clipping against a cube Extended from 2D clipping Basic ideas stay the same

23 The rasterizer stage Now we enter the rasterizer stage Triangle setup
Texture mapping Fog Translucency Test Depth buffering Antialiasing

24 Rasterizer stage

25 Rasterizer stage

26 Triangle setup Can be considered a phase between the geometry stage and the rasterizer stage, or part of the rasterizer stage Major tasks: Back-face culling (optional) Scan conversion

27 Back-face culling Back-face culling discards triangles that are facing away from the view camera since you won't be able to see them On average, half of a model's triangles will be facing away from the view camera at any given time This would save a lot of time The one exception is if the triangles in front of them are translucent or transparent Can be done in different points in the pipeline Before lighting in view space In 2D screen space

28 Determining the back face
Depends on the space where the tests are done. In view space, OpenGL looks at each triangle's normal. Measure the angle between the normal vector and the viewing vector If the angle is greater than 90º, the triangle is facing away from the camera, and can be discarded. If it's less than or equal to 90º, then it's visible to the view camera and cannot be thrown out.

29 View space back face test

30 Screen space back face test
Calculate normal for a projected triangle using cross product Determine if this normal points at the camera or away from the camera.

31 Screen space back face test
(if use right hand Rule)

32 How front (back) faces are determined in OpenGL?
Each triangle has two faces: front & back faces Use glFrontFace(GLenum mode) to tell OpenGL how to determine front (or back) face Mode: GL_CCW or GL_CW GL_CCW: triangles with counterclockwise oriented vertices are front face (right hand rule) GL_CW: triangles with clockwise oriented vertices are front face (left hand rule) The order you specify the triangle vertices determines its orientation

33 Control back (or front) face culling in OpenGL
glEnable(GL_CULL_FACE) glDisable(GL_CULL_FACE) Use glCullFace(GLenum mode) to tell OpenGL which face to cull Mode: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK GL_FRONT: cull front faces GL_BACK: cull back faces GL_FRONT_AND_BACK: cull all polygons

34 Scan conversion Also know as scan-line conversion
Up till now we are dealing with vertices of triangles For each triangle, the scan-line conversion operation starts with 3 vertices and returns with a set of pixels which fill that triangle First draw the three edges of the triangle Then fill the triangle with horizontal lines Thus the fundamental problem of scan conversion is how to draw lines

35 Scan-line conversion process
Input: 3 vertices, each with a (x, y) coordinate, a depth (z) value, a color, and a texture coordinate (s, t) Output: a set of pixels that fill the triangle, each with a (x, y) coordinate, depth (z) value, color, and texture coordinates (s, t)

36 Scan-line conversion process

37 Line rasterization

38 Bresenham Algorithm Bresenham Algorithm for 1st octant:

39 Bresenham Method See textbook or for a more detailed discussion

40 Line drawing demo program
DDA and Bresenham algorithm demo program (

41 Polygon Rasterization

42 Color interpolation for pixels
Bresenham algorithm returns the (x, y) coordinates of the pixels that form a line How to assign colors to these pixels? Color values are interpolated for each pixel Gouraud shading is performed at this point These values are interpolated using a weighted average of the color values of the two end vertices (e.g. obtained from lighting calculation) Interpolate them along the three edges and then along the horizontal lines

43 Depth value interpolation for pixels
Similarly, depth (Z) values are interpolated for each pixel These values are interpolated using a weighted average of the depth (Z) values of the edge's vertices Interpolate them along the three edges and then along the horizontal lines The depth value of a pixel is its distance from the eye (camera) Will be used in depth test

44 Color & depth interpolation
Interpolate color and depth along edges first Next interpolate color and depth along horizontal lines

45 Texture coordinates interpolation
Similar to color and depth values, the texture coordinates (s, t) are interpolated as well Texture coordinates (s, t) are specified for each vertex in your OpenGL program Will be used in texture mapping process Note that normals are generally not interpolated In the end, each pixel has a 2D window coordinate (x, y) a depth (Z) value (will be used in depth buffer test) a color value (obtained from Gouraud shading) a texture coordinate (will be used in texture mapping)

46 Summary Line-Segment Clipping Polygon Clipping Line drawing
Cohen-Sutherland Liang-Barsky Polygon Clipping Sutherland-Hodgeman Line drawing Bresenham’s algorithm Color, depth, texture coordinates interpolation These algorithms are usually implemented on graphics hardware OpenGL programmers have little control over clipping and scan conversion process No shader access at this point

47 Readings Demo program:

48 Next Lecture Texture mapping


Download ppt "Lecture 13 Clipping & Scan Conversion"

Similar presentations


Ads by Google