55
February 5, 2015 1 [email protected] University of Kufa

Advanced computer graphics - Shading

Embed Size (px)

Citation preview

Page 1: Advanced computer graphics - Shading

February 5, 2015 1 [email protected]

University of Kufa

Page 2: Advanced computer graphics - Shading

February 5, 2015 2

Why we need shading

Suppose we build a model of a

sphere using many polygons and

color it. We get something like

But we want

[email protected]

Page 3: Advanced computer graphics - Shading

February 5, 2015 3

Shading refers to the application of a reflection model over the surface of

an object.

To use the lighting and reflectance model to

shade facets of a polygonal mesh — that is, to

assign intensities to pixels to give the

impression of opaque surfaces rather than

wireframes.

Shading is a way to paint the object with light

What is Shading

[email protected]

Page 4: Advanced computer graphics - Shading

February 5, 2015 4

In creating and interpreting images, we need to understand two things:

• Geometry –Where scene points appear in the image (image locations)

• Radiometry –How “bright” they are (image Computer Graphics values)

Geometric enables us to know something about the scene location of a point

imaged at pixel (u, v)

Radiometric enables us to know what a pixel value implies about surface

lightness and illumination

Geometry and Radiometry

[email protected]

Page 5: Advanced computer graphics - Shading

February 5, 2015 5

• For “matte” objects with no shininess

• Diffuse/matte objects are called Lambertian

• In general, shading does not vary with viewpoint

ƒ Example: a piece of paper

• Reflected light is scattered equally in all directions

Diffuse Objects

[email protected]

Page 6: Advanced computer graphics - Shading

February 5, 2015 6

Lambertian Surface Obeys Lambert’s Law (from physics): The color, c, of a

surface is proportional to the cosine of the angle between the surface normal

and the direction of the light source

Lambertian Surface

[email protected]

Page 7: Advanced computer graphics - Shading

February 5, 2015 7

Lambertian Surface

[email protected]

Page 8: Advanced computer graphics - Shading

February 5, 2015 [email protected] 8

Diffuse Lighting Model

Page 9: Advanced computer graphics - Shading

February 5, 2015 [email protected] 9

Ambient Term

Page 10: Advanced computer graphics - Shading

February 5, 2015 10

• Want to add highlights for shiny surfaces

• Highlight moves with eye location

Phong Illumination

[email protected]

Page 11: Advanced computer graphics - Shading

February 5, 2015 11

• The exponent p controls the “sharpness” of the highlight: larger p

produces sharper highlight

Phong Exponent

Values of p between 100 and

200 correspond to metals

Values between 5 and 10 give

surface that look like plastic

The Shininess Coefficient

[email protected]

Page 12: Advanced computer graphics - Shading

February 5, 2015 12

Phong Illumination: Computing r

• How can we compute the reflection vector r?

[email protected]

Page 13: Advanced computer graphics - Shading

February 5, 2015 [email protected] 13

Page 14: Advanced computer graphics - Shading

February 5, 2015 [email protected] 14

Page 15: Advanced computer graphics - Shading

February 5, 2015 15

Complete Illumination Equation

• The complete illumination equations models: ƒ

• Ambient light ƒ

• Diffuse reflections ƒ

• Specular reflections

[email protected]

Page 16: Advanced computer graphics - Shading

February 5, 2015 [email protected] 16

Shadows & Multiple Lights

Page 17: Advanced computer graphics - Shading

February 5, 2015 17

• Illumination of a point on a surface:

• Technical detail: clamp all colors at 1

• Illumination of an entire surface = SHADING! ƒ

• Very expensive to compute at every visible point. We have 3 typical options

- Per polygon (Flat shading): entire polygon gets same color

- Per vertex (Gourad Shading): compute illumination at each vertex and

interpolate vertex colors

- Per pixel (Phong Shading): interpolate vertex normals and compute

illumination for each pixel

Illumination vs. Shading

[email protected]

Page 18: Advanced computer graphics - Shading

February 5, 2015 [email protected] 18

Page 19: Advanced computer graphics - Shading

February 5, 2015 19

One illumination calculation per polygon, assign all pixels inside each

polygon the same color

Flat shading

[email protected]

