Lecture 13 Clipping & Scan Conversion

Slides:



Advertisements
Similar presentations
Computer Graphics - Rasterization -
Advertisements

Rezanje črt in poligonov. World window & viewport window viewport screen window world window.
Computer Graphics CLIPPING.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
Graphics Pipeline.
10/10/02 (c) 2002 University of Wisconsin, CS 559 Last Time Finished viewing: Now you know how to: –Define a region of space that you wish to view – the.
CHAPTER 12 Height Maps, Hidden Surface Removal, Clipping and Level of Detail Algorithms © 2008 Cengage Learning EMEA.
Computer Graphics Viewing.
CMPE 466 COMPUTER GRAPHICS Chapter 8 2D Viewing Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition by Donald Hearn,
Introduction to Computer Graphics Chapter 6 – 2D Viewing Pt 2 1.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner Interpolation Clipping.
Informationsteknologi Thursday, November 22, 2007Computer Graphics - Class 111 Today’s class Clipping Parametric and point-normal form of lines Intersecting.
Implementation Dr. Amy Zhang. Reading 2  Hill, Chapters  Hill, Chapter 10.
Vertices and Fragments I CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Course Website: Computer Graphics 9: Clipping In 3D.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Clipping.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Implementation I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Graphics Pipeline Clipping CMSC 435/634. Graphics Pipeline Object-order approach to rendering Sequence of operations – Vertex processing – Transforms.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
CS 325 Introduction to Computer Graphics 03 / 08 / 2010 Instructor: Michael Eckmann.
Objectives Introduce basic implementation strategies Clipping Scan conversion.
CS 480/680 Computer Graphics Shading in OpenGL Dr. Frederick C Harris, Jr. Fall 2013.
CSE Real Time Rendering Week 9. Post Geometry Shaders Courtesy: E. Angel and D. Shreiner – Interactive Computer Graphics 6E © Addison-Wesley 2012.
Windows, Viewports, and Clipping
1 Visiblity: Culling and Clipping Computer Graphics COMP 770 (236) Spring 2009 January 21 & 26: 2009.
Implementation of a Renderer Consider Programs are processd by the system line & polygon, outside the view volume Efficiently Understanding of the implementation.
Rendering Pipeline Fall, D Polygon Rendering Many applications use rendering of 3D polygons with direct illumination.
Computer Graphics Lecture 20 Fasih ur Rehman. Last Class Clipping – What is clipping – Why we do clipping – How clipping is done.
1Computer Graphics Implementation 1 Lecture 15 John Shearer Culture Lab – space 2
Graphics Graphics & Graphical Programming Lecture 23 - Viewing & Clipping.
Computer Graphics Viewing. 2 of 30 Viewing in 2D Window in world coordinates. 45  250  Viewport in Device coords 250 x 250 Pixels.
Lecture 9 From Vertices to Fragments. Objectives Introduce basic implementation strategies Clipping Rasterization hidden-surface removal.
Chapter 71 Computer Graphics - Chapter 7 From Vertices to Fragments Objectives are: How your program are processed by the system that you are using, Learning.
CENG 538 Advanced Graphics and UIs
Three Dimensional Viewing
Computer Graphics Lecture 14 CLIPPING I Taqdees A. Siddiqi
Computer Graphics Clipping.
Rendering Pipeline Fall, 2015.
- Introduction - Graphics Pipeline
Hidden Surface Removal
Computer Graphics CC416 Week 13 Clipping.
Transformations contd.
Computer Graphics Shading in OpenGL
Clipping Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
Computer Graphics Viewing
Concepts, algorithms for clipping
3D Clipping.
The Graphics Rendering Pipeline
CS451Real-time Rendering Pipeline
Implementation I Ed Angel
Graphics Pipeline Clipping
Computer Graphics 9: Clipping In 3D
3D Rendering Pipeline Hidden Surface Removal 3D Primitives
WINDOWING AND CLIPPING
Computer Graphics : Viewing In 2D
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
CSCE 441 Computer Graphics: Clipping Lines Jinxiang Chai
WINDOWING AND CLIPPING
Chapter V Vertex Processing
Chapter VII Rasterizer
CSCE 441 Computer Graphics: Clipping Polygons Jinxiang Chai
Joshua Barczak CMSC 435 UMBC
© University of Wisconsin, CS559 Fall 2004
(c) 2002 University of Wisconsin, CS559
Clipping Clipping Sutherland-Hodgman Clipping
Computer Graphics Viewing. 2 of 30 Viewing in 2D Window in world coordinates. 45  250  Viewport in Device coords 250 x 250 Pixels.
Clipping University of British Columbia CPSC 314 Computer Graphics
Implementation I Ed Angel Professor Emeritus of Computer Science
COMPUTER GRAPHICS Clipping
Presentation transcript:

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

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

Clipping

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.

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

Clipping in OpenGL pipeline

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

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.

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.

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.

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

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

Cohen-Sutherland Method Example

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.

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.

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

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

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

Clipping Against Cube Can be derived from the 2D cases

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

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

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

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

Rasterizer stage

Rasterizer stage

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

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

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.

View space back face test

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.

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

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

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

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

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)

Scan-line conversion process

Line rasterization

Bresenham Algorithm Bresenham Algorithm for 1st octant:

Bresenham Method See textbook or http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html for a more detailed discussion

Line drawing demo program DDA and Bresenham algorithm demo program (http://www.siggraph.org/education/materials/HyperGraph/scanline/outprims/drawline_java/drawline.html)

Polygon Rasterization

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

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

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

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)

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

Readings Demo program: http://www.siggraph.org/education/materials/HyperGraph/scanline/outprims/drawline_java/drawline.html

Next Lecture Texture mapping