CS380: Introduction to Computer Graphics Rasterization ...

43
Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012 CS380: Introduction to Computer Graphics Rasterization Chapter 12 Min H. Kim KAIST School of Computing

Transcript of CS380: Introduction to Computer Graphics Rasterization ...

Page 1: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

CS380: Introduction to Computer GraphicsRasterizationChapter 12

Min H. KimKAIST School of Computing

Page 2: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

SUMMARYDepth

2

Page 3: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Visibility

• We could explicitly store everything hit along a ray and then compute the closest.– Make sense in a ray tracing setting, where we are

working one pixel per ray at a time, but not for OpenGL, where we are working one triangle at a time.

3

Page 4: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Z-buffer• We will use z-buffer• Triangle are drawn in any order• Each pixel in frame buffer stores ‘depth’ value of

closest geometry observed so far.• When a new triangle tries to set the color of a

pixel, we first compare its depth to the value stored in the z-buffer.

• Only if the observed point in this triangle is closer, we overwrite the color and depth values of this pixel.

4

Page 5: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Proj. Trans.: Eye coor. à NDC

5

• Camera projection transformation

xnwn

ynwn

znwn

wn

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

=

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

=M

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

M =

1atan θ

2⎛

⎝⎜⎞

⎠⎟

0 0 0

0 1tan θ

2⎛

⎝⎜⎞

⎠⎟

0 0

0 0 f +nf −n

−2 fnf −n

0 0 −1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥

or

M =

−2nr − l

0 r+ lr − l

0

0 −2nt −b

t +bt −b

0

0 0 f +nf −n

−2 fnf −n

0 0 −1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥

Page 6: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Stages of vertex transformation

6

Modelviewmatrix

Projectionmatrix

Perspectivedivision

Viewporttransforma

tion

eyecoordinates

http://www.glprogramming.com/red/chapter03.html

clipcoordinates

normalized devicecoordinates

windowcoordinates

vertex

E−1O

xoyozo1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

xoyozo1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

M

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

1wn

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

=

xnynzn1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Page 7: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

RASTERIZATIONChapter 12

7

Page 8: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Vertex shader (per vertex)• Takes the object coordinates of every vertex

position and turns them into eye coordinates, as well as the vertex’s normal coordinates

8

#version 130

uniform Matrix4 uModelViewMatrix;uniform Matrix4 uNormalMatrix;uniform Matrix4 uProjMatrix;

in vec3 aColor;in vec4 aNormal;in vec4 aVertex;

out vec3 vColor;out vec3 vNormal;out vec3 vPosition;

void main(){

vColor = aColor;vPosition = uModelViewMatrix * aVertex;vec4 normal = vec4(aNormal.x, aNormal.y,

aNormal.z, 0.0);vNormal = vec3(uNormalMatrix * normal);gl_Position = uProjMatrix * vPosition;

}

E−1Oc

PE−1OcCamera projection (3D à 2D)

Vertex in the eye frame (no projection here!)

Page 9: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Fragment shader (pixel color)

9

#version 130

uniform vec3 uLight1, uLight2; // the eye coordinates of lightsuniform vec3 uColor; // use glVertexUniform3f call to pass them to the shaders

in vec3 vNormal; // eye coordinatesin vec3 vPosition; // eye coordinates

out fragColor; // output variable of the interpolated color data per pixel

void main(){

vec3 toLight = normalize(uLight – vec3(vPosition)); // direction to the point lightvec3 normal = normalize(vNormal);float diffuse = max(0.0, dot(normal, toLight1)); // negative clamping of the cosine lawdiffuse += max(0.0, dot(normal, toLight2)); // add light2 energyvec3 intensity = uColor * diffuse; // only apply diffuse reflectance function of BRDFfragColor = vec4(intensity.x, intensity.y, intensity.z, 1.0); // 4th element is opacity.

}

Vertex in the eye frame (no projection here!)

Page 10: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Path from vertex to pixel

10

• Three vertices have passed through the vertex shader

• Follow it journey to be a bunch of pixels

Page 11: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Varying variables• Varying variables provides an interface between the

vertex and fragment shader. • When the primitives (triangles) are assembled and

fragments computed, for each fragment there is a set of variables that are interpolated automatically and provided to the fragment shader.

• An example is the color of the fragment. The color that arrives at the fragment shader is the result of the interpolation of the colors of the vertices that make up the primitive.– varying float intensity;

11

Page 12: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Clipping• What if there is a vertex behind you?

12

Δp1p2p3

p1 p2

p2

Page 13: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Clipping• Not only front vertex

but also back vertex will be projected

• We will be drawing in completely the wrong area.

13

• Beware interpolation nature of OpenGL:Projection of vertices followed by interpolation