Page 20: Advanced computer graphics - Shading

February 5, 2015 20

The simplest shading algorithm, called flat shading, consists of using an

illumination model to determine the corresponding intensity value for the

incident light, then shade the entire polygon according to this value. Flat

shading is also known as constant shading or constant intensity shading. Its

main advantage is that it is easy it implement.

Flat shading produces satisfactory results under the following conditions:

1. The subject is illuminated by ambient light and there are no surface textures or

shadows.

2. In the case of curved objects, when the surface changes gradually and the

light source and viewer are far from the surface.

3. In general, when there are large numbers of plane surfaces.

Figure shows three cases of flat shading of a conical surface. The more polygons,

the better the rendering.

With flat shading, each triangle of a mesh is filled with a single color.

Flat Shading

[email protected]

Page 21: Advanced computer graphics - Shading

February 5, 2015 21

• each entire polygon is drawn with the same color.

• need to know one normal for the entire polygon.

• fast .

• lighting equation used once per polygon.

Flat shading

Given a single normal to the plane the lighting equations and the material

properties are used to generate a single color. The polygon is filled with that

color.

[email protected]

Page 22: Advanced computer graphics - Shading

February 5, 2015 22 [email protected]

Flat Shading

Page 23: Advanced computer graphics - Shading

February 5, 2015 23 [email protected]

Flat Shading

Page 24: Advanced computer graphics - Shading

February 5, 2015 24

Flat Shading

[email protected]

Page 25: Advanced computer graphics - Shading

February 5, 2015 25

The major limitation of flat shading is that each polygon is rendered in a

single color.

Very often the only way of improving the rendering is by increasing the

number of polygons, as shown in the previous Figure. An alternative scheme

is based on using more than one shade in each polygon, which is

accomplished by interpolating the values calculated for the vertices to the

polygon's interior points.

This type of manipulation, called interpolative or incremental shading,

under some circumstances is capable of producing a more satisfactory

shade rendering with a smaller number of polygons. Two incremental

shading methods, called Gouraud and Phong shading, are almost in 3D

rendering software.

Interpolative (incremental) Shading

[email protected]

Page 26: Advanced computer graphics - Shading

February 5, 2015 26

This shading algorithm was first described by H. Gouraud in 1971. It is also

called bilinear intensity interpolation. Gouraud shading is easier to understand in

the context of the scan-line algorithm used in hidden surface removal, For now,

assume that each pixel is examined according to its horizontal (scan-line)

placement, usually left to right. Following figure shows a triangular polygon with vertices at A, B, and C.

Gouraud Shading

[email protected]

Page 27: Advanced computer graphics - Shading

February 5, 2015 [email protected] 27

Gouraud Shading

Page 28: Advanced computer graphics - Shading

February 5, 2015 [email protected] 28

Gouraud Shading

Page 29: Advanced computer graphics - Shading

February 5, 2015 29

Gouraud Shading

[email protected]

Page 30: Advanced computer graphics - Shading

February 5, 2015 30

The intensity value at each of these vertices is

based on the reflection model. As scan-line

processing proceeds, the intensity of pixel p1

is determined by interpolating the intensities at

vertices A and B, according to the formula In

the next slide, the intensity of p1 is closer to

the intensity of vertex A than that of vertex B.

The intensity of p2 is determined similarly, by

interpolating the intensities of vertices A and C.

Gouraud Shading

[email protected]

Page 31: Advanced computer graphics - Shading

February 5, 2015 31

To find the intensity of Ip, we need to know the intensity of Ia and Ib. To

find the intensity of Ia we need to know the intensity of I1 and I2. To find

the intensity of Ib we need to know the intensity of I1 and I3.

Ia = (Ys - Y2) / (Y1 - Y2) * I1 + (Y1 - Ys) / (Y1 - Y2) * I2

Ib = (Ys - Y3) / (Y1 - Y3) * I1 + (Y1 - Ys) / (Y1 - Y3) * I3

Ip = (Xb - Xp) / (Xb - Xa) * Ia + (Xp - Xa) / (Xb - Xa) * Ib

Whether we are interpolating normal's or

colors the procedure is the same:

The process is continued for each pixel in the polygon, and for each

polygon in the scene.

