1 of 85

Lighting/

Illumination/

Shading

(打光/光源/塗色)

Tong-Yee Lee

2 of 85

Lecture 15

Slide 2

6.837 Fall 2001

Ambient 是指環境光

Diffuse 散射光強度

Emission。這是指會發光的物體

Specular 是當入射光對物體產生全反射的情形

3 of 85

Lighting

Lecture 15

Slide 3

6.837 Fall 2001

diffuse

specular

ambient

環境光

散射光

全反射

4 of 85

Lighting and

Illumination

5 of 85

Illumination Models

Illumination 光源(light sources)照明度變化

The transport luminous flux

(i.e., the the rate the rate of the rate of flow the rate of flow of the rate of flow of light energy)

from light sources between

points via direct and indirect paths

Lighting 物體光線的明暗度 via different illumination sources

The process of computing the luminous

intensity reflected from a specified 3-D point

Shading 描影法,明暗法

The process of assigning a colors to

a pixels

Illumination Models光源模形

Simple approximations of light transport

Physical models of light transport

Lecture 15

Slide 5

6.837 Fall 2001

6 of 85

Lecture 15

Slide 6

6.837 Fall 2001

7 of 85

Lecture 15

Slide 7

Depositphotos: City street at night in degradation

Lightning Particle System for Unity 3D – Procedural Lightning

夜幕降臨的夜城交通燈。移動交通的顏色模糊

OpenGL does not account this

effect! Needs more advanced

methods in ray tracing etc.

An example of motion blur showing a London busshowing a London bus passing a telephone box in London

動態模糊

8 of 85

Lecture 15

Slide 8

6.837 Fall 2001

Ray tracing

9 of 85

Lecture 15

Slide 9

6.837 Fall 2001

10 of 85

Ray tracing

Slide 10

6.837 Fall 2001

This recursive ray tracing of a sphere demonstrates the effects of shallow depth of field, area light sources and diffuse inter-reflection.

11 of 85

Lighting

Lecture 15

6.837 Fall 2001

Direct Lighting

Indirect Lighting

散射

Glossy (散射) reflection, glossy refraction

,i.e., summarize and average all glossy ray colors (i.e., blurred effect)

12 of 85

Glossy (散射) reflection, glossy refraction

Glossy surfaces are actually specular surfaces with micro surfaces at angles to surface plane. These micro surfaces refract light not only specularly but also diffusely (at angles very close to the specular transmission), giving the surface a glossy appearance.

Lecture 15

Slide 12

13 of 85

Direct and Indirect Lighting

Lighting intensity

will be reduced (i.e., 0<=k<1) for each bounce

OpenGL does direct

lighting without considering blocking (堵塞) and indirect

lighting

14 of 85

15 of 85

Lecture 15

Slide 15

https://www.scratchapixel.com/lessons/3d-basic-rendering/global-illumination-path-tracing

16 of 85

Radiosity 輻射度演算法 (computer graphics)

In 3D computer graphicsradiosity is an application of the finite element method to solving the rendering equation to solving the rendering equationfor scenes with surfaces that reflect light diffusely to solving the rendering equationfor scenes with surfaces that reflect light diffusely. Unlike rendering to solving the rendering equationfor scenes with surfaces that reflect light diffusely. Unlike rendering methods that use Monte Carlo algorithms to solving the rendering equationfor scenes with surfaces that reflect light diffusely. Unlike rendering methods that use Monte Carlo algorithms(such as path tracing to solving the rendering equationfor scenes with surfaces that reflect light diffusely. Unlike rendering methods that use Monte Carlo algorithms(such as path tracing), which handle all types of light paths, typical radiosity only account for paths (represented by the code "LD*E") which leave a light source and are reflected diffusely some number of times (possibly zero) before hitting the eye. Radiosity is a global illumination to solving the rendering equationfor scenes with surfaces that reflect light diffusely. Unlike rendering methods that use Monte Carlo algorithms(such as path tracing), which handle all types of light paths, typical radiosity only account for paths (represented by the code "LD*E") which leave a light source and are reflected diffusely some number of times (possibly zero) before hitting the eye. Radiosity is a global illumination algorithm in the sense that the illumination arriving on a surface comes not just directly from the light sources, but also from other surfaces reflecting light. Radiosity is viewpoint independent, which increases the calculations involved, but makes them useful for all viewpoints.

Lecture 15

Slide 16

6.837 Fall 2001

17 of 85

Radiosity is a method of rendering based on an detailed analysis of light reflections off diffuse surfaces. The images that result from a radiosity renderer are characterized by soft gradual shadows. Radiosity is typically used to render images of the interior of buildings, and can achieve extremely photo-realistic results for scenes that are comprised of diffuse reflecting surfaces. i.e., 1) very slow to store all radiosity in the environment, 2) ray tracing is from lighting sources