p1 p2

p2

Page 14: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Clipping• Processing for

triangles that are fully or partially out of view

• We don’t want to see behind us

• We want to minimize processing

• The tricky part will be to deal with eye-spanning triangles.

14

p1 p2

Page 15: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Eye spanners• Back vertex

projects higher up in the image

• Filling in the in-between pixels willfill in the wrong region.

• Solution: slice up the geometry by the six faces of the view frustum

15

p1 p2

Page 16: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Clipping coordinates• Primitives (triangles) totally inside the clipping volume

are not altered. • Primitives outside the viewing volume are discarded. • Primitives whose edges intersect the boundaries of the

clipping volume are clipped (learn later).

16

Page 17: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Clipping coordinates• Eye coordinates (projected) à clip coordinates à normalized device coordinates (NDCs)

• (reminder) Dividing clip coordinates by the component (the fourth component in the homogeneous coordinates) yields normalized device coordinates (NDCs).

17

(xc , yc , zc ,wc )wc(wc = wn )

xnwn

ynwn

znwn

wn

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xcyczcwc

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

sx 0 −cx 00 sy −cy 0

0 0 f + nf − n

− 2 fnf − n

0 0 −1 0

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Page 18: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Clipping coordinates• Suppose that is the same as .

What if is zero? • If you wait for normalized device coordinates (NDCs)

where the vertex has flipped, and it’s too late to do the clipping (à divided-by-zero error!).

• We could do in the eye space, but then would need to use the camera parameters

• The solution is to use clip coordinates:post-matrix-multiply but pre-divide.

• No divide = no flipping

18

xc /wc −xc / −wc

wc

E−1O

xoyozo1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

M

xeyeze1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

1wn

xcyczcwc

⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥

=

xnynzn1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥ wn =wc = −ze

Page 19: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Clipping coordinates• Recall that we want points in the range:

• In clip coordinates this is:

19

−1< xn <1−1< yn <1−1< zn <1

−wc < xc < wc

−wc < yc < wc

−wc < zc < wc

xnwn

ynwn

znwn

wn

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xcyczcwc

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Page 20: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Clipping coordinates à NDCs• Clipping is done, we can now divide by

to obtain normalize device coordinates (NDCs).

20

wc (typically -ze )

xnynzn1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

xc /wc

yc /wc

zc /wc

wc /wc

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

wn =wc = −ze

Page 21: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Backface culling (killing?)• When drawing a closed solid object, we will only

ever see one ‘front’ side of each triangle.• For efficiency we can drop these from the

processing.

21

Page 22: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Without backface culling

22

Page 23: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

With backface culling

23

Page 24: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Backface culling• To do this, in OpenGL, we use the convention of

ordering the three vertices in the draw call (IBO/VBO) so that they are counterclockwise (CCW) when looking at its front side.

• During setup, we call glEnable(GL_CULL_FACE)• To implement culling, OpenGL does the

following…

24

Page 25: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

• Let be the three vertices of the triangle projected down to the plane.

• Define the vector • Next compute the cross

product• If the three vertices are

counterclockwise in theplane, then will be in the direction (frontface).

Math of Backface Culling

25

p1, p2, and p3(xn , yn ,0)

a = p3 − p2 and

b = p1 − p2

c = a ×

b

c

+zn

NB There are typos at page 114 in our textbook.The negative and positive were flipped around in the textbook. These slides are typo-corrected.

Page 26: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

• So the area of the triangle is: • Taking account of the direction,

we could calculate the direction of the cross product of these two vectors.

• If this is negative, the polygonlooks backward

Math of Backface Culling

26

Area = 1

2a ×b

(xn3 − xn

2 )(yn1 − yn

2 )− (yn3 − yn

2 )(xn1 − xn

2 )

NB There are typos at page 114 in our textbook.The negative and positive were flipped around in the textbook. These slides are typo-corrected.

Page 27: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Viewport (NDC à Window)• Now we want to position the vertices in the

window. So it is time to move the NDCs to window coordinates.– in NDCs, lower left corner is [-1, -1]t and upper right

corner is [1,1]t.

• Each pixel center has a positive integer coordinate.– This will make subsequent pixel computations more

natural.

• We want the lower left pixel center to have 2D window coordinates of and the upper right pixel center to have coordinates

27

[0,0]t

[W −1,H−1]t

Page 28: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Viewport• We think of each pixel as owning the real estate

which extends 0.5 pixel units in the positive and negative, horizontal and vertical directions from the pixel center.

28

12 pixels

8 pixels

Page 29: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Viewport• Thus the extent of 2D window rectangle covered

by the union of all our pixels is the rectangle in window coordinates with lower left corner

and upper right corner • NB W and H are zero-based indices.

