3 Illumination
R. Mukundan Department of Computer Science and Software Engineering University of Canterbury, .
//Enable lighting
Copyright By PowCoder代写 加微信 powcoder
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
In “initialize()” function
In “display()” function
(x, y, z) glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
Light’s position is an imaginary point that can be transformed using transformation matrices including the view matrix (Lec 02).
No 3D primitive created.
Light’s position is used only for constructing a few vectors
for the computation of color values.
//Specify light’s position
float light_pos[] = {14, 20, 0, 1}
R. Mukundan, CSSE, University of Canterbury
Simple Facts
The colour of an object is the colour of light reflected from the object towards the direction of the viewer.
Reflected Colour
The brightness of an object depends on its orientation with respect to the light source vector and the viewers direction
R. Mukundan, CSSE, University of Canterbury
A scale factor
Colour Modulation
(1, 0, 0) (0, 1, 1)
(1, 0, 0) (1, 1, 0)
Reflected Colour =
Light’s Colour * Material Color
R. Mukundan, CSSE, University of *M
Local Illumination Model
OpenGL uses a local illumination model where the colour value at each vertex is computed using
The positions of the vertex, light and the viewer (l, v) Color properties of light and material (L, M)
The surface normal orientation at the vertex (n)
The local illumination model does not take into account any other geometrical or colour information in the scene (Eg. light reflected from other objects, transparency of material, visibility of an object from the light source)
The illumination model is highly suitable for lighting computation inside the vertex processor of the rendering pipeline.
R. Mukundan, CSSE, University of Canterbury
Three Types of Reflections
There can be three types of reflections from a surface:
Ambient reflection: This is caused by the ambient light. Ambient light (a.k.a background light) is the base level of constant brightness for a scene.
Diffuse reflection: The most common form of reflection where the intensity varies according to the angle between the light’s direction and the surface normal vector.
Specular reflection: This is a mirror-like reflection of high intensity along a narrow cone around the direction of reflection. This reflection is view-dependent, unlike the other two.
In general, the above three components are added
together to get the net reflection.
Ambient+Diffuse+Specular=
R. Mukundan, CSSE, University of Canterbury
Ambient Reflection
Ambient light is constant everywhere in the scene.
It does not depend on light’s position, viewer’s position or
surface orientation.
Ambient light is typically defined as a low intensity gray value. Example: (0.2, 0.2, 0.2)
Ambient light interacts with the material colour to provide a uniform dark shade of the material colour
Light’s Color
Material’s Color
(0.2, 0.2, 0.2)
L*M = (0, 0.2, 0.2) R. Mukundan, CSSE, University of Canterbury
Diffuse Reflection
The intensity of reflection depends on the orientation of the surface relative to light’s direction. It reduces when the angle between the light source vector and the surface normal vector increases. Diffuse reflection becomes maximum when =0, and minimum (zero) when 90.
Normal Vector
Light Source Vector
Light’s Color
Material’s Color
R. Mukundan, CSSE, University of Canterbury
Specular Reflection
Specular highlights are bright reflections from mirror-like or polished surfaces.
The reflection is directional and view dependent.
The material colour for specular reflection is usually set as
white, to provide a bright highlight.
Light’s Color
Material’s Color
R. Mukundan, CSSE, University of Canterbury
“Shininess” of Specular Reflection
The term “shininess” (GL_SHININESS) refers to the width (or spread) of the specular highlight.
Increasing the value of the shininess term reduces the spread of the highlight, making it more concentrated around a vertex.
R. Mukundan, CSSE, University of Canterbury
Shininess = 40
Shininess = 100
Ambient, Diffuse, Specular Components
(0.2, 0.2, 0.2)
R. Mukundan, CSSE, University of Canterbury
Ambient, Diffuse, Specular Components
Both light and material have 3 color properties:
Ambient color, diffuse color and specular color.
float white[3] = {1, 1, 1}; float black[3] = {0, 0, 0}; float gray[3] = {0.2, 0.2, 0.2}; float cyan[3] = {0, 1, 1};
glLightfv(GL_LIGHT0, GL_AMBIENT, gray); glLightfv(GL_LIGHT0, GL_DIFFUSE, white); glLightfv(GL_LIGHT0, GL_SPECULAR, white);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, cyan); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, cyan); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100);
Note: Material’s ambient and diffuse colours are the same.
To disable specular highlights from a surface, set its specular material colour to 0.
R. Mukundan, CSSE, University of Canterbury
Vertex Colours
Colour is a vertex attribute. A colour for a vertex may be specified by the user or computed using lighting calculations
Example showing user specified colour values at each vertex:
Bi-linear interpolation of colours
glBegin(GL_TRIANGLES); glColor3f(1, 0, 0); glVertex3f(2, 1, 0); glColor3f(0, 1, 0); glVertex3f(8, 1, 0); glColor3f(0, 0, 1); glVertex3f(5, 8, 0);
R. Mukundan, CSSE, University of Canterbury
Vertex Colours
Enabling lighting in an application causes user defined colors (glColor3f(..)) to be ignored.
initialize()
glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);
glBegin(GL_TRIANGLES); glColor3f(1, 0, 0); glVertex3f(2, 1, 0); glColor3f(0, 1, 0); glVertex3f(8, 1, 0); glColor3f(0, 0, 1); glVertex3f(5, 8, 0);
R. Mukundan, CSSE, University of Canterbury
Vertex Colours
When lighting is enabled, OpenGL uses light’s and material’s
colour properties, light’s position and vertex normal vector
to compute the colour values at each vertex.
initialize()
float col[3] = { 1, 1, 0 }; //yellow glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glMaterialfv(GL_FRONT, GL_DIFFUSE, col);
float light[4] = { 2, 1, 1, 1};
glLightfv(GL_LIGHT0, GL_POSITION, light);
glBegin(GL_TRIANGLES);
glNormal3f(0, 0, 1);
glColor3f(1, 0, 0);
glVertex3f(2, 1, 0);
glColor3f(0, 1, 0);
glVertex3f(8, 1, 0);
glColor3f(0, 0, 1);
glVertex3f(5, 8, 0);
R. Mukundan, CSSE, University of Canterbury
Vertex Colours
We can force OpenGL to use the colour value defined using glColor3f(…) for the current material’s ambient and
diffuse properties. This is done by calling
glEnable(GL_COLOR_MATERIAL);
initialize()
glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL);
float light[4] = { 2, 1, 1, 1}; glLightfv(GL_LIGHT0, GL_POSITION, light); glBegin(GL_TRIANGLES);
glNormal3f(0, 0, 1); glColor3f(1, 1, 0); glVertex3f(2, 1, 0); glVertex3f(8, 1, 0); glVertex3f(5, 8, 0);
R. Mukundan, CSSE, University of Canterbury
Surface Normal Vectors
Lighting calculations require the normal vector at each vertex. This vector must be supplied by the user.
Light source
glEnable(GL_NORMALIZE);
glBegin(GL_TRIANGLES); glNormal3f(nx1, ny1, nz1); glVertex3f(x1, y1, z1); glNormal3f(nx2, ny2, nz2); glVertex3f(x2, y2, z2); glNormal3f(nx3, ny3, nz3); glVertex3f(x3, y3, z3); …
… glEnd();
R. Mukundan, CSSE, University of -vertex normal definition 17
Polygonal Element
Surface Normal Vectors
The code on the previous slide specifies the normal vector for each vertex. For objects such as a sphere, we can easily compute per-vertex normals.
Objects like the floor plane need to have only one normal definition for the entire object.
Surface normals may also be defined on a per-face basis (see next slide)
glEnable(GL_NORMALIZE);
glBegin(GL_QUADS); glNormal3f(0, 1, 0); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); …
glEnable(GL_NORMALIZE);
glBegin(GL_TRIANGLES); normal(..); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3);
(nx, ny, nz) (x2, y2, z2)
(x1, y1, z1)
(x3, y3, z3) normal
R. Mukundan, CSSE, University of -face
definition
Surface Normal Computation
(nx, ny, nz) (x1, y1, z1)
(x2, y2, z2) (x3, y3, z3) See
Model3D.cpp
void normal(int index) {
nx = y1*(z2-z3) + y2*(z3-z1) + y3*(z1-z2); ny = z1*(x2-x3) + z2*(x3-x1) + z3*(x1-x2); nz = x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2); glNormal3f(nx, ny, nz);
R. Mukundan, CSSE, University of Canterbury
Computation of face normals
Face normal vs Vertex normal
In a face-based normal definition, the same normal vector is assigned to all three vertices of a triangle. This gives a nearly constant colour for each face and reveals the polygonal structure of the object.
Per-vertex normal vectors generate a smooth variation of shades across a surface.
Using face normals
Using vertex normals
R. Mukundan, CSSE, University of Canterbury
Spot Lights
By default, all OpenGL lights are omni-directional lights. They behave as if they “emit” light in all directions.
A light can be converted to a spot light by specifying A spot cutoff angle. This is the half cone angle of the
spotlight.
A spot direction. This is a vector specifying the cone’s axis.
A spot exponent. This specifies how fast the intensity drops off as a vertex is moved from the centre of the spotlight towards its edge.
float spotdir[]={5.0, -2.0, -4.0}; glEnable(GL_LIGHT1);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10.0); glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0); glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spotdir);
R. Mukundan, CSSE, University of Canterbury
float spotDir[] = {0.866, -0.5, 0};
float spotPosn[] = {0, 10, 0, 1}; glEnable(GL_LIGHT1);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10.0); glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spotDir); glLightfv(GL_LIGHT1, GL_POSITION, spotPosn);
float spotDir[] = {-1, -1, 0};
float spotPosn[] = {0, 10, 0, 1}; glEnable(GL_LIGHT1);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10.0); glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spotDir); glLightfv(GL_LIGHT1, GL_POSITION, spotPosn);
Spot Lights (Examples)
R. Mukundan, CSSE, University of Canterbury
Cutoff = 8 degs. Exponent = 2
Cutoff = 15 degs. Exponent = 2
Spot Lights
R. Mukundan, CSSE, University of Canterbury
Cutoff = 15 degs. Exponent = 50
Cutoff = 15 degs. Exponent = 100
OpenGL Lighting
A spot light in the middle of a large quad (eg. floor plane) will not be visible unless the plane is subdivided into smaller quads. This is because lighting calculations are done only at the vertices of every polygon.
R. Mukundan, CSSE, University of Canterbury
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com