Slide 17

Radiosity was perhaps the first rendering algorithm in widespread use which accounted for diffuse indirect lighting.

18 of 85

Lecture 15

Slide 18

6.837 Fall 2001

Radiosity 輻射度演算法

19 of 85

Two Components of Illumination

Light Sources (Emitters)

Emission Spectrum (color)

Geometry (position and direction)

Directional Attenuation

Surface Properties (Reflectors)

Reflectance Spectrum (color)

Geometry (position, orientation, and micro-structure)

Absorption

Approximations

Only direct illumination from the emitters to the reflectors

Ignore the geometry of light emitters, and consider only the geometry of reflectors such as OpenGL

散射

20 of 85

Ambient Light Source

Even though an object in a scene is not directly lit it will still be visible. This is because light is reflected indirectly from nearby objects. A simple hack that is commonly used to model this indirect illumination is to use of an ambient light source. Ambient light has no spatial or directional characteristics. The amount of ambient light incident on each object is a constant for all surfaces in the scene. An ambient light can have a color.

The amount of ambient light that is reflected by an object is independent of the object's position or orientation. Surface properties are used to determine how much ambient light is reflected.

Lecture 15

Slide 20

6.837 Fall 2001

21 of 85

Directional Light Sources

All of the rays from a directional light source have a common direction, and no point of origin. It is as if the light source was infinitely far away from the surface that it is illuminating. Sunlight is an example of an infinite light source. ��

The direction from a surface to a light source is important for computing the light reflected from the surface. With a directional light source this direction is a constant for every surface. A directional light source can be colored.

No attenuation with distance.

22 of 85

Difference between Light source

And point light source

23 of 85

Point Light Sources

The point light source emits rays in radial directions from its source. A point light source is a fair approximation to a local light source such as a light bulb.

��

The direction of the light to each point on a surface changes when a point light source is used. Thus, a normalized vector to the light emitter must be computed for each point that is illuminated.

24 of 85

Other Light Sources

Spotlights

    • Point source whose intensity falls off away from a given direction
    • Requires a color, a point, a direction, parameters that control the rate of fall off

Area Light Sources

    • Light source occupies a 2-D area (usually a polygon or disk)
    • Generates soft shadows

Extended Light Sources

    • Spherical Light Source
    • Generates soft shadows

25 of 85

Hard Shadow and Soft Shadow

Lecture 15

Average color from all different rays

26 of 85

Spot Light Source

Lecture 15

Spotlights in use at a music performance

27 of 85

Lecture 15

Slide 27

6.837 Fall 2001

Point, Directional and Spot Light Source

28 of 85

Surface Types

The smoother a surface, the more reflected light is concentrated in the direction a perfect mirror would reflected the light

A very rough surface scatters light in all directions

smooth surface

rough surface

Glossy (散射) reflection, glossy refraction

29 of 85

Lecture 15

Slide 29

6.837 Fall 2001

i.e,

Ambient : 周圍的, 環境的

Diffuse:  漫射

Specular: 反射的,鏡射

In the real world most objects have convoluted surfaces that exhibit a diffuse reflection, with the incident light being reflected in all directions

Smooth surface vs. Rough surface

30 of 85

Lecture 15

Slide 30

6.837 Fall 2001

31 of 85

Lecture 15

Slide 31

6.837 Fall 2001

wireframe rendering

散射光強度

全反射

環境光

32 of 85

