1 / 20

Lighting and Shading

Lighting and Shading. An introduction to lighting and shading using OpenGL. From OpenGL’s perspective,. Shading is the process of filling in the pixels that make up lines and polygons. Lighting is the process of calculating illumination at the vertices. OpenGL’s shading model.

axelle
Télécharger la présentation

Lighting and Shading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lighting and Shading An introduction to lighting and shading using OpenGL From OpenGL’s perspective, Shading is the process of filling in the pixels that make up lines and polygons Lighting is the process of calculating illumination at the vertices

  2. OpenGL’s shading model Basic way to specify a colour is with the commands: glColor3f r g b alpha value, a, defaults to 1.0 glColor4f r g b a OpenGL can work in two colour modes RGBA Color-index mode Only going to consider RGBA mode within this course Colours can be specified per-vertex; a shading model is selected that specifies how colours are generated between the vertices glShadeModel GL_FLAT GL_SMOOTH

  3. Shading glShadeModel(GL_SMOOTH); glShadeModel(GL_FLAT); glBegin( GL_TRIANGLES ); glColor3f( 1, 0, 0 ); glVertex2f( 0, 0.5 ); glColor3f( 0, 1, 0 ); glVertex2f( 0.5, -0.5 ); glColor3f( 0, 0, 1 ); glVertex2f( -0.5, -0.5 ); glEnd();

  4. Lighting on or off? By default, OpenGL’s lighting model is turned off Turn it on with glEnable(GL_LIGHTING); If lighting is off, you specify vertex colours directly: glColor3f(0.1,0.8,0.2); If lighting is on, you don’t specify vertex colours directly, but rather specify material properties and illumination sources; OpenGL calculates vertex colours itself glColor3f(0.1,0.8,0.2); // redundant glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, some parameters for light0); glMaterialfv(GL_FRONT, some parameters for material);

  5. How do surfaces reflect light? In very complex ways... • Different wavelengths (colours) are reflected to differing amounts • Light is scattered; very complex functions relate the intensity of light reflected at a particular angle to the incident angle • Light may be multiply scattered within the surface layer • Fluorescence and phosphorescence may occur

  6. What light is present in the environment? Ambient light Self-luminous objects Point light sources Large light sources

  7. How to approximate realistic surfaces in computer graphics reflected ^ “Let there be light” Specify light source characteristics {luminance, colour, direction, spreading characteristics} Specify surface material characteristics {reflectance, colour, shininess} Use a lighting model that takes account of the relative positioning and orientation of: • each surface relative to the light source[s] • each surface relative to the observer (viewpoint) Give surfaces textures (a later lecture...) Give surfaces roughness ( later course...)

  8. A Lit sphere Basic lit sphere With polygons With surface normals A sphere can be drawn with the utility function glutSolidSphere( fRadius,nSlices,nStacks );

  9. OpenGL light sources OpenGL supports at least 8 lights, which may be positioned within the world, as well as global ambient illumination Lights themselves are invisible...!! Their effects are only seen when they illuminate something (a polygon or a line)

  10. Combination of illumination and surface Emissive characteristics Ambient light characteristics × Ambient reflection measures Diffuse light characteristics Light characteristics × Diffuse reflection measures Specular light characteristics × Specular reflection measures OpenGL’s model How a surface is rendered (or, in the real world, perceived) is a function of both the surface material and the illumination characteristics

  11. Ambient lighting Ambient reflectance affects the overall colour of the object Since ambient light is considered to be light that has been scattered so many times that its direction is impossible to determine, the intensity of the ambient reflection is independent of the surface orientation An object’s total ambient reflectance is proportional to the sum of the global ambient light and ambient light from individual light sources The ambient reflectance isn’t affected by the viewpoint position

  12. Diffuse reflections Diffuse reflections are maximised when light falls directly on a surface... ..and reduce with oblique illumination... surface normal  direct illumination This surface is seen more or less face-on This surface is seen obliquely direct illumination Diffuse reflections play the most important role in perception of colour Viewpoint makes no difference: The intensities of the diffuse reflections are identical in both cases

  13. Specular reflections Light source angle of = angle of incidence reflection imperfect mirror = narrow cone   mirror Specular reflections are the mirror like reflections that add highlights to a shiny object Reflected light OpenGL does not consider a scene reflected in a mirror surface to be comprised of specular reflections: other advanced techniques are available to handle such levels of realism

  14. OpenGL’s model • A vertex’s colour, Cv, is sum of the contributions from: • a material emission term, Em • global ambient lighting, Al, and a material ambient term, Am • individual light’s contributions, Cvi where,

  15. Other lighting models Phong-shading is not directly supported by OpenGL. To create an equivalent image, would require that all polygons were, at most, one pixel wide on the screen. In the literature, three types of shading model referred to are: flat-shading Gourand-shading Phong-shading The first two correspond to OpenGL’s lighting model with glShadeModel set to GL_FLAT and GL_SMOOTH, respectively

  16. Distance attenuation In physics, the amount of illumination from a point light source at distance, d, follows an inverse square law: d d If the light were perfectly collimated (such as from an idealised laser), distance would have no effect on illumination: d d OpenGL allows factors to be set that mimic these extremes (or intermediate states): constant linear quadratic d d OpenGL models how illumination falls with distance to a light source

  17. Spotlight effect Spotlights point in a particular direction, , Define to be the direction vector from the light to a vertex The amount of illumination received at a vertex depends on the angle between V2 Abrupt cutoff V1 Reducing illumination Brightest illumination  V1 receives some light V2 receives no light

  18. Distance fading or fogging Setting Cf to white would recreate the haziness of looking a large distance through the atmosphere where, Setting Cf to black may be used to make near features stand out more Fogging can be used to recreate more natural scenes, by having distant objects merge into the colour of the ‘background’ Ci is the incoming colour; Cf is the fog colour

  19. OpenGL fogging functions inverse exponential inverse exponential squared linear GL_EXP2 GL_LINEAR GL_EXP f can be generated by selecting one of three functions of distance (of the vertex from the camera position): glFogi( GL_FOG_MODE, FogMode ); GLfloat fogColour[4] = {1.0, 1.0, 1.0, 1.0}; glFogi( GL_FOG_MODE, GL_LINEAR ); glFogfv(GL_FOG_COLOR, fogColour); glFogf( GL_FOG_START, 5.0 ); glFogf( GL_FOG_END, 10.0 );

  20. Light local / global approximations In OpenGL, one of these aspects is defined by the direction from the vertex to the viewpoint For accurate rendering of the specular reflections within a scene it is necessary to take account of the position and orientation of a surface relative to both the light source[s] and the observer If the viewpoint is a long way away from the vertices that make up the scene, then this direction is approximately constant By default, OpenGL assumes that this is the case and saves time by assuming an infinite viewpoint – which means that it is not necessary to calculate a vertex-viewpoint direction for every vertex in the scene To get accurate rendering, it is, however, necessary, to turn on the correct [local] method of calculation: glLightModel( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE );

More Related