Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,

Similar presentations


Presentation on theme: "1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,"— Presentation transcript:

1 1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

2 2 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Objectives Learn to shade objects so their images appear three-dimensional Introduce the types of light-material interactions Build a simple reflection model---the Phong model--- that can be used with real time graphics hardware

3 3 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Why we need shading Suppose we build a model of a sphere using many polygons and color it with glColor. We get something like But we want

4 4 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading Why does the image of a real sphere look like Light-material interactions cause each point to have a different color or shade Need to consider ­Light sources ­Material properties ­Location of viewer ­Surface orientation

5 5 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Scattering Light strikes A ­Some scattered ­Some absorbed Some of scattered light strikes B ­Some scattered ­Some absorbed Some of this scattered light strikes A and so on

6 6 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Rendering Equation The infinite scattering and absorption of light can be described by the rendering equation ­Cannot be solved in general ­Ray tracing is a special case for perfectly reflecting surfaces (no absorption) Rendering equation is global and includes ­Shadows ­Multiple scattering from object to object

7 Rendering Equation The rendering equation Source International Conference on Computer Graphics and Interactive Techniques Proceedings of the 13th annual conference on Computer graphics and interactive techniques Pages: 143 - 150 Year of Publication: 1986 ISSN:0097-8930 Author James T. Kajiya Sponsor SIGGRAPH: ACM Special Interest Group on Computer Graphics and Interactive Techniques 7 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

8 8 Global Effects translucent surface shadow multiple reflection

9 9 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Local vs Global Rendering Correct shading requires a global calculation involving all objects and light sources ­Incompatible with pipeline model which shades each polygon independently (local rendering) However, in computer graphics, especially real time graphics, we are happy if things “look right” ­Exist many techniques for approximating global effects

10 10 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Light-Material Interaction Light that strikes an object is partially absorbed and partially scattered (reflected) The amount reflected determines the color and brightness of the object ­A surface appears red under white light because the red component of the light is reflected and the rest is absorbed The reflected light is scattered in a manner that depends on the smoothness and orientation of the surface

11 11 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Light Sources General light sources are difficult to work with because we must integrate light coming from all points on the source

12 12 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Simple Light Sources Point source ­Model with position and color ­Distant source = infinite distance away (parallel) Spotlight ­Restrict light from ideal point source Ambient light ­Same amount of light everywhere in scene ­Can model contribution of many sources and reflecting surfaces

13 13 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Surface Types The smoother a surface, the more reflected light is concentrated in the direction a perfect mirror would reflected the light A very rough surface scatters light in all directions smooth surface rough surface

14 14 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Phong Model A simple model that can be computed rapidly Has three components ­Diffuse ­Specular ­Ambient Uses four vectors ­To source ­To viewer ­Normal ­Perfect reflector

15 15 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Ideal Reflector Normal is determined by local orientation Angle of incidence = angle of relection The three vectors must be coplanar r = 2 (l · n ) n - l

16 16 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Lambertian Surface Perfectly diffuse reflector Light scattered equally in all directions Amount of light reflected is proportional to the vertical component of incoming light ­reflected light ~ cos  i ­cos  i = l · n if vectors normalized ­There are also three coefficients, k r, k b, k g that show how much of each color component is reflected

17 17 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Specular Surfaces Most surfaces are neither ideal diffusers nor perfectly specular (ideal reflectors) Smooth surfaces show specular highlights due to incoming light being reflected in directions concentrated close to the direction of a perfect reflection specular highlight

18 18 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Modeling Specular Relections Phong proposed using a term that dropped off as the angle between the viewer and the ideal reflection increased  I r ~ k s I cos   shininess coef absorption coef incoming intensity reflected intensity

19 19 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2006 Ambient Light Ambient light is the result of multiple interactions between (large) light sources and the objects in the environment Amount and color depend on both the color of the light(s) and the material properties of the object Add k a I a to diffuse and specular terms reflection coef intensity of ambient light

20 20 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2006 Distance Terms The light from a point source that reaches a surface is inversely proportional to the square of the distance between them We can add a factor of the form 1/(ad + bd +cd 2 ) to the diffuse and specular terms The constant and linear terms soften the effect of the point source

21 21 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2006 Light Sources In the Phong Model, we add the results from each light source Each light source has separate diffuse, specular, and ambient terms to allow for maximum flexibility even though this form does not have a physical justification Separate red, green and blue components Hence, 9 coefficients for each point source ­I dr, I dg, I db, I sr, I sg, I sb, I ar, I ag, I ab

22 22 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2006 Material Properties Material properties match light source properties ­Nine absorbtion coefficients k dr, k dg, k db, k sr, k sg, k sb, k ar, k ag, k ab ­Shininess coefficient 

23 23 Ambient Light Sources A scene lit only with an ambient light source:

24 24 Ambient/Diffuse/Specular Just ambient light: Diffuse + Specular and change Ambient Left: Sphere with just diffuse reflection Right: Sphere with just specular reflection

