250 likes | 365 Views
CD2012 Principles of Interactive Graphics Lecture 09. Lights and Shading Abir Hussain www.cms.livjm.ac.uk/cmsahus1. Previous Lecture. Object Oriented programming and design Object oriented graphical user interface Designing your own graphical object. Today’s lecture and Lab.
E N D
CD2012Principles of Interactive GraphicsLecture 09 Lights and Shading Abir Hussain www.cms.livjm.ac.uk/cmsahus1
Previous Lecture • Object Oriented programming and design • Object oriented graphical user interface • Designing your own graphical object CD2012-09
Today’s lecture and Lab CD2012-09
Introduction • We have learned how to build three dimensional graphical models and how to display them. • We also noticed that under orthographic projection, a sphere appears as a uniformly coloured circle. • On the other hand, when looking at a photograph of a lit sphere, we see a circular sphere with many graduations or shade of colours. • What left out is the interaction between light and the surface in the models. CD2012-09
Lights and shade • A surface can either emit light by self-emission as a light bulb does or reflect light from other surfaces that illuminate it. • The interactions between light and materials can be classified into three groups: • Specular surfaces: most of the light that is reflected is scattered in a narrow range • Diffuse surfaces: the reflected light is scattered in all directions • Translucent surfaces: allow some light to penetrate the surface and to emerge from another. CD2012-09
Light sources • Light can leave a surface through two fundamental processes: • Self-emission • reflection. • The uniform lighting is called ambient light. • The narrow range of angles through which light is emitted is called spotlight. CD2012-09
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. CD2012-09
Shading models • More realistic shading models need more calculation • so simpler models are used for interactive graphics. CD2012-09
Shading Models in 3D graphics • Shading in OpenGL is controlled by: • The shading model, we consider two typed of shading: flat and smooth shading. • 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). CD2012-09
Flat Shading • The three vectors (I, n, and v) can vary as we move from point to point on s surface. • For a flat shape, however, n is constant. • This means that for flat shading all the pixels in the same quad, triangle or polygon have the same colour value. CD2012-09
Flat Shading • In OpenGL, we specify flat shading through: • GlShadeModel(GL_FLAT). CD2012-09
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. • In OpenGL, we specify shading smooth through: • GlShadeModel(GL_SMOOTH). CD2012-09
Smooth Shading CD2012-09
Lights in OpenGL • OpenGL support the following light sources: ambient, diffuse and specular and allow eight light sources in a program • there are eight light ID: GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7). CD2012-09
Lights in OpenGL • The OpenGL function: • glLightfv(source, parameter,pointer_to_array); • allow us to set the required vector and scalar parameters, respectively. • There are four vector parameters that we can set: the position or direction of the light source, the amount of ambient, diffuse and specular light associated with the source. CD2012-09
Example • Suppose we wish to specify the first source GL_LIGHT0 and to locate its position at the point (1.0, 2.0, 3.0). • We store its position as follows: • GLfloat light0_pos[]= {1.0, 2.0, 3.0, 1.0}; • We want a white specular component, and red ambient and diffuse components CD2012-09
Example • GLfloat specular0[] ={1.0, 1.0, 1.0, 1.0}; • GLfloat diffuse0[] ={1.0, 0.0, 0.0, 1.0}; • GLfloat ambient0[] ={1.0, 0.0, 0.0, 1.0}; • Firstly we enable lighting • glEnable(GL_LIGHTING); • glEnable(GL_LIGHT0); CD2012-09
Example • Then use the following code: • glLightfv(GL_LIGHT0, GL_SPECULAR, specular0); • glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); • glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0); • glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); • 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. CD2012-09
Specifications of Materials in OpenGL • Material properties in OpenGL match up directly with the supported light sources. • We can also specify different materials properties for the font and back faces of a surface. • All the reflection parameters are specified through the function: • glMaterialfv(face,type, pointer_to_array); CD2012-09
Example • We may define ambient, diffuse, and specular reflection coefficient for each primary colour through: • GLfloat specular[]={1.0,1.0,1.0, 1.0}; • GLfloat diffuse[] ={1.0, 0.8, 0.0, 1.0}; • GLfloat ambient[] ={0.2, 0.2, 0.2, 1.0}; CD2012-09
Example • In this example, a small amount of white ambient reflectivity, yellow diffuse properties, and white specular reflections were defined. We set the material properties for the font and back faces by the calls: • glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient); • glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse); • glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); CD2012-09
Example • 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 (< 1) for matt surfaces and high (>20) for shiny surfaces. CD2012-09
Today’s lab CD2012-09
Today’s lab CD2012-09
Summary • Light and shading • Shading models in 3D graphics • Flat shade • Smooth shade • Light sources in OpenGL • Material Specifications in OpenGL CD2012-09