CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

26
CS 445 / 645 Introduction to Computer Graphics Lecture 18 Lecture 18 Shading Shading

Transcript of CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Page 1: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

CS 445 / 645Introduction to Computer Graphics

Lecture 18Lecture 18

ShadingShading

Lecture 18Lecture 18

ShadingShading

Page 2: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Ray Tracing Assignment

Deadline extended until Tuesday the 13thDeadline extended until Tuesday the 13thDeadline extended until Tuesday the 13thDeadline extended until Tuesday the 13th

Page 3: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Ray Tracing Assignment

Barycentric coordinates Barycentric coordinates (M(Mööbius, 1827)bius, 1827)

• Consider a triangle defined by (AConsider a triangle defined by (A11, A, A22, A, A33))

– These points are defined relative to world originThese points are defined relative to world origin

– A point within triangle could also be defined as (x, y, z) A point within triangle could also be defined as (x, y, z) relative to world originrelative to world origin

• A point can be defined as (tA point can be defined as (t11, t, t22, t, t33) corresponding to its ) corresponding to its

position with respect to Aposition with respect to A11, A, A22, A, A33..

Barycentric coordinates Barycentric coordinates (M(Mööbius, 1827)bius, 1827)

• Consider a triangle defined by (AConsider a triangle defined by (A11, A, A22, A, A33))

– These points are defined relative to world originThese points are defined relative to world origin

– A point within triangle could also be defined as (x, y, z) A point within triangle could also be defined as (x, y, z) relative to world originrelative to world origin

• A point can be defined as (tA point can be defined as (t11, t, t22, t, t33) corresponding to its ) corresponding to its

position with respect to Aposition with respect to A11, A, A22, A, A33..

Page 4: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Barycentric Coordinates

Solve for (tSolve for (t11, t, t22, t, t33) such that) such that

– tt11 + t + t22 + t + t3 3 = 1= 1

– tt11AA11 + t + t22AA22 + t + t33AA33 = P = P

Solve for (tSolve for (t11, t, t22, t, t33) such that) such that

– tt11 + t + t22 + t + t3 3 = 1= 1

– tt11AA11 + t + t22AA22 + t + t33AA33 = P = P

mathworld.com

Page 5: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Barycentric Coordinates

An observationAn observation

• tt11, t, t22, and t, and t3 3 are weights such are weights such

that when they are used to that when they are used to represent the mass at the represent the mass at the vertices of the triangle, P is vertices of the triangle, P is at its center of massat its center of mass

• tt11, t, t22, and t, and t33 are weights that represent the ratio of the area of are weights that represent the ratio of the area of

each of the three subtriangles to the area of the wholeeach of the three subtriangles to the area of the whole

An observationAn observation

• tt11, t, t22, and t, and t3 3 are weights such are weights such

that when they are used to that when they are used to represent the mass at the represent the mass at the vertices of the triangle, P is vertices of the triangle, P is at its center of massat its center of mass

• tt11, t, t22, and t, and t33 are weights that represent the ratio of the area of are weights that represent the ratio of the area of

each of the three subtriangles to the area of the wholeeach of the three subtriangles to the area of the whole

Page 6: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Barycentric coordinates

The cross product computes a triangle’s areaThe cross product computes a triangle’s area

• || (A|| (A2 2 - A- A11) x (A) x (A33-A-A11) || = (area of A) || = (area of A11AA22AA33)) * 2* 2

Where || x || = the area of the triangle x… the cross productWhere || x || = the area of the triangle x… the cross product

The cross product computes a triangle’s areaThe cross product computes a triangle’s area

• || (A|| (A2 2 - A- A11) x (A) x (A33-A-A11) || = (area of A) || = (area of A11AA22AA33)) * 2* 2

Where || x || = the area of the triangle x… the cross productWhere || x || = the area of the triangle x… the cross product

Page 7: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Barycentric Coordinates

All points of triangle are unique, all points in All points of triangle are unique, all points in space can be represented with barycentric space can be represented with barycentric coordinatescoordinates

• AA11, A, A22, and A, and A33 form an affine space form an affine space

• If 0 If 0 ≤ t≤ t11, t, t22, t, t33 ≤ 1, the point is in the triangle ≤ 1, the point is in the triangle

All points of triangle are unique, all points in All points of triangle are unique, all points in space can be represented with barycentric space can be represented with barycentric coordinatescoordinates

• AA11, A, A22, and A, and A33 form an affine space form an affine space

• If 0 If 0 ≤ t≤ t11, t, t22, t, t33 ≤ 1, the point is in the triangle ≤ 1, the point is in the triangle

Page 8: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Applying Illumination

We have an illumination model for a point on a We have an illumination model for a point on a surfacesurface

Assuming that our surface is defined as a mesh of Assuming that our surface is defined as a mesh of polygonal facets, polygonal facets, which pointswhich points should we use? should we use?

Keep in mind:Keep in mind:

