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

Slides:



Advertisements
Similar presentations
Computer Graphics - Shading -
Advertisements

CSPC 352: Computer Graphics
Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive.
1 Computer Graphics Chapter 9 Rendering. [9]-2RM Rendering Three dimensional object rendering is the set of collective processes which make the object.
Illumination and Shading
Based on slides created by Edward Angel
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Shading I.
University of New Mexico
Computer Graphics - Class 10
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner Lighting/Shading III Week.
Shading in OpenGL CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
IMGD 1001: Illumination by Mark Claypool
Lighting and Shading Wen-Chieh (Steve) Lin
Shading in OpenGL Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
1 CSCE 641: Computer Graphics Lighting Jinxiang Chai.
Objectives Learn to shade objects so their images appear three- dimensional Learn to shade objects so their images appear three- dimensional Introduce.
1 Lecture 10 Lighting in OpenGL. 2 Sources of light GLfloat myLightPosition[] = {3.0, 6.0, 5.0, 1.0}; GLLightfv(GL_LIGHT0, GL_POSITION, myLightPosition);
CS5500 Computer Graphics March 26, Shading Reference: Ed Angel’s book.
Computer Graphics (Spring 2008) COMS 4160, Lecture 14: OpenGL 3
Illumination and Shading
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
CS 480/680 Computer Graphics Shading I Dr. Frederick C Harris, Jr.
Computer Graphics Lighting.
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Shading I Shandong University Software College Instructor: Zhou Yuanfeng
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Chapter 6: Shading Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Shading (introduction to rendering). Rendering  We know how to specify the geometry but how is the color calculated.
Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL.
Shading in OpenGL.
1 Chapter 6 Shading. 2 Objectives Learn to shade objects so their images appear three-dimensional Introduce the types of light-material interactions Build.
19/17/ :25 UML Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera Real Light Synthetic.
Shading 03/19/2003. Lighting Principles Lighting based on how objects reflect light –Surface characteristics –Light color and direction –Global lighting.
Shading and Illumination. OpenGL Shading Without ShadingWith Shading.
Shading & Texture. Shading Flat Shading The process of assigning colors to pixels. Smooth Shading Gouraud ShadingPhong Shading Shading.
Computer Graphics I, Fall 2010 Shading in OpenGL.
Lecture 9: Lighting and Shading 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271
Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1.
Steve Sterley. Real World Lighting Physical objects tend to interact with light in three ways: Absorption (black body) Reflection (mirror) Transmission.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Illumination and Shading How to shade surfaces based on the position,orientation,characteristics of the surfaces and the light sources illuminating them.
CG Summary: OpenGL Shading andTextures Angel, Chapters 5, 7; “Red Book” slides from AW, red book, etc. CSCI 6360/4360.
Chi-Cheng Lin, Winona State University CS430 Computer Graphics Lighting and Shading Part II.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
RENDERING Introduction to Shading models – Flat and Smooth shading – Adding texture to faces – Adding shadows of objects – Building a camera in a program.
RENDERING Introduction to Shading models – Flat and Smooth shading – Adding texture to faces – Adding shadows of objects – Building a camera in a program.
Lecture Fall 2001 Illumination and Shading in OpenGL Light Sources Empirical Illumination Shading Transforming Normals Tong-Yee Lee.
Where We Stand So far we know how to: –Transform between spaces –Rasterize –Decide what’s in front Next –Deciding its intensity and color.
In the name of God Computer Graphics. Where We Stand So far we know how to: –Transform between spaces –Draw polygons Next –Deciding a pixel’s intensity.
OpenGL Shading. 2 Objectives Learn to shade objects so their images appear three-dimensional Introduce the types of light-material interactions Build.
Lighting and Reflection Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Shading NOTE: Some of these slides are from Ed Angel’s presentation at SIGGRAPH February 27, 2008.
Illumination Models and Surface-Rendering Methods CEng 477 Introduction to Computer Graphics.
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 55 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 55 Computer Graphics Three-Dimensional Graphics V.
Graphics Graphics Korea University kucg.korea.ac.kr 1 Lights & Material 고려대학교 컴퓨터 그래픽스 연구실.
CSC Graphics Programming
Shading To determine the correct shades of color on the surface of graphical objects.
Computer Graphics Chapter 9 Rendering.
CSC461: Lecture 23 Shading Computation
Vectors, Normals, & Shading
Shading in OpenGL Ed Angel
CSC461: Lecture 24 Lighting and Shading in OpenGL
Illumination and Shading
Lighting and Materials
CS5500 Computer Graphics April 10, 2006.
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Presentation transcript:

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

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

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

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

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

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

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

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

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.

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.

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

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

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

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

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]

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.

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

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

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.

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

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();

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); } }

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); } }

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);}}

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

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 |

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

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

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

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…..

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);

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);

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

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

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 = glMaterialf(GL_FRONT, GL_AMBIENT, ambient); glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse); glMaterialf(GL_FRONT, GL_SPECULAR, specular); glMaterialf(GL_FRONT, GL_SHININESS, shine);

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

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

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

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();

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

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);

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);

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

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

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

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

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

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 )

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)

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)

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)

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)