Gouraud Shading

[email protected]

Page 32: Advanced computer graphics - Shading

February 5, 2015 32

Gouraud shading also has limitations. • One of the most important ones is the loss of highlights on surfaces and

highlights that are displayed with unusual shapes. Following figure shows a

polygon with an interior highlight. Since Gouraud shading is based on the

intensity of the pixels located at the polygon edges, this highlight is missed.

In this case pixel p3 is rendered by interpolating the values of p1 and p2,

which produces a darker color than the one required.

• Another error associated with Gouraud shading is the appearance of bright or

dark streaks, called Mach bands.

Gouraud shading

[email protected]

Page 33: Advanced computer graphics - Shading

February 5, 2015 33

• colors are interpolated across the polygon.

• need to know a normal for each vertex of the polygon.

• slower than flat shading.

• lighting equation used at each vertex.

Gouraud Shading

Given a normal at each vertex of the polygon, the color at each vertex is

determined from the lighting equations and the material properties. Linear

interpolation of the color values at each vertex are used to generate color

values for each pixel on the edges. Linear interpolation across each scan line

is used to then fill in the color of the polygon.

Gouraud Shading Properties

[email protected]

Page 34: Advanced computer graphics - Shading

February 5, 2015 34

- The intensity value is calculated once for each vertex of a polygon.

- The intensity values for the inside of the polygon are obtained by

interpolating the vertex values.

- Eliminates the intensity discontinuity problem.

- Still not model the specular reflection correctly.

- The interpolation of color values can cause bright or dark intensity streaks,

called the Mach- bands, to appear on the surface.

Gouraud Shading

[email protected]

Page 35: Advanced computer graphics - Shading

February 5, 2015 35

Gouraud Shading Limitations - Mach Bands

The rate of change of pixel

intensity is even across any

polygon, but changes as

boundaries are crossed

This ‘discontinuity’ is

accentuated by the human

visual system, so that we see

either light or dark lines at the

polygon edges - known as

Mach banding

[email protected]

Page 36: Advanced computer graphics - Shading

February 5, 2015 [email protected] 36

The individual bands should appear as gradients,

and they may even appear to be curved. In fact,

they are all solid colors. Now look at this one:

If you look closely at the area above the center

two arrows, you should see a thin bright line

(left-middle arrow) and a thin dark line (right-

middle arrow). Once again, this is despite the

fact that each of the three areas (dark, light,

and in between) are solid colors.

Mach Bands

Page 37: Advanced computer graphics - Shading

February 5, 2015 [email protected] 37

In the graph on the bottom, the

black line represents the actual

luminance of the figure. The red

line reflects the perceived

luminance. The red line’s

deviation from the black line

represents the Mach Band

phenomena, and the little spikes

over and above the black line

represent the bright and dark

lines you see in the second

figure above.

Mach Bands

Page 38: Advanced computer graphics - Shading

February 5, 2015 38

Gouraud Shading

[email protected]

Page 39: Advanced computer graphics - Shading

February 5, 2015 [email protected] 39

Gouraud Shading Flat Shading

Page 40: Advanced computer graphics - Shading

February 5, 2015 40

Phong shading is the most popular shading algorithm in use today. This

method was developed by Phong Bui-Toung.

Pong shading, also called normal-vector interpolation, is based on

calculating pixel intensities by means of the approximated normal vector

at the point in the polygon.

Although more calculation expensive, Phong shading improves the

rendering of bright points and highlights that are mis-rendered in Gouraud

shading.

Phong Shading

[email protected]

Page 41: Advanced computer graphics - Shading

February 5, 2015 41

One lighting calculation per pixel

[email protected]

Phong Shading

Page 42: Advanced computer graphics - Shading

February 5, 2015 [email protected] 42

Phong Shading

Page 43: Advanced computer graphics - Shading

February 5, 2015 [email protected] 43

Phong Shading

Page 44: Advanced computer graphics - Shading

February 5, 2015 44

- Instead of interpolating the intensity values, the normal vectors are being

interpolated between the vertices.

- The intensity value is then calculated at each pixel using the interpolated

normal vector.

- This method greatly reduces the Mach-band problem but it requires more

computational time.

Phong Shading