• It’s a fairly expensive calculationIt’s a fairly expensive calculation

• Several possible answers, each with different implications Several possible answers, each with different implications for the visual quality of the resultfor the visual quality of the result

We have an illumination model for a point on a We have an illumination model for a point on a surfacesurface

Assuming that our surface is defined as a mesh of Assuming that our surface is defined as a mesh of polygonal facets, polygonal facets, which pointswhich points should we use? should we use?

Keep in mind:Keep in mind:

• It’s a fairly expensive calculationIt’s a fairly expensive calculation

• Several possible answers, each with different implications Several possible answers, each with different implications for the visual quality of the resultfor the visual quality of the result

Page 9: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Applying Illumination

With polygonal/triangular models:With polygonal/triangular models:

• Each facet has a constant surface normalEach facet has a constant surface normal

• If the light is directional, the diffuse reflectance is constant If the light is directional, the diffuse reflectance is constant across the facet. across the facet. Why?Why?

With polygonal/triangular models:With polygonal/triangular models:

• Each facet has a constant surface normalEach facet has a constant surface normal

• If the light is directional, the diffuse reflectance is constant If the light is directional, the diffuse reflectance is constant across the facet. across the facet. Why?Why?

Page 10: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Flat ShadingThe simplest approach, The simplest approach, flat shadingflat shading,, calculates illumination at a single point for calculates illumination at a single point for

each polygon:each polygon:

If an object really If an object really isis faceted, is this accurate? faceted, is this accurate?

The simplest approach, The simplest approach, flat shadingflat shading,, calculates illumination at a single point for calculates illumination at a single point for each polygon:each polygon:

If an object really If an object really isis faceted, is this accurate? faceted, is this accurate?

Page 11: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Is flat shading realistic for faceted object?

No:No:

• For point sources, the direction to light varies across the facetFor point sources, the direction to light varies across the facet

No:No:

• For point sources, the direction to light varies across the facetFor point sources, the direction to light varies across the facet

– For specular reflectance, direction to eye varies across the facet

Page 12: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Flat Shading

We can refine it a bit by evaluating the Phong We can refine it a bit by evaluating the Phong lighting model at each pixel of each polygon, lighting model at each pixel of each polygon, but the result is still clearly faceted:but the result is still clearly faceted:

We can refine it a bit by evaluating the Phong We can refine it a bit by evaluating the Phong lighting model at each pixel of each polygon, lighting model at each pixel of each polygon, but the result is still clearly faceted:but the result is still clearly faceted:

Page 13: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Vertex Normals

To get smoother-looking surfacesTo get smoother-looking surfaceswe introduce we introduce vertex normalsvertex normals at each at eachvertexvertex

• Usually different from facet normalUsually different from facet normal

• Used Used onlyonly for shadingfor shading

• Think of as a better approximation of theThink of as a better approximation of the realreal surface that the surface that the polygons approximatepolygons approximate

To get smoother-looking surfacesTo get smoother-looking surfaceswe introduce we introduce vertex normalsvertex normals at each at eachvertexvertex

• Usually different from facet normalUsually different from facet normal

• Used Used onlyonly for shadingfor shading

• Think of as a better approximation of theThink of as a better approximation of the realreal surface that the surface that the polygons approximatepolygons approximate

Page 14: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Vertex Normals

Vertex normals may be Vertex normals may be

• Provided with the modelProvided with the model

• Computed from first principles Computed from first principles

• Approximated by Approximated by averaging the normals averaging the normals of the facets that of the facets that share the vertexshare the vertex

Vertex normals may be Vertex normals may be

• Provided with the modelProvided with the model

• Computed from first principles Computed from first principles

• Approximated by Approximated by averaging the normals averaging the normals of the facets that of the facets that share the vertexshare the vertex

Page 15: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Gouraud Shading

This is the most common approachThis is the most common approach• Perform Phong lighting at the verticesPerform Phong lighting at the vertices

• Linearly interpolate the resulting colors over facesLinearly interpolate the resulting colors over faces

– Along edgesAlong edges

– Along scanlinesAlong scanlines

This is the most common approachThis is the most common approach• Perform Phong lighting at the verticesPerform Phong lighting at the vertices

• Linearly interpolate the resulting colors over facesLinearly interpolate the resulting colors over faces

– Along edgesAlong edges

– Along scanlinesAlong scanlinesC1

C2

C3

c1 + t1(c2-c1)

c1 + t2(c3-c1)c1 + t1(c2-c1) + t3(c1 + t2(c3-c1)- c1 + t1(c2-c1))

– This is what OpenGL does

Does this eliminate the facets?

Page 16: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Gouraud Shading

ArtifactsArtifacts

• Often appears dull, chalkyOften appears dull, chalky

• Lacks accurate specular componentLacks accurate specular component

– If included, will be averaged over entire polygonIf included, will be averaged over entire polygon

ArtifactsArtifacts

• Often appears dull, chalkyOften appears dull, chalky

