surface shading: lights and rasterization

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

Upload: others

Post on 18-Apr-2022

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Surface shading: lights and rasterization

Surface shading: lightsand rasterization

Computer GraphicsCSE 167Lecture 6

Page 2: Surface shading: lights and rasterization

CSE 167: Computer Graphics

• Surface shading– Materials– Lights

• Rasterization

CSE 167, Winter 2018 2

Page 3: Surface shading: lights and rasterization

Rendering pipeline

CSE 167, Winter 2018 3

Modeling and viewingtransformation

Shading

Projection

Scan conversion,visibility

Scene data

Image

• Position object in 3D

• Map triangles to 2D

• Draw triangles

– Per pixel shading

• Determine colors of vertices

– Per vertex shading

Page 4: Surface shading: lights and rasterization

Types of light sources

• At each point on surfaces we need to know– Direction of incoming light (the L vector)– Intensity of incoming light (the cl values)

• Three light types:– Directional: from a specific direction – Point light: from a specific point– Spotlight: from a specific point with intensity that depends on direction

CSE 167, Winter 2018 4Based on slides courtesy of Jurgen Schulze

Page 5: Surface shading: lights and rasterization

Directional light source

• Light from a distant source– Light rays are parallel– Direction and intensity are the same everywhere – As if the source were infinitely far away– Good approximation of sunlight

• Specified by a unit length direction vector, and a color

CSE 167, Winter 2018 5

Light source

Receiving surface

Page 6: Surface shading: lights and rasterization

Point light source

• Similar to light bulbs• An infinitesimally small point that radiates light equally in all directions– Light vector varies across receiving surface– Intensity drops off proportionally to the inverse square of the distance from the light

• Reason for inverse square falloff: Surface area of sphere A = 4r2

CSE 167, Winter 2018 6

Page 7: Surface shading: lights and rasterization

Point light source

cl

v

pcsrc

cl

v

Light source

Receiving surface

7

At any point v on the surface:

Attenuation:

CSE 167, Winter 2018

Unitize

Page 8: Surface shading: lights and rasterization

Point light source, attenuation

• Adding constant factor    to denominator for better control

• Quadratic attenuation– Most computationally expensive, most physically accurate

• Linear attenuation– Less expensive, less accurate

• Constant attenuation– Least expensive, least accurate

CSE 167, Winter 2018 8

Page 9: Surface shading: lights and rasterization

Spotlight source• Like point light source, but intensity depends on direction

• Parameters– Position p: location of light source– Cone direction d: center axis of light source– Intensity falloff:

• Beam width (cone angle  )• The way the light tapers off at the edges of the beam (cosine exponent f)

CSE 167, Winter 2018 9

Page 10: Surface shading: lights and rasterization

Spotlight source

Light source

Receiving surface

10CSE 167, Winter 2018

Unitize

Page 11: Surface shading: lights and rasterization

Rasterization

CSE 167, Winter 2018 11

Page 12: Surface shading: lights and rasterization

The graphics pipeline

• The standard approach to object‐order graphics• Many versions exist

– Software (e.g., Pixar’s REYES architecture)• Many options for quality and flexibility

– Hardware (e.g., graphics cards in PCs)• Amazing performance: millions of triangles per frame

• We’ll focus on an abstract version of hardware pipeline• Called a “pipeline” because of the many stages

– Very parallelizable– Leads to remarkable performance of graphics cards (many times the flops of the CPU at ~1/5 the clock speed)

12CSE 167, Winter 2018Based on slides courtesy of Steve Marschner

Page 13: Surface shading: lights and rasterization

APPLICATION

COMMAND STREAM

VERTEX PROCESSING

TRANSFORMED GEOMETRY

RASTERIZATION

FRAGMENTS

FRAGMENT PROCESSING

FRAMEBUFFER IMAGE

DISPLAY

You are here

3D transformations, shading

Conversion of primitives to pixels

blending, compositing, shading

user sees this

Pipeline overview

CSE 167, Winter 2018 13

Page 14: Surface shading: lights and rasterization

Primitives• Points• Line segments (and chains of connected line segments)• Triangles• And that’s all!

– Curves• Approximate them with chains of line segments

– Polygons• Break them up into triangles

– Curved surfaces• Approximate them with triangles

• Trend over the decades towards minimal primitives– Simple, uniform, repetitive = good for parallelism

14CSE 167, Winter 2018

Page 15: Surface shading: lights and rasterization

Rasterization

• First job: enumerate the pixels covered by a primitive– Simple, aliased definition: pixels whose centers fall inside

• Second job: interpolate values across the primitive– e.g., colors computed at vertices– e.g., normals at vertices– e.g., texture coordinates

15CSE 167, Winter 2018

Page 16: Surface shading: lights and rasterization

Rasterizing lines

• Define line as a rectangle

• Specify by two endpoints

• Ideal image: black inside, white outside

16CSE 167, Winter 2018

Page 17: Surface shading: lights and rasterization

