Lecture 6: Rasterization 1 (Triangles)lingqi/teaching/... · CS180, Spring 2019 Lingqi Yan, UC...

Post on 02-Oct-2020

3 views 0 download

Transcript of Lecture 6: Rasterization 1 (Triangles)lingqi/teaching/... · CS180, Spring 2019 Lingqi Yan, UC...

CS180: Introduction to Computer GraphicsSpring 2019, Lingqi Yan, UC Santa Barbara

Rasterization 1 (Triangles)Lecture 6:

http://www.cs.ucsb.edu/~lingqi/teaching/cs180.html

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Last Lectures

• Transformation- Modeling + Viewing

- Model + View + Projection

- Linear, Affine, Projection transformations

- Homogeneous coordinates

!2

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Today

!3

• Finishing up Viewing

- Viewport transformation

• Rasterization

- Different raster displays

- Rasterizing a triangle

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

What’s after MVP?

• Model transformation (placing objects)

• View transformation (placing camera)

• Projection transformation- Orthographic projection (cuboid to “canonical” cube [-1, 1]3)

- Perspective projection (frustum to “canonical” cube)

• Canonical cube to ?

!4

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Canonical Cube to Screen

• What is a screen?

- A typical kind of raster display

• Raster == discretize (personally)

- Usually to an array of pixels

- Size of the array: resolution

• Pixel (FYI, short for “picture element”)

- For this course only: A pixel is a little square with uniform color

- Color is a mixture of (red, green, blue)

!5

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Canonical Cube to Screen

• Defining the screen space- Slightly different from the “tiger book”

!6

array of pixels

X

Y

(0, 0)

Pixel (x, y) is centered at(x + 0.5, y + 0.5)

Pixels’ indices are from (0, 0) to (width - 1, height - 1)

Pixels’ indices are in the form of (x, y), where both x and y are integers

The screen covers range (0, 0) to (width, height)

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Canonical Cube to Screen

!7

array of pixels

X

Y

(0, 0)

• Irrelevant to z

• Transform in xy plane: [-1, 1]2 to [0, width] x [0, height]

x

z

y

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Canonical Cube to Screen

!8

• Irrelevant to z

• Transform in xy plane: [-1, 1]2 to [0, width] x [0, height]

• Viewport transform matrix:

Mviewport =

0

BB@

width2 0 0 width

2

0 height2 0 height

20 0 1 00 0 0 1

1

CCA

