Drawing a Triangle (and an Intro to Sampling)

Similar documents
Drawing a Triangle (and an introduction to sampling)

Parallel Triangle Rendering on a Modern GPU

Lecture 4: Visibility. (coverage and occlusion using rasterization and the Z-buffer) Visual Computing Systems CMU , Fall 2014

Lecture 2: Digital Drawing. Computer Graphics and Imaging UC Berkeley CS184/284

Theoretically Perfect Sensor

Theoretically Perfect Sensor

Sampling, Aliasing, & Mipmaps

Sampling, Aliasing, & Mipmaps

Lecture 6 Basic Signal Processing

Computer Graphics. Sampling Theory & Anti-Aliasing. Philipp Slusallek

Aliasing and Antialiasing. ITCS 4120/ Aliasing and Antialiasing

Sampling, Aliasing, & Mipmaps

Outline. Foundations of Computer Graphics (Spring 2012)

Sampling and Reconstruction

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

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

Outline. Sampling and Reconstruction. Sampling and Reconstruction. Foundations of Computer Graphics (Fall 2012)

Aliasing. Can t draw smooth lines on discrete raster device get staircased lines ( jaggies ):

0. Introduction: What is Computer Graphics? 1. Basics of scan conversion (line drawing) 2. Representing 2D curves

Sampling and Monte-Carlo Integration

Advanced Computer Graphics. Aliasing. Matthias Teschner. Computer Science Department University of Freiburg

Motivation. The main goal of Computer Graphics is to generate 2D images. 2D images are continuous 2D functions (or signals)

Basics. Sampling and Reconstruction. Sampling and Reconstruction. Outline. (Spatial) Aliasing. Advanced Computer Graphics (Fall 2010)

Reading. 2. Fourier analysis and sampling theory. Required: Watt, Section 14.1 Recommended:

To Do. Advanced Computer Graphics. Sampling and Reconstruction. Outline. Sign up for Piazza

Computer Graphics. Texture Filtering & Sampling Theory. Hendrik Lensch. Computer Graphics WS07/08 Texturing

The Rasterization Pipeline

Aliasing And Anti-Aliasing Sampling and Reconstruction

The main goal of Computer Graphics is to generate 2D images 2D images are continuous 2D functions (or signals)

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

Fourier analysis and sampling theory

Computer Graphics. - Rasterization - Philipp Slusallek

CPSC 425: Computer Vision

Topics and things to know about them:

EF432. Introduction to spagetti and meatballs

EF432. Introduction to spagetti and meatballs

Reading. 18. Projections and Z-buffers. Required: Watt, Section , 6.3, 6.6 (esp. intro and subsections 1, 4, and 8 10), Further reading:

Topic 0. Introduction: What Is Computer Graphics? CSC 418/2504: Computer Graphics EF432. Today s Topics. What is Computer Graphics?

Anti-aliasing and sampling

Perspective Projection and Texture Mapping

CS354 Computer Graphics Sampling and Aliasing

Acknowledgement: Images and many slides from presentations by Mark J. Kilgard and other Nvidia folks, from slides on developer.nvidia.

Today. Acceleration Data Structures for Ray Tracing. Cool results from Assignment 2. Last Week: Questions? Schedule

Parallelizing Graphics Pipeline Execution (+ Basics of Characterizing a Rendering Workload)

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

The simplest and most obvious method to go from a continuous to a discrete image is by point sampling,

COMP371 COMPUTER GRAPHICS

Today. Anti-aliasing Surface Parametrization Soft Shadows Global Illumination. Exercise 2. Path Tracing Radiosity

Direct Rendering. Direct Rendering Goals

CMSC427 Advanced shading getting global illumination by local methods. Credit: slides Prof. Zwicker

Line Drawing. Foundations of Computer Graphics Torsten Möller

Distribution Ray-Tracing. Programação 3D Simulação e Jogos

Lecture 9: Hough Transform and Thresholding base Segmentation

Fourier Transform in Image Processing. CS/BIOEN 6640 U of Utah Guido Gerig (slides modified from Marcel Prastawa 2012)

Computer Vision I. Announcements. Fourier Tansform. Efficient Implementation. Edge and Corner Detection. CSE252A Lecture 13.

Image Acquisition + Histograms

Texture-Mapping Tricks. How Bad Does it Look? We've Seen this Sort of Thing Before. Sampling Texture Maps

Ray Tracing. Johns Hopkins Department of Computer Science Course : Rendering Techniques, Professor: Jonathan Cohen

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

INFOMAGR Advanced Graphics. Jacco Bikker - February April Welcome!

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