25 25 Ambient Only Cheap global illumination

26 26 Ambient + Diffuse

27 27 Ambient + Diffuse + Specular

28 28 Phong Illumination

29 29 The Image - Ambient

30 30 The Image - Diffuse

31 31 The Image - Specular

32 32 Diffuse Specular

33 33 OpenGL Coding Color Specifying shading model – void glShadeModel(GLenum mode ); mode: GL_FLAT, GL_SMOOTH GL_FLAT GL_SMOOTH

34 34 OpenGL shading How pixels are shaded? – Flat shading Apply Phong model to get a color per face – Gouraud shading Apply Phong model at vertices to get color Interpolate color across pixels – Phong shading Interpolate model parameters – normal – light vector Apply Phong model at each pixel

35 35 Gouraud vs. Phong shading Means per-pixel vs. per vertex shading Per pixel is much nicer – And more “correct”

36 36 Polygon Shading Algorithms

37 37 Polygon Shading Algorithms – Flat – Gouraud – Phong – Ray casting Less expensive More accurate

38 38 Light Sources in OpenGL Enabling Lighting and Lightts Lighting in general must be enabled glEnable(GL_LIGHTING); Each individual light must be enabled glEnable(GL_LIGHT0); OpenGL supports at least 8 light sources

39 39 Global Ambient Light Set ambient intensity for entire scene GLfloat al[] = {0.2, 0.2, 0.2, 1.0}; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, al); – The above is default Also: properly light backs of polygons glLightModeli(GL_LIGHT_MODEL_TWO_SIDED, GL_TRUE)

40 40 Use vectors {r, g, b, a} for light properties Beware: light source will be transformed! GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_position[] = {-1.0, 1.0, -1.0, 0.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); Defining a Light Source

41 41 Point Source vs Directtional Source Directional light given by “position” vector GLfloat light_position[] = {-1.0, 1.0, -1.0, 0.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position); Point source given by “position” point GLfloat light_position[] = {-1.0, 1.0, -1.0, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position);

42 42 Create point source as before Specify additional properties to create spotlight GLfloat sd[] = {-1.0, -1.0, 0.0}; glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, sd); glLightf (GL_LIGHT0, GL_SPOT_CUTOFF, 45.0); glLightf (GL_LIGHT0, GL_SPOT_EXPONENT, 2.0); Spotlights

43 43 Material properties stay in effect Set both specular coefficients and shininess Diffuse component is analogous GLfloat mat_a[] = {0.1, 0.5, 0.8, 1.0}; GLfloat mat_d[] = {0.1, 0.5, 0.8, 1.0}; GLfloat mat_s[] = {1.0, 1.0, 1.0, 1.0}; GLfloat low_sh[] = {5.0}; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_a); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_d); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_s); glMaterialfv(GL_FRONT, GL_SHININESS, low_sh); Defining Material Properties

44 44 Distance and Direction The coefficients in the distance terms are by default a=1.0 (constant terms), b=c=0.0 (linear and quadratic terms). Change by glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0); glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0); glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0); Distance term: add a factor of the form f att = 1/(a + bd light + cd light 2 ) to the diffuse and specular terms

45 45 Ambient light depends on color of light sources A red light in a white room will cause a red ambient term that disappears when the light is turned off OpenGL also allows a global ambient term that is often helpful for testing glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient) Global Ambient Light

46 46 The default is shade only front faces which works correctly for convex objects If we set two sided lighting, OpenGL will shade both sides of a surface Each side can have its own properties which are set by using GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK in glMaterialf Front and Back Faces back faces not visibleback faces visible

47 47 Emissive Term We can simulate a light source in OpenGL by giving a material an emissive component This component is unaffected by any sources or transformations GLfloat emission[] = (0.0, 0.3, 0.3, 1.0); glMaterialf(GL_FRONT, GL_EMISSION, emission);

48 48 OpenGL Coding Example 1/5 #include void display(); void reshape(int, int); void init(); int main(int argc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(400, 400); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); glutCreateWindow("Planet"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMainLoop(); }

49 49 OpenGL Coding Example 2/5 void init() { GLfloat mat_diffuse[] = {1.0, 0.0, 0.0, 1.0}; GLfloat mat_specular[] = {0.0, 0.0, 1.0, 1.0}; GLfloat mat_ambient[] = {0.0, 1.0, 0.0, 1.0}; GLfloat mat_shininess[] = {50.0}; GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0}; GLfloat light_position[] = {5.0, 5.0, 10.0, 0.0}; glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH);

50 50 OpenGL Coding Example 3/5 // z buffer enable glEnable(GL_DEPTH_TEST); // enable lighting glEnable(GL_LIGHTING); // set light property glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); // set material property glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); }

51 51 OpenGL Coding Example 4/5 void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSolidSphere(0.8, 128, 128); glFlush(); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }

52 52 Specular : blue Diffuse : red Ambient : green OpenGL Coding Example 5/5


Download ppt "1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,"

Similar presentations


Ads by Google