漫射光強度

33 of 85

Lambert's Cosine Law

Ideal diffuse reflectors reflect light according to Lambert's cosine law, (there are sometimes called Lambertian reflectors). Lambert's law states that the reflected energy from a small surface area in a particular direction is proportional to cosine of the angle between that direction and the surface normal. Lambert's law determines how much of the incoming light energy is reflected. Remember that the amount energy that is reflected in any one direction is constant in this model. In other words the reflected intensity is independent of the viewing direction. The intensity does however depend on the light source's orientation relative to the surface, and it is this property that is governed by Lambert's law.

Lecture 15

Slide 33

6.837 Fall 2001

射光強度

Diffuse Lighting

34 of 85

Lecture 15

Slide 34

6.837 Fall 2001

Glossy (散射) reflection, glossy refraction

35 of 85

Computing Diffuse Reflection

The angle between the surface normal and the incoming light ray is called the angle of incidence and we can express a intensity of the light in terms of this angle.

The Ilight term represents the intensity of the incoming light at the particular wavelength (the wavelength determines the light's color). The kd term represents the diffuse reflectivity of the surface at that wavelength.

In practice we use vector analysis to compute cosine term indirectly. If both the normal vector and the incoming light vector are normalized (unit length) then diffuse shading can be computed as follows:

Slide 35

6.837 Fall 2001

36 of 85

Diffuse Lighting Examples

We need only consider angles from 0 to 90 degrees. Greater angles (where the dot product is negative) are blocked by the surface, and the reflected energy is 0. Below are several examples of a spherical diffuse reflector with a varying lighting angles.

Why do you think spheres are used as examples when shading?

Lecture 15

6.837 Fall 2001

37 of 85

Lecture 15

Slide 37

6.837 Fall 2001

Diffuse Lighting Examples

38 of 85

Specular Reflection

A second surface type is called a specular reflector. When we look at a shiny surface, such as polished metal or a glossy car finish, we see a highlight, or bright spot. Where this bright spot appears on the surface is a function of where the surface is seen from. This type of reflectance is view dependent.

At the microscopic level a specular reflecting surface is very smooth, and usually these microscopic surface elements are oriented in the same direction as the surface itself. Specular reflection is merely the mirror reflection of the light source in a surface. Thus it should come as no surprise that it is viewer dependent, since if you stood in front of a mirror and placed your finger over the reflection of a light, you would expect that you could reposition your head to look around your finger and see the light again. An ideal mirror is a purely specular reflector. In order to model specular reflection we need to understand the physics of reflection.

Lecture 15

Slide 38

6.837 Fall 2001

全反射

39 of 85

Non-ideal Reflectors

Snell's law, however, applies only to ideal mirror reflectors. Real materials, other than mirrors and chrome tend to deviate significantly from ideal reflectors. At this point we will introduce an empirical model that is consistent with our experience, at least to a crude approximation.

In general, we expect most of the reflected light to travel in the direction of the ideal ray. However, because of microscopic surface variations we might expect some of the light to be reflected just slightly offset from the ideal reflected ray. As we move farther and farther, in the angular sense, from the reflected ray we expect to see less light reflected.

Lecture 15

Slide 39

6.837 Fall 2001

40 of 85

Phong Illumination

One function that approximates this fall off is called the Phong Illumination model. This model has no physical basis, yet it is one of the most commonly used illumination models in computer graphics.

The cosine term is maximum when the surface is viewed from the mirror direction and falls off to 0 when viewed at 90 degrees away from it. The scalar nshiny controls the rate of this fall off.

41 of 85

Effect of the nshiny coefficient

The diagram below shows the how the reflectance drops off in a Phong illumination model. For a large value of the nshiny coefficient, the reflectance decreases rapidly with increasing viewing angle.

Lecture 15

Slide 41

6.837 Fall 2001

42 of 85

Specular Reflection

  • Glossiness of surfaces

43 of 85

Lecture 15

Slide 43

6.837 Fall 2001

44 of 85

Computing Phong Illumination

The V vector is the unit vector in the direction of the viewer and the R vector is the mirror reflectance direction. The vector R can be computed from the incoming light direction and the surface normal:

Lecture 15

Slide 44

6.837 Fall 2001

45 of 85

Blinn & Torrance Variation

Jim Blinn introduced another approach for computing Phong-like illumination based on the work of Ken Torrance. His illumination function uses the following equation:

In this equation the angle of specular dispersion is computed by how far the surface's normal is from a vector bisecting the incoming light direction and the viewing direction.

On your own you should consider

how this approach and the previous

one differ.

6.837 Fall 2001

It is faster than computing by Phong illumination

46 of 85

Phong Examples

The following spheres illustrate specular reflections as the direction of the light source and the coefficient of shininess is varied.

Lecture 15

Slide 46

6.837 Fall 2001

47 of 85

Putting it all together Phong Illumination Model

Lecture 15

Slide 47

6.837 Fall 2001

ambient

specular

diffuse

48 of 85

Li

49 of 85

50 of 85

51 of 85

Lecture 15

Slide 51

52 of 85

OpenGL Reflectance Model

Lecture 15

Slide 52

6.837 Fall 2001

53 of 85

Emission

54 of 85

Slide 54

6.837 Fall 2001

With/without Emission Effect

on other objects

OpenGL does not light other objects with emission from a selected object

55 of 85

OpenGL �Lighting

Lecture 15

Slide 55

6.837 Fall 2001

56 of 85

OpenGL �Material

Lecture 15

Slide 56

6.837 Fall 2001

57 of 85

OpenGL Mathematical Model

Lecture 15

Slide 57

6.837 Fall 2001

s = (L^+V^)/|L^+V^|

Jim Blinn’s method

shininess

58 of 85

Normals

  • In OpenGL the normal vector is part of the state
  • Set by glNormal*()
    • glNormal3f(x, y, z);
    • glNormal3fv(p);
  • Usually we want to set the normal to have unit length so cosine calculations are correct
    • Length can be affected by transformations
    • Note the scale does not preserved length
    • glEnable(GL_NORMALIZE) allows for autonormalization at a performance penalty

Lecture 15

Slide 58

6.837 Fall 2001

59 of 85

Normal for Triangle

p1

Lecture 15

Slide 59

6.837 Fall 2001

p0

p2

n

plane n ·(p - p0 ) = 0

n = (p1 - p0 ) ×(p2 - p0 )

normalize n ← n/ |n|

p

Note that right-hand rule determines outward face

60 of 85

Enabling Shading

  • Shading calculations are enabled by
    • glEnable(GL_LIGHTING)
    • Once lighting is enabled, glColor() ignored
  • Must enable each light source individually
    • glEnable(GL_LIGHTi) i=0,1..

Lecture 15

Slide 60

6.837 Fall 2001

61 of 85

Defining a Point Light Source

For each light source, we can set an RGB for the diffuse, specular, and ambient parts, and the position

Lecture 15

Slide 61

6.837 Fall 2001

GL float diffuse0[]={1.0, 0.0, 0.0, 1.0};

GL float ambient0[]={1.0, 0.0, 0.0, 1.0};

GL float specular0[]={1.0, 0.0, 0.0, 1.0};

Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};

glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

glLightv(GL_LIGHT0, GL_POSITION, light0_pos);

glLightv(GL_LIGHT0, GL_AMBIENT, ambient0);

glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0);

glLightv(GL_LIGHT0, GL_SPECULAR, specular0);

62 of 85

Distance and Direction

The source colors are specified in RGBA

The position is given in homogeneous coordinates

If w =1.0, we are specifying a finite location (i.e., point light source)

If w =0.0, we are specifying a parallel source with the given direction vector (i.e. direction light source)

The coefficients in the distance terms are by default a=1.0 (constant terms), b=c=0.0 (linear and quadratic terms). Change by

a= 0.80;

glLightf(GL_LIGHT0, GLCONSTANT_ATTENUATION, a);

63 of 85

Spotlights

Use glLightv to set

Direction GL_SPOT_DIRECTION

Cutoff GL_SPOT_CUTOFF

Attenuation GL_SPOT_EXPONENT

      • Proportional to cosαφ

θ

−θ

φ

64 of 85

Global Ambient Light

Ambient light depends on color of light sources

A red light in a white room will cause a red ambient term that disappears when the light is turned off

OpenGL allows a global ambient term that is often helpful

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient)

65 of 85

Moving Light Sources

Light sources are geometric objects whose positions or directions are affected by the model-view matrix