Sampling: Application to 2D Transformations

Sampling and Reconstruction

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

Numerical Integration

Edges, interpolation, templates. Nuno Vasconcelos ECE Department, UCSD (with thanks to David Forsyth)

Lecture 25: Board Notes: Threads and GPUs

Computer Graphics. Attributes of Graphics Primitives. Somsak Walairacht, Computer Engineering, KMITL 1

Real-Time Graphics Architecture

EECS 556 Image Processing W 09. Interpolation. Interpolation techniques B splines

Point-Based Rendering

Ray Tracing. Computer Graphics CMU /15-662, Fall 2016

Image Filtering, Warping and Sampling

Prof. Feng Liu. Fall /25/2018

This work is about a new method for generating diffusion curve style images. Although this topic is dealing with non-photorealistic rendering, as you

CS5670: Computer Vision

Computer Vision and Graphics (ee2031) Digital Image Processing I

Parallelizing Graphics Pipeline Execution (+ Basics of Characterizing a Rendering Workload)

CS 563 Advanced Topics in Computer Graphics Lecture 2: Bare-Bones Raytracer. by Emmanuel Agu

TDA362/DIT223 Computer Graphics EXAM (Same exam for both CTH- and GU students)

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

Image Sampling and Quantisation

Lets assume each object has a defined colour. Hence our illumination model is looks unrealistic.

I have a meeting with Peter Lee and Bob Cosgrove on Wednesday to discuss the future of the cluster. Computer Graphics

Image Sampling & Quantisation

Computer Graphics. Chapter 4 Attributes of Graphics Primitives. Somsak Walairacht, Computer Engineering, KMITL 1

Digital Image Processing. Prof. P. K. Biswas. Department of Electronic & Electrical Communication Engineering

Intro to Ray-Tracing & Ray-Surface Acceleration

CS 130 Final. Fall 2015

Buffers, Textures, Compositing, and Blending. Overview. Buffers. David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E.

CS201 Computer Vision Lect 4 - Image Formation

Anti-aliased and accelerated ray tracing. University of Texas at Austin CS384G - Computer Graphics

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

Digital Image Processing COSC 6380/4393

Soft shadows. Steve Marschner Cornell University CS 569 Spring 2008, 21 February

Image Processing

Filtering theory: Battling Aliasing with Antialiasing. Department of Computer Engineering Chalmers University of Technology

Anti-aliasing. Images and Aliasing

Segmentation I: Edges and Lines

I may not have gone where I intended to go, but I think I have ended up where I needed to be. Douglas Adams

Transcription:

Lecture 4: Drawing a Triangle (and an Intro to Sampling) Computer Graphics CMU 15-462/15-662, Spring 2018

HW 0.5 Due, HW 1 Out Today! GOAL: Implement a basic rasterizer - (Topic of today s lecture) - We hand you a bunch of lines, triangles, etc. - You draw them by lighting up pixels on the screen! Code skeleton available (later today) from course webpage DUE February 12

OpenGL tutorial session tomorrow You will use a very small amount of OpenGL in this project - longstanding and standard rendering library - get something up on the screen quickly - have some understanding of how OpenGL does things for you - you will rewrite some of its functionality in the remainder of the assignment Tutorial TOMORROW January 30-8-9pm - location will be announced on Piazza

TODAY: Rasterization Two major techniques for getting stuff on the screen Rasterization - for each primitive (e.g., triangle), which pixels light up? - extremely fast (BILLIONS of triangles per second on GPU) - harder (but not impossible) to achieve photorealism - perfect match for 2D vector art, fonts, quick 3D preview, Ray Tracing - for each pixel, which primitives are seen? - easier to get photorealism - generally slower - much more later in the semester!

Let s draw some triangles on the screen Question 1: what pixels does the triangle overlap? ( coverage ) Pixel Question 2: what triangle is closest to the camera in each pixel? ( occlusion )

The visibility problem An informal definition: what scene geometry is visible within each screen pixel? - What scene geometry projects into a screen pixel? (coverage) - Which geometry is visible from the camera at that pixel? (occlusion) x-axis x/z Virtual Sensor 1 Pinhole Camera (0,0) (x,z) z-axis (Recall pinhole camera from first lecture)

The visibility problem An informal definition: what scene geometry is visible within each screen pixel? - What scene geometry projects into a screen pixel? (coverage) - Which geometry is visible from the camera at that pixel? (occlusion) x-axis Pinhole Camera (0,0) 1 x/z Virtual Sensor (x,z) z-axis

