Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER GRAPHICS Hochiminh city University of Technology Faculty of Computer Science and Engineering CHAPTER 6: Lighting and Shading.

Similar presentations


Presentation on theme: "COMPUTER GRAPHICS Hochiminh city University of Technology Faculty of Computer Science and Engineering CHAPTER 6: Lighting and Shading."— Presentation transcript:

1 COMPUTER GRAPHICS Hochiminh city University of Technology Faculty of Computer Science and Engineering CHAPTER 6: Lighting and Shading

2 Slide 2Faculty of Computer Science and Engineering - HCMUT OUTLINE  Introduction  Shading model  Flat shading & smooth shading  Using Light Sources in OpenGL  Working with material in OpenGL  Computation of Vectors

3 Slide 3Faculty of Computer Science and Engineering - HCMUT Introduction  Calculating the pixel’s color  Introduce the types of light-material Interactions  Build a simple reflection model---the Phong model--- that can be used with real time graphics hardware  Don’t try to simulate all of the physical principles having to do with the scattering and reflection of light  Models have been invented that use approximations and still do a good job producing various levels of realism

4 Slide 4Faculty of Computer Science and Engineering - HCMUT Introduction  Wireframe –Simple, only edges of each object are drawn –Can see through object –It can be difficult to see what’s what

5 Slide 5Faculty of Computer Science and Engineering - HCMUT Introduction  Line Drawing: wire-frame with hidden surface removal –Only edges are drawn –The objects now look solid, and it is easy to tell where one stops and the next begins

6 Slide 6Faculty of Computer Science and Engineering - HCMUT Introduction  Flat shading: –A calculation of how much light is scattered from each face is computed at a single point. –All points in a face are rendered with the same gray level –Can see the Boundary between polygons

7 Slide 7Faculty of Computer Science and Engineering - HCMUT Introduction  Smooth shading (Gouraud shading): –Different points of a face are drawn with different gray levels found through an interpolation scheme –The edges of polygons disappear specular

8 Slide 8Faculty of Computer Science and Engineering - HCMUT Introduction  Adding texture, shadow

9 Slide 9Faculty of Computer Science and Engineering - HCMUT Shading model  Light sources “shine” on the various surfaces of the objects, and the incident light interacts with the surface in three different ways: –Some is absorbed by the surface and is convert to heat –Some is reflected from the surface –Some is transmitted into the interior of the objects, as in the case of a piece of glass.

10 Slide 10Faculty of Computer Science and Engineering - HCMUT Shading model  Two types of reflection of incident light: –Diffuse: re-radiated uniformly in all directions. Interacts strongly with the surface, so it color is usually affected by the nature of material. –Specular: highly directional, incident light doesnot penetrate the object, but instead is reflected directly from its outer surface. The reflected light has the same color as incident light.

11 Slide 11Faculty of Computer Science and Engineering - HCMUT Shading model  Why does the image of a real teapot look like  Light-material interactions cause each point to have a different color or shade  To calculate color of the object, need to consider: –Light sources –Material properties –Location of viewer –Surface orientation

12 Slide 12Faculty of Computer Science and Engineering - HCMUT Shading model  Geometric ingredients for finding reflected light –The normal vector m to the surface at P –The vector v from P to the viewer’s eye –The vector s from P to the light source

13 Slide 13Faculty of Computer Science and Engineering - HCMUT Shading model  Computing the Diffuse Component –Diffuse component with an intensity denoted by I d –Scattering uniform in all directions  depend only on m, s –Lambert’s law: – I s : intensity of light source, ρ d : diffuse reflection coefficient

14 Slide 14Faculty of Computer Science and Engineering - HCMUT Shading model  Computing the Diffuse Component –diffuse reflection coefficient : 0, 0.2, 0.4, 0.6, 0.8, 1.0

15 Slide 15Faculty of Computer Science and Engineering - HCMUT Shading model  Specular Reflection –Specular reflection causes highlights, which can add significantly to the realism of a picture when objects are shiny –The amount of light reflected is greated in the direction r. f : [1, 200]

16 Slide 16Faculty of Computer Science and Engineering - HCMUT Shading model  Specular Reflection –As f increase, the reflection becomes more mirror like and is more highly concentrated along the direction r.

17 Slide 17Faculty of Computer Science and Engineering - HCMUT Shading model  Specular Reflection –  s from top to bottom: 0.25, 0.5, 0.75. f from left to right: 3, 6, 9, 25, 200

18 Slide 18Faculty of Computer Science and Engineering - HCMUT Shading model  Specular Reflection –Reduce computation time, use halfway vector h = s + v. –Use  as the approximation of angle between r and v

19 Slide 19Faculty of Computer Science and Engineering - HCMUT Shading model  Ambient Light –To overcome the problem of totally dark shadows, we imagine that a uniform “background glow” called ambient light exist in the environment –Not situated at any particular place, and spreads in all direction uniformly –The source is assigned an intensity I a.  a : ambient reflection coefficient.

20 Slide 20Faculty of Computer Science and Engineering - HCMUT Shading model  Combining Light Contribution  Adding color

