1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.

Slides:



Advertisements
Similar presentations
1 Graphics CSCI 343, Fall 2013 Lecture 18 Lighting and 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 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.
Illumination and Shading
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2007 Tamara Munzner Lighting/Shading III Week.
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.
CS 4731: Computer Graphics Lecture 16: Illumination Models Part 2 Emmanuel Agu.
Shading in OpenGL Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
Hidden Surfaces and Shading CS BSP Tree T1T1 T2T2 E if (f 1 (E) < 0) then draw T 1 draw T 2 else draw T 2 draw T 1 f 1 (p) = 0 is the.
Normals Specifying the normal –void glNormal3d( GLdouble nx, GLdouble ny, GLdouble nz ); When using lighting effects, normals must be defined for each.
6.1 Vis_04 Data Visualization Lecture 6 - A Rough Guide to Rendering.
CS5500 Computer Graphics March 26, Shading Reference: Ed Angel’s book.
Computer Graphics (Spring 2008) COMS 4160, Lecture 14: OpenGL 3
Illumination and Shading
Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner Lighting and Shading Week.
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
Computer Graphics Lighting.
COMPUTER GRAPHICS CS 482 – FALL 2014 AUGUST 27, 2014 FIXED-FUNCTION 3D GRAPHICS MESH SPECIFICATION LIGHTING SPECIFICATION REFLECTION SHADING HIERARCHICAL.
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 and Illumination. OpenGL Shading Without ShadingWith 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.
08 |Lighting and Shading Eriq Muhammad Adams J |
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,
OpenGL Lighting Jian-Liang Lin 2002 Hidden-Surface Removal -1 Original Code: while (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT);
Project 5 Lamp Shader Fri, Nov 7, 2003 Due Mon, Nov 17, 2003.
Illumination and Shading
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
CS 325 Introduction to Computer Graphics 03 / 29 / 2010 Instructor: Michael Eckmann.
Chap 5 Global Illumination
RENDERING Introduction to Shading models – Flat and Smooth shading – Adding texture to faces – Adding shadows of objects – Building a camera in a program.
1 Graphics CSCI 343, Fall 2015 Lecture 21 Lighting and Shading III.
Specular Reflection Lecture 27 Mon, Nov 10, 2003.
Lighting and Shading Part 2. Global Ambient Light There are at least 8 OpenGL lights and 1 Global Ambient Setting the Global Ambient globalAmbient[] =
Illumination and Shading Sang Il Park Sejong University.
Shading NOTE: Some of these slides are from Ed Angel’s presentation at SIGGRAPH February 27, 2008.
Illumination CSE 410. Basic steps of lighting Enable smooth shading Set global ambient light glShadeModel(GL_SMOOTH); glEnable(GL_NORMALIZE); GLfloat.
CS559: Computer Graphics Lecture 16: Shading and OpenGL Li Zhang Spring 2008.
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
Illumination Models and Surface-Rendering Methods CEng 477 Introduction to Computer Graphics.
1 Dr. Scott Schaefer Lighting. 2/49 Lighting/Illumination Color is a function of how light reflects from surfaces to the eye Global illumination accounts.
CSC Graphics Programming
Shading To determine the correct shades of color on the surface of graphical objects.
Lecture 16: Shading and OpenGL Li Zhang Spring 2008
Illumination and Shading
CSC461: Lecture 23 Shading Computation
Vectors, Normals, & Shading
Shading in OpenGL Ed Angel
Lighting and Shading Lab 8:.
CSC461: Lecture 24 Lighting and Shading in OpenGL
Lighting Phong's Lighting Model normals
Illumination and Shading
Lighting and Materials
Lighting – Light Sources
CS5500 Computer Graphics April 10, 2006.
Lighting – Material Properties
Lighting and Shading Lab 8:.
ייצוג בעולם 3D ייצוג מצולעים (פוליגונים) צלע קודקוד צלעe0 : {v1,v2}
Last Time Liang-Barsky Details Weiler-Atherton clipping algorithm
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Presentation transcript:

1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III

2 The Phong Reflection Model The phong reflection model leads to efficient computation of surface lighting and gives reasonably realistic renderings. It uses 4 vectors to calculate the color and intensity of a point on a surface. n = normal to the surface at p l = direction to light source v = direction to viewer (COP) r = direction of perfectly reflected ray (determined by n and l) p l n v r Last time we saw how to calculate n and r.

3 Computation of r l r ii rr r must lie in the same plane as l and n. Therefore, n From the above three equations we can derive r in terms of l and n (we will do this in class):

4 The halfway vector Computing r takes a lot of computation. We can approximate the angle between r and v by computing the halfway vector. l n r v ii  h  The angle between n and h, , is 1/2 the angle between r and v, . What is  in terms of  ? We can use  instead of  in our calculation of specular reflection: We adjust  ' to account for the fact that  <  2  = 

5 Example of computing h Compute h from the following l and r:

6 The Phong reflection model and OpenGL The Phong reflection model: In openGL we must specify: 1) Lighting properties: Ld, Ls and La 2) Light position (or direction): l 3) Material reflectance properties: kd, ks, ka,  4) Surface normals