The visibility problem (said differently) In terms of rays: - What scene geometry is hit by a ray from a pixel through the pinhole? (coverage) - What object is the first hit along that ray? (occlusion) x-axis z-axis Virtual Sensor Pinhole Camera (0,0) (x,z) Hold onto this thought for later in the semester.

Computing triangle coverage What pixels does the triangle overlap? Input: projected position of triangle vertices: P 0, P 1, P 2 Output: set of pixels covered by the triangle

What does it mean for a pixel to be covered by a triangle? Question: which triangles cover this pixel? 4 1 3 2 Pixel

One option: compute fraction of pixel area covered by triangle, then color pixel according to this fraction. 10% Intuition: if triangle covers 10% of pixel, then pixel should be 10% red. 35% 60% 85% 15%

Coverage gets tricky when considering occlusion 1 2 2 1 Pixel covered by triangle 1, other half covered by triangle 2 2 1 Interpenetration of triangles: even trickier Two regions of triangle 1 contribute to pixel. One of these regions is not even convex.

Sampling 101

1D signal f (x) x

Sampling: taking measurements a signal Below: 5 measurements ( samples ) of f(x) f (x) f(x4) f(x0) f(x1) f(x2) f(x3) x0 x1 x2 x3 x4

Audio file: stores samples of a 1D signal Most consumer audio is sampled at 44.1 KHz Amplitude time

Reconstruction: given a set of samples, how might we attempt to reconstruct the original signal f(x)? f(x4) f(x0) f(x1) f(x2) f(x3) x0 x1 x2 x3 x4

Piecewise constant approximation frecon(x) = value of sample closest to x frecon(x) approximates f (x) f (x) frecon (x) x0 x1 x2 x3 x4

Piecewise linear approximation frecon(x) = linear interpolation between values of two closest samples to x f (x) frecon (x) x0 x1 x2 x3 x4

How can we represent the signal more accurately? Sample signal more densely (increase sampling rate) x0 x1 x2 x3 x4 x5 x6 x7 x8

Reconstruction from denser sampling x0 x1 x2 x3 x4 x5 x6 x7 x8 = reconstruction via nearest = reconstruction via linear interpolation

Mathematical Z representation Z of sampling 1 Consider the Dirac delta: (x) Z Z 1 where for all x 6= 0, (x) =0 and (x)dx =1 1 When applied to a function f,!(x) acts to pull out the value of f at x = 0: Z 1 1 f(x) (x)dx = f(0)

Sampling via Dirac Comb Consider a sequence of impulses with period T: f (x) 0 T 2T 3T 4T 5T 6T 7T 8T 0 T 2T 3T 4T 5T 6T 7T 8T Discrete sampling of a continuous function f can be expressed as product between f and Dirac comb: ( = Cyrillic sha ) 0 T 2T 3T 4T 5T 6T 7T 8T

Reconstruction via Convolution Z 1 (f g)(x) = f(y)g(x y)dy 1 Z output signal Z filter input signal It may be helpful to consider the effect of convolution with the simple unit-area box function: 1 1 x apple 0.5 f(x) = 0 otherwise 1 (f g)(x) = Z 0.5 Z g(x y)dy -0.5 0.5 0.5 f * g is a smoothed version of g

X Piecewise Constant Reconstruction: Box Filter X Sampled signal: (with period T) g(x) =X T (x)f(x) =T 1 1X Z i= 1 f(it ) (x it ) 1 Reconstruction filter: Z (unit area box of width T) h(x) = 1/T x apple T/2 0 otherwise Z X 0 T 2T 3T 4T 5T 6T 7T 8T T Reconstructed signal: (chooses nearest sample) f recon (x) =(h g)(x) =T Z 1 1 h(y) 1X i= 1 f(it ) (x y it )dy = Z T/2 T/2 1X i= 1 f(it ) (x y it ) non-zero only for it closest to x

X Piecewise Linear Reconstruction: Triangle Filter Sampled signal: (with period T) Reconstruction filter: X g(x) =X T (x)f(x) =T Z X 1 1X i= 1 (unit area triangle of width T) Z f(it ) (x it ) Z h(x) = (1 x T )/T x apple T 0 otherwise 0 T 2T 3T 4T 5T 6T 7T 8T T 1/T -T T

Summary Sampling = measurement of a signal - Represent signal as discrete set of samples - Mathematically described by multiplication by impulse train Reconstruction = generating signal from a discrete set of samples - Convolution of sampled signal with a reconstruction filter - Intuition: value of reconstructed function at any point in domain is a weighted combination of sampled values - We discussed simple box, triangle filters, but much higher quality filters exist sinc(x) = sin( x)) x Z Normalized sinc filter Truncated sinc filter Truncated gaussian filter [Image credit: Wikipedia]

