Lighting/
Illumination/
Shading
(打光/光源/塗色)
Tong-Yee Lee
Lecture 15
Slide 2
6.837 Fall 2001
Ambient 是指環境光
Diffuse 散射光強度
Emission。這是指會發光的物體
Specular 是當入射光對物體產生全反射的情形
Lighting
Lecture 15
Slide 3
6.837 Fall 2001
diffuse
specular
ambient
環境光
散射光
全反射
Lighting and
Illumination
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
Lecture 15
Slide 6
6.837 Fall 2001
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
動態模糊
Lecture 15
Slide 8
6.837 Fall 2001
Ray tracing
Lecture 15
Slide 9
6.837 Fall 2001
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.
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)
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
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
Lecture 15
Slide 15
https://www.scratchapixel.com/lessons/3d-basic-rendering/global-illumination-path-tracing
Radiosity 輻射度演算法 (computer graphics)
In 3D computer graphics, radiosity 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
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.
Lecture 15
Slide 18
6.837 Fall 2001
Radiosity 輻射度演算法
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
散射
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
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.
Difference between Light source
And point light source
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.
Other Light Sources
Spotlights
Area Light Sources
Extended Light Sources
Hard Shadow and Soft Shadow
Lecture 15
Average color from all different rays
Spot Light Source
Lecture 15
Spotlights in use at a music performance
Lecture 15
Slide 27
6.837 Fall 2001
Point, Directional and Spot Light Source
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
Lecture 15
Slide 29
6.837 Fall 2001
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
Lecture 15
Slide 30
6.837 Fall 2001
Lecture 15
Slide 31
6.837 Fall 2001
wireframe rendering
散射光強度
全反射
環境光
漫射光強度
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
Lecture 15
Slide 34
6.837 Fall 2001
Glossy (散射) reflection, glossy refraction
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
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
Lecture 15
Slide 37
6.837 Fall 2001
Diffuse Lighting Examples
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
全反射
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
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.
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
Specular Reflection
Lecture 15
Slide 43
6.837 Fall 2001
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
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
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
Putting it all together Phong Illumination Model�
Lecture 15
Slide 47
6.837 Fall 2001
ambient
specular
diffuse
Li
Lecture 15
Slide 51
OpenGL Reflectance Model
Lecture 15
Slide 52
6.837 Fall 2001
Emission
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
OpenGL �Lighting
Lecture 15
Slide 55
6.837 Fall 2001
OpenGL �Material
Lecture 15
Slide 56
6.837 Fall 2001
OpenGL Mathematical Model
Lecture 15
Slide 57
6.837 Fall 2001
s = (L^+V^)/|L^+V^|
Jim Blinn’s method
shininess
Normals
Lecture 15
Slide 58
6.837 Fall 2001
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
Enabling Shading
Lecture 15
Slide 60
6.837 Fall 2001
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);
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);
Spotlights
Use glLightv to set
Direction GL_SPOT_DIRECTION
Cutoff GL_SPOT_CUTOFF
Attenuation GL_SPOT_EXPONENT
θ
−θ
φ
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)
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
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);
Light Material Tutorial
Lecture 15
Slide 67
6.837 Fall 2001
Controlling a Light’s Position
Modelview matrix affects a light’s position
Different effects based on when position is specified
Push and pop matrices to uniquely control a light’s position
Lecture 15
Slide 68
6.837 Fall 2001
Light Position Tutorial
Lecture 15
Slide 69
6.837 Fall 2001
Front and Back Faces
back faces not visible
back faces visible
Emissive Term
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
Transparency
Lecture 15
6.837 Fall 2001
Lecture 15
Slide 73
6.837 Fall 2001
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/
Efficiency
Lecture 15
Slide 75
6.837 Fall 2001
typedef struct materialStruct {
GLfloat ambient[4];
GLfloat diffuse[4];
GLfloat specular[4];
GLfloat shineness;
} MaterialStruct;
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
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();
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();
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 don’t 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
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
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
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
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
Don’t use specular intensity if you don’t have to
It’s expensive - turn it off by giving 0,0,0 as specular color of light
Don’t forget normals
Many other things to control appearance
Lecture 15
Slide 83
6.837 Fall 2001
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:
Lecture 15
Slide 84
6.837 Fall 2001
Phong Illumination Model