Depending on where we place the position (direction) setting function, we can

Move the light source(s) with the object(s)

Fix the object(s) and move the light source(s)

Fix the light source(s) and move the object(s)

Move the light source(s) and object(s) independently

Lecture 15

Slide 65

6.837 Fall 2001

66 of 85

Material Properties

Material properties are also part of the OpenGL state and match the terms in the Phong model

Set by glMaterialv()

Lecture 15

Slide 66

6.837 Fall 2001

GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};

GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0};

GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};

GLfloat shine = 100.0

glMaterialf(GL_FRONT, GL_AMBIENT, ambient);

glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse);

glMaterialf(GL_FRONT, GL_SPECULAR, specular);

glMaterialf(GL_FRONT, GL_SHININESS, shine);

67 of 85

Light Material Tutorial

Lecture 15

Slide 67

6.837 Fall 2001

68 of 85

Controlling a Lights Position

Modelview matrix affects a lights position

Different effects based on when position is specified

      • eye coordinates
      • world coordinates
      • model coordinates

Push and pop matrices to uniquely control a lights position

Lecture 15

Slide 68

6.837 Fall 2001

69 of 85

Light Position Tutorial

Lecture 15

Slide 69

6.837 Fall 2001

70 of 85

Front and Back Faces

  • The default is shade only front faces which works correct for convex objects
  • If we set two sided lighting, OpenGL will shaded both sides of a surface
  • Each side can have its own properties which are set by using GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK in glMaterialf

back faces not visible

back faces visible

71 of 85

Emissive Term

  • We can simulate a light source in OpenGL by giving a material an emissive component
  • This color is unaffected by any sources or transformations

Lecture 15

Slide 71

6.837 Fall 2001

GLfloat emission[] = 0.0, 0.3, 0.3, 1.0);

glMaterialf(GL_FRONT, GL_EMISSION, emission);

Emission

72 of 85

Transparency

  • Material properties are specified as RGBA values
  • The A value can be used to make the surface translucent
  • The default is that all surfaces are opaque regardless of A
  • Later we will enable blending and use this feature

Lecture 15

6.837 Fall 2001

73 of 85

Lecture 15

Slide 73

6.837 Fall 2001

74 of 85

Lecture 15

Slide 74

6.837 Fall 2001

Transparent Image overlay(Alpha blending)

Medical Volume Rendering

https://learnopencv.com/alpha-blending-using-opencv-cpp-python/

75 of 85

Efficiency

  • Because material properties are part of the state, if we change materials for many surfaces, we can affect performance
  • We can make the code cleaner by defining a material structure and setting all materials during initialization

  • We can then select a material by a pointer

Lecture 15

Slide 75

6.837 Fall 2001

typedef struct materialStruct {

GLfloat ambient[4];

GLfloat diffuse[4];

GLfloat specular[4];

GLfloat shineness;

} MaterialStruct;

76 of 85

Polygonal Shading

Shading calculations are done for each vertex

Vertex colors become vertex shades

By default, vertex colors are interpolated across the polygon

glShadeModel(GL_SMOOTH);

If we use glShadeModel(GL_FLAT); the color at the first vertex will determine the color of the whole polygon

Lecture 15

Slide 76

6.837 Fall 2001

GL_FLAT

GL_SMOOTH

77 of 85

Lecture 15

Slide 77

6.837 Fall 2001

Normals and OpenGL

You must supply per-vertex normal vectors if you enable lighting computations

A common oversight - all surfaces are black and may be invisible

Before specifying each vertex, specify a color and a normal vector:

glColor4f(r, g, b, a) defines a color, with many variants

glNormal3f(x, y, z) defines a normal, with many variants

Chapters 2, 4 and 5 of the OpenGL programming guide have many examples