Now back to computing coverage

Think of coverage as a 2D signal coverage(x,y) = 1 if the triangle contains point (x,y) 0 otherwise

Estimate triangle-screen coverage by sampling the binary function: coverage(x,y) 1 4 3 2 (x+0.5, y+0.5) Pixel (x,y) Example: Here I chose the coverage sample point to be at a point corresponding to the pixel center. = triangle covers sample, fragment generated for pixel = triangle does not cover sample, no fragment generated

Edge cases (literally) Is this sample point covered by triangle 1? or triangle 2? or both? 2 1

Breaking Ties* When edge falls directly on a screen sample point, the sample is classified as within triangle if the edge is a top edge or left edge - Top edge: horizontal edge that is above all other edges - Left edge: an edge that is not exactly horizontal and is on the left side of the triangle. (triangle can have one or two left edges) *These are the rules used in OpenGL/Direct3D, i.e., in modern GPUs. Source: Direct3D Programming Guide, Microsoft

Results of sampling triangle coverage

I have a sampled signal, now I want to display it on a screen

Pixels on a screen Each image sample sent to the display is converted into a little square of light of the appropriate color: (a pixel = picture element) LCD display pixel on my laptop * Thinking of each LCD pixel as emitting a square of uniform intensity light of a single color is a bit of an approximation to how real displays work, but it will do for now.

So if we send the display this:

We see this when we look at the screen (assuming a screen pixel emits a square of perfectly uniform intensity of light)

Recall: the real coverage signal was this

Aliasing

Sound can be expressed as a superposition of frequencies f1(x) = sin("x) f2(x) = sin(2"x) f4(x) = sin(4"x) f(x) = f1(x) + 0.75 f2(x) + 0.5 f4(x)

An audio spectrum analyzer shows the amplitude of each frequency Intensity of low-frequencies (bass) Image credit: ONYX Apps Intensity of high frequencies

Visualizing the frequency content of images Spatial domain result Spectrum

Low frequencies only (smooth gradients) Spatial domain result Spectrum (after low-pass filter) All frequencies above cutoff have 0 magnitude

Mid-range frequencies Spatial domain result Spectrum (after band-pass filter)

Mid-range frequencies Spatial domain result Spectrum (after band-pass filter)

High frequencies (edges) Spatial domain result (strongest edges) Spectrum (after high-pass filter) All frequencies below threshold have 0 magnitude

An image as a sum of its frequency components + + + =

1D example: Undersampling high-frequency signals results in aliasing f1(x) Low-frequency signal: sampled adequately for accurate reconstruction f2(x) f3(x) f4(x) f5(x) High-frequency signal is insufficiently sampled: reconstruction appears to be from a low frequency signal x Aliasing : high frequencies in the original signal masquerade as low frequencies after reconstruction (due to undersampling)

Temporal aliasing: wagon wheel effect Camera s frame rate (temporal sampling rate) is too low for rapidly spinning wheel. https://www.youtube.com/watch?v=vnftf5qlpia

Nyquist-Shannon theorem Consider a band-limited signal: has no frequencies above ω0-1d: consider low-pass filtered audio signal - 2D: recall the blurred image example from a few slides ago -ω 0 ω 0 The signal can be perfectly reconstructed if sampled with period T = 1 / 2ω0 And reconstruction is performed using a sinc filter Ideal filter with no frequencies above cutoff (infinite extent!) sinc(x) = sin( x)) x Z

Challenges of sampling-based approaches in graphics Our signals are not always band-limited in computer graphics. Why? Hint: Also, infinite extent of ideal reconstruction filter (sinc) is impractical for efficient implementations. Why?

Aliasing artifacts in images Undersampling high-frequency signals and the use of nonideal resampling filters yields image artifacts - Jaggies in a single image - Roping or shimmering of images when animated - Moiré patterns in high-frequency areas of images

Sampling a zone plate: sin(x 2 + y 2 ) Rings in center-left: Actual signal (low frequency oscillation) Rings on right: aliasing from undersampling high frequency oscillation and then resampling back to Keynote slide resolution (0,0) Middle: (interaction between actual signal and aliased resconstruction) Figure credit: Pat Hanrahan and Bryce Summers

Initial coverage sampling rate (1 sample per pixel)

Increase density of sampling coverage signal (high frequencies exist in coverage signal because of triangle edges)

Supersampling Example: stratified sampling using four samples per pixel