21 Slide 21Faculty of Computer Science and Engineering - HCMUT Shading model  Shading and the graphics pipeline –Specify light source, camera, normal vector glBegin(GL_POLYGON); for(int i = 0; i< 3; i++){ glNormal3fv(norm[i]); glVertex3fv(pt[i]);} glEnd();

22 Slide 22Faculty of Computer Science and Engineering - HCMUT Flat shading & smooth shading  Painting a face –The pixels in a polygon are visited in a regular order, usually scan line by scan line from bottom to top, and across each scan line from left to right –Convex polygons can be made highly efficient, since, at each scan line, there is a single unbroken “run” of pixels. for (int y = ybott ; y <= ytop ; y++) { find xleft and xright; for (int x = xleft; x <= xright; x++) { find the color c for this pixel; put c into the pixel at (x, y); } }

23 Slide 23Faculty of Computer Science and Engineering - HCMUT Flat shading & smooth shading  Flat shading –Face is flat, light source are quite distant  diffuse light component varies little over different points –glShadeModel(GL_FLAT); for (int y = ybott ; y <= ytop ; y++) { find xleft and xright; find the color c for this scan line; for (int x = xleft; x <= xright; x++) { put c into the pixel at (x, y); } }

24 Slide 24Faculty of Computer Science and Engineering - HCMUT Flat shading & smooth shading  Smooth shading – Gouraud shading color left = lerp(color 1, color 4, f), for (int y = ybott ; y <= ytop ; y++){ find xleft and xright; find colorleft and colorright; colorinc = (colorright – colorleft)/ (xright – xleft) for (int x = xleft, c = colorleft; x <= xright; x++, c += colorinc ) { put c into the pixel at (x, y);}}

25 Slide 25Faculty of Computer Science and Engineering - HCMUT Flat shading & smooth shading  Smooth shading – Gouraud shading –glShadeModel(GL_SMOOTH);

26 Slide 26Faculty of Computer Science and Engineering - HCMUT Flat shading & smooth shading  Smooth shading – Gouraud shading –For polygonal models, Gouraud proposed we use the average of the normals around a mesh vertex n = (n 1 +n 2 +n 3 +n 4 )/ |n 1 +n 2 +n 3 +n 4 |

27 Slide 27Faculty of Computer Science and Engineering - HCMUT Flat shading & smooth shading  Smooth shading – Phong shading –Slow calculation, better realism –OpenGL doesn’t support

28 Slide 28Faculty of Computer Science and Engineering - HCMUT Steps in OpenGL shading  Specify normals  Enable shading and select model  Specify lights  Specify material properties

29 Slide 29Faculty of Computer Science and Engineering - HCMUT Specify normals  In OpenGL the normal vector is part of the state  Set by glNormal*() –glNormal3f(x, y, z); –glNormal3fv(p);  Usually we want to set the normal to have unit length so cosine calculations are correct –glEnable(GL_NORMALIZE) allows for autonormalization at a performance penalty

30 Slide 30Faculty of Computer Science and Engineering - HCMUT Enabling Shading  Shading calculations are enabled by –glEnable(GL_LIGHTING) –Once lighting is enabled, glColor() ignored  Must enable each light source individually –glEnable(GL_LIGHTi) i=0,1…..

31 Slide 31Faculty of Computer Science and Engineering - HCMUT Using Light Sources in OpenGL  Creating a light Source - Position GLfloatmyLightPosition[] = {3.0, 6.0, 5.0, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION, myLightPosition); (x, y, z, 1)  point light source, (x, y, z, 0)  directional light source - Color GLfloatamb0[] = {0.2, 0.4, 0.6, 1.0}; GLfloatdiff0[] = {0.8, 0.9, 0.5, 1.0}; GLfloatspec0[] = {1.0, 0.8, 1.0, 1.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, amb0); glLightfv(GL_LIGHT0, GL_DIFFUSE, diff0); glLightfv(GL_LIGHT0, GL_SPECULAR, spec0);

32 Slide 32Faculty of Computer Science and Engineering - HCMUT Using Light Sources in OpenGL  Splotlights glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0); // angle glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 4.0); //  = 4.0 GLfloat dir[] = {2.0, 1.0, -4.0}; // direction glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);

33 Slide 33Faculty of Computer Science and Engineering - HCMUT Using Light Sources in OpenGL  Attenuation of Light with distance glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0); GL_LINEAR_ATTENUATION and GL_QUADRATIC_ATTENUATION

34 Slide 34Faculty of Computer Science and Engineering - HCMUT Working with material in OpenGL GLfloatmyColor[] = {0.8, 0.2, 0.0, 1.0}; glMaterialfv(GL_FRONT, GL_DIFFUSE, myColor); GL_BACK, GL_FRONT_AND_BACK GL_AMBIENT, GL_SPECULAR,GL_EMISSION

35 Slide 35Faculty of Computer Science and Engineering - HCMUT Working with material 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);

36 Slide 36Faculty of Computer Science and Engineering - HCMUT Front and Back Faces  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 back faces not visibleback faces visible