• Lacks accurate specular componentLacks accurate specular component

– If included, will be averaged over entire polygonIf included, will be averaged over entire polygonC1

C2

C3

Can’t shade that effect!

Page 17: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Gouraud Shading

C1

C2

C3

Artifacts– Mach Banding

Artifact at discontinuities in intensity or intensity slope

C4

Discontinuity in rateof color change

occurs herehttp://www.edcenter.sdsu.edu/slides/GA/visteacher/sld048.htm

Page 18: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Phong Shading

Phong shadingPhong shading is is notnot the same as Phong lighting, the same as Phong lighting, though they are sometimes mixed upthough they are sometimes mixed up• Phong lighting:Phong lighting: the empirical model we’ve been discussing the empirical model we’ve been discussing

to calculate illumination at a point on a surfaceto calculate illumination at a point on a surface

• Phong shading:Phong shading: linearly interpolating the surface normal linearly interpolating the surface normal across the facet, applying the Phong lighting model at across the facet, applying the Phong lighting model at every pixelevery pixel

– Same input as Gouraud shadingSame input as Gouraud shading

– Usually very smooth-looking results:Usually very smooth-looking results:

– But, considerably more expensiveBut, considerably more expensive

Phong shadingPhong shading is is notnot the same as Phong lighting, the same as Phong lighting, though they are sometimes mixed upthough they are sometimes mixed up• Phong lighting:Phong lighting: the empirical model we’ve been discussing the empirical model we’ve been discussing

to calculate illumination at a point on a surfaceto calculate illumination at a point on a surface

• Phong shading:Phong shading: linearly interpolating the surface normal linearly interpolating the surface normal across the facet, applying the Phong lighting model at across the facet, applying the Phong lighting model at every pixelevery pixel

– Same input as Gouraud shadingSame input as Gouraud shading

– Usually very smooth-looking results:Usually very smooth-looking results:

– But, considerably more expensiveBut, considerably more expensive

Page 19: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Phong Shading

Linearly interpolate the vertex normalsLinearly interpolate the vertex normals

• Compute lighting equations at each pixelCompute lighting equations at each pixel

• Can use specular componentCan use specular component

Linearly interpolate the vertex normalsLinearly interpolate the vertex normals

• Compute lighting equations at each pixelCompute lighting equations at each pixel

• Can use specular componentCan use specular componentN1

N2

N3

N4

lights

i

n

isidiambientatotal

shinyRVkLNkIIkI

#

1

ˆˆˆˆ

Remember: Normals used in diffuse and specular terms

Discontinuity in normal’s rate of change is harder to detect

Page 20: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Shortcomings of Shading

Polygonal silhouettes remainPolygonal silhouettes remainPolygonal silhouettes remainPolygonal silhouettes remain

Gouraud Phong

Page 21: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Perspective Distortion

Z – into the scene

Imageplane

Notice that linear interpolation in screen spacedoes not align with linear interpolation in world space

Break up large polygonswith many smaller ones

Nonlinear color shift fromblue (left) to red (right)

Linear interpolationin image space isnot accuratesampling of 3Dspace

Polygon

Page 22: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Perspective Distortion

Notice that linear interpolation in screen spacedoes not align with linear interpolation in world space

Z – into the scene

Imageplane Break up large polygons

with many smaller ones

Page 23: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Interpolation dependent on polygon orientation

A

D

C

B

Interpolate betweenAB and AD

B

A

D

C

Interpolate betweenCD and AD

Rotate -90o

and colorsame point

Page 24: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Problems at Shared Vertices

B

A

C

Vertex B is shared by the two rectangles on the right, but not by the one on the left

E

D

F

H

GThe first portion of the scanlineis interpolated between DE and AC

The second portion of the scanlineis interpolated between BC and GH

A large discontinuity could arise

Page 25: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Bad Vertex Averaging

Page 26: CS 445 / 645 Introduction to Computer Graphics Lecture 18 Shading Shading.

Shading Models (Direct lighting)

Flat ShadingFlat Shading• Compute Phong lighting once for entire polygonCompute Phong lighting once for entire polygon

Gouraud ShadingGouraud Shading• Compute Phong lighting at the vertices and interpolate Compute Phong lighting at the vertices and interpolate

lighting values across polygonlighting values across polygon

Phong ShadingPhong Shading• Interpolate normals across polygon and perform Phong Interpolate normals across polygon and perform Phong

lighting across polygonlighting across polygon

Flat ShadingFlat Shading• Compute Phong lighting once for entire polygonCompute Phong lighting once for entire polygon

Gouraud ShadingGouraud Shading• Compute Phong lighting at the vertices and interpolate Compute Phong lighting at the vertices and interpolate

lighting values across polygonlighting values across polygon

Phong ShadingPhong Shading• Interpolate normals across polygon and perform Phong Interpolate normals across polygon and perform Phong

lighting across polygonlighting across polygon