27
PRESENTATION TOPIC: CHAPTER 8 SHADING AND LIGHTING IN OPEN GL

Effects of lighting and material properties of an object in OpenGl

Embed Size (px)

Citation preview

Page 1: Effects of lighting and material properties of an object in OpenGl

PRESENTATION TOPIC: CHAPTER 8 SHADING AND LIGHTING IN OPEN GL

Page 2: Effects of lighting and material properties of an object in OpenGl

INTRODUCTION OF SHADING MODELS• Shading is the process of computing the color for the

components of a scene. • It is usually done by calculating the effect of light on

each object in a scene to create an effective lighted presentation.

• The shading process is thus based on the physics of light, and the way light scatters from various kinds of materials with various details of surface treatments.

• OpenGL supports two shading models for polygons: flat shading and smooth shading.

• Shading is performed during the rendering process by a program called a shader.• The Shading model is set up with a call to glShadeModel and can be either set to SMOOTH or FLAT model.•glShadeModel (GL SMOOTH) ;

Page 3: Effects of lighting and material properties of an object in OpenGl

FLAT SHADING• Flat shading of a polygon

presents each polygon with a single color.

• The term flat can be taken to mean that the color is flat (does not vary) across the polygon, or that the polygon is colored as though it is flat (planar) and thus does not change color as it is lighted.

• Result of flat shading all of the polygon's vertices are colored with one color, allowing differentiation between adjacent polygons.

Page 4: Effects of lighting and material properties of an object in OpenGl

SMOOTH SHADING• Smooth shading of a polygon

displays the pixels in the polygon with smoothly-changing colors across the surface of the polygon.

• This requires that you provide information that defines a separate color for each vertex of your polygon .

• To compute the color for each vertex separately you must define a separate normal vector for each vertex of the polygon so that the lighting model will produce different colors at each vertex.

Page 5: Effects of lighting and material properties of an object in OpenGl

FLAT VS SMOOTH SHADING

Page 6: Effects of lighting and material properties of an object in OpenGl

CREATION OF LIGHT SOURCE IN OPENGL• With OpenGL, you can manipulate the lighting

and objects in a scene to create many different kinds of effects.

• OpenGL approximates light and lighting as if light can be broken into red, green, and blue components.

Lighting model has to be enabled. glEnable (GL_LIGHTING) ;Lighting support needs the depth buffer to be enabled.glEnable (GL_DEPTH_TEST);The function glEnable can enable many features of OpenGL, the feature you want to enable is provided as an input parameter.

Page 7: Effects of lighting and material properties of an object in OpenGl

The above code enables the lighting,however we need lights as well. OpenGL has direct support for about 8 lights . To enable a light, call:glEnable (GL_LIGHT0) ;Likewise, LIGHT1 or 2::8 shall be enabled when you want to handle multiple lights.Similar to glEnable function, OpenGL also has glDisable function that disables the features set before by glEnable.

Page 8: Effects of lighting and material properties of an object in OpenGl

To set light positionGlfloat lightpos[] = f1; 1; 1; 1g;glLightfv (GL_LIGHT0;GL POSITION; lightpos) ;glLightv(GL_LIGHT0, GL_POSITION, origin);

To specify the visual behavior of the lighting model. two ways:glLightModelf(GLenum pname; GLfloat param); // scalar paramsglLightModelfv(GLenum pname; const GLfloat params); // vectorsuppose you want to have a GLOBAL Ambient light that casts on allGlfloat global ambient[] = f0:5f; 0:5f; 0:5f; 1:0fg // R,G,B,AlphaglLightModelfv (GL_LIGHT_MODEL AMBIENT; global ambient) ;

Page 9: Effects of lighting and material properties of an object in OpenGl

TYPES OF LIGHT SOURCES WHICH CAN BE USED TO LIGHT A SCENE

• Directional light - produced by a light source an infinite distance from the scene., All of the light rays emanating from the light strike the polygons in the scene from a single parallel direction, and with equal intensity everywhere.

• Sunlight is for all intents and purposes a directional light.In fig. directional light from the front of the owl.

• Characterized by color, intensity, and direction.

• Point light - a light that gives off equal amounts of light in all directions. Polygons, and parts of polygons which are closer to the light appear brighter than those that are further away.

• In fig. point light slightly in front of the owl.

Page 10: Effects of lighting and material properties of an object in OpenGl

• The simplest spotlight would just be a point light that is restricted to a certain angle around its primary axis of direction - Think of something like a flashlight or car headlight as opposed to a bare bulb hanging on a wire.

• In fig. spotlight slightly in front of the owl aimed at the owl.

• Light sources are point sources by default, meaning that they emit light uniformly in all directions. But OpenGL allows you to make them into spotlights, so they emit light in a restricted set of directions.

SPOTLIGHT

Page 11: Effects of lighting and material properties of an object in OpenGl

EFFECTS OF MATERIAL PROPERTIES OF AN OBJECT WITH LIGHT

Page 12: Effects of lighting and material properties of an object in OpenGl

• Adding a light to a scene causes many variations in shading and creates more interesting images.Together, lighting and an object's material properties determine its appearance. A lighting model describes the way light interacts with an object, based on the light's characteristics and the object material's characteristics.

• Figure shows a set of objects that were rendered using various lighting models. Notice how the different formulations resemble an assortment of real-world materials.