[email protected]

Page 45: Advanced computer graphics - Shading

February 5, 2015 45

• Compute normal, ni, at each vertex (if not already given)

• Interpolate normals during scan conversion

• Compute color with the interpolated normals ƒ

- Expensive: compute illumination for every visible point on a surface ƒ

- Captures highlights in the middle of a polygon ƒ

- Looks smoother across edges

Phong Shading

[email protected]

Page 46: Advanced computer graphics - Shading

February 5, 2015 46

•normals are interpolated across the polygon.

•need to know a normal for each vertex of the polygon.

•better at dealing with highlights than Goraud shading.

•slower than Goraud shading.

•lighting equation used at each pixel.

Where Goraud shading uses normals at the vertices and then interpolates the

resulting colors across the polygon, Phong shading goes further and

interpolates than normals. Linear interpolation of the normal values at each

vertex are used to generate normal values for the pixels on the edges. Linear

interpolation across each scan line is used to then generate normals at each

pixel across the scan line.

Phong Shading

[email protected]

Page 47: Advanced computer graphics - Shading

February 5, 2015 47

Phong Shading

[email protected]

Page 48: Advanced computer graphics - Shading

February 5, 2015 [email protected] 48

Phong Shading Gouraud Shading

Page 49: Advanced computer graphics - Shading

February 5, 2015 49

Fog, or atmospheric attenuation allows us to simulate this affect.

Fog is implemented by blending the calculated color of a pixel with a given

background color ( usually grey or black ), in a mixing ratio that is somehow

proportional to the distance between the camera and the object. Objects that are

farther away get a greater fraction of the background color relative to the object's

color, and hence "fade away" into the background. In this sense, fog can ( sort of )

be thought of as a shading effect.

Fog is typically given a starting distance, an ending distance, and a color. The fog

begins at the starting distance and all the colors slowly transition to the fog color

towards the ending distance. At the ending distance all colors are the fog color.

Fog

[email protected]

Distance fog is a technique used in 3D computer graphics to enhance the

perception of distance by simulating fog.

Page 50: Advanced computer graphics - Shading

February 5, 2015 50

Here is a scene from battalion

without fog. The monster sees a

very sharp edge to the world

Here is the same scene with fog.

The monster sees a much softer

horizon as objects further away tend

towards the black color of the sky

[email protected]

Fog

Page 51: Advanced computer graphics - Shading

February 5, 2015 [email protected] 51

Along a particular ray from some object to your eye, fog particles suspended in

the air "scatter" some of the light that would otherwise reach your eye.

Pollutants, smoke, and other airborne particles can also absorb light, rather than

scattering the light, thereby reducing the intensity of the light that reaches your

eye.

Uniform Fog

Page 52: Advanced computer graphics - Shading

February 5, 2015 [email protected] 52

If light travels a unit of distance along

some ray through a uniformly foggy

atmosphere

Cleave = Center - Creduction + Cincrease

where:

Cleave is the color intensity of the light leaving a unit segment along a ray,

Center is the color intensity of the light entering a unit segment along a ray,

Creduction is the color intensity of all the light absorbed or scattered away along the

unit segment, and

Cincrease is the color intensity of additional light redirected by the fog in the direction

of the ray along the unit segment.

Uniform Fog

Page 53: Advanced computer graphics - Shading

February 5, 2015 [email protected] 53

The intensity of light absorbed or scattered away cannot be greater than the

intensity of incoming light. Assuming a uniform fog density, there must be a fixed

ratio between the color of incoming light and the color of light scattered away. So

we have:

where:

h is a constant scale factor dependent on the fog

density.

Uniform Fog

Page 54: Advanced computer graphics - Shading

February 5, 2015 [email protected] 54

Assuming a uniform fog color, the color of additional light scattered by the fog

in the direction of the ray is:

Cincrease = h x Cfog

where:

Cfog is the constant fog color.

Now you can express the color of the light leaving the fogged unit distance in

terms of the color of the entering light, a constant percentage of light scattering

and extinction, and a constant fog color. This relationship is:

Cleave = (1 - h) x Center + h x Cfog

Uniform Fog

Page 55: Advanced computer graphics - Shading

February 5, 2015 55 [email protected]