Rasterization, Depth Sorting and Culling

Similar documents
FROM VERTICES TO FRAGMENTS. Lecture 5 Comp3080 Computer Graphics HKBU

Topics. From vertices to fragments

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

Pipeline implementation II

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

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

Realtime 3D Computer Graphics Virtual Reality

From Vertices To Fragments-1

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

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

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

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

Introduction to Computer Graphics 5. Clipping

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

Clipping and Scan Conversion

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

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

Chapter 8: Implementation- Clipping and Rasterization

3D Rendering Pipeline (for direct illumination)

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

COMP371 COMPUTER GRAPHICS

Rendering. Basic Math Review. Rasterizing Lines and Polygons Hidden Surface Remove Multi-pass Rendering with Accumulation Buffers.

COMP30019 Graphics and Interaction Scan Converting Polygons and Lines

Computer Graphics. Bing-Yu Chen National Taiwan University

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

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

Hidden Surface Removal

UNIT -8 IMPLEMENTATION

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

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

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

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

Painter s HSR Algorithm

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

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

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

Rasterization: Geometric Primitives

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

Graphics (Output) Primitives. Chapters 3 & 4

Scan Conversion. Drawing Lines Drawing Circles

The Traditional Graphics Pipeline

Topic #1: Rasterization (Scan Conversion)

CSE528 Computer Graphics: Theory, Algorithms, and Applications

Line Rasterization and Antialiasing

The Traditional Graphics Pipeline

The Traditional Graphics Pipeline

Computer Graphics 7 - Rasterisation

CS475m - Computer Graphics. Lecture 1 : Rasterization Basics

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

Einführung in Visual Computing

CS 543: Computer Graphics. Rasterization

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

Rendering the Teapot 1

Computer Graphics. - Clipping - Philipp Slusallek & Stefan Lemme

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

CS 4731: Computer Graphics Lecture 21: Raster Graphics: Drawing Lines. Emmanuel Agu

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

Computer Graphics. Lecture 9 Hidden Surface Removal. Taku Komura

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

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

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Hidden surface removal. Computer Graphics

Computer Graphics and GPGPU Programming

Rasterization. COMP 575/770 Spring 2013

Institutionen för systemteknik

Computer Graphics II

Computer Graphics. - Rasterization - Philipp Slusallek

8. Hidden Surface Elimination

From 3D World to 2D Screen. Hendrik Speleers

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

OpenGL Graphics System. 2D Graphics Primitives. Drawing 2D Graphics Primitives. 2D Graphics Primitives. Mathematical 2D Primitives.

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

9. Visible-Surface Detection Methods

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

Werner Purgathofer

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

Spatial Data Structures and Speed-Up Techniques. Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology

Rasteriza2on and Clipping

CHAPTER 1 Graphics Systems and Models 3

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

Rasterization. CS4620 Lecture 13

Rendering approaches. 1.image-oriented. 2.object-oriented. foreach pixel... 3D rendering pipeline. foreach object...

CS 130 Exam I. Fall 2015

Chapter - 2: Geometry and Line Generations

2D rendering takes a photo of the 2D scene with a virtual camera that selects an axis aligned rectangle from the scene. The photograph is placed into

Pipeline and Rasterization. COMP770 Fall 2011

CS602 Midterm Subjective Solved with Reference By WELL WISHER (Aqua Leo)

Computer Graphics Geometry and Transform

CS4620/5620: Lecture 14 Pipeline

Rasterization. CS4620/5620: Lecture 12. Announcements. Turn in HW 1. PPA 1 out. Friday lecture. History of graphics PPA 1 in 4621.