<latexit sha1_base64="tdp9C2WJdTQt5m0qzzOxt80jqdQ=">AAACjHicbZFRS+NAEMc30bvTnufV89GXxXLiUy8R4TxEEA/EF6EHthWaUjabSbO42YTdiV4J+TR+I9/u27hNw1FbB4b98/vPsLOzYS6FQc/757gbmx8+ftrabn3e+bL7tb33bWCyQnPo80xm+j5kBqRQ0EeBEu5zDSwNJQzDh99zf/gI2ohM3eEsh3HKpkrEgjO0aNJ+vp2UjwKe8kxjdRGEMBWqzFOGWvytglgzXj6JCJOqPKnoEfWaXHWCYAknIKYJLnWs4aZ8nn59LoEaBqCi/2NM2h2v69VB14XfiA5pojdpvwRRxosUFHLJjBn5Xo7jkmkUXELVCgoDOeMPbAojKxVLwYzLepkV/W5JRONM21RIa7rcUbLUmFka2ko7X2JWvTl8zxsVGJ+NS6HyAkHxxUVxISlmdP4zNBIaOMqZFYxrYWelPGF2c2j/r2WX4K8+eV0MTrq+1/X/nHYur5p1bJEDckiOiU9+kktyQ3qkT7iz7fxwzpxf7q576p67F4tS12l69smbcK9fAWhOvu0=</latexit><latexit sha1_base64="tdp9C2WJdTQt5m0qzzOxt80jqdQ=">AAACjHicbZFRS+NAEMc30bvTnufV89GXxXLiUy8R4TxEEA/EF6EHthWaUjabSbO42YTdiV4J+TR+I9/u27hNw1FbB4b98/vPsLOzYS6FQc/757gbmx8+ftrabn3e+bL7tb33bWCyQnPo80xm+j5kBqRQ0EeBEu5zDSwNJQzDh99zf/gI2ohM3eEsh3HKpkrEgjO0aNJ+vp2UjwKe8kxjdRGEMBWqzFOGWvytglgzXj6JCJOqPKnoEfWaXHWCYAknIKYJLnWs4aZ8nn59LoEaBqCi/2NM2h2v69VB14XfiA5pojdpvwRRxosUFHLJjBn5Xo7jkmkUXELVCgoDOeMPbAojKxVLwYzLepkV/W5JRONM21RIa7rcUbLUmFka2ko7X2JWvTl8zxsVGJ+NS6HyAkHxxUVxISlmdP4zNBIaOMqZFYxrYWelPGF2c2j/r2WX4K8+eV0MTrq+1/X/nHYur5p1bJEDckiOiU9+kktyQ3qkT7iz7fxwzpxf7q576p67F4tS12l69smbcK9fAWhOvu0=</latexit><latexit sha1_base64="tdp9C2WJdTQt5m0qzzOxt80jqdQ=">AAACjHicbZFRS+NAEMc30bvTnufV89GXxXLiUy8R4TxEEA/EF6EHthWaUjabSbO42YTdiV4J+TR+I9/u27hNw1FbB4b98/vPsLOzYS6FQc/757gbmx8+ftrabn3e+bL7tb33bWCyQnPo80xm+j5kBqRQ0EeBEu5zDSwNJQzDh99zf/gI2ohM3eEsh3HKpkrEgjO0aNJ+vp2UjwKe8kxjdRGEMBWqzFOGWvytglgzXj6JCJOqPKnoEfWaXHWCYAknIKYJLnWs4aZ8nn59LoEaBqCi/2NM2h2v69VB14XfiA5pojdpvwRRxosUFHLJjBn5Xo7jkmkUXELVCgoDOeMPbAojKxVLwYzLepkV/W5JRONM21RIa7rcUbLUmFka2ko7X2JWvTl8zxsVGJ+NS6HyAkHxxUVxISlmdP4zNBIaOMqZFYxrYWelPGF2c2j/r2WX4K8+eV0MTrq+1/X/nHYur5p1bJEDckiOiU9+kktyQ3qkT7iz7fxwzpxf7q576p67F4tS12l69smbcK9fAWhOvu0=</latexit><latexit sha1_base64="tdp9C2WJdTQt5m0qzzOxt80jqdQ=">AAACjHicbZFRS+NAEMc30bvTnufV89GXxXLiUy8R4TxEEA/EF6EHthWaUjabSbO42YTdiV4J+TR+I9/u27hNw1FbB4b98/vPsLOzYS6FQc/757gbmx8+ftrabn3e+bL7tb33bWCyQnPo80xm+j5kBqRQ0EeBEu5zDSwNJQzDh99zf/gI2ohM3eEsh3HKpkrEgjO0aNJ+vp2UjwKe8kxjdRGEMBWqzFOGWvytglgzXj6JCJOqPKnoEfWaXHWCYAknIKYJLnWs4aZ8nn59LoEaBqCi/2NM2h2v69VB14XfiA5pojdpvwRRxosUFHLJjBn5Xo7jkmkUXELVCgoDOeMPbAojKxVLwYzLepkV/W5JRONM21RIa7rcUbLUmFka2ko7X2JWvTl8zxsVGJ+NS6HyAkHxxUVxISlmdP4zNBIaOMqZFYxrYWelPGF2c2j/r2WX4K8+eV0MTrq+1/X/nHYur5p1bJEDckiOiU9+kktyQ3qkT7iz7fxwzpxf7q576p67F4tS12l69smbcK9fAWhOvu0=</latexit>

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Today: Rasterizing Triangles into Pixels

!9

Life of Pi (2012)

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Drawing Machines

!10

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

CNC Sharpie Drawing Machine

Aaron Panone with Matt W. Moore

!11

http://44rn.com/projects/numerically-controlled-poster-series-with-matt-w-moore/

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Laser Cutters

!12

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Oscilloscope

!13

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Cathode Ray Tube

!14

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Oscilloscope Art

Jerobeam Fenderson!15

