Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.
OpenGL L04-Lighting
-
Upload
mohammad-shaker -
Category
Software
-
view
205 -
download
1
description
Transcript of OpenGL L04-Lighting
![Page 1: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/1.jpg)
Mohammad Shaker
mohammadshaker.com
@ZGTRShaker
2014
OpenGL Graphics
L04-Lighting
![Page 2: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/2.jpg)
What we’ve learnt so far..
![Page 3: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/3.jpg)
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)
![Page 4: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/4.jpg)
Lighting
![Page 5: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/5.jpg)
How We See Things?
![Page 6: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/6.jpg)
How We See Things?
![Page 7: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/7.jpg)
Light and Color Systems
Additive Colors Subtractive Colors
![Page 8: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/8.jpg)
Colors in OpenGL
• glColor3f(red, green, blue) [0,1]
• glColor4f(red, green, blue, alpha) [0,1]
![Page 9: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/9.jpg)
Colors in OpenGL
• glColor3f(red, green, blue) [0,1]
• glColor4f(red, green, blue, alpha) [0,1]– alpha for transparency [0; full,1: no]
![Page 10: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/10.jpg)
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
![Page 11: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/11.jpg)
Light’s Type
• DIFFUSE color's alpha value actually determines the transparency of the polygon
Only ambient light only specular lightonly diffuse light
![Page 12: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/12.jpg)
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
![Page 13: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/13.jpg)
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.
![Page 14: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/14.jpg)
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.
![Page 15: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/15.jpg)
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.
![Page 16: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/16.jpg)
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
![Page 17: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/17.jpg)
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
![Page 18: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/18.jpg)
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
![Page 19: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/19.jpg)
Color Theory
![Page 20: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/20.jpg)
Color Theory
![Page 21: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/21.jpg)
Lighting in OpenGL
![Page 22: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/22.jpg)
Lighting in OpenGLLight Source and Object Material
![Page 23: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/23.jpg)
Lighting in OpenGLLight Source and Object Material
![Page 24: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/24.jpg)
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);
![Page 25: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/25.jpg)
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);
![Page 26: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/26.jpg)
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);
![Page 27: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/27.jpg)
Lighting ExampleComplete Code
![Page 28: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/28.jpg)
Lighting Example
![Page 29: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/29.jpg)
SpotLight Lighting ExampleComplete Code
![Page 30: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/30.jpg)
Lighting Example
![Page 31: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/31.jpg)
Normals for Lighting
![Page 32: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/32.jpg)
Why Normals are essential for lighting?
![Page 33: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/33.jpg)
Why Normals are essential for lighting?
![Page 34: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/34.jpg)
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)
![Page 35: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/35.jpg)
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
![Page 36: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/36.jpg)
Vector Normalization
![Page 37: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/37.jpg)
Vector NormalizationWhy to?
![Page 38: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/38.jpg)
Advanced Lighting, Spotlight
![Page 39: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/39.jpg)
Shadowing
![Page 40: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/40.jpg)
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)
![Page 41: OpenGL L04-Lighting](https://reader034.fdocuments.net/reader034/viewer/2022050816/54b4daab4a79592f4e8b46d5/html5/thumbnails/41.jpg)
Shadowing