29

[−0.5,−0.5]t [W − 0.5,H − 0.5]t

Page 30: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Summary: Depth values

• With this option on:• With near/far field à– Far object:– Near object:

30

zn =

1−ze

ze =−1,000,000 ⇒ zn =

11,000,000 ≈0

ze =−1 ⇒ zn =1

glDepthFunc(GL_GREATER)

−1(far)< zn <1(near)

NB The default of OpenGL is glDepthFunc(GL_LESS)!

−2nr − l

0 r+ lr − l

0

0 −2nt −b

t +bt −b

0

0 0 f +nf −n

−2 fnf −n

0 0 −1 0

⎢⎢⎢⎢⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥⎥⎥⎥⎥

Page 31: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Viewport matrix

31

• We need a transform that maps the lower left corner to and upper right corner to

• The appropriate scale and shift can be done using the viewport matrix:

[−0.5,−0.5]t

[W − 0.5,H − 0.5]t

xwywzw1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

W /2 0 0 (W −1)/20 H /2 0 (H−1)/20 0 1/2 1/20 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

xnynzn1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

zw=-1 is far and zw=1 is near è zw=0 is far and zw=1 is near.

Page 32: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Viewport matrix• This does a scale and shift in both x and y• You can verify that it maps the corners

appropriately• In OpenGL, we set up this viewport matrix with

the call glViewport(0,0,W,H)• The third row of this matrix is used to map the

range of values to the more convenient range.

• So now (in our conventions), is far andis near.

32

[−1,−1] zn[0...1]

zw =0zw =1

Page 33: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Viewport matrix• So we must also tell OpenGL that when we clear

the z-buffer, we should set it to 0; we do this with the call glClearDepth(0.0)

33

Page 34: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Texture Viewport• The abstract domain for textures is not the

canonical square, but instead is the unit square.• Its lower left corner is [0,0]t and upper right

corner is [1,1]t.• In this case the coordinate transformation

matrix is:

34

xwyw−1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

=

W 0 0 −1/20 H 0 −1/2− − − −0 0 0 1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

xtyt−1

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

Page 35: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Summary of origins

35

OpenGLNDCs�

(0,0)

(-1,-1)

(1,1)(-1,1)

(1,-1)

GLUT & Image

Coordinates

(0,1)

(1,0)(0,0)

(1,1)

OpenGL

Texture

Coordinates

(0,0)

(1,1)(0,1)

(1,0)

OpenGL

viewport

coordinates

(0,0)

(W-1,H-1)(0,H-1)

(W-1,0)

Page 36: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Rasterization• Starting from the window coordinates for the three

vertices, the rasterizer needs to figure out which pixel centers are inside of the triangle (per each primitive).

• Each triangle on the screen can be defined as the intersection of three half-spaces.

36

Page 37: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Rasterization• Starting from the window coordinates for the three

vertices, the rasterizer needs to figure out which pixel centers are inside of the triangle (per each primitive).

• Each triangle on the screen can be defined as the intersection of three half-spaces.

37

Page 38: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Math of rasterization• Each such halfspace is defined by a line that

coincides with one of the edges of the triangle, and can be tested using an ‘edge function’ of the form:

where the are constants thatdepend on thegeometry of the edge.

38

edge=axw +byw +c(a,b,c)

Page 39: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Math of rasterization• A positive value of this function at a pixel with

coordinates means that the pixel is inside the specified halfspace (on the left-hand side).

• If all three tests pass, then the pixel is inside the triangle.

39

[xw , yw ]t

posit

ive

Page 40: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Speed up• Only look at pixels in the bounding box of the

triangle• Test if a pixel block is entirely outside of triangle• Use incremental calculations along a scanline.

40

Page 41: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Interpolation• As input to rasterization, each vertex also has

some auxiliary data associated with it.– This data includes a value,– As well as other data that is related, but not identical

to the varying variables.

• It is also the job of the rasterizer to linearly interpolate this dataover the triangle.

41

zw

Page 42: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Math of interpolation• Each such value to be linearly interpolated can

be represented as an affine function over screen space with the form:

• An affine function can be easily evaluated at each pixel by the rasterizer.

• Indeed, this is no different from evaluating the edge test functions just described.

42

v

v = axw + byw + c

p = a+α(b−a)p = a+α!vp = (1−α)a+αb

a bp

α (1-α) !v

Page 43: CS380: Introduction to Computer Graphics Rasterization ...

Min H. Kim (KAIST) Foundations of 3D Computer Graphics, S. Gortler, MIT Press, 2012

Boundaries• For pixel on edge or vertex it should be rendered

exactly once.• Need special care in the implementation to

avoid duty edge representation (learn later).

43