https://www.youtube.com/watch?v=rtR63-ecUNo

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Television - Raster Display CRT

Cathode Ray Tube Raster Scan(modulate intensity)

!16

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Frame Buffer: Memory for a Raster Display

!17

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

A Sampling of Different Raster Displays

!18

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Flat Panel Displays

!19

B.Woods, Android Pit

Low-Res LCD Display

Color LCD, OLED, …

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

LCD (Liquid Crystal Display) Pixel

Principle: block or transmit light by twisting polarization Illumination from backlight(e.g. fluorescent or LED) Intermediate intensity levels by partial twist

!20

[H&

B fig

. 2-1

6]

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

LED Array Display

!21

Light emitting diode array

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

DMD Projection Display

!22

[Y.K

. Rab

inow

itz; E

KB

Tec

hnol

ogie

s

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

DMD Projection Display

!23

[Tex

as In

stru

men

ts]

Array of micro-mirror pixels DMD = Digital Micromirror Device

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Electrophoretic (Electronic Ink) Display

!24

[Wik

imed

ia C

omm

ons

—Se

narc

lens

]

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Drawing to Raster Displays

!25

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Polygon Meshes

!26

Life of Pi (2012)

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Triangle Meshes

!27

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Triangle Meshes

!28

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Triangles - Fundamental Shape Primitives

Why triangles?

• Most basic polygon

• Break up other polygons

• Optimize one implementation

• Triangles have unique properties

• Guaranteed to be planar

• Well-defined interior

• Well-defined method for interpolating values at vertices over triangle (barycentric interpolation)

!29

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Drawing a Triangle To The Framebuffer

(“Rasterization”)

!30

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

What Pixel Values Approximate a Triangle?

Input: position of triangle vertices projected on screen

Output: set of pixel valuesapproximating triangle

!31

?(2.2, 1.3)

(4.4, 11.0)(15.3, 8.6)

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Today, Let’s Start With A Simple Approach: Sampling

!32

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Sampling a Function

Evaluating a function at a point is sampling. We can discretize a function by sampling. for( int x = 0; x < xmax; x++ ) output[x] = f(x);

Sampling is a core idea in graphics.We sample time (1D), area (2D), direction (2D), volume (3D) …

!33

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Let’s Try Rasterization As 2D Sampling

!34

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Sample If Each Pixel Center Is Inside Triangle

!35

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Sample If Each Pixel Center Is Inside Triangle

!36

Define Binary Function: inside(tri,x,y)

inside(t,x,y) = 1

0

pixel (x,y) in triangle t

otherwise

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Rasterization = Sampling A 2D Indicator Function

for( int x = 0; x < xmax; x++ ) for( int y = 0; y < ymax; y++ ) Image[x][y] = f(x + 0.5, y + 0.5);

Rasterize triangle tri by sampling the function f(x,y) = inside(tri,x,y)

!38

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Recall: Sample Locations

!39

(0,0) (w,0)

(0,h) (w,h)

Sample location for pixel (x,y)

(x+1/2,y+1/2)

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Evaluating inside(tri,x,y)

!40

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Triangle = Intersection of Three Half Planes

!41

P0

P1

P2

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Each Line Defines Two Half-Planes

Implicit line equation • L(x,y) = Ax + By + C

• On line: L(x,y) = 0 • Above line: L(x,y) > 0 • Below line: L(x,y) < 0

!42

> 0

< 0

= 0

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Line Equation Derivation

!43

P0

P1

T

T = P1 � P0 = (x1 � x0, y1 � y0)

Line Tangent Vector

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Line Equation Derivation

!44

(x,y)

(-y,x)

Perp(x, y) = (�y, x)

General Perpendicular Vector in 2D

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Line Equation Derivation

!45

P0

P1

T

N

N = Perp(T ) = (�(y1 � y0), x1 � x0)

Line Normal Vector

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Line Equation Derivation

!46

P0

P1

N

P = (x, y)

V

V = P � P0 = (x� x0, y � y0)

L(x, y) = V ·N = �(x� x0)(y1 � y0) + (y � y0)(x1 � x0)

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Line Equation

!47

P0

P1

N

P = (x, y)

V

V = P � P0 = (x� x0, y � y0)

L(x, y) = V ·N = �(x� x0)(y1 � y0) + (y � y0)(x1 � x0)

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Line Equation Tests

!48

P0

P1

N

P

V

L(x, y) = V ·N > 0

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

L(x, y) = V ·N = 0

Line Equation Tests

!49

P0

P1

N

PV

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

L(x, y) = V ·N < 0

Line Equation Tests

!50

P0

P1

N

PV

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Point-in-Triangle Test: Three Line Tests

!51

P0

P1

P2

Compute line equations from pairs of vertices

Pi = (Xi, Yi)

dXi = Xi+1 - Xi

dYi = Yi+1 - Yi

Li (x, y) = –(x - Xi) dYi + (y - Yi) dXi

= Ai x + Bi y + Ci

Li (x, y) = 0 : point on edge < 0 : outside edge > 0 : inside edge

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Point-in-Triangle Test: Three Line Tests

!52

P0

P1

P2

L0(x, y) > 0

Pi = (Xi, Yi)

dXi = Xi+1 - Xi

dYi = Yi+1 - Yi

Li (x, y) = –(x - Xi) dYi + (y - Yi) dXi

= Ai x + Bi y + Ci

Li (x, y) = 0 : point on edge < 0 : outside edge > 0 : inside edge

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Point-in-Triangle Test: Three Line Tests

!53

P0

P1

P2

L1(x, y) > 0

Pi = (Xi, Yi)

dXi = Xi+1 - Xi

dYi = Yi+1 - Yi

Li (x, y) = –(x - Xi) dYi + (y - Yi) dXi

= Ai x + Bi y + Ci

Li (x, y) = 0 : point on edge < 0 : outside edge > 0 : inside edge

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Point-in-Triangle Test: Three Line Tests

!54

P0

P1

P2

L2(x, y) > 0

Pi = (Xi, Yi)

dXi = Xi+1 - Xi

dYi = Yi+1 - Yi

Li (x, y) = –(x - Xi) dYi + (y - Yi) dXi

= Ai x + Bi y + Ci

Li (x, y) = 0 : point on edge < 0 : outside edge > 0 : inside edge

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Point-in-Triangle Test: Three Line Tests

!55

Sample point s = (sx, sy) is inside the triangle if it is inside all three lines.

inside(sx, sy) =L0 (sx, sy) > 0 &&L1 (sx, sy) > 0 &&L2 (sx, sy) > 0;

P0

P1

P2

Note: actual implementation of inside(sx,sy) involves ≤ checks based on edge rules

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Edge Cases (Literally)

Is this sample point covered by triangle 1, triangle 2, or both?

!56

1

2

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Incremental Triangle Traversal (Faster?)

!57

P0

P1

P2

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Modern Approach: Tiled Triangle Traversal

!58

P0

P1

P2Traverse triangle in blocks

Test all samples in block in parallel

All modern GPUs have special-purpose hardware for efficient point-in-triangle tests

Advantages: - Simplicity of wide parallel execution

overcomes cost of extra point-in-triangle tests (most triangles cover many samples, especially when super-sampling)

- Can skip sample testing work: entire block not in triangle (“early out”), entire block entirely within triangle (“early in”)

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Signal Reconstruction on Real Displays

!59

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Real LCD Screen Pixels (Closeup)

iPhone 6S Galaxy S5

!60

iphonearena.com iphonearena.com

Notice R,G,B pixel geometry! But in this class, we will assume a colored square full-color pixel.

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Aside: What About Other Display Methods?

!61

Color print: observe half-tone pattern

Assume Display Pixels Emit Square of Light

LCD pixelon laptop

Each image sample sent to the display is converted into a little square of light of the appropriate color: (a pixel = picture element)

* LCD pixels do not actually emit light in a square of uniform color, but this approximation suffices for our current discussion

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

So, If We Send The Display This Sampled Signal

!63

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

The Display Physically Emits This Signal

!64

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Compare: The Continuous Triangle Function

!65

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

What’s Wrong With This Picture?

!66

Jaggies!

Slide courtesy of Prof. Ren Ng, UC Berkeley

CS180, Spring 2019 Lingqi Yan, UC Santa Barbara

Aliasing (Jaggies)

!67

Is this the best we can do?

Thank you!