Point sampling

• Approximate rectangle by drawing all pixels whose centers fall within the line

• Problem: sometimes turns on adjacent pixels

17CSE 167, Winter 2018

Page 18: Surface shading: lights and rasterization

Point samplingin action

18CSE 167, Winter 2018

Page 19: Surface shading: lights and rasterization

Bresenham lines (midpoint algorithm)

• Point sampling unit width rectangle leads to uneven line width

• Define line width parallel to pixel grid

• That is, turn on the single nearest pixel in each column

19CSE 167, Winter 2018

Page 20: Surface shading: lights and rasterization

Midpoint algorithmin action

20CSE 167, Winter 2018

Page 21: Surface shading: lights and rasterization

Algorithms for drawing lines

• Line equation:y = b + mx

• Simple algorithm: evaluate line equation per column

• Without loss of generality, x0 < x1and 0 ≤ m ≤ 1

21CSE 167, Winter 2018

for x = ceil(x0) to floor(x1)y = b + m*xoutput(x, round(y))

y = 1.91 + 0.37 x

Page 22: Surface shading: lights and rasterization

Optimizing line drawing

• Multiplying and rounding is slow

• At each pixel the only options are E and NE

• d = m(x + 1) + b – y• d > 0.5 decides between E and NE

22CSE 167, Winter 2018

Page 23: Surface shading: lights and rasterization

Optimizing line drawing

• d = m(x + 1) + b – y• Only need to update d for integer steps in x and y

• Do that with addition

• Known as digital differential analyzer (DDA)

23CSE 167, Winter 2018

Page 24: Surface shading: lights and rasterization

Midpoint line algorithm

x = ceil(x0)y = round(m*x + b)d = m*(x + 1) + b – ywhile x < floor(x1)

if d > 0.5y += 1d –= 1

x += 1d += moutput(x, y)

24CSE 167, Winter 2018

Page 25: Surface shading: lights and rasterization

Linear interpolation

• We often attach attributes to vertices– For example, computed diffuse color of a hair being drawn using lines

– Desire for color to vary smoothly along a chain of line segments

• Linear interpolation in 1D– f(x) = (1 – α) y0 + α y1, where α = (x – x0) / (x1 – x0)

• In the 2D case of a line segment, α is just the fraction of the distance from (x0, y0) to (x1, y1)

25CSE 167, Winter 2018

Page 26: Surface shading: lights and rasterization

Linear interpolation

• Pixels are not exactly on the line

• Define 2D function by projection on line– This is linear in 2D, so DDA can be used to interpolate

26CSE 167, Winter 2018

Page 27: Surface shading: lights and rasterization

Alternate interpretation

• We are updating d and α as we step from pixel to pixel– d tells us how far from the line we are– α tells us how far along the line we are

• So d and α are coordinates in a coordinate system oriented to the line

27CSE 167, Winter 2018

Page 28: Surface shading: lights and rasterization

Alternate interpretation

• View loop as visiting all pixels the line passes  through– Interpolate d and αfor each pixel 

– Only output fragment if pixel is in band

• This makes linear interpolation the primary operation

28CSE 167, Winter 2018

Page 29: Surface shading: lights and rasterization

Pixel‐walk line rasterization

x = ceil(x0)y = round(m*x + b)d = m*x + b – ywhile x < floor(x1)

if d > 0.5y += 1; d –= 1;

elsex += 1; d += m;

if –0.5 < d ≤ 0.5output(x, y)

29CSE 167, Winter 2018

Page 30: Surface shading: lights and rasterization

Rasterizing triangles

• The most common case in most applications– With good antialiasing can be the only case, as some systems render a line as two skinny triangles

• Triangle represented by three vertices• Simple way to think of algorithm follows the pixel‐walk interpretation of line rasterization– Walk from pixel to pixel over (at least) the polygon’s area

– Evaluate linear functions as you go– Use those functions to decide which pixels are inside

30CSE 167, Winter 2018

Page 31: Surface shading: lights and rasterization

Rasterizing triangles

• Input:– Three 2D points (the triangle’s vertices in pixel space)

• (x0, y0); (x1, y1); (x2, y2)– Parameter values at each vertex

• q00, …, q0n; q10, …, q1n; q20, …, q2n

• Output:– A list of fragments, each with

• The integer pixel coordinates (x, y)• Interpolated parameter values q0, …, qn

31CSE 167, Winter 2018

Page 32: Surface shading: lights and rasterization

Rasterizing triangles

• Summary1.Evaluation of linear functions on pixel grid

2.Functions defined by parameter values  at vertices

3.Use of extra parameters to determine fragment set

32CSE 167, Winter 2018

Page 33: Surface shading: lights and rasterization

Incremental linear evaluation

• A linear (affine, really) function on the plane is:

• Linear functions are efficient to evaluate on a grid:

33CSE 167, Winter 2018

Page 34: Surface shading: lights and rasterization

Incremental linear evaluation

CSE 167, Winter 2018 34

