Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9: Lighting and Shading 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271

Similar presentations


Presentation on theme: "Lecture 9: Lighting and Shading 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271"— Presentation transcript:

1 Lecture 9: Lighting and Shading 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 d.england@livjm.ac.uk

2 Lecture 9: Lighting and Shading 2 Lighting and Shading  Today - shading models in 3D graphics  How OpenGL supports shading  Interaction of lights with shade models  Lights in OpenGL  Material properties of objects  OpenGL materials - interaction of materials, lighting and shading  Tutorial examples

3 Lecture 9: Lighting and Shading 3 Shading models in 3D graphics Flat shading all the pixels in the same quad, triangle or polygon have the same colour value

4 Lecture 9: Lighting and Shading 4 Shading models in 3D graphics Smooth shading pixel colour values are re-calculated across a polygon according to how the light is reflected from a light source towards the viewer

5 Lecture 9: Lighting and Shading 5 Shading models  There are a variety of shading models in 3D using different algorithms to give a more realistic effect  Gouraud  Phong  They try to approximate the changes in colour values of pixels across a polygon matching its angle in the scene  Sometimes artifacts are visible from shading models:  Try movelight.cpp and you might see slight rings on the torus shape showing where the polygon edges meet  More realistic shading models need more calculation so simpler models are used for interactive graphics

6 Lecture 9: Lighting and Shading 6 Shading in OpenGL  Shading in OpenGL is controlled by  The shading model  glShadeModel(GL_FLAT)  glShadeModel(GL_SMOOTH)  The lights in the scene  The materials of the objects  The final appearance of an object is calculated from the shade model used, the combined effects of any lights and the reflective properties of the materials (plus textures)

7 Lecture 9: Lighting and Shading 7 Lights in OpenGL 1  Most 3D graphics API’s support some form of lighting  Directional lights which can be a parallel beam or a cone light a spot light  Point source lights which shine equally in all directions  Today we will concentrate on point source lights.  Lights are created in OpenGL by calling glLightfv() with a light ID followed by some attributes  OpenGL only has eight light ID’s  GL_LIGHT0, GL_LIGHT1, … GL_LIGHT7  However we can reuse lights if necessary

8 Lecture 9: Lighting and Shading 8 Lights in OpenGL 1  Firstly we enable lighting  glEnable(GL_LIGHTING);  For point source lights we need to specify a position in the 3D scene  e.g. glLightfv(GL_LIGHT0, GL_POSITION, position);  where position is an array of position values in x, y, and z  e.g. GLfloat position[] = {0.0, 0.0, 1.5, 1.0};  movelight.cpp shows the effect of moving a light with the mouse with flat and smooth shading

9 Lecture 9: Lighting and Shading 9 Lights in OpenGL 2  We can also specify the colour of the light  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_colour);  where light_colour is an array of colour values  e.g. GLfloat light_colour[] = {0.0, 1.0, 1.0, 1.0};   For Direction lights we can specify additional attributes such as the angle of the light cone and its attenuation - how much the light fades with distance

10 Lecture 9: Lighting and Shading 10 Material properties of objects  The shape and angular position of polygons is one way of modeling illumination  We can also set the material properties of polygons in terms of their reflective behaviour  For example:  The ambient reflectiveness of the object - how it reflects all light  The specular reflectiveness - how it reflects at highlights  Shininess - how bright are the highlights  Emission - the object itself shines

11 Lecture 9: Lighting and Shading 11 Material properties in OpenGL  We can set the different material properties to model different light reflections in OpenGL.  For example, lightlab.cpp shows examples of  A metallic (brass) surface  A more diffuse reflective (plastic) surface  A reflective gem-stone surface (emerald)  A non-reflective (slate) surface

12 Lecture 9: Lighting and Shading 12  Material values are set via glMaterialfv(),  e.g glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);  Again ambient is an array of colour values  We can control how shiny the object is with glMaterialfv(GL_FRONT, GL_SPECULAR, specular); glMaterialf(GL_FRONT, GL_SHININESS, shininess);  Where shininess can be a low value ( 20) for shiny surfaces  Polygons have two sides so we must specify which is illuminated (we can ignore the back side mostly)

13 Lecture 9: Lighting and Shading 13 Example Plastic Brass Emerald

14 Lecture 9: Lighting and Shading 14 Example...  With lightlab.cpp you can experiment with the interaction of  shading model - flat or smooth  material properties  lighting colour  Having many lights in a scene requires a lot of computation  So OpenGL lights are usually reserved for moving lights  We can mimic the effects of lights by textures  Rather than apply an image texture map we apply an light map  This is used in many games engines for speed

15 Lecture 9: Lighting and Shading 15 Tutorial  Copy and compile the two example programs  lightlab.cpp and movelight.cpp  With movelight.cpp experiment with different values for light_colour  With lightlab.cpp  Create a new material  copy and paste an existing definition and alter its values  Change the menu options to use your new material


Download ppt "Lecture 9: Lighting and Shading 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271"

Similar presentations


Ads by Google