7 Light Sources in OpenGL OpenGL allows up to 8 light sources. Enable lighting with: glEnable(GL_FLAT);//Or GL_SMOOTH glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);//first light source glEnable(GL_LIGHT1);//etc. up to GL_LIGHT7 glShadeModel(GL_FLAT);//Or GL_SMOOTH These are usually called once before the glutMainLoop( ). myInit( ) is a good place to call them.

8 Specify Light properties To specify values of the ambient, diffuse and specular light parameters, use the following: glLightf(SourceNumber, Parameter, Value) ORglLightfv(SourceNumber, Parameter, PointerToArray) Example: GLfloatlight0_pos[ ] = {1.0, 2.0, 3.0, 1.0};//x, y, z, w GLfloatdiffuse0[ ] = {1.0, 0.0, 0.0, 1.0};//r, g, b, a glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); Can also use GL_AMBIENT and GL_SPECULAR for Parameter.

9 Lighting Code GLfloat light_ambient[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light0_pos[] = {100.0, 200.0, 0.0, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

10 Distant vs. Nearby light sources For distant light sources, we only need to specify the direction, because the light rays hitting the surface are all approximately parallel: postion0[ ] = {1.0, 1.0, 1.0, 0.0};//Note w is zero. For nearby light sources, we must specify position explicitly. postion0[ ] = {100.0, 100.0, 100.0, 1.0};//Note w is one. Distant sourceNearby source

11 Specifying Material Properties To specify material properties, use: glMaterialfv(face, type, pointer_to_array); Values for face: GL_FRONT or GL_FRONT_AND_BACK Values for type: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_SHININESS Example: GLfloat ambient[ ] = {0.2, 0.2, 0.2, 1.0};//r, g, b, a GLMaterialfv(GL_FRONT, GL_AMBIENT, ambient);

12 Code for Material Properties GLfloat mat_specular[] = {1.0, 0.0, 1.0, 1.0}; GLfloat mat_diffuse[] = {0.3, 0.0, 0.3, 1.0}; GLfloat mat_ambient[] = {0.1, 0.0, 0.1, 1.0}; GLfloat mat_shininess[] = {200.0}; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

13 Specifying the Normal vectors (-100, -100, 100) (100, 100, -100) The normals for a box with faces parallel to the x-y, y-z and z-x planes are the unit vectors along the x, y or z axes, in the positive or negative directions. n 1 = (1.0, 0.0, 0.0) n 4 = (0.0, 1.0, 0.0) n 0 = (0.0, 0.0, 1.0)

14 Set up vertices and normals point3 vertices[8] = { {-100, -100, 100}, {100, -100, 100}, {100, 100, 100}, {-100, 100, 100}, {-100, -100, -100}, {100, -100, -100}, {100, 100, -100}, {-100, 100, -100}}; point3 normals[6] = { {0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}, {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, -1.0, 0.0}};

15 Flat Shading In flat shading, the light source is modeled as a distant source. Each point on each plane has the same light hitting it and so each plane is a uniform color. In this case, we specify each surface normal once, immediately before listing the vertices of the corresponding polygon: glNormal3fv(normals[0]);// (0, 0, 1) for (i=0; i<4; i++){/*First face--front of cube*/ glVertex3fv(vertices[i]); } glNormal3fv(normals[1]);// (1, 0, 0) glVertex3fv(vertices[1]); /*Second face--right side of cube*/ glVertex3fv(vertices[5]); glVertex3fv(vertices[6]); glVertex3fv(vertices[2]); etc.

16 Calculating Surface Normals Not all surface normals are as easy to compute as for the cube. We can calculate surface normal for any flat surface from 3 of its vertices. p0p0 p1p1 p3p3 Calculating the cross product: (p 1 - p 0 ) x (p 3 - p 0 ) = ? vertices from cube

17 Code for computing the cross product /* Compute (b - a) x (c - a). Store the result in d. After computing the cross product, normalize the length of d to 1. */ void cross(point3 a, point3 b, point3 c, point3 d){ //we will complete this code in class. normalize(d);//Set length to one. Not technically // part of cross product computation } a b c

18 Code for computing the cross product /* Compute (b - a) x (c - a). Store the result in d. After computing the cross product, normalize the length of d to 1. */ void 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[1] = (b[2] - a[2])*(c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]); d[2] = (b[0] - a[0])*(c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); normalize(d);//Set length to one. Not technically // part of cross product computation } a b c

19 Problem with Flat Shading Flat shading models a distant viewer and a distant light source. If the polygon is flat, then n, l, and v are constant over the entire polygon. OpenGL uses the surface normal associated with the first vertex to specify the normals for the entire polygon. Problem: The changes in lighting look too abrupt. Sometimes see light and dark lines at edges (Mach bands), because of the way the visual system processes the intensity changes. l l l v v v Sharp Edge I x I x IntensityAppearance

20 Smooth Shading Smooth shading varies the color across the polygon, so that the intensity changes are not so abrupt. Models for Smooth shading: Interpolative Shading Gouraud Shading Phong Shading We will examine each of these in the next lecture!