linEval(xm, xM, ym, yM, cx, cy, ck) {

// setupqRow = cx*xm + cy*ym + ck;

// traversalfor y = ym to yM {

qPix = qRow;for x = xm to xM {

output(x, y, qPix);qPix += cx;

}qRow += cy;

}}

cx = .005; cy = .005; ck = 0(image size 100x100)

Page 35: Surface shading: lights and rasterization

Rasterizing triangles

• Summary1.Evaluation of linear functions on pixel grid

2.Functions defined by parameter values  at vertices

3.Use of extra parameters to determine fragment set

35CSE 167, Winter 2018

Page 36: Surface shading: lights and rasterization

Defining parameter functions

• To interpolate parameters across a triangle we need to find the cx, cy, and ck that define the (unique) linear function that matches the given values at all 3 vertices– 3 constraints on 3 unknown coefficients

– In matrix form

36CSE 167, Winter 2018

Page 37: Surface shading: lights and rasterization

Defining parameter functions

• More efficient version: shift origin to (x0, y0)

– Which is a 2x2 linear system (since q0 falls out):

– Solve using Cramer’s rule

37CSE 167, Winter 2018

Page 38: Surface shading: lights and rasterization

Defining parameter functions

CSE 167, Winter 2018 38

linInterp(xm, xM, ym, yM, x0, y0, q0,x1, y1, q1, x2, y2, q2) {

// setupdet = (x1–x0)*(y2–y0) – (x2–x0)*(y1–y0);cx = ((q1–q0)*(y2–y0) – (q2–q0)*(y1–y0)) / det;cy = ((q2–q0)*(x1–x0) – (q1–q0)*(x2–x0)) / det;qRow = cx*(xm–x0) + cy*(ym–y0) + q0;

// traversal (same as before)for y = ym to yM {

qPix = qRow;for x = xm to xM {

output(x, y, qPix);qPix += cx;

}qRow += cy;

}}

q = 0 q = 1

q = 0

Page 39: Surface shading: lights and rasterization

Interpolating several parameterslinInterp(xm, xM, ym, yM, n, x0, y0, q0[],

x1, y1, q1[], x2, y2, q2[]) {

// setupfor k = 0 to n–1

// compute cx[k], cy[k], qRow[k]// from q0[k], q1[k], q2[k]

// traversalfor y = ym to yM {

for k = 1 to n, qPix[k] = qRow[k];for x = xm to xM {

output(x, y, qPix);for k = 1 to n, qPix[k] += cx[k];

}for k = 1 to n, qRow[k] += cy[k];

}}

39CSE 167, Winter 2018

Page 40: Surface shading: lights and rasterization

Rasterizing triangles

• Summary1.Evaluation of linear functions on pixel grid

2.Functions defined by parameter values  at vertices

3.Use of extra parameters to determine fragment set

40CSE 167, Winter 2018

Page 41: Surface shading: lights and rasterization

Barycentric coordinates

• A coordinate system for triangles– Algebraic viewpoint

– Geometric viewpoint• Areas of subtriangles

• Triangle interior test

41CSE 167, Winter 2018

[Shirle

y 2000]

Page 42: Surface shading: lights and rasterization

Barycentric coordinates

• A coordinate system for triangles– Geometric viewpoint

• Distances

– Linear viewpoint• Basis of edges

42CSE 167, Winter 2018

Page 43: Surface shading: lights and rasterization

Barycentric coordinates

• Linear viewpoint– Basis for the plane

– Triangle interior test

43CSE 167, Winter 2018

[Shirle

y 2000]

Page 44: Surface shading: lights and rasterization

Clipping to the triangle

• Interpolate three  barycentric coordinates across the plane– Each barycentriccoordinate is 1 at one vertex and 0 at the other two

• Output fragments onlywhen all three are > 0

44CSE 167, Winter 2018

Page 45: Surface shading: lights and rasterization

Pixel‐walk (Pineda) rasterization

• Conservatively visit a superset of the pixels you want

• Interpolate linear functions

• Use those functions to determine when to emit a fragment

45CSE 167, Winter 2018

Page 46: Surface shading: lights and rasterization

Rasterizing triangles with shared edge

• Exercise caution with rounding and arbitrary decisions– Need to visit these pixels once

– But it is important not to visit them twice!

46CSE 167, Winter 2018

Page 47: Surface shading: lights and rasterization

Clipping

• Rasterizer tends to assume entire triangles are on screen

• After projection transformation (in normalized device coordinate frame)– Clip against the planes X, Y, Z = 1, –1

• 6 planes defining canonical view volume

– Primitive operation• Clip triangle against axis‐aligned plane

47CSE 167, Winter 2018

Page 48: Surface shading: lights and rasterization

Clipping a triangle against a plane

• 4 cases, based on sidedness of vertices– All inside (keep)– All outside (discard)– One inside, two outside (one clipped triangle)– Two inside, one outside (two clipped triangles)

48CSE 167, Winter 2018