Upload
mohammad-shaker
View
197
Download
1
Tags:
Embed Size (px)
DESCRIPTION
OpenGL L04-Lighting
Citation preview
Mohammad Shaker
mohammadshaker.com
@ZGTRShaker
2014
OpenGL Graphics
L04-Lighting
What we’ve learnt so far..
RULETO SEE A 3D SCENE YOU SHOULD SET UP:
CAMERA (Singleton, for all objects)
PROJECTION (Singleton, for all objects)
WORLD MATRIX (For each object separately)
Lighting
How We See Things?
How We See Things?
Light and Color Systems
Additive Colors Subtractive Colors
Colors in OpenGL
• glColor3f(red, green, blue) [0,1]
• glColor4f(red, green, blue, alpha) [0,1]
Colors in OpenGL
• glColor3f(red, green, blue) [0,1]
• glColor4f(red, green, blue, alpha) [0,1]– alpha for transparency [0; full,1: no]
Color Filling Mode
• Filling Mode
– glShadeModel(GL_FLAT);
• Fill the polygon with solid color (the same as last vertex color)
– glShadeModel(GL_SMOOTH);
• Interpolate colors between polygon edges
Light’s Type
• DIFFUSE color's alpha value actually determines the transparency of the polygon
Only ambient light only specular lightonly diffuse light
The Modified Phong Model
• Computes a color or shade for each vertex using a lighting model (the modified
Phong model) that takes into account
– Diffuse reflections
– Specular reflections
– Ambient light
– Emission
• Vertex shades are interpolated across polygons by the rasterizer
Lighting - Types
• Ambient Light: doesn’t come from any particular direction. It has an original
source somewhere, but the rays of light have bounced around the room or scene
and become directionless.
Lighting - Types
• Diffuse Light: The diffuse part of an OpenGL light is the directional component
that appears to come from a particular direction and is reflected off a surface with
an intensity proportional to the angle at which the light rays strike the surface.
Thus, the object surface is brighter if the light is pointed directly at the surface
than if the light grazes the surface from a greater angle.
Lighting - Types
• Specular light: Like diffuse light, specular light is a highly directional property, but
it interacts more sharply with the surface and in a particular direction.
The Modified Phong Model
• The model is a balance between simple computation and physical realism
• The model uses
– Light positions and intensities
– Surface orientation (normals)
– Material properties (reflectivity)
– Viewer location
• Computed for each source and each color component
Material Properties
• Define the surface properties of a primitive
• you can have separate materials for front and back
Property Description
Diffuse Base object color
Specular Highlight color
Ambient Low-light color
Emission Glow color
Shininess Surface smoothness
Material Properties
• Define the surface properties of a primitive
• you can have separate materials for front and back
Property Description
GL_DIFFUSE Base color
GL_SPECULAR Highlight Color
GL_AMBIENT Low-light Color
GL_EMISSION Glow Color
GL_SHININESS Surface Smoothness
Color Theory
Color Theory
Lighting in OpenGL
Lighting in OpenGLLight Source and Object Material
Lighting in OpenGLLight Source and Object Material
Turning on the Lights
• For any type of light, you should first
– Flip each light’s switch
glEnable(GL_LIGHT0);
– Turn on the power
glEnable(GL_LIGHTING);
Lighting in OpenGL – Light Source
• For the light source:
– Set light properties:float Light_Ambient [ ] = {0.2,0.2,0.2,1};
float Light_Diffuse [ ] = {1,1,1,1};
float Light_Specular [ ] = {1,1,1,1};
float Light_Position [ ] = {0,0,20,0};
– Enable a specific light (GL_LIGHT0):glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
– Bind the light properties with the specific light (GL_LIGHT0):glLightfv(GL_LIGHT0 , GL_AMBIENT , Light_Ambient);
glLightfv(GL_LIGHT0 , GL_DIFFUSE , Light_Diffuse);
glLightfv(GL_LIGHT0 , GL_SPECULAR , Light_Specular);
glLightfv(GL_LIGHT0 , GL_POSITION , Light_Position);
Lighting in OpenGL – Object Material
• For the object material:
– Set material properties:float Cone_Ambient [ ] = {0.1,0.1,0.1,1};
float Cone_Diffuse [ ] = {0.2,0.1,0.9,1};
float Cone_Specular [ ] = {1,1,1,1};
float Cone_Shininess = 100;
– Bind the material properties with a specific object:glMaterialfv(GL_FRONT_AND_BACK , GL_AMBIENT , Cone_Ambient);
glMaterialfv(GL_FRONT_AND_BACK , GL_DIFFUSE , Cone_Diffuse);
glMaterialfv(GL_FRONT_AND_BACK , GL_SPECULAR , Cone_Specular);
glMaterialf (GL_FRONT_AND_BACK , GL_SHININESS , Cone_Shininess);
• Now draw whatever you want.GLUquadric *quadric = gluNewQudric();
gluCylinder(quadric, 4, 1, 7, 32, 32);
Lighting ExampleComplete Code
Lighting Example
SpotLight Lighting ExampleComplete Code
Lighting Example
Normals for Lighting
Why Normals are essential for lighting?
Why Normals are essential for lighting?
Normals for Lighting
• Vertex Normals– Prefixed w/ “vn” (Wavefront)
– Contains x,y,z of normal
– Not necessarily unit length
– Not necessarily in vertex order
– Indexed as with vertices(x0,y0,z0)
(a0,b0,c0)
(u0,v0)
(x1,y1,z1)
(a1,b1,c1)
(u1,v1)
(x2,y2,z2)
(a2,b2,c2)
(u2,v2)
Surface Normals
• Normals define how a surface reflects light
– Application usually provides normals as a vertex atttribute
– Current normal is used to compute vertex’s color
– Use unit normals for proper lighting
• scaling affects a normal’s length
Vector Normalization
Vector NormalizationWhy to?
Advanced Lighting, Spotlight
Shadowing
Shadowing
• How to make a shadow?
– Draw the whole object again, projecting it on the shadow plane (the plane in which the
shadow will appear)
Shadowing