CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines...

32
L:CC, MI:ERSI CG T5 - Rasterization Miguel Tavares Coimbra (course and slides designed by Verónica Costa Orvalho)

Transcript of CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines...

Page 1: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

L:CC, MI:ERSI

CG – T5 - Rasterization

Miguel Tavares Coimbra

(course and slides designed by

Verónica Costa Orvalho)

Page 2: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

What is rasterization?

CG 12/13 - T5

Page 3: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Basic steps for creating a 2D image

out of a 3D world

CG 12/13 - T5

• Create the 3D world

– Vertexes and triangles in a 3D space

• Project it to a 2D ‘camera’

– Use perspective to transform coordinates into a

2D space

• Paint each pixel of the 2D image

– Rasterization, shading, texturing

– Will break this into smaller things later on

• Enjoy the super cool image you have created

Today

Page 4: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

pipeline

. collision detection

. animation global acceleration . physics simulation process on CPU or GPU

. transformation

. projection Computes: . what is to be drawn . how should be drawn . where should be drawn process on GPU

. draws images generated by geometry stage process on GPU CG 12/13 - T5

Page 5: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Rasterization

rasterization:

wireframe

filling with colors

CG 12/13 - T5

Page 6: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

CG 12/13 - T5 Slide by Ron Fedkiw, Stanford University

Page 7: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

How do we do this?

CG 12/13 - T5

Page 8: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Primitives

• Only three!

– Points

– Line segments

– Triangles

• How do I rasterize them?

– Points are simple

– Lines?

– Triangles?

CG 12/13 - T5

Page 9: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Rasterizing lines

• Lines are defined by

two points

– Projected into my 2D

screen from my 3D

world

• Consider it a

rectangle

– So that it occupies a

non-zero area

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 10: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Rasterizing lines

• Lines are defined by

two points

– Projected into my 2D

screen from my 3D

world

• Consider it a

rectangle

– So that it occupies a

non-zero area

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 11: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Point sampling

• Draw all the pixels

whose centers fall

within the rectangle

• It may draw undesired

adjacent pixels…

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 12: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Point sampling

• Draw all the pixels

whose centers fall

within the rectangle

• It may draw undesired

adjacent pixels…

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 13: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 14: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Bresenham lines (midpoint alg.)

• Idea:

– Define line width

parallel to pixel grid

• What does this

mean?

– Turn on the single

nearest pixel in each

column

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 15: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Bresenham lines (midpoint alg.)

• Idea:

– Define line width

parallel to pixel grid

• What does this

mean?

– Turn on the single

nearest pixel in each

column

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 16: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 17: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Algorithms for drawing lines

• Simple

– Evaluate line equation

per column

• Line equation

– y=b+m.x

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 18: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Optimized line drawing

• Multiplying and rounding is slow

• We can add the vertical displacement to our previous vertical coordinate (d) – Initially: d = m(x+1)+b-y

– Then: d+=m

• We call this DDA (digital differential analyzer)

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 19: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Interpolation along lines

• We don’t want to simply

know which pixels are

on the line

– Boolean

• Vertexes hold attributes

– Ex: Color

• We want these to vary

smoothly along the line

– Linear interpolation

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 20: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Linear interpolation

• Pixels are not exactly

on the line

• Must project pixels on

the line for the correct

percentage

• We can use DDA!

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 21: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Linear interpolation

• Pixels are not exactly

on the line

• Must project pixels on

the line for the correct

percentage

• We can use DDA!

CG 12/13 - T5 Adapted from Steve Marschner, Cornell University

Page 22: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

What about triangles?

CG 12/13 - T5

Page 23: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Rasterizing triangles

• Pixel belongs to the

triangle if its center is

inside the triangle

• Need two things:

– Which pixels belong to

the triangle?

– How do we interpolate

values from 3

vertexes?

CG 12/13 - T5 Adapted from Ron Fedkiw, Stanford University

Page 24: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Using directed lines

• Point is inside the

triangle if it is on the

left of three directed

lines

– They could be on the

right too…

• How do we build a

simple test for this?

CG 12/13 - T5 Adapted from Ron Fedkiw, Stanford University

Page 25: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Start by defining a directed line

CG 12/13 - T5 Adapted from Ron Fedkiw, Stanford University

Page 26: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Easy to obtain its normal

CG 12/13 - T5 Adapted from Ron Fedkiw, Stanford University

Page 27: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Dot product gives us a simple test

CG 12/13 - T5 Adapted from Ron Fedkiw, Stanford University

Page 28: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Using our coordinate system

CG 12/13 - T5 Adapted from Ron Fedkiw, Stanford University

Page 29: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Line divides the plane in two

CG 12/13 - T5 Adapted from Ron Fedkiw, Stanford University

Page 30: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Point inside triangle test

CG 12/13 - T5 Adapted from Ron Fedkiw, Stanford University

Page 31: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Barycentric interpolation

CG 12/13 - T5 Adapted from Ron Fedkiw, Stanford University

Advanced topic!

Check the ref. book

Page 32: CG T5 - Rasterizationmcoimbra/lectures/CG_1213/CG_1213_T5_Raster… · Interpolation along lines • We don’t want to simply know which pixels are on the line – Boolean • Vertexes

Summary

• Rasterization

– Which pixels belong to the primitive

– How do I interpolate vertex atributes?

• Lines

– Consider them rectangles

– Linear interpolation

• Triangles

– Use three directed lines

– Barycentric interpolation

CG 12/13 - T5