glBegin(GL_QUADS);

glColor3f(1,1,1);

glNormal3f(0,0,1);

glVertex3f(1,1,0);

glColor3f(1,1,1);

glNormal3f(0,0,1);

glVertex3f(-1,1,0);

glColor3f(1,1,1);

glNormal3f(0,0,1);

glVertex3f(-1,-1,0);

glColor3f(1,1,1);

glNormal3f(0,0,1);

glVertex3f(1,-1,0);

glEnd();

78 of 85

Lecture 15

Slide 78

6.837 Fall 2001

More Normals and OpenGL

Specifying fewer colors and normals

OpenGL uses the notion of a current color and a current normal

The current normal is applied to all vertices up to the next normal definition

glBegin(GL_QUADS);

glColor3f(1,1,1);

glNormal3f(0,0,1);

glVertex3f(1,1,0);

glVertex3f(-1,1,0);

glVertex3f(-1,-1,0);

glVertex3f(1,-1,0);

glEnd();

  • Normalizing normals
    • Normal vectors must be unit vectors for lighting to work correctly (they must be normalized)
    • By default, vectors are not normalized for you
    • Causes problems with scaling transformations, but OK for translations and rotations
    • glEnable(GL_NORMALIZE) or glEnable(GL_RESCALE_NORMAL) will fix it for you, but they are expensive and slow rendering

79 of 85

Lecture 15

Slide 79

6.837 Fall 2001

OpenGL: Local Shading Models

Local shading models provide a way to determine the intensity and color of a point on a surface

The models are local because they dont consider other objects at all

We use them because they are fast and simple to compute

They do not require knowledge of the entire scene, only the current piece of surface

80 of 85

Lecture 15

Slide 80

6.837 Fall 2001

OpenGL: Approximations for Speed

The viewer direction, V, and the light direction, L, depend on the surface position being considered, x

Distant light approximation:

Assume L is constant for all x

Good approximation if light is distant, such as sun

Distant viewer approximation

Assume V is constant for all x

Rarely good, but only affects specularities

81 of 85

Lecture 15

Slide 81

6.837 Fall 2001

OpenGL Model

Allows emission, E: Light being emitted by surface

Allows separate light intensity for diffuse and specular

Ambient light can be associated with light sources

Allows spotlights that have intensity that depends on outgoing light direction

Allows attenuation of light intensity with distance

Can specify coefficients in multiple ways

Too many variables and commands to present in class

The OpenGL programming guide goes through it all

82 of 85

OpenGL Commands (1)

glMaterial{if}(face, parameter, value)

Changes one of the coefficients for the front or back side of a face (or both sides)

glLight{if}(light, property, value)

Changes one of the properties of a light (intensities, positions, directions, etc)

There are 8 lights: GL_LIGHT0, GL_LIGHT1,

glLightModel{if}(property, value)

Changes one of the global light model properties (global ambient light, for instance)

glEnable(GL_LIGHT0) enables GL_LIGHT0

Lecture 15

Slide 82

6.837 Fall 2001

83 of 85

OpenGL Commands (2)

glColorMaterial(face, mode)

Causes a material property, such as diffuse reflectance coefficient, to track the current glColor()

Speeds things up, and makes coding easier

glEnable(GL_LIGHTING) turns on lighting

Dont use specular intensity if you dont have to

Its expensive - turn it off by giving 0,0,0 as specular color of light

Dont forget normals

Many other things to control appearance

Lecture 15

Slide 83

6.837 Fall 2001

84 of 85

Where do we Illuminate?

To this point we have discussed how to compute an illumination model at a point on a surface. But, at which points on the surface is the illumination model applied? Where and how often it is applied has a noticeable effect on the result.

Illuminating can be a costly process involving the computation of and normalizing of vectors to multiple light sources and the viewer.

For models defined by collections of polygonal facets or triangles:

    • Each facet has a common surface normal
    • If the light is directional then the diffuse contribution is constant across the facet
    • If the eye is infinitely far away and the light is directional then the specular contribution is constant across the facet.

Lecture 15

Slide 84

6.837 Fall 2001

85 of 85

Phong Illumination Model