5. 5
Lighting Principles
• Lighting simulates how objects reflect light
– material composition of object
– light’s color and position
– global lighting parameters
• ambient light
• two sided lighting
– available in both color index
and RGBA mode
Budditha Hettige
6. 6
How OpenGL Simulates Lights
• Phong lighting model
– Computed at vertices
• Lighting contributors
– Surface material properties
– Light properties
– Lighting model properties
Budditha Hettige
9. 9
Surface Normals
• Normals define how a surface reflects
light
• glNormal3f( x, y, z )
– Current normal is used to compute vertex’s
color
– Use unit normals for proper lighting
• scaling affects a normal’s length
glEnable( GL_NORMALIZE )
or
glEnable( GL_RESCALE_NORMAL )
CPUCPU DLDL
Poly.Poly.
Per
Vertex
Per
Vertex
RasterRaster FragFrag FBFB
PixelPixel
TextureTexture
Budditha Hettige
10. 10
Material Properties
• Define the surface properties of a primitive
glMaterialfv( face, property, value );
– separate materials for front and back
GL_DIFFUSE Base color
GL_SPECULAR Highlight Color
GL_AMBIENT Low-light Color
GL_EMISSION Glow Color
GL_SHININESS Surface Smoothness
Budditha Hettige
11. 11
Light Properties
• glLightfv( light, property, value
);
– light specifies which light
• multiple lights, starting with GL_LIGHT0
glGetIntegerv( GL_MAX_LIGHTS, &n );
– properties
• colors
• position and type
• attenuation
Budditha Hettige
12. Surface lighting
Surface lighting properties
Values defined in RGB colorspace
Ambient
Light reaching all points on a surface
Diffuse
Light scattering from rough surfaces
Specular
Light reflection from shiny surfaces
Shininess
Defines the spread of the specular term
Larger values have more “mirror” like specularity
12Budditha Hettige
13. Lighting model
Light Properties
Ambient
Color and intensity of light that interacts with a
surface materials ambient property
Diffuse
Color and intensity of light that interacts with a
surface materials diffuse property
Specular
Color and intensity of light that interacts with a
surface materials specular property
13Budditha Hettige
15. 15
Controlling a Light’s Position
• Modelview matrix affects a light’s position
– Different effects based on when position is
specified
• eye coordinates
• world coordinates
• model coordinates
– Push and pop matrices to uniquely control a
light’s position
Budditha Hettige
16. GLUT simple geometry
GLUT can render simple geometry
Sphere, Box, Cone, Torus, Teapot, etc
void glutSolidSphere(GLdouble radius,
GLint slices, GLint stacks);
radius: radius of the sphere
slices: number of longitudinal subdivitions
stacks: number of latitudinal subdivitions
void glutSolidCube(GLdouble size);
size: length of all sides of the cube
16Budditha Hettige
17. 17
Tips for Better Lighting
• Recall lighting computed only at vertices
– model tessellation heavily affects lighting
results
• better results but more geometry to process
• Use a single infinite light for fastest
lighting
– minimal computation per vertex
Budditha Hettige
18. Setting up state for lighting
Lighting and lights are disabled by default
Enabling lighting overrides the use of assigned colors
from glColor calls
glShadeModel(GL_SMOOTH);
Set Gouraud shading
glShadeModel(GL_FLAT);
Set Flat Shading
glEnable(GL_LIGHTING);
Enable fixed function lighting
glEnable(GL_LIGHT0);
Enable light 0 in the scene
18Budditha Hettige
19. Hidden Surface removal
• First Enable Culling
– glEnable(GL_CULL_FACE);
• Define which face to cull
– glCullFace(GLenum face);
– Face: The polygon facing
• GL_FRONT, GL_BACK,
GL_FRONT_AND_BACK
19Budditha Hettige
20. Assigning Material Properties
void glMaterialfv( GLenum face,
GLenum pname,
const GLfloat * params);
face: The geometry facing to assign
GL_FRONT,GL_BACK, or GL_FRONT_AND_BACK
pname: The material property to assign
GL_AMBIENT,GL_DIFFUSE,GL_SPECULAR, and
others
params: pointer to an array of values
20Budditha Hettige
21. OpenGl calls array pointers
Some sets of OpenGL methods take multiple
numbers and types of arguments
glVertex2i
Type of argumentNumber of arguments
glMaterialfv
Type Suffix
GLbyte b
GLshort s
i
f
d
ub
GLushort us
ui
GLint,
GLsizei
GLfloat,
GLclampf
GLdouble,
GLclampd
GLubyte,
GLboolean
GLuint,
GLenum,
GLbitfield
Method takes an array
pointer argument
GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
Budditha Hettige 21
22. Assign Material Property
22
//Define GLfloat arrays to hold material data
GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//Shininess will be an array of length 1
GLfloat mat_shininess[] = { 50.0 };
//Apply the material properties to the front
facing
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
Budditha Hettige
23. Assigning Light Properties
Similar to Materials
void glLightfv(GLenum light,
GLenum pname,
const GLfloat * params);
light: The light to assign
GL_LIGHT0, GL_LIGHT1, …, GL_LIGHT8
pname: The light property to assign
GL_AMBIENT,GL_DIFFUSE, GL_SPECULAR, and others
params: pointer to an array of values
23Budditha Hettige
25. Point Light
A light source originating from a zero-
volume point in the scene
Casts light in all directions
//Light position
GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0
};
//Apply the light position
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
Position
For Positions this
should be 1.0
Budditha Hettige 25
26. Directional Light
A light infinitely far away from the drawn
scene
Used most often for emulating sunlight
Distance from sun to earth is large
Light direction can be considered the same
//Light direction down the negative x axis
GLfloat light_direction[] = { -1.0, 0.0, 0.0, 0.0 };
//Apply the light direction
glLightfv(GL_LIGHT0, GL_POSITION, light_ambient);
Direction
-should be normalized- For Directions this
should be 0.0
Budditha Hettige 26
28. Spotlight
A light source originating from a zero-
volume point in the scene
Direction
Direction the light is focused on
Cutoff
angle that defines light cone
Exponent
Concentration of the light
Brightest around the center
Budditha Hettige 28
33. Drawing a Smooth-Shaded Triangle
// Enable smooth shading
glShadeModel(GL_SMOOTH);
// Draw the triangle
glBegin(GL_TRIANGLES);
// Red Apex
glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)0);
glVertex3f(0.0f,200.0f,0.0f);
// Green on the right bottom corner
glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)0);
glVertex3f(200.0f,-70.0f,0.0f);
// Blue on the left bottom corner
glColor3ub((GLubyte)0,(GLubyte)0,(GLubyte)255);
glVertex3f(-200.0f, -70.0f, 0.0f);
glEnd();
33Budditha Hettige