37 Slide 37Faculty of Computer Science and Engineering - HCMUT Front and Back Faces  Specify Front Faces –glFrontFace(GL_CCW), glFrontFace(GL_CW)  Cull Face –glEnable(GL_CULL_FACE) –glCullFace(GLenum mode); GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

38 Slide 38Faculty of Computer Science and Engineering - HCMUT Front and Back Faces

39 Slide 39Faculty of Computer Science and Engineering - HCMUT Front and Back Faces glBegin(GL_QUADS); glColor3f(1, 0, 0); // x = 1 glVertex3f(1, 1, 1);glVertex3f(1, -1, 1); glVertex3f(1, -1, -1);glVertex3f(1, 1, -1); glColor3f(0, 1, 0); //x = -1 glVertex3f(-1, 1, 1);glVertex3f(-1, 1, -1); glVertex3f(-1, -1, -1);glVertex3f(-1, -1, 1); glColor3f(0, 0, 1); // y = 1 glVertex3f( 1, 1, 1);glVertex3f( 1, 1, -1); glVertex3f(-1, 1, -1);glVertex3f(-1, 1, 1); glColor3f(1, 0, 1); // y = -1 glVertex3f( 1, -1, 1);glVertex3f(-1, -1, 1); glVertex3f(-1, -1, -1);glVertex3f( 1, -1, -1); ……………… // z = -1 glEnd();

40 Slide 40Faculty of Computer Science and Engineering - HCMUT Front and Back Faces glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_LINE);

41 Slide 41Faculty of Computer Science and Engineering - HCMUT Front and Back Faces glFrontFace(GL_CW); glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_LINE);

42 Slide 42Faculty of Computer Science and Engineering - HCMUT Front and Back Faces glFrontFace(GL_CCW); //default glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_FILL);

43 Slide 43Faculty of Computer Science and Engineering - HCMUT Front and Back Faces glFrontFace(GL_CCW); glEnable(GL_CULL_FACE); glCullFace(GL_BACK);

44 Slide 44Faculty of Computer Science and Engineering - HCMUT Computation of Vectors  s and v are specified by the application  Can computer r from s and m  Problem is determining m  For simple surfaces is can be determined but how we determine m differs depending on underlying representation of surface  OpenGL leaves determination of normal to application

45 Slide 45Faculty of Computer Science and Engineering - HCMUT Computation of Vectors  If the face is flat  face’s normal vector is vertices normal vector  m = (V1 – V2) x (V3 – V4)  Two problem: 1) two vector nearly parallel, 2) not all the vertices lie in the same plane - next(j) = (j + 1) mod N - Traversed in CCW - m outward

46 Slide 46Faculty of Computer Science and Engineering - HCMUT Computation of Vectors  Normal vector for a surface given parametrically

47 Slide 47Faculty of Computer Science and Engineering - HCMUT Computation of Vectors  Normal vector for a surface given parametrically –EX: plane P(u, v) = C + au + bv n(u, v) = a  b

48 Slide 48Faculty of Computer Science and Engineering - HCMUT Computation of Vectors  Normal vector for a surface given implicitly EX: the implicit form of plane: F(x, y, z) = n  ((x, y, z) – A) = 0 or: n x x + n y y + n z z - n  A =0  normal is (n x, n y, n z )

49 Slide 49Faculty of Computer Science and Engineering - HCMUT Generic Shapes  The sphere: Implicit form F(x, y, z) = x 2 + y 2 + z 2 – 1  normal (2x, 2y, 2z) Parametric form P(u, v) = (cos(v)cos(u), cos(v)sin(u), sin(v))  normal n(u, v) = -cos(u, v)p(u, v)

50 Slide 50Faculty of Computer Science and Engineering - HCMUT Generic Shapes  Cylinder: –Implicit form F(x, y, z) = x 2 + y 2 – (1 + (s – 1)z) 2 0< z<1  n(x, y, z) = (x, y, -(s – 1)(1 + (s -1)z)) –Parametric form P(u, v) = ((1 + (s – 1)v)cos(u), (1 + (s – 1)v)sin(u), v)  n(u, v) = (cos(u), sin(u), 1-s)

51 Slide 51Faculty of Computer Science and Engineering - HCMUT Generic Shape  Cone - Implicit: F(x, y, z) = x 2 + y 2 – (1 – z) 2 = 0 0 < z < 1  n = (x, y, 1 – z) -parametric P(u, v) = ((1 – v) cos(u), (1 – v)sin(u), v)  n(u, v) = (cos(u), sin(u), 1)

52 Slide 52Faculty of Computer Science and Engineering - HCMUT Further Reading  “Interactive Computer Graphics: A Topdown Approach Using OpenGL”, Edward Angel –Chapter 6: Lighting and Shading  “Đồ họa máy tính trong không gian ba chiều”, Trần Giang Sơn –Chương 3: Tô màu vật thể ba chiều (3.1  3.4)


Download ppt "COMPUTER GRAPHICS Hochiminh city University of Technology Faculty of Computer Science and Engineering CHAPTER 6: Lighting and Shading."

Similar presentations


Ads by Google