Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shading Chapter 6. CS 480/680 2Chapter 6 -- Shading Introduction: Introduction: We have learned to build three-dimensional models and to display them.

Similar presentations


Presentation on theme: "Shading Chapter 6. CS 480/680 2Chapter 6 -- Shading Introduction: Introduction: We have learned to build three-dimensional models and to display them."— Presentation transcript:

1 Shading Chapter 6

2 CS 480/680 2Chapter 6 -- Shading Introduction: Introduction: We have learned to build three-dimensional models and to display them. We have learned to build three-dimensional models and to display them. However, if you render one of our models, you might be disappointed to see images that look flat. However, if you render one of our models, you might be disappointed to see images that look flat. This appearance is a consequence of our unnatural assumption that each surface is lit such that it appears to the viewer in a single color. This appearance is a consequence of our unnatural assumption that each surface is lit such that it appears to the viewer in a single color. We have left out the interaction between light and the surfaces in our models We have left out the interaction between light and the surfaces in our models So, we will begin by developing models of light sources and the most common light-material interactions. So, we will begin by developing models of light sources and the most common light-material interactions.

3 CS 480/680 3Chapter 6 -- Shading We then will investigate how we can apply shading to a polygonal model. We then will investigate how we can apply shading to a polygonal model. We then develop a recursive approximation to a sphere to test our shading algorithms. We then develop a recursive approximation to a sphere to test our shading algorithms. We then discuss how light and material properties are specified in OpenGL We then discuss how light and material properties are specified in OpenGL and can be added to our sphere approximating program. and can be added to our sphere approximating program. We conclude the chapter with a short discussion of the two most important methods for handling global lighting effects: We conclude the chapter with a short discussion of the two most important methods for handling global lighting effects: Ray Tracing and Radiosity Ray Tracing and Radiosity

4 CS 480/680 4Chapter 6 -- Shading 1. Light and Matter From a physical perspective, a surface can either From a physical perspective, a surface can either emit light by self-emission (as a light bulb does) emit light by self-emission (as a light bulb does) or reflect light from other surfaces that illuminate it. or reflect light from other surfaces that illuminate it.

5 CS 480/680 5Chapter 6 -- Shading The equation for solving this shading is called the rendering equation. The equation for solving this shading is called the rendering equation. This cannot be solved in general, so we use approximations. This cannot be solved in general, so we use approximations. Radiosity and ray-tracing are approximations to this. Radiosity and ray-tracing are approximations to this. Unfortunately these approximations cannot yet be used to render scenes at the rate we can pass polygons through the modeling-projection pipeline. Unfortunately these approximations cannot yet be used to render scenes at the rate we can pass polygons through the modeling-projection pipeline. Therefore, we will focus on a simpler rendering model Therefore, we will focus on a simpler rendering model This model is based upon the Phong reflection model This model is based upon the Phong reflection model

6 CS 480/680 6Chapter 6 -- Shading To get an overview of the process, we can start following rays of light from a point-light-source To get an overview of the process, we can start following rays of light from a point-light-source

7 CS 480/680 7Chapter 6 -- Shading In terms of Computer graphics, we replace the viewer with the projection plane In terms of Computer graphics, we replace the viewer with the projection plane Note that most rays leaving a source do not contribute to the image and are thus of no interest to us. Note that most rays leaving a source do not contribute to the image and are thus of no interest to us.

8 CS 480/680 8Chapter 6 -- Shading The interaction between light and materials can be classified into three groups The interaction between light and materials can be classified into three groups (a) specular (a) specular (b) diffuse (b) diffuse (c) translucent (c) translucent

9 CS 480/680 9Chapter 6 -- Shading Specular Surfaces Specular Surfaces appear shiny because most of the light that is reflected is reflected in a narrow range of angles close to the angle of reflection. appear shiny because most of the light that is reflected is reflected in a narrow range of angles close to the angle of reflection. Angle of Incidence is equal to the angle of reflection. Angle of Incidence is equal to the angle of reflection. Diffuse Surfaces Diffuse Surfaces reflected light is scattered in all directions. reflected light is scattered in all directions. Translucent Surfaces Translucent Surfaces allow some light to penetrate the surface and to emerge from another location on the object. allow some light to penetrate the surface and to emerge from another location on the object. Refraction Refraction