Page 13: Effects of lighting and material properties of an object in OpenGl

• Now consider The Basic model, modification of classic Phong model. In the Basic model, an object's surface color is the sum of emissive, ambient, diffuse, and specular lighting contributions.

With multiple lights the affect of all the lights are additive.

• Each contribution depends on the combination of the surface's material properties (such as shininess and material color) and the light source's properties (such as light color and position).

Page 14: Effects of lighting and material properties of an object in OpenGl

IN OPENGL AN OBJECT CAN HAVE THE FOLLOWING MATERIAL PROPERTIES

Page 15: Effects of lighting and material properties of an object in OpenGl

1) EMISSIVE LIGHTING (NO LIGHTING )• There are no lights in the scene • Each object is self-luminous (it lights itself,

but does not give off light) • This is also known as "emissive" lighting in

some systems . Truly "no" lighting would be a totally black screen and not very interesting. • Emissive lighting can have some

interesting effects, such as neon signs.• The emissive term can simulate glowing ,

like car head lights. Unlike in the real world, an object's emissive glow does not actually illuminate other nearby objects in the scene.

• An emissive object is not itself a light source—it does not illuminate other objects or cast shadows.

Page 16: Effects of lighting and material properties of an object in OpenGl

• the emissive term is that it is a color added after computing all the other lighting terms.

• Each polygon has its own colour which is constant over its surface

• That colour is not affected by anything like position or orientation else in the world

• This is very fast, but not very realistic • position of viewer is not important • No 3-D information provided.

Page 17: Effects of lighting and material properties of an object in OpenGl

2)DIFFUSED (R, G, B) • The diffuse term accounts

for directed light reflected off a surface equally in all directions.

• In general, diffuse surfaces are rough on a microscopic scale, with small nooks that reflect light in many directions. When incoming rays of light hit these nooks, the light bounces off in all directions, as shown in fig. Diffuse Light Scattering

• The amount of light reflected is proportional to the angle of incidence of the light striking the surface.The diffuse contribution at any particular point on a surface is the same, regardless of where the viewpoint is.

Page 18: Effects of lighting and material properties of an object in OpenGl

3)AMBIENT• Ambient light does not appear to

come from any particular direction; rather, it appears to come from all directions.

• Because of this, the ambient lighting term does not depend on the light source position.

• The ambient term depends on a material's ambient reflectance, as well as the color of the ambient light that is incident on the material.

• Like the emissive term, the ambient term on its own is just a constant color. Unlike the emissive color, however, the ambient term is affected by the global ambient lighting.

Page 19: Effects of lighting and material properties of an object in OpenGl

4)SPECULAR• The specular term represents

light scattered from a surface predominantly around mirror direction. Figure illustrates the concept of specular reflection.

• Unlike the emissive, ambient, and diffuse lighting terms, the specular contribution depends on the location of the viewer.

• reflection off of shiny surfaces - you see a highlight

• shiny metal or plastic has high specular component .

Page 20: Effects of lighting and material properties of an object in OpenGl

5)SHININESS (0.0 - 1.0)

• Controls the contribution of the specular component.

• The specular term is affected not only by the specular color properties of the light source and material,

• but also by how shiny the surface is • Shinier materials have smaller, tighter highlights,• whereas less shiny materials have highlights that

are more spread out.• Figure shows some examples of shininess, with

the shininess exponent increasing from left to right

Page 21: Effects of lighting and material properties of an object in OpenGl

6)TRANSPARENCY 0.0 - 1.0• Material properties specified as RGBA values• A(alpha) value can make surface translucent• Default: all surfaces opaque regardless of A

Page 22: Effects of lighting and material properties of an object in OpenGl

• One important thing to note about all of the above is that each object is dealt with separately. That is, one object does not block light from reaching another object.

Page 23: Effects of lighting and material properties of an object in OpenGl

FUNCTIONS USED FOR LIGHT AND SHADING

Page 24: Effects of lighting and material properties of an object in OpenGl

• There are two types of light properties you should consider while programming the lighting model in OpenGL.

• The first type describes a light source and the second type describes the light reflected by the material of an object's surface.

• For every type of light source you need to call the glLightfv function with parameters.

• For example, To add a component of specular light to a light source, you

• would make the following function call:• GLfloat specular[] = f1:0f; 1:0f; 1:0f; 1:0fg;• glLightv(GL_LIGHT0, GL_SPECULAR, BLACK);• glLightv(GL_LIGHT0, GL_AMBIENT, WHITE);• glLightv(GL_LIGHT0, GL_DIFFUSE, WHITE);

Page 25: Effects of lighting and material properties of an object in OpenGl

• GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};• GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0};• GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};• GLfloat shine = 100.0• glMaterialf(GL_FRONT, GL_AMBIENT, ambient);• glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse);• glMaterialf(GL_FRONT, GL_SPECULAR, specular);• glMaterialf(GL_FRONT, GL_SHININESS, shine);

• The material property of an object,is usually the colour reflected by that object. The first parameter of the glMaterialfv command indicates which face of the polygon should reflect the light specfied by mcolor.

• Apparently, there are two sides to a polygon - front and back. • OpenGl provides two ways to specify a polygon in 3D space in

order to decide the front face. The clockwise or counterclockwise direction describes which side is the front and which is the back.

Page 27: Effects of lighting and material properties of an object in OpenGl

QUESTIONS…..???