From Ver(ces to Fragments: Rasteriza(on

Surface shading: lights and rasterization. Computer Graphics CSE 167 Lecture 6

EECS150 - Digital Design Lecture 15 - Video

Tópicos de Computação Gráfica Topics in Computer Graphics 10509: Doutoramento em Engenharia Informática. Chap. 2 Rasterization.

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

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

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

8. Hidden Surface Elimination

EECS150 - Digital Design Lecture 12 - Video Interfacing. MIPS150 Video Subsystem

Computer Graphics: 8-Hidden Surface Removal

Transcription:

Rasterization, Depth Sorting and Culling

Rastrerzation How can we determine which pixels to fill?

Reading Material These slides OH 17-26, OH 65-79 and OH 281-282, by Magnus Bondesson You may also read chapter 7 of course book, Angel, Interactive Computer Graphics A Top Down Approach Let s start with Line Drawing Algorithms DDA Bresenham

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 = kx + m k = Δy Δx

DDA Algorithm Digital Differential Analyzer DDA was a mechanical device for numerical solution of differential equations Line y=kx+ m satisfies differential equation dy/dx = k = Δy/Δx = y 2 -y 1 /x 2 -x 1 Along scan line Δx = 1 y=y1; For(x=x1; x<=x2,ix++) { write_pixel(x, round(y), line_color) y+=k; }

Problem DDA = for each x plot pixel at closest y Problems for steep lines

Using Symmetry Use for 1 k 0 For k > 1, swap role of x and y For each y, plot closest x

The problem with DDA is that it uses floats which was slow in the old days Bresenhams algorithm only uses integers

Bresenham s line drawing algorithm The line is drawn between two points (x 0, y 0 ) and (x 1, y 1 ) k = ( y ( x Slope 1 0 (y = kx + m) 1 y x 0 ) ) Each time we step 1 in x-direction, we should increment y with k. Otherwise the error in y increases with k. If the error surpasses 0.5, the line has become closer to the next y- value, so we add 1 to y simultaneously decreasing the error by 1 function line(x0, x1, y0, y1) int deltax := abs(x1 - x0) int deltay := abs(y1 - y0) real error := 0 real deltaerr := deltay / deltax int y := y0 for x from x0 to x1 plot(x,y) error := error + deltaerr if error 0.5 y := y + 1 error := error - 1.0 See also http://en.wikipedia.org/wiki/bresenham's_line_algorithm Ulf Assarsson 2006

Bresenham s line drawing algorithm Now, convert algorithm to only using integer computations The trick we use is to multiply all the fractional numbers above by (x 1, x 0 ), which enables us to express them as integers. The only problem remaining is the constant 0.5 to deal with this, we multiply both sides of the inequality by 2 Old float version: function line(x0, x1, y0, y1) int deltax := abs(x1 - x0) int deltay := abs(y1 - y0) real error := 0 real deltaerr := deltay / deltax int y := y0 for x from x0 to x1 plot(x,y) error := error + deltaerr if error 0.5 y := y + 1 error := error - 1.0 New integer version: function line(x0, x1, y0, y1) int deltax := abs(x1 - x0) int deltay := abs(y1 - y0) real error := 0 real deltaerr := deltay int y := y0 for x from x0 to x1 plot(x,y) error := error + deltaerr if 2*error deltax y := y + 1 error := error - deltax Ulf Assarsson 2006

Complete Bresenham s line drawing algorithm function line(x0, x1, y0, y1) boolean steep := abs(y1 - y0) > abs(x1 - x0) if steep then swap(x0, y0) swap(x1, y1) if x0 > x1 then swap(x0, x1) swap(y0, y1) int deltax := x1 - x0 int deltay := abs(y1 - y0) int error := 0 int ystep int y := y0 if y0 < y1 then ystep := 1 else ystep := -1 for x from x0 to x1 if steep then plot(y,x) else plot(x,y) error := error + deltay if 2 error deltax y := y + ystep error := error - deltax The first case is allowing us to draw lines that still slope downwards, but head in the opposite direction. I.e., swapping the initial points if x0 > x1. To draw lines that go up, we check if y0 >= y1; if so, we step y by -1 instead of 1. To be able to draw lines with a slope less than one, we take advantage of the fact that a steep line can be reflected across the line y=x to obtain a line with a small slope. The effect is to switch the x and y variables. Ulf Assarsson 2006

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

Winding Number Count clockwise encirclements of point winding number = 1 winding number = 2 Alternate definition of inside: inside if winding number 0

Rasterizing a Triangle 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

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

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); } }

Scan Line Fill Set active edges to AB and AC For y = A.y, A.y-1,...,C.y If y=b.y exchange AB with BC Compute xstart and xend. Interpolate color, depth, texcoords etc for points (xstart,y) and (xend,y) For x = xstart, xstart+1,...,xend Compute color, depth etc for (x,y) using interpolation. xend This is the modern way to rasterize a triangle

Aliasing Ideal rasterized line should be 1 pixel wide Choosing best y for each x (or visa versa) produces aliased raster lines

Antialiasing by Area Averaging Color multiple pixels for each x depending on coverage by ideal line original antialiased magnified

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

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 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

Pipeline Clipping of Polygons Three dimensions: add front and back clippers Strategy used in SGI Geometry Engine Small increase in latency

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

Bounding boxes Can usually determine accept/reject based only on bounding box reject accept requires detailed clipping

Hidden Surface Removal Object-space approach: use pairwise testing between polygons (objects) partially obscuring can draw independently Worst case complexity O(n log n) for n polygons

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 Requires ordering of polygons first O(n log n) calculation for ordering Not every polygon is either in front or behind all other polygons

Hard Cases Overlap in x, y, z, but one object is fully on one side of the other cyclic overlap penetration

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

Polygon-aligned BSP tree Used for visibility and occlusion/depth testing (BSP=Binary Space Partitioning) Allows exact sorting Each node stores: Polygon (triangle) The splitting plane (defined by the triangle) Front and back subtree Ulf Assarsson 2006

Algorithm for BSP trees Tree SkapaBSP(PolygonLista L) { Om L tom returnera ett tomt träd; Annars: Välj en polygon P i listan. Bilda en lista B med de polygoner som ligger bakom P och en annan H med övriga. Returnera ett träd med P som rot och SkapaBSP(B) och SkapaBSP(H) som vänsterbarn respektive högerbarn. } Uppritningsteget (kolla även om trädet tomt! Fick ej plats i koden): void RitaBSP(Tree t) { Om observatören hitom roten i t: RitaBSP(t:s vänsterbarn); Rita polygonen i t:s rot; RitaBSP(t:s högerbarn); Annars: RitaBSP(t:s högerbarn); Rita polygonen i t:s rot; RitaBSP(t:s vänsterbarn); } Ulf Assarsson 2006

Different culling techniques (red objects are skipped) view frustum detail backface portal occlusion Tomas Akenine-Mőller 2002

Bonus Material Following slides are not part of the course but could be interesting for the curious students

Clipping 2D Line Segments Brute force approach: compute intersections with all sides of clipping window Inefficient: one division per intersection

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

The Cases Case 1: both endpoints of line segment inside all four lines Draw (accept) line segment as is y = y max x = x min x = x max y = y min Case 2: both endpoints outside all lines and on same side of a line Discard (reject) the line segment

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

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

Using Outcodes Consider the 5 cases below AB: outcode(a) = outcode(b) = 0 Accept line segment

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 interesections

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

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

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

Cohen Sutherland in 3D Use 6-bit outcodes When needed, clip line segment against planes

Liang-Barsky Clipping Consider the parametric form of a line segment p(α) = (1-α)p 1 + αp 2 1 α 0 p 1 p 2 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

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

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