10 CS 480/680 10Chapter 6 -- Shading 2. Light Sources Light can leave a surface through Light can leave a surface through self-emission and reflection. self-emission and reflection. When we discuss OpenGL lighting in section 7 we shall see that we can easily simulate a self-emission term. When we discuss OpenGL lighting in section 7 we shall see that we can easily simulate a self-emission term.

11 CS 480/680 11Chapter 6 -- Shading If we consider a source such as: If we consider a source such as: each point on the surface can emit light that is characterized by each point on the surface can emit light that is characterized by the direction of emission ( the direction of emission (  (u,t) the intensity of energy emitted at each wavelength Thus, a general light source can be characterized by an illumination function I (x, y, z, 

12 CS 480/680 12Chapter 6 -- Shading From the perspective of the surface that is being illuminated, we can obtain the total contribution of the source by integrating over the surface of the source. From the perspective of the surface that is being illuminated, we can obtain the total contribution of the source by integrating over the surface of the source. This can be difficult, so we will consider four basic types of sources: ambient lighting, point sources, spotlights, and distant lights These four are sufficient for rendering most simple scenes.

13 CS 480/680 13Chapter 6 -- Shading 2.1 Color Sources 2.1 Color Sources Not only do light sources emit different amounts of light at different frequencies, but also their directional properties can very with frequency. Not only do light sources emit different amounts of light at different frequencies, but also their directional properties can very with frequency. Consequently, a physically correct model can be complex. Consequently, a physically correct model can be complex. However, since we our visual system is based upon three colors, However, since we our visual system is based upon three colors, for most applications, we can use each of the three colors to obtain what the human observer sees. for most applications, we can use each of the three colors to obtain what the human observer sees.

14 CS 480/680 14Chapter 6 -- Shading 2.2 Ambient Light 2.2 Ambient Light In some rooms, such as certain classrooms or kitchens, the lights have been designed and positioned to provide uniform illumination throughout the room. In some rooms, such as certain classrooms or kitchens, the lights have been designed and positioned to provide uniform illumination throughout the room. Often this is achieved with light sources that have diffusers whose purpose is to scatter light in all directions. Often this is achieved with light sources that have diffusers whose purpose is to scatter light in all directions. Florescent lights have covers designed to do this. Florescent lights have covers designed to do this.

15 CS 480/680 15Chapter 6 -- Shading Making such a model and rendering the scene with it would be a daunting task for a graphics system. Making such a model and rendering the scene with it would be a daunting task for a graphics system. Alternatively, we can look at the desired effect: Alternatively, we can look at the desired effect: to achieve a uniform light level in the room to achieve a uniform light level in the room This uniform lighting is called Ambient Light. This uniform lighting is called Ambient Light.

16 CS 480/680 16Chapter 6 -- Shading 2.3 Point Sources 2.3 Point Sources An ideal point source emits light equally in all directions. An ideal point source emits light equally in all directions. We can characterize a point light source by a three- component color matrix. We can characterize a point light source by a three- component color matrix. The intensity of illumination received from a point source is proportional to the inverse square of the distance from the source to the surface. The intensity of illumination received from a point source is proportional to the inverse square of the distance from the source to the surface.

17 CS 480/680 17Chapter 6 -- Shading Scenes rendered with only point sources tend to have high contrast Scenes rendered with only point sources tend to have high contrast (objects appear either bright or dark) (objects appear either bright or dark) In the real world, it is the large size of most light sources that contributes to softer scenes. In the real world, it is the large size of most light sources that contributes to softer scenes. Umbra Umbra Penumbra Penumbra

18 CS 480/680 18Chapter 6 -- Shading 2.4 Spotlights 2.4 Spotlights spotlights are characterized by a narrow range of angles through which light is emitted. spotlights are characterized by a narrow range of angles through which light is emitted. We can construct a spotlight from a point source by limiting the angles. We can construct a spotlight from a point source by limiting the angles. For example, we can use a cone For example, we can use a cone

19 CS 480/680 19Chapter 6 -- Shading More realistic spotlights are characterized by the distribution of light in the cone. More realistic spotlights are characterized by the distribution of light in the cone. Usually most of the light is concentrated at the center of the cone. Usually most of the light is concentrated at the center of the cone. The intensity is a function of the angle The intensity is a function of the angle  As we will see throughout this chapter, cosines are convenient functions for lighting calculations. As we will see throughout this chapter, cosines are convenient functions for lighting calculations.

20 CS 480/680 20Chapter 6 -- Shading 2.5 Distant Light Sources 2.5 Distant Light Sources Most shading calculations require the direction from the point on the surface to the light source Most shading calculations require the direction from the point on the surface to the light source As we move across a surface, calculating the intensity at each point, we should recompute this vector repeatedly. As we move across a surface, calculating the intensity at each point, we should recompute this vector repeatedly. This is very expensive and is a significant part of the shading calculation. This is very expensive and is a significant part of the shading calculation. However, if the light source is far from the surface, the vector does not change much However, if the light source is far from the surface, the vector does not change much

21 CS 480/680 21Chapter 6 -- Shading In this case, we are effectively replacing a point light source with a source that illuminates objects with parallel rays of light. In this case, we are effectively replacing a point light source with a source that illuminates objects with parallel rays of light. Graphics systems can carry out rendering calculations more efficiently for distant light sources than for near ones. Graphics systems can carry out rendering calculations more efficiently for distant light sources than for near ones. OpenGL allows both OpenGL allows both

22 CS 480/680 22Chapter 6 -- Shading 3. The Phong Reflection Model Although we could approach light-material interactions through physical models, Although we could approach light-material interactions through physical models, we have chosen to use a model that leads to efficient computations and to be a close enough approximation to physical reality to produce good renderings under a variety of lighting conditions and material properties. we have chosen to use a model that leads to efficient computations and to be a close enough approximation to physical reality to produce good renderings under a variety of lighting conditions and material properties.

23 CS 480/680 23Chapter 6 -- Shading The model uses the four vectors shown here to calculate a color for a point p on a surface. The model uses the four vectors shown here to calculate a color for a point p on a surface. n is the normal vector at p n is the normal vector at p We discuss its computation in section 6.4 We discuss its computation in section 6.4 v is the vector from p to the viewer or COP v is the vector from p to the viewer or COP l is the vector from p to a light source l is the vector from p to a light source r is in the direction that a perfectly reflected ray from l would take. r is in the direction that a perfectly reflected ray from l would take. This is determined by n and l (calculated in 6.4) This is determined by n and l (calculated in 6.4)

24 CS 480/680 24Chapter 6 -- Shading The Phong model supports three types of light- material interactions The Phong model supports three types of light- material interactions Ambient, Diffuse, and Specular. Ambient, Diffuse, and Specular. For the total Intensity we write: For the total Intensity we write: I = I a + I d + I s = L a R a + L d R d + L s R s I = I a + I d + I s = L a R a + L d R d + L s R s where R is the reflection term where R is the reflection term with the understanding that the computation will be done for each of the primaries and each source. with the understanding that the computation will be done for each of the primaries and each source.

25 CS 480/680 25Chapter 6 -- Shading 3.1 Ambient Reflection 3.1 Ambient Reflection The intensity of ambient light L a is the same at every point on the surface. The intensity of ambient light L a is the same at every point on the surface. Some light is absorbed and some is reflected. Some light is absorbed and some is reflected. The amount reflected is given by the ambient reflection coefficient, R a =k a (0 k a The amount reflected is given by the ambient reflection coefficient, R a =k a (0  k a  Thus I a = k a L a Thus I a = k a L a A surface has of course, three ambient coefficients A surface has of course, three ambient coefficients k ar, k ag, and k ab k ar, k ag, and k ab and they can be different and they can be different Hence, a sphere appears yellow under white ambient light if its blue ambient coefficient is small and its red and green coefficients are large. Hence, a sphere appears yellow under white ambient light if its blue ambient coefficient is small and its red and green coefficients are large.

26 CS 480/680 26Chapter 6 -- Shading 3.2 Diffuse Reflection 3.2 Diffuse Reflection A perfectly diffuse reflector scatters the light that it reflects equally in all directions. A perfectly diffuse reflector scatters the light that it reflects equally in all directions. Perfectly diffuse surfaces are so rough that there is no preferred angle of reflection Perfectly diffuse surfaces are so rough that there is no preferred angle of reflection

27 CS 480/680 27Chapter 6 -- Shading 3.3 Specular Reflection 3.3 Specular Reflection If we employ only ambient and diffuse reflections, our images will be shaded and will appear three-dimensional, but all the surfaces will look dull, somewhat like chalk. If we employ only ambient and diffuse reflections, our images will be shaded and will appear three-dimensional, but all the surfaces will look dull, somewhat like chalk. What is missing is the highlights What is missing is the highlights

28 CS 480/680 28Chapter 6 -- Shading A specular surface is smooth. A specular surface is smooth. A mirror is a perfectly specular surface. A mirror is a perfectly specular surface. Modeling specular surfaces realistically can be complex Modeling specular surfaces realistically can be complex This is because the pattern is not symmetric, it depends upon the wavelength and it changes with the reflection angle. This is because the pattern is not symmetric, it depends upon the wavelength and it changes with the reflection angle. The Phong model adds a term for specular reflection. The Phong model adds a term for specular reflection. The amount of light that the viewer sees depends upon the angle  between r and the reflector v The amount of light that the viewer sees depends upon the angle  between r and the reflector v

29 CS 480/680 29Chapter 6 -- Shading The Phong Model uses the equation The Phong Model uses the equation I s = k s L s cos   I s = k s L s cos   k s is the fraction of the incoming specular light that is reflected. k s is the fraction of the incoming specular light that is reflected.  is the shininess coefficient.  is the shininess coefficient. As  is increased, the reflected light is concentrated in a narrower region, centered on the perfect reflector. As  is increased, the reflected light is concentrated in a narrower region, centered on the perfect reflector.

30 CS 480/680 30Chapter 6 -- Shading 4. Computation of Vectors The illumination and reflection models that we have derived are sufficiently general that they can be applied to either curved or flat surfaces, to parallel or perspective views, and to distant or near surfaces. The illumination and reflection models that we have derived are sufficiently general that they can be applied to either curved or flat surfaces, to parallel or perspective views, and to distant or near surfaces. Most require vectors and dot products. Most require vectors and dot products. In this section we see what additional techniques can be applied when our objets are composed of flat polygons. In this section we see what additional techniques can be applied when our objets are composed of flat polygons.

31 CS 480/680 31Chapter 6 -- Shading 4.1 Normal Vectors 4.1 Normal Vectors Plane Plane Typically the definition is given as Typically the definition is given as ax+ by+ cz + d = 0 ax+ by+ cz + d = 0 It could also be given as It could also be given as n. (p-p 0 ) = 0 n. (p-p 0 ) = 0 If we were given 3 noncollinear points, we could find the normal by If we were given 3 noncollinear points, we could find the normal by n = (p 2 -p 0 ) x (p 1 -p 0 ) n = (p 2 -p 0 ) x (p 1 -p 0 ) we must be careful about the order of the vectors. Reversing the order changes the surface from outward pointing to inward pointing. we must be careful about the order of the vectors. Reversing the order changes the surface from outward pointing to inward pointing.

32 CS 480/680 32Chapter 6 -- Shading Curved Surfaces Curved Surfaces We could go through the math of how a sphere is defined. We could go through the math of how a sphere is defined. We could go through parametric equations of a sphere We could go through parametric equations of a sphere But we are only interested in the direction of the normal, so if the sphere is centered at the origin, we can say the normal is p But we are only interested in the direction of the normal, so if the sphere is centered at the origin, we can say the normal is p

33 CS 480/680 33Chapter 6 -- Shading In OpenGL we can associate a normal with a vertex through functions such as In OpenGL we can associate a normal with a vertex through functions such as glNormal3f(nx, ny, nz); glNormal3f(nx, ny, nz); glNormal3fv(pointer_to_normal); glNormal3fv(pointer_to_normal); Normals are modal variables. Normals are modal variables. If we define a normal before a sequence of vertices If we define a normal before a sequence of vertices this normal is associated with all the vertices this normal is associated with all the vertices and is used for the lighting calculations at all the vertices and is used for the lighting calculations at all the vertices The problem remains, however, that we have to determine these normals ourselves. The problem remains, however, that we have to determine these normals ourselves.

34 CS 480/680 34Chapter 6 -- Shading 4.2 Angle of Reflection 4.2 Angle of Reflection Once we have calculated the normal at a point, we can use this normal and the direction of light to compute the direction of a perfect reflection Once we have calculated the normal at a point, we can use this normal and the direction of light to compute the direction of a perfect reflection angle of incidence is equal to the angle of reflection angle of incidence is equal to the angle of reflection

35 CS 480/680 35Chapter 6 -- Shading 4.3 Use of the Halfway Vector 4.3 Use of the Halfway Vector If we use the Phong model with specular reflections in our rendering, the dot product r. v should be recalculated at every point on the surface If we use the Phong model with specular reflections in our rendering, the dot product r. v should be recalculated at every point on the surface We can obtain an interesting approximation by using the unit vector halfway between the viewer vector and the light-source vector: We can obtain an interesting approximation by using the unit vector halfway between the viewer vector and the light-source vector:

36 CS 480/680 36Chapter 6 -- Shading If we replace r. v with n. h, we avoid calculation of r If we replace r. v with n. h, we avoid calculation of r However, the halfway angle is smaller than the angle used for specular computation, so if we use the same exponent e in (n. h) e the specular highlights will be smaller, However, the halfway angle is smaller than the angle used for specular computation, so if we use the same exponent e in (n. h) e the specular highlights will be smaller, so we use a different value of e to fix this. so we use a different value of e to fix this. Exercise 6.8 helps you see how much work this saves. Exercise 6.8 helps you see how much work this saves.

37 CS 480/680 37Chapter 6 -- Shading 4.4 Transmitted Light 4.4 Transmitted Light Consider a surface that transmits all the light that strikes it Consider a surface that transmits all the light that strikes it If the speed of light differs in the two materials, the light is bent at the surface If the speed of light differs in the two materials, the light is bent at the surface Let  l and  t be the indices of refraction Let  l and  t be the indices of refraction Snell’s law states that Snell’s law states that

38 CS 480/680 38Chapter 6 -- Shading Therefore, we can calculate the direction of the transmitted light. Therefore, we can calculate the direction of the transmitted light. A more general expression for what happens at a transmitting surface corresponds to this figure A more general expression for what happens at a transmitting surface corresponds to this figure some light is transmitted, some light is transmitted, some is reflected, some is reflected, and the rest is absorbed. and the rest is absorbed.

39 CS 480/680 39Chapter 6 -- Shading 5. Polygonal Shading Assuming that we can compute normal vectors, given a set of light sources and a viewer, both the lighting and illumination models that we have developed can be applied at every point on a surface. Assuming that we can compute normal vectors, given a set of light sources and a viewer, both the lighting and illumination models that we have developed can be applied at every point on a surface. Consider the polygon mesh shown here. We will consider three ways to shade the polygons: flat, interpolative or Gourand, and Phong shading Consider the polygon mesh shown here. We will consider three ways to shade the polygons: flat, interpolative or Gourand, and Phong shading

40 CS 480/680 40Chapter 6 -- Shading 5.1 Flat Shading 5.1 Flat Shading The three vectors - l, n, and v - can vary The three vectors - l, n, and v - can vary but for a flat polygon, n is constant but for a flat polygon, n is constant if we assume the viewer does not change, v is constant if we assume the viewer does not change, v is constant if the light source is distance, l is constant if the light source is distance, l is constant If all three are constant, the shading calculations only need to be carried out once for each polygon. If all three are constant, the shading calculations only need to be carried out once for each polygon. In OpenGL we specify flat shading through In OpenGL we specify flat shading through glShadeModel(GL_FLAT); glShadeModel(GL_FLAT); This can be disappointing in its results This can be disappointing in its results

41 CS 480/680 41Chapter 6 -- Shading 5.2 Interpolative and Gourand Shading 5.2 Interpolative and Gourand Shading If we use: If we use: glShadeModel(GL_SMOOTH); glShadeModel(GL_SMOOTH); Then OpenGL interpolates colors for other primitives like lines Then OpenGL interpolates colors for other primitives like lines The normals are computed at each vertex. Colors and intensities of interior points are interpolated between vertices. The normals are computed at each vertex. Colors and intensities of interior points are interpolated between vertices. Problem: normals are discontinuous at the vertex Problem: normals are discontinuous at the vertex Solution: average them Solution: average them Color plates 3-5 show flat shading up to interpolative. Color plates 3-5 show flat shading up to interpolative.

42 CS 480/680 42Chapter 6 -- Shading 5.3 Phong Shading 5.3 Phong Shading Even the smoothness introduced by Gourand shading may not prevent bands. Even the smoothness introduced by Gourand shading may not prevent bands. Phong proposed that instead of interpolating the intensities, interpolate the normals Phong proposed that instead of interpolating the intensities, interpolate the normals and then do calculation of intensities using the interpolated normal (typically at scan conversion) and then do calculation of intensities using the interpolated normal (typically at scan conversion) This is much smoother, but at a greater computational cost. This is much smoother, but at a greater computational cost.

43 CS 480/680 43Chapter 6 -- Shading 6. Approximation of a Sphere by Recursive Subdivision We have used the sphere as an example curved surface to illustrate shading calculations. We have used the sphere as an example curved surface to illustrate shading calculations. However, the sphere is not an object that is supported within OpenGL However, the sphere is not an object that is supported within OpenGL Both the GL utility library (GLU) and the GL Utility Toolkit (GLUT) contain spheres Both the GL utility library (GLU) and the GL Utility Toolkit (GLUT) contain spheres GLU via quadric surfaces GLU via quadric surfaces A topic discussed in Chapter 10 A topic discussed in Chapter 10 GLUT via polygonal approximation GLUT via polygonal approximation

44 CS 480/680 44Chapter 6 -- Shading This section develops a polygonal approximation to a sphere. This section develops a polygonal approximation to a sphere. This figure shows an approximation to a sphere drawn with this code This figure shows an approximation to a sphere drawn with this code

45 CS 480/680 45Chapter 6 -- Shading 7. Light Sources in OpenGL OpenGL supports the four types of light sources that we just described, and allows at least 8 light sources per program. OpenGL supports the four types of light sources that we just described, and allows at least 8 light sources per program. Each light source must be individually specified and enabled Each light source must be individually specified and enabled glLightfv(source, parameter, pointer_to_array) glLightfv(source, parameter, pointer_to_array) glLightf(source, parameter, value); glLightf(source, parameter, value);

46 CS 480/680 46Chapter 6 -- Shading There are four vector parameters that we can set There are four vector parameters that we can set The position (or direction) of the light, the amount of ambient, diffuse, and specular light associated with a source The position (or direction) of the light, the amount of ambient, diffuse, and specular light associated with a source For example: For example: glEnable(GL_LIGHTING); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); glLightfv(GL_LIGHT0, GL_AMBIENT, ambinet0); glLightfv(GL_LIGHT0, GL_AMBIENT, ambinet0); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); glLightfv(GL_LIGHT0, GL_SPECULAR, specular0); glLightfv(GL_LIGHT0, GL_SPECULAR, specular0); Note that we must enable both lighting and the particular source. Note that we must enable both lighting and the particular source.

47 CS 480/680 47Chapter 6 -- Shading We can also set a global ambient term that is independent of any light We can also set a global ambient term that is independent of any light glLightModel(GL_LIGHT_MODEL_AMIENT, global_ambient_data); glLightModel(GL_LIGHT_MODEL_AMIENT, global_ambient_data); We can convert a source to a spotlight by choosing: We can convert a source to a spotlight by choosing: The spotlight direction (GL_SPOT_DIRECTION) The spotlight direction (GL_SPOT_DIRECTION) The exponent (GL_SPOT_EXPONENT) The exponent (GL_SPOT_EXPONENT) And the angle (GL_SPOT_CUTOFF) And the angle (GL_SPOT_CUTOFF) If we have objects that we need to have the light interact properly with both sides we may need to set If we have objects that we need to have the light interact properly with both sides we may need to set glLightModel(GL_LIGHT_MODEL_TWO_SIDED, GL_TRUE); glLightModel(GL_LIGHT_MODEL_TWO_SIDED, GL_TRUE);

48 CS 480/680 48Chapter 6 -- Shading 8. Specification of Materials in OpenGL Material properties in OpenGL match up directly with the supported light sources and with the Phong reflection model. Material properties in OpenGL match up directly with the supported light sources and with the Phong reflection model. We can also specify different material properties for the front and the back faces of a surface We can also specify different material properties for the front and the back faces of a surface All our reflection parameters are specified through the functions: All our reflection parameters are specified through the functions: glMaterialfv(face, type, pointer_to_array); glMaterialfv(face, type, pointer_to_array); glMaterialf(face, value); glMaterialf(face, value);

49 CS 480/680 49Chapter 6 -- Shading For Example: For Example: glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient); Note: Note: If both the specular and diffuse coefficients are the same (as is of the the case), we can specify both by using GL_DIFFUSE_AND_SPECULAR If both the specular and diffuse coefficients are the same (as is of the the case), we can specify both by using GL_DIFFUSE_AND_SPECULAR To specify different front- and back-face properties, To specify different front- and back-face properties, we use GL_FRONT and GL_BACK we use GL_FRONT and GL_BACK The shininess of a surface (the exponent in the specular-reflection term) is specified as follows: The shininess of a surface (the exponent in the specular-reflection term) is specified as follows: glMatrialg(GL_FRONT, GL_SHININESS, 100.0); glMatrialg(GL_FRONT, GL_SHININESS, 100.0);

50 CS 480/680 50Chapter 6 -- Shading OpenGL also allows us to define surfaces that have an emissive component OpenGL also allows us to define surfaces that have an emissive component This characterizes self-luminous sources. This characterizes self-luminous sources. This is useful if you want a light source to appear in your image. This is useful if you want a light source to appear in your image. It is unaffected by any light source It is unaffected by any light source And it does not affect any other surface. And it does not affect any other surface. For example this defines a small amount of blue-green emission: For example this defines a small amount of blue-green emission: Glfloat emission[ ] = {0.0, 0.3, 0.3, 1.0}; Glfloat emission[ ] = {0.0, 0.3, 0.3, 1.0}; glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission);

51 CS 480/680 51Chapter 6 -- Shading 9. Shading of the Sphere Model We can now shade our spheres. We can now shade our spheres. If we are to shade our approximate spheres with OpenGL’s shading model, we must assign normals. If we are to shade our approximate spheres with OpenGL’s shading model, we must assign normals. Following our previous approach, we use the cross product, and then normalize the result Following our previous approach, we use the cross product, and then normalize the result

52 CS 480/680 52Chapter 6 -- Shading cross(point3 a, point3 b, point3 c, point3 d) { d[0]=(b[1]-a[1])*(c[2]-a[2])-(b[2]-a[2])*(c[1]-a[1]); d[0]=(b[1]-a[1])*(c[2]-a[2])-(b[2]-a[2])*(c[1]-a[1]); d[0]=(b[2]-a[2])*(c[0]-a[0])-(b[0]-a[0])*(c[2]-a[2]); d[0]=(b[2]-a[2])*(c[0]-a[0])-(b[0]-a[0])*(c[2]-a[2]); d[0]=(b[0]-a[0])*(c[1]-a[1])-(b[1]-a[1])*(c[0]-a[0]); d[0]=(b[0]-a[0])*(c[1]-a[1])-(b[1]-a[1])*(c[0]-a[0]); normal(d); normal(d);} void normal(point3 p) { float d = 0.0; float d = 0.0; int i; int i; for(i=0; i<3; i++) d += p[i]; for(i=0; i<3; i++) d += p[i]; d = sqrt(d); d = sqrt(d); if(d>0.0) for(i=0;i 0.0) for(i=0;i<3;i++) p[i]/=d;}

53 CS 480/680 53Chapter 6 -- Shading Assuming the light sources have been defined and enabled: Assuming the light sources have been defined and enabled: void triangle(point3 a, point3 b, point3 c) { point3 n; point3 n; cross(a,b,c,n); cross(a,b,c,n); glBegin(GL_POLYGON); glBegin(GL_POLYGON); glNormal3fv(n); glNormal3fv(n); glVertex3fv(a); glVertex3fv(a); glVertex3fv(b); glVertex3fv(b); glVertex3fv(c); glVertex3fv(c); glEnd(); glEnd();}

54 CS 480/680 54Chapter 6 -- Shading The results f flat shading the sphere done this way is shown here: The results f flat shading the sphere done this way is shown here: Notice the silhouette edge of the sphere showing the polygons. Notice the silhouette edge of the sphere showing the polygons. We can easily apply interpolative shading by setting the true normal at the vertex. We can easily apply interpolative shading by setting the true normal at the vertex.

55 CS 480/680 55Chapter 6 -- Shading void triangle(point3 a, point3 b, point3 c) { point3 n; int i; point3 n; int i; glBegin(GL_POLYGON); glBegin(GL_POLYGON); for(i=0;i<3;i++)n[i]=a[i]; for(i=0;i<3;i++)n[i]=a[i]; normal(n); normal(n); glNormal3fv(n); glNormal3fv(n); glVertex3fv(a); glVertex3fv(a); for(i=0;i<3;i++)n[i]=a[i]; for(i=0;i<3;i++)n[i]=a[i]; normal(n); normal(n); glNormal3fv(n); glNormal3fv(n); glVertex3fv(b); glVertex3fv(b); for(i=0;i<3;i++)n[i]=a[i]; for(i=0;i<3;i++)n[i]=a[i]; normal(n); normal(n); glNormal3fv(n); glNormal3fv(n); glVertex3fv(c); glVertex3fv(c); glEnd(); glEnd();}

56 CS 480/680 56Chapter 6 -- Shading The results of this are shown here: The results of this are shown here: Although this produced a more realistic image, this method is not generalizable. Although this produced a more realistic image, this method is not generalizable. See exercises 6.9 and 6.10 to see where it falls apart. See exercises 6.9 and 6.10 to see where it falls apart.

57 CS 480/680 57Chapter 6 -- Shading 10. Global Rendering There are limitations imposed by the local lighting model that we have used. There are limitations imposed by the local lighting model that we have used. Consider, for example, an array of spheres Consider, for example, an array of spheres

58 CS 480/680 58Chapter 6 -- Shading Our lighting model cannot handle these situations. Our lighting model cannot handle these situations. It also cannot produce shadows. It also cannot produce shadows. If these effects are important, than we can use a more sophisticated (and slower) rendering technique, such as ray tracing and radiosity. If these effects are important, than we can use a more sophisticated (and slower) rendering technique, such as ray tracing and radiosity. Ray Tracing works well for highly specular surfaces; for example, in scenes composed of highly reflective and translucent objects, such as glass balls. Ray Tracing works well for highly specular surfaces; for example, in scenes composed of highly reflective and translucent objects, such as glass balls. Radiosity is best suited for scenes with perfectly diffuse surfaces, such as the interior of buildings. Radiosity is best suited for scenes with perfectly diffuse surfaces, such as the interior of buildings.

59 CS 480/680 59Chapter 6 -- Shading 11. Summary We have developed a lighting model that fits well with our pipeline approach to graphics. We have developed a lighting model that fits well with our pipeline approach to graphics. With it we can create a variety of lighting effects, and can employ different types of light sources. With it we can create a variety of lighting effects, and can employ different types of light sources. Although we cannot create the global effects of a ray tracer, a good graphics workstation can render a polygonal scene using the Phong reflection model and interpolative shading in the same amount of time it can render a scene without shading!!! Although we cannot create the global effects of a ray tracer, a good graphics workstation can render a polygonal scene using the Phong reflection model and interpolative shading in the same amount of time it can render a scene without shading!!!

60 CS 480/680 60Chapter 6 -- Shading The recursive subdivision technique is a powerful one that reappears in Chapter 11 when curves are rendered. The recursive subdivision technique is a powerful one that reappears in Chapter 11 when curves are rendered. This chapter concludes our development of polygonal-based graphics. This chapter concludes our development of polygonal-based graphics. You should now be able to generate scenes with lighting and shading. You should now be able to generate scenes with lighting and shading. Techniques for even more sophisticated images, such as texture mapping involve using the pixel-level capabilities of the graphics systems are considered in Chapter 8. Techniques for even more sophisticated images, such as texture mapping involve using the pixel-level capabilities of the graphics systems are considered in Chapter 8. Now is a good time for you to write an application program. Now is a good time for you to write an application program.

61 CS 480/680 61Chapter 6 -- Shading 12. Suggested Readings The use of lighting and reflection in computer graphics has followed two parallel paths: the physical and the computational The use of lighting and reflection in computer graphics has followed two parallel paths: the physical and the computational Foley (90) gives a great discussion on these models. Foley (90) gives a great discussion on these models. Phong put together his model in 1975 Phong put together his model in 1975 Ray tracing was introduced to computer graphics by Appel in 1968. Ray tracing was introduced to computer graphics by Appel in 1968. Radiosity is based upon a method first used in heat transfer (Sie 81) and was applied to computer graphics in 1984. Radiosity is based upon a method first used in heat transfer (Sie 81) and was applied to computer graphics in 1984. The OpenGL Programmer’s Guide (97a) contains many good hints on effective use of OpenGL’s rendering capability. The OpenGL Programmer’s Guide (97a) contains many good hints on effective use of OpenGL’s rendering capability.

62 CS 480/680 62Chapter 6 -- Shading Exercises -- Due next class


Download ppt "Shading Chapter 6. CS 480/680 2Chapter 6 -- Shading Introduction: Introduction: We have learned to build three-dimensional models and to display them."

Similar presentations


Ads by Google