Resampling Converting from one discrete sampled representation to another Original signal (high frequency edge) Dense sampling of reconstructed signal Reconstructed signal (lacks high frequencies) Coarsely sampled signal

Resample to display s pixel resolution (Because a screen displays one sample value per screen pixel...)

Resample to display s pixel rate (box filter)

Resample to display s pixel rate (box filter)

Displayed result (note anti-aliased edges) 100% 0% 50% 50% 100% 100% 25%

Recall: the real coverage signal was this

Sampling coverage We want the light emitted from a display to be an accurate to match the ground truth signal: coverage(x,y)) Resampling a densely sampled signal (supersampled) integrates coverage values over the entire pixel region. The integrated result is sent to the display (and emitted by the pixel) so that the light emitted by the pixel is similar to what would be emitted in that screen region by an infinite resolution display

Sampling triangle coverage (evaluating coverage(x,y) for a triangle)

Point-in-triangle test Compute triangle edge equations from projected positions of vertices Pi = (Xi, Yi) P 2 dxi = Xi+1 - Xi dyi = Yi+1 - Yi Ei (x, y) = (x - Xi) dyi - (y - Yi) dxi = Ai x + Bi y + Ci Ei (x, y) = 0 : point on edge > 0 : outside edge < 0 : inside edge P 1 P 0

Point-in-triangle test Pi = (Xi, Yi) P 2 dxi = Xi+1 - Xi dyi = Yi+1 - Yi Ei (x, y) = (x - Xi) dyi - (y - Yi) dxi = Ai x + Bi y + Ci Ei (x, y) = 0 : point on edge > 0 : outside edge < 0 : inside edge P 1 P 0

Point-in-triangle test Pi = (Xi, Yi) P 2 dxi = Xi+1 - Xi dyi = Yi+1 - Yi Ei (x, y) = (x - Xi) dyi - (y - Yi) dxi = Ai x + Bi y + Ci Ei (x, y) = 0 : point on edge > 0 : outside edge < 0 : inside edge P 1 P 0

Point-in-triangle test Pi = (Xi, Yi) P 2 dxi = Xi+1 - Xi dyi = Yi+1 - Yi Ei (x, y) = (x - Xi) dyi - (y - Yi) dxi = Ai x + Bi y + Ci Ei (x, y) = 0 : point on edge > 0 : outside edge < 0 : inside edge P 1 P 0

Point-in-triangle test Sample point s = (sx, sy) is inside the triangle if it is inside all three edges. P 2 inside(sx, sy) = E0 (sx, sy) < 0 && E1 (sx, sy) < 0 && E2 (sx, sy) < 0; Note: actual implementation of inside(sx,sy) involves checks based on the triangle coverage edge rules (see beginning of lecture) P 0 Sample points inside triangle are highlighted red. P 1

Incremental triangle traversal Pi = (Xi, Yi) P 2 dxi = Xi+1 - Xi dyi = Yi+1 - Yi Ei (x, y) = (x - Xi) dyi - (y - Yi) dxi = Ai x + Bi y + Ci Ei (x, y) = 0 : point on edge > 0 : outside edge < 0 : inside edge Efficient incremental update: P 1 dei (x+1,y) = Ei (x,y) + dyi = Ei (x,y) + Ai dei (x,y+1) = Ei (x,y) + dxi = Ei (x,y) + Bi Incremental update saves computation: Only one addition per edge, per sample test P 0 Many traversal orders are possible: backtrack, zig-zag, Hilbert/Morton curves (locality maximizing)

Modern approach: tiled triangle traversal Traverse triangle in blocks P 2 Test all samples in block against triangle in parallel Advantages: - Simplicity of wide parallel execution overcomes cost of extra point-in-triangle tests (most triangles cover many samples, especially when super-sampling coverage) - Can skip sample testing work: entire block not in triangle ( early out ), entire block entirely within triangle ( early in ) P 1 - Additional advantaged related to accelerating occlusion computations (not discussed today) P 0 All modern GPUs have special-purpose hardware for efficiently performing point-in-triangle tests

Summary We formulated computing triangle-screen coverage as a sampling problem - Triangle-screen coverage is a 2D signal - Undersampling and the use of simple (non-ideal) reconstruction filters may yield aliasing - In today s example, we reduced aliasing via supersampling Image formation on a display - When samples are 1-to-1 with display pixels, sample values are handed directly to display - When supersampling, resample densely sampled signal down to display resolution Sampling screen coverage of a projected triangle: - Performed via three point-inside-edge tests - Real-world implementation challenge: balance conflicting goals of avoiding unnecessary point-in-triangle tests and maintaining parallelism in algorithm implementation