CS180 Lecture 06lingqi/teaching/resources/CS... · 2021. 1. 25. · CS180 / 280, Winter 2021 Lingqi...

52
Rasterization 1 (Raster Device & Sampling) Lecture 6: CS180 / 280: Introduction to Computer Graphics Winter 2021, Lingqi Yan, UC Santa Barbara http://www.cs.ucsb.edu/~lingqi/teaching/cs180.html

Transcript of CS180 Lecture 06lingqi/teaching/resources/CS... · 2021. 1. 25. · CS180 / 280, Winter 2021 Lingqi...

  • Rasterization 1
(Raster Device & Sampling)

    Lecture 6:

    CS180 / 280: Introduction to Computer GraphicsWinter 2021, Lingqi Yan, UC Santa Barbara

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

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Announcement

    • Assignment 2 will be released today

    - Due next Thursday AoE (any changes will be announced later)

    • Reminder: my office hour will be canceled next week

    - Jan 26 only, any other office hours will not be affected

    • This lecture will be easy

    2

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Last Lecture• Perspective Projection

    - Defining the frustum - Relationship between perspective and orthographic projections - The “squish”-ing operation - Derivation

    • Viewport Transform after MVP

    3

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Today• After MVP

    - Viewport transformation

    • Rasterization

    - Different raster displays - Rasterizing a triangle

    4

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    What’s after MVP?• Model transformation (placing objects)

    • View transformation (placing camera == placing objects)

    • Projection transformation

    - Orthographic projection (cuboid to “canonical” cube [-1, 1]3) - Perspective projection (frustum to “canonical” cube)

    • Coordinates in the canonical cube

    - A.k.a. normalized device coordinates (NDC) - Will be transformed to ?

    5

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Canonical Cube to Screen• What is a screen?

    - An array of pixels - Size of the array: resolution - A typical kind of raster display

    • Raster == screen in German

    - Rasterize == drawing onto the screen

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

    - For now: A pixel is a little square with uniform color - Color is a mixture of (red, green, blue)

    6

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Canonical Cube to Screen

    • Defining the screen space

    - Slightly different from the “tiger book”

    7

    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 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Canonical Cube to Screen

    8

    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 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Canonical Cube to Screen

    9

    • 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

    width2

    0 height2 0height

    20 0 1 00 0 0 1

    1

    CCA

    AAACjHicbZFRS+NAEMc30bvTnufV89GXxXLiUy8R4TxEEA/EF6EHthWaUjabSbO42YTdiV4J+TR+I9/u27hNw1FbB4b98/vPsLOzYS6FQc/757gbmx8+ftrabn3e+bL7tb33bWCyQnPo80xm+j5kBqRQ0EeBEu5zDSwNJQzDh99zf/gI2ohM3eEsh3HKpkrEgjO0aNJ+vp2UjwKe8kxjdRGEMBWqzFOGWvytglgzXj6JCJOqPKnoEfWaXHWCYAknIKYJLnWs4aZ8nn59LoEaBqCi/2NM2h2v69VB14XfiA5pojdpvwRRxosUFHLJjBn5Xo7jkmkUXELVCgoDOeMPbAojKxVLwYzLepkV/W5JRONM21RIa7rcUbLUmFka2ko7X2JWvTl8zxsVGJ+NS6HyAkHxxUVxISlmdP4zNBIaOMqZFYxrYWelPGF2c2j/r2WX4K8+eV0MTrq+1/X/nHYur5p1bJEDckiOiU9+kktyQ3qkT7iz7fxwzpxf7q576p67F4tS12l69smbcK9fAWhOvu0=AAACj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XxXLiUy8R4TxEEA/EF6EHthWaUjabSbO42YTdiV4J+TR+I9/u27hNw1FbB4b98/vPsLOzYS6FQc/757gbmx8+ftrabn3e+bL7tb33bWCyQnPo80xm+j5kBqRQ0EeBEu5zDSwNJQzDh99zf/gI2ohM3eEsh3HKpkrEgjO0aNJ+vp2UjwKe8kxjdRGEMBWqzFOGWvytglgzXj6JCJOqPKnoEfWaXHWCYAknIKYJLnWs4aZ8nn59LoEaBqCi/2NM2h2v69VB14XfiA5pojdpvwRRxosUFHLJjBn5Xo7jkmkUXELVCgoDOeMPbAojKxVLwYzLepkV/W5JRONM21RIa7rcUbLUmFka2ko7X2JWvTl8zxsVGJ+NS6HyAkHxxUVxISlmdP4zNBIaOMqZFYxrYWelPGF2c2j/r2WX4K8+eV0MTrq+1/X/nHYur5p1bJEDckiOiU9+kktyQ3qkT7iz7fxwzpxf7q576p67F4tS12l69smbcK9fAWhOvu0=

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Next: Rasterizing Triangles into Pixels

    10

    Life of Pi (2012)

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Drawing Machines

    11

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    CNC Sharpie Drawing Machine

    Aaron Panone with Matt W. Moore

    12

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

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Laser Cutters

    13

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Different Raster Displays

    14

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Oscilloscope

    15

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Oscilloscope Art

    Jerobeam Fenderson16

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

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Cathode Ray Tube

    17

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Television - Raster Display CRT

    Cathode Ray TubeRaster Scan

    (modulate intensity)

    18

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Frame Buffer: Memory for a Raster Display

    19

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Flat Panel Displays

    20

    B.Woods, Android Pit

    Low-Res LCD Display

    Color LCD, OLED, …

  • CS180 / 280, Winter 2021 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

    21

    [H&

    B fi

    g. 2

    -16]

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    LED Array Display

    22

    Light emitting diode array

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Electrophoretic (Electronic Ink) Display

    23

    [Wik

    imed

    ia C

    omm

    ons

    —Se

    narc

    lens

    ]

    http://commons.wikimedia.org/wiki/User:Senarclens

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Rasterization: Drawing to Raster Displays

    24

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Polygon Meshes

    25

    Life of Pi (2012)

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Triangle Meshes

    26

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Triangle Meshes

    27

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Triangles - Fundamental Shape Primitives

    Why triangles?

    • Most basic polygon • Break up other polygons

    • Unique properties • Guaranteed to be planar • Well-defined interior • Well-defined method for interpolating values at

    vertices over triangle (barycentric interpolation)

    28

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    What Pixel Values Approximate a Triangle?

    Input: position of triangle vertices projected on screen

    Output: set of pixel values approximating triangle

    29

    ?(2.2, 1.3)

    (4.4, 11.0)(15.3, 8.6)

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    A Simple Approach: Sampling

    30

  • CS180 / 280, Winter 2021 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) …

    31

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Rasterization As 2D Sampling

    32

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Sample If Each Pixel Center Is Inside Triangle

    33

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Sample If Each Pixel Center Is Inside Triangle

    34

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

    inside(t, x, y) = 1

    0

    Point (x, y) in triangle t

    otherwise

    x, y: not necessarily integers

  • CS180 / 280, Winter 2021 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] = inside(tri, x + 0.5, y + 0.5);

    36

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Recall: Sample Locations

    37

    (0,0) (w,0)

    (0,h) (w,h)

    Sample location for pixel (x, y)

    (x+0.5, y+0.5)

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Evaluating inside(tri, x, y)

    38

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Inside? Recall: Three Cross Products!

    39

    P0

    P1

    P2

    Q

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Edge Cases (Literally)

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

    40

    1

    2

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Checking All Pixels on the Screen?

    41

    P0

    P1

    P2

    Use a Bounding Box!

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Incremental Triangle Traversal (Faster?)

    42

    P0

    P1

    P2

    suitable for thin and rotated triangles

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Rasterization on Real Displays

    43

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Real LCD Screen Pixels (Closeup)

    iPhone 6S Galaxy S5

    44

    iphonearena.com iphonearena.com

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

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Aside: What About Other Display Methods?

    45

    Color print: observe half-tone pattern

  • Assume Display Pixels Emit Square of Light

    LCD pixel on laptop

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

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    So, If We Send the Display the Sampled Signal

    47

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    The Display Physically Emits This Signal

    48

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Compare: The Continuous Triangle Function

    49

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    What’s Wrong With This Picture?

    50

    Jaggies!

  • CS180 / 280, Winter 2021 Lingqi Yan, UC Santa Barbara

    Aliasing (Jaggies)

    51

    Is this the best we can do?

  • Thank you!