FROM VERTICES TO FRAGMENTS. Lecture 5 Comp3080 Computer Graphics HKBU

Similar documents
Overview. Pipeline implementation I. Overview. Required Tasks. Preliminaries Clipping. Hidden Surface removal

Clipping. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Topics. From vertices to fragments

Pipeline implementation II

Implementation III. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Realtime 3D Computer Graphics Virtual Reality

CS452/552; EE465/505. Clipping & Scan Conversion

From Vertices To Fragments-1

Rasterization, Depth Sorting and Culling

Incremental Form. Idea. More efficient if we look at d k, the value of the decision variable at x = k

Renderer Implementation: Basics and Clipping. Overview. Preliminaries. David Carr Virtual Environments, Fundamentals Spring 2005

Clipping and Scan Conversion

Introduction to Computer Graphics 5. Clipping

Fall CSCI 420: Computer Graphics. 7.1 Rasterization. Hao Li.

CSCI 420 Computer Graphics Lecture 14. Rasterization. Scan Conversion Antialiasing [Angel Ch. 6] Jernej Barbic University of Southern California

COMP371 COMPUTER GRAPHICS

Rasterization. Rasterization (scan conversion) Digital Differential Analyzer (DDA) Rasterizing a line. Digital Differential Analyzer (DDA)

3D Rendering Pipeline (for direct illumination)

Rasterization Computer Graphics I Lecture 14. Scan Conversion Antialiasing Compositing [Angel, Ch , ]

CHAPTER 1 Graphics Systems and Models 3

Hidden Surface Removal

Painter s HSR Algorithm

CSE528 Computer Graphics: Theory, Algorithms, and Applications

COMP30019 Graphics and Interaction Scan Converting Polygons and Lines

Computer Graphics (CS 543) Lecture 10: Rasterization and Antialiasing

Examples. Clipping. The Rendering Pipeline. View Frustum. Normalization. How it is done. Types of operations. Removing what is not seen on the screen

Identifying those parts of a scene that are visible from a chosen viewing position, and only process (scan convert) those parts

CSE328 Fundamentals of Computer Graphics: Concepts, Theory, Algorithms, and Applications

Computer Graphics. - Rasterization - Philipp Slusallek

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

Computer Graphics. Bing-Yu Chen National Taiwan University The University of Tokyo

Page 1. Area-Subdivision Algorithms z-buffer Algorithm List Priority Algorithms BSP (Binary Space Partitioning Tree) Scan-line Algorithms

VISIBILITY & CULLING. Don t draw what you can t see. Thomas Larsson, Afshin Ameri DVA338, Spring 2018, MDH

Chapter 8: Implementation- Clipping and Rasterization

CS4620/5620: Lecture 14 Pipeline

Hidden surface removal. Computer Graphics

From Ver(ces to Fragments: Rasteriza(on

Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology

8. Hidden Surface Elimination

Computer Graphics. Bing-Yu Chen National Taiwan University

Announcements. Midterms graded back at the end of class Help session on Assignment 3 for last ~20 minutes of class. Computer Graphics

EECE 478. Learning Objectives. Learning Objectives. Rasterization & Scenes. Rasterization. Compositing

The Traditional Graphics Pipeline

Einführung in Visual Computing

Today. CS-184: Computer Graphics. Lecture #10: Clipping and Hidden Surfaces. Clipping. Hidden Surface Removal

Topic #1: Rasterization (Scan Conversion)

Computer Graphics and GPGPU Programming

S U N G - E U I YO O N, K A I S T R E N D E R I N G F R E E LY A VA I L A B L E O N T H E I N T E R N E T

QUESTION BANK 10CS65 : COMPUTER GRAPHICS AND VISUALIZATION

Rasterization: Geometric Primitives

Graphics and Interaction Rendering pipeline & object modelling

CSE 167: Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

The Traditional Graphics Pipeline

Visibility: Z Buffering

Computer Graphics. - Clipping - Philipp Slusallek & Stefan Lemme

The Traditional Graphics Pipeline

Rendering the Teapot 1

Graphics (Output) Primitives. Chapters 3 & 4

Computer Graphics. Lecture 9 Hidden Surface Removal. Taku Komura

Werner Purgathofer

Computer Graphics Geometry and Transform

Clipping & Culling. Lecture 11 Spring Trivial Rejection Outcode Clipping Plane-at-a-time Clipping Backface Culling

From 3D World to 2D Screen. Hendrik Speleers

9. Visible-Surface Detection Methods

From Vertices to Fragments: Rasterization. Reading Assignment: Chapter 7. Special memory where pixel colors are stored.

2D Image Synthesis. 2D image synthesis. Raster graphics systems. Modeling transformation. Vectorization. u x u y 0. o x o y 1

Computer Graphics (CS 543) Lecture 9 (Part 2): Clipping. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Two-Dimensional Viewing. Chapter 6

Scan Conversion. Drawing Lines Drawing Circles

Interactive Computer Graphics A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL

8. Hidden Surface Elimination

Computer Graphics. Rendering. by Brian Wyvill University of Calgary. cpsc/enel P 1

Line Drawing. Foundations of Computer Graphics Torsten Möller

Visible-Surface Detection Methods. Chapter? Intro. to Computer Graphics Spring 2008, Y. G. Shin

UNIT -8 IMPLEMENTATION

Line Drawing. Introduction to Computer Graphics Torsten Möller / Mike Phillips. Machiraju/Zhang/Möller

Rasterization, or What is glbegin(gl_lines) really doing?

Hidden Surfaces II. Week 9, Mon Mar 15

Last Time: Acceleration Data Structures for Ray Tracing. Schedule. Today. Shadows & Light Sources. Shadows

CEng 477 Introduction to Computer Graphics Fall 2007

Two basic types: image-precision and object-precision. Image-precision For each pixel, determine which object is visable Requires np operations

CS 498 VR. Lecture 18-4/4/18. go.illinois.edu/vrlect18

Institutionen för systemteknik

Rasterization. CS 4620 Lecture Kavita Bala w/ prior instructor Steve Marschner. Cornell CS4620 Fall 2015 Lecture 16

Computing Visibility. Backface Culling for General Visibility. One More Trick with Planes. BSP Trees Ray Casting Depth Buffering Quiz

Last class. A vertex (w x, w y, w z, w) - clipping is in the - windowing and viewport normalized view volume if: - scan conversion/ rasterization

Direct Rendering of Trimmed NURBS Surfaces

Pipeline Operations. CS 4620 Lecture 10

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

EF432. Introduction to spagetti and meatballs

CSCI 4620/8626. Computer Graphics Clipping Algorithms (Chapter 8-5 )

Computer Graphics I Lecture 11

Visible Surface Detection Methods

Pipeline and Rasterization. COMP770 Fall 2011

CSE 167: Introduction to Computer Graphics Lecture #9: Visibility. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2018

CS 543: Computer Graphics. Rasterization

INFOGR Computer Graphics. J. Bikker - April-July Lecture 11: Visibility. Welcome!

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

Transcription:

FROM VERTICES TO FRAGMENTS Lecture 5 Comp3080 Computer Graphics HKBU

OBJECTIVES Introduce basic implementation strategies Clipping Scan conversion OCTOBER 9, 2011 2

OVERVIEW At end of the geometric pipeline, vertices have been assembled into primitives Must clip out primitives that are outside the view frustum Algorithms based on representing primitives by lists of vertices Must find which pixels can be affected by each primitive Fragment generation Rasterization or scan conversion OCTOBER 9, 2011 3

REQUIRED TASKS Clipping Rasterization or scan conversion Some tasks deferred until fragment processing Hidden surface removal Antialiasing Transformations OCTOBER 9, 2011 4

RASTERIZATION META ALGORITHMS Consider two approaches to rendering a scene with opaque objects For every pixel, determine which object that projects on the pixel is closest to the viewer and compute the shade of this pixel Ray tracing paradigm For every object, determine which pixels it covers and shade these pixels Pipeline approach Must keep track of depths OCTOBER 9, 2011 5

CLIPPING 2D against clipping window Easy for line segments polygons 3D against clipping volume Hard for curves and text Convert to lines and polygons first OCTOBER 9, 2011 6

CLIPPING 2D LINE SEGMENTS Brute force approach: compute intersections with all sides of clipping window Inefficient: one division per intersection OCTOBER 9, 2011 7

COHEN-SUTHERLAND ALGORITHM Idea: eliminate as many cases as possible without computing intersections Start with four lines that determine the sides of the clipping window y = y max x = x min x = x max y = y min OCTOBER 9, 2011 8

THE CASES Case 1: Both endpoints of line segment inside all four lines Draw (accept) line segment as is Case 2: Both endpoints outside all lines and on same side of a line Discard (reject) the line segment y = y max x = x min x = x max y = y min OCTOBER 9, 2011 9

THE CASES Case 3: One endpoint inside, one outside Must do at least one intersection Case 4: Both outside May have part inside Must do at least one intersection y = y max x = x min x = x max OCTOBER 9, 2011 10

DEFINING OUTCODES For each endpoint, define an outcode b 0 b 1 b 2 b 3 b 0 = 1 if y > y max, 0 otherwise b 1 = 1 if y < y min, 0 otherwise b 2 = 1 if x > x max, 0 otherwise b 3 = 1 if x < x min, 0 otherwise Outcodes divide space into 9 regions Computation of outcode requires at most 4 subtractions OCTOBER 9, 2011 11

USING OUTCODES Consider the 5 cases below AB: outcode(a) = outcode(b) = 0 Accept line segment OCTOBER 9, 2011 12

USING OUTCODES CD: outcode (C) = 0, outcode(d) 0 Compute intersection Location of 1 in outcode(d) determines which edge to intersect with Note if there were a segment from A to a point in a region with 2 ones in outcode, we might have to do two intersections OCTOBER 9, 2011 13

USING OUTCODES EF: outcode(e) logically ANDed with outcode(f) (bitwise) 0 Both outcodes have a 1 bit in the same place Line segment is outside of corresponding side of clipping window reject OCTOBER 9, 2011 14

USING OUTCODES GH and IJ: same outcodes, neither zero but logical AND yields zero Shorten line segment by intersecting with one of sides of window Compute outcode of intersection (new endpoint of shortened line segment) Reexecute algorithm OCTOBER 9, 2011 15

EFFICIENCY In many applications, the clipping window is small relative to the size of the entire data base Most line segments are outside one or more side of the window and can be eliminated based on their outcodes Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step OCTOBER 9, 2011 16

COHEN SUTHERLAND IN 3D Use 6-bit outcodes When needed, clip line segment against planes OCTOBER 9, 2011 17

LIANG-BARSKY CLIPPING Consider the parametric form of a line segment p(α) = (1 - α)p 1 + αp 2 1 α 0 p 2 p 1 We can distinguish between the cases by looking at the ordering of the values of α where the line determined by the line segment crosses the lines that determine the window OCTOBER 9, 2011 18

LIANG-BARSKY CLIPPING In (a): α 4 > α 3 > α 2 > α 1 Intersect right, top, left, bottom: shorten In (b): α 4 > α 2 > α 3 > α 1 Intersect right, left, top, bottom: reject OCTOBER 9, 2011 19

ADVANTAGES Can accept/reject as easily as with Cohen-Sutherland Using values of α, we do not have to use algorithm recursively as with C-S Extends to 3D OCTOBER 9, 2011 20

CLIPPING AND NORMALIZATION General clipping in 3D requires intersection of line segments against arbitrary plane Example: oblique view OCTOBER 9, 2011 21

PLANE-LINE INTERSECTIONS a n n ( ( p p o 2 p p 1 1 ) ) OCTOBER 9, 2011 22

NORMALIZED FORM top view before normalization after normalization Normalization is part of viewing (pre clipping) but after normalization, we clip against sides of right parallelepiped Typical intersection calculation now requires only a floating point subtraction, e.g. is x > x max? OCTOBER 9, 2011 23

POLYGON CLIPPING AND HIDDEN SURFACE REMOVAL OCTOBER 9, 2011 24

POLYGON CLIPPING Not as simple as line segment clipping Clipping a line segment yields at most one line segment Clipping a polygon can yield multiple polygons However, clipping a convex polygon can yield at most one other polygon OCTOBER 9, 2011 25

TESSELLATION AND CONVEXITY One strategy is to replace nonconvex (concave) polygons with a set of triangular polygons (a tessellation) Also makes fill easier Tessellation code in GLU library OCTOBER 9, 2011 26

CLIPPING AS A BLACK BOX Can consider line segment clipping as a process that takes in two vertices and produces either no vertices or the vertices of a clipped line segment OCTOBER 9, 2011 27

PIPELINE CLIPPING OF LINE SEGMENTS Clipping against each side of window is independent of other sides Can use four independent clippers in a pipeline OCTOBER 9, 2011 28

PIPELINE CLIPPING OF POLYGONS Three dimensions: add front and back clippers Strategy used in SGI Geometry Engine Small increase in latency OCTOBER 9, 2011 29

BOUNDING BOXES Rather than doing clipping on a complex polygon, we can use an axis-aligned bounding box or extent Smallest rectangle aligned with axes that encloses the polygon Simple to compute: max and min of x and y OCTOBER 9, 2011 30

BOUNDING BOXES Can usually determine accept/reject based only on bounding box accept reject requires detailed clipping OCTOBER 9, 2011 31

CLIPPING AND VISIBILITY Clipping has much in common with hidden-surface removal In both cases, we are trying to remove objects that are not visible to the camera Often we can use visibility or occlusion testing early in the process to eliminate as many polygons as possible before going through the entire pipeline OCTOBER 9, 2011 32

HIDDEN SURFACE REMOVAL Object-space approach: use pairwise testing between polygons (objects) partially obscuring can draw independently Worst case complexity O(n 2 ) for n polygons OCTOBER 9, 2011 33

PAINTER S ALGORITHM Render polygons a back to front order so that polygons behind others are simply painted over B behind A as seen by viewer Fill B then A OCTOBER 9, 2011 34

DEPTH SORT Requires ordering of polygons first O(n log n) calculation for ordering Not every polygon is either in front or behind all other polygons Order polygons and deal with easy cases first, harder later Polygons sorted by distance from COP OCTOBER 9, 2011 35

EASY CASES A lies behind all other polygons Can render Polygons overlap in z but not in either x or y Can render independently OCTOBER 9, 2011 36

HARD CASES Overlap in all directions but can one is fully on one side of the other cyclic overlap penetration OCTOBER 9, 2011 37

BACK-FACE REMOVAL (CULLING) Face is visible iff 90 θ -90 equivalently cosθ 0 or v n 0 Plane of face has form ax + by + cz + d = 0 but after normalization n = ( 0 0 1 0 ) T Need only test the sign of c In OpenGL we can simply enable culling but may not work correctly if we have nonconvex objects OCTOBER 9, 2011 38

IMAGE SPACE APPROACH Look at each projector (nm for an n m frame buffer) and find closest of k polygons Complexity O(nmk) Ray tracing z-buffer OCTOBER 9, 2011 39

Z-BUFFER ALGORITHM Use a buffer called the z or depth buffer to store the depth of the closest object at each pixel found so far As we render each polygon, compare the depth of each pixel to depth in z buffer If less, place shade of pixel in color buffer and update z buffer OCTOBER 9, 2011 40

EFFICIENCY If we work scan line by scan line as we move across a scan line, the depth changes satisfy a x + b y + c z = 0 Along scan line y = 0 z = - a c x In screen space x = 1 OCTOBER 9, 2011 41

SCAN-LINE ALGORITHM Can combine shading and hsr through scan line algorithm scan line i: no need for depth information, can only be in no or one polygon scan line j: need depth information only when in more than one polygon OCTOBER 9, 2011 42

IMPLEMENTATION Need a data structure to store Flag for each polygon (inside/outside) Incremental structure for scan lines that stores which edges are encountered Parameters for planes OCTOBER 9, 2011 43

VISIBILITY TESTING In many realtime applications, such as games, we want to eliminate as many objects as possible within the application Reduce burden on pipeline Reduce traffic on bus Partition space with Binary Spatial Partition (BSP) Tree OCTOBER 9, 2011 44

SIMPLE EXAMPLE consider 6 parallel polygons top view The plane of A separates B and C from D, E and F OCTOBER 9, 2011 45

BSP TREE Can continue recursively Plane of C separates B from A Plane of D separates E and F Can put this information in a BSP tree Use for visibility and occlusion testing OCTOBER 9, 2011 46

LINE DRAWING ALGORITHMS AND ANTIALIASING OCTOBER 9, 2011 47

RASTERIZATION Rasterization (scan conversion) Determine which pixels that are inside primitive specified by a set of vertices Produces a set of fragments Fragments have a location (pixel location) and other attributes such color and texture coordinates that are determined by interpolating values at vertices Pixel colors determined later using color, texture, and other vertex properties OCTOBER 9, 2011 48

SCAN CONVERSION OF LINE SEGMENTS Start with line segment in window coordinates with integer values for endpoints Assume implementation has a write_pixel function y m x y = mx + h OCTOBER 9, 2011 49

DDA ALGORITHM Digital Differential Analyzer DDA was a mechanical device for numerical solution of differential equations Line y = mx + h satisfies differential equation dy/dx = m = Dy/Dx = (y 2 - y 1 )/(x 2 - x 1 ) Along scan line Dx = 1 For(x = x1; x <= x2, ix++) { y += m; write_pixel(x, round(y), line_color) } OCTOBER 9, 2011 50

PROBLEM DDA = for each x plot pixel at closest y Problems for steep lines OCTOBER 9, 2011 51

USING SYMMETRY Use for 1 m 0 For m > 1, swap role of x and y For each y, plot closest x OCTOBER 9, 2011 52

BRESENHAM S ALGORITHM DDA requires one floating point addition per step Assume pixel centers are at half integers We can eliminate all fp through Bresenham s algorithm Consider only 1 m 0 Other cases by symmetry If we start at a pixel that has been written, there are only two candidates for the next pixel to be written into the frame buffer OCTOBER 9, 2011 53

CANDIDATE PIXELS 1 m 0 candidates last pixel Note that line could have passed through any part of this pixel OCTOBER 9, 2011 54

DECISION VARIABLE d = Dx(b - a) d is an integer d > 0 use upper pixel d < 0 use lower pixel OCTOBER 9, 2011 55

INCREMENTAL FORM More efficient if we look at d k, the value of the decision variable at x = k d k+1 = d k 2Dy, if d k < 0 d k+1 = d k 2(Dy - Dx), otherwise For each x, we need do only an integer addition and a test Single instruction on graphics chips OCTOBER 9, 2011 56

POLYGON SCAN CONVERSION Scan Conversion = Fill How to tell inside from outside Convex easy Nonsimple difficult Odd even test Count edge crossings Winding number odd-even fill OCTOBER 9, 2011 57

WINDING NUMBER Count clockwise encirclements of point winding number = 1 winding number = 2 Alternate definition of inside: inside if winding number 0 OCTOBER 9, 2011 58

FILLING IN THE FRAME BUFFER Fill at end of pipeline Convex Polygons only Nonconvex polygons assumed to have been tessellated Shades (colors) have been computed for vertices (Gouraud shading) Combine with z-buffer algorithm March across scan lines interpolating shades Incremental work small OCTOBER 9, 2011 59

USING INTERPOLATION C 1 C 2 C 3 specified by glcolor or by vertex shading C 4 determined by interpolating between C 1 and C 2 C 5 determined by interpolating between C 2 and C 3 Interpolate between C 4 and C 5 along span C 1 scan line C 4 C 2 C 5 span C 3 OCTOBER 9, 2011 60

FLOOD FILL Fill can be done recursively if we know a seed point located inside (WHITE) Scan convert edges into buffer in edge/inside color (BLACK) flood_fill(int x, int y) { if(read_pixel(x, y) == WHITE) { write_pixel(x, y, BLACK); flood_fill(x-1, y); flood_fill(x+1, y); flood_fill(x, y+1); flood_fill(x, y-1); } } OCTOBER 9, 2011 61

SCAN LINE FILL Can also fill by maintaining a data structure of all intersections of polygons with scan lines Sort by scan line Fill each span vertex order generated by vertex list desired order OCTOBER 9, 2011 62

DATA STRUCTURE OCTOBER 9, 2011 63

ALIASING Ideal rasterized line should be 1 pixel wide Choosing best y for each x (or visa versa) produces aliased raster lines OCTOBER 9, 2011 64

ANTIALIASING BY AREA AVERAGING Color multiple pixels for each x depending on coverage by ideal line original antialiased magnified OCTOBER 9, 2011 65

POLYGON ALIASING Aliasing problems can be serious for polygons Jaggedness of edges Small polygons neglected Need compositing so color of one polygon does not totally determine color of pixel All three polygons should contribute to color OCTOBER 9, 2011 66