cs475m - computer graphics › ~paragc › teaching › 2016 › ... · cs475m - computer graphics...

25
CS475m - Computer Graphics Lecture 1 : Rasterization Basics

Upload: others

Post on 04-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m - Computer Graphics

Lecture 1 : Rasterization Basics

Page 2: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image FormationLight Source

Camera

Image

World

Page 3: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image FormationLight Source

Camera

Image

World

Reflected Ray

Incident Ray

Transmitted Ray

Page 4: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Point Light Source

Camera

Image

World

Reflected Ray

Incident Ray

Transmitted Ray

Image Formation

Page 5: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image Formation

Camera

Image

World

Reflected Ray

Incident Ray

Transmitted Ray

● In this model can you reason about:

— Shadows?

— Area light sources?

— Diffraction?

— Energy transfer?

Point Light Source

Page 6: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image Formation

Camera

Image Model

World

Reflected Ray

Incident Ray

Transmitted Ray

Point Light Source

Page 7: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image ModelHow is this image drawn on the computer screen?

Page 8: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image ModelAn image is an array of raster elements called pixels.

Page 9: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image ModelAn image is an array of raster elements called pixels. Every pixel has at least a colour value.

Page 10: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image ModelLet's take a closer look.

Page 11: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image Model

How is the sphere drawn using the pixels?

Page 12: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image Model

To draw a geometrical figure... 

Page 13: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image Model

...we assign the correct pixels with the correct colour. This process is rasterization.

Page 14: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Image Model

Continue the pixel colouring to get regions filled with colour.

Page 15: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Why this image model?

Input

Processor

Framebuffer

Display

The framebuffer is a memory buffer storing the colour value for each pixel displayed.

Page 16: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Why this image model?

Input

Processor

Framebuffer

Display

So the image model mimics the memory model from hardware.

Page 17: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Why this image model?

Input

Processor

Framebuffer

Display

So the image model mimics the memory model from hardware. 

An alternate image model is a vector image model.

Page 18: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

How to colour the correct pixels?

function line(int x0, int x1, int y0, int y1)     int deltax = x1 ­ x0     int deltay = y1 ­ y0     float error = 0     float deltaerr = deltay / deltax    

// Assume deltax != 0 (line is not vertical),      // note that this division needs to be done in a way 

  that preserves the fractional part     int y = y0     for x = x0 to x1         plot(x,y)         error = error + deltaerr         if error ≥ 0.5 then             y = y + 1             error = error ­ 1.0

Bresenham's Line Drawing Algorithm

O(0,0)

(x1,y1)

(x0, y0)

X

Y

Page 19: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

How to colour the correct pixels?

Bresenham's Line Drawing Algorithm

● Extension for all line directions.

● Optimize.

● Demo

● Curves ­ read!

O(0,0)

(x1,y1)

(x0, y0)

X

Yfunction line(int x0, int x1, int y0, int y1)     int deltax = x1 ­ x0     int deltay = y1 ­ y0     float error = 0     float deltaerr = deltay / deltax    

// Assume deltax != 0 (line is not vertical),      // note that this division needs to be done in a way 

  that preserves the fractional part     int y = y0     for x = x0 to x1         plot(x,y)         error = error + deltaerr         if error ≥ 0.5 then             y = y + 1             error = error ­ 1.0

Page 20: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

How to fill pixels?O(0,0) X

Y

Page 21: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

How to fill pixels?O(0,0) X

Y

Page 22: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

How to fill pixels?

Page 23: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

How to fill pixels?

Scanfill Algorithm

O(0,0) X

Y

Page 24: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Edge Ymin

Ymax

X for Y=Y

min

1/m

e2

V3

V2

X3

1/m2

e3

V3

V4

X3

1/m3

e1

V1

V2

X1

1/m1

e5

V1

V5

X1

1/m5

e4

V4

V5

X4

1/m4

How to fill pixels – Scanfill Algorithm

V1

V2

V5

V4

V3e1

e4

e3

e2

e5

O(0,0) X

Y

The Edge List

● Edges in the edge list become active when the y­coordinate of the current scan line matches their Ymin value.

● First intersection point between an active edge and a scan line is always the endpoint corresponding to Ymin.

Page 25: CS475m - Computer Graphics › ~paragc › teaching › 2016 › ... · CS475m - Computer Graphics Lecture 1 : Rasterization Basics. CS475m: Lecture 1 Parag Chaudhuri Image Formation

CS475m: Lecture 1 Parag Chaudhuri

Inside / Outside

Special Cases

How to fill pixels – Scanfill Algorithm

● For monotonically increasing/decreasing edges across a shared vertex count one intersection.

● Else count two.

● Ignore horizontal edges.