Professional Documents
Culture Documents
Graphics Programming
Lighting
Budditha Hettige
Budditha Hettige
Budditha Hettige
Lighting Principles
Lighting simulates how objects reflect light
material composition of object lights color and position global lighting parameters
ambient light two sided lighting
Budditha Hettige
Lighting contributors
Surface material properties Light properties Lighting model properties
Budditha Hettige
OpenGL Lights
Common Properties
Types
Lighting.
Budditha Hettige
Surface Normals
CPU
Poly. DL
Pixel
Current normal is used to compute vertexs color Use unit normals for proper lighting
scaling affects a normals length glEnable( GL_NORMALIZE ) or glEnable( GL_RESCALE_NORMAL )
9 Budditha Hettige
Material Properties
Define the surface properties of a primitive
glMaterialfv( face, property, value );
GL_DIFFUSE GL_SPECULAR GL_AMBIENT GL_EMISSION GL_SHININESS
Base color Highlight Color Low-light Color Glow Color Surface Smoothness
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
11
Budditha Hettige
Surface lighting
Light reaching all points on a surface Light scattering from rough surfaces
Diffuse
Specular
Shininess
Budditha Hettige
12
Lighting model
Light Properties
Ambient
Color and intensity of light that interacts with a surface materials ambient property Color and intensity of light that interacts with a surface materials diffuse property Color and intensity of light that interacts with a surface materials specular property
Diffuse
Specular
Budditha Hettige
13
Lighting model
Ambient
Diffuse
Specular
Combination
Budditha Hettige
14
15
Budditha Hettige
void glutSolidSphere(GLdouble
GLint slices, GLint stacks);
radius: radius of the sphere slices: number of longitudinal subdivitions stacks: number of latitudinal subdivitions
16
17
Budditha Hettige
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
Budditha Hettige
18
Budditha Hettige
19
face,
GL_FRONT,GL_BACK, or GL_FRONT_AND_BACK
20
Some sets of OpenGL methods take multiple numbers and types of arguments
glVertex2i glMaterialfv
Type Suffix GLbyte b GLshort s GLint, GLsizei i GLfloat, GLclampf f Type of argument Number of arguments GLdouble, GLclampd d Method takes an array GLubyte, pointer argument GLboolean ub GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; GLushort us GLuint, glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); GLenum, GLbitfield ui
Budditha Hettige
21
Budditha Hettige
Similar to Materials
void glLightfv(GLenum
light, GLenum pname, const GLfloat *
params);
23
Budditha Hettige
24
Point Light
A light source originating from a zerovolume point in the scene Casts light in all directions
For Positions this should be 1.0
Position
//Light position GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0 }; //Apply the light position Budditha Hettige glLightfv(GL_LIGHT0, GL_POSITION, light_position);
25
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
Direction -should be normalizedFor Directions this should be 0.0
//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);
Budditha Hettige
26
Spotlights
Budditha Hettige
27
Spotlight
Cutoff
Exponent
28
Spotlight example
//Spotlight properties
GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0 }; GLfloat light_spot_direction[] = { 0.0, 0.0, -1.0}; GLfloat light_spot_cutoff[] = { 25.0 }; GLfloat light_spot_exp[] = { 2.0 };
//Apply the light position glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION , light_spot_direction); glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF , light_spot_cufoff); glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT , light_spot_exp);
Budditha Hettige
29
Shading
Flat Shading
Flat shading selects the computed color of just one vertex
Budditha Hettige
30
Shading
Gouraud Shading Lighting color calculated per-vertex
saves computation
Budditha Hettige
31
Flat
Budditha Hettige
Gouraud
32
33