1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

Slides:



Advertisements
Similar presentations
Polygon Rendering Flat Rendering Goraud Rendering Uses Phong Reflectance Phong Rendering.
Advertisements

1 Graphics CSCI 343, Fall 2013 Lecture 18 Lighting and Shading.
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.
Illumination and Shading
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.
Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 11: OpenGL 3
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.
Now Playing: Multiply Jamie Lidell from Multiply Released June 13, 2005.
CS5500 Computer Graphics March 26, Shading Reference: Ed Angel’s book.
Computer Graphics (Spring 2008) COMS 4160, Lecture 14: OpenGL 3
CS425 OpenGL Materials. What Color Is It? green & blue absorbed white light Looks red.
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,
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
Color in OpenGL (Chapter 4) Presented by: Stacy C. Lovell.
Computer Graphics Lighting.
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.
Shading in OpenGL.
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.
Computer Graphics I, Fall 2010 Shading in OpenGL.
Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.
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,
Chi-Cheng Lin, Winona State University CS430 Computer Graphics Lighting and Shading Part II.
OpenGL Color and Lighting 2003 Spring Keng Shih-Ling.
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);
11/04/04© University of Wisconsin, CS559 Fall 2004 Last Time Visibility –Z-Buffer and transparency –A-buffer –Area subdivision –BSP Trees –Exact Cell-Portal.
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.
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.
Lecture Fall 2001 Illumination and Shading in OpenGL Light Sources Empirical Illumination Shading Transforming Normals Tong-Yee Lee.
1 CSCE 441: Computer Graphics Lighting Jinxiang Chai.
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.
Lighting Dave Shreiner. 2 Lighting Principles Lighting simulates how objects reflect light Lighting simulates how objects reflect light material composition.
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 CSE 410. Basic steps of lighting Enable smooth shading Set global ambient light glShadeModel(GL_SMOOTH); glEnable(GL_NORMALIZE); GLfloat.
1 CSCE 441: Computer Graphics Lighting Jinxiang Chai.
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.
Chapter 5. Lighting Computer Graphics (spring, 2009) School of Computer Science University of Seoul.
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.
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.
Lecture 16: Shading and OpenGL Li Zhang Spring 2008
Illumination and Shading
@ 2017 by Jim X. Chen George Mason University
Open GL: Colors and Lighting
Shading in OpenGL Ed Angel
Lighting Phong's Lighting Model normals
Illumination and Shading
Lighting and Materials
Lighting – Light Sources
CS5500 Computer Graphics April 10, 2006.
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 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL

2 Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera Real Light Synthetic Light Source

3 Lights, Surfaces, and Imaging Proj. Plane Viewer Objects

4 Lights, Surfaces, and Imaging Proj. Plane Viewer Light Source Objects

5 Modes of Interaction of Light with Materials Specular Surface Diffuse Surface Translucent Surface Note: Any given surface can have some of all three properties.

6 Illuminating Surfaces z x y We can define illumination function: To obtain total light, must integrate over total surface.

7 Simplified Model Exact model can easily get complicated! Three simplifications help. 1. We can consider four classes of light sources ambient point spotlight distant 2. Human color perception allows us to consider illumination function in terms of the three primary colors. 3. We can neglect (OpenGL caveats): multiple reflections obstruction of light path by objects

8 Light Sources Ambient light -no identifiable source or direction -hack for replacing true global illumination =(light bouncing off from other objects)

9 Ambient Light Simulates situations where light sources are designed to produce uniform lighting throughout a scene. Characterized by a scalar intensity, I a, that is identical at every point in the scene. Although every surface receives the same illumination, each surface can reflect the light differently.

10 Point Sources An ideal point source radiates equally in all directions. It can be characterized by: Intensity of illumination from a point source is proportional to the inverse square of the distance between the point and the illuminated surface.

11 Light Sources Types of light sources - glLightfv(GL_LIGHT0,GL_POSITION,light[]) -directional/parallel lights =real-life example: sun =infinitely far source: homogeneous co-ord. w=0 -point lights =same intensity in all directions -spot lights =limited set of directions: point+direction+cutoff angle

12 Spotlights Spotlights are point sources for which the angle through which light has been emitted has been limited.  l s

13 Distant Sources Most shading calculations require the direction from the point on a surface to the light source. As light sources are moved to larger distances, this direction approaches a constant. Therefore the point source location will be replaced by a vector indicating the direction of the source.

14 Lighted Sphere Demo and Source Code

15 Reflection Rough Surface: Light is reflected equally in all directions. Diffuse Smooth Surface: Light is reflected at an angle near to the incident angle. Specular

16 Vectors Used by Phong Lighting Model p is an arbitrary point on a surface. n is the unit normal to the surface at p. l is a unit vector in the direction of the light source. r is a unit vector in the direction of a perfectly (specular) reflection v is a unit vector in the direction of the viewer. n l r v p Viewer

17 Elements of the Phong Lighting Model At each point p there is a reflection matrix for the ith light source: Assuming the calculations will be done separately for each primary, we can sum over light sources to get: At each point p there is an illumination matrix from the ith light source: r ed g reen b lue a mbient d iffuse s pecular

18 Elements of the Phong Lighting Model Ambient Reflection The intensity of ambient light is the same at every point on the surface. The percentage of light reflected is given by: Note that the ambient reflection terms can be different for red, green and blue.

19 Elements of the Phong Lighting Model Diffuse Reflection Intensity of illumination is dependent upon the incidence angle of light from the source: Adding a term for attenuation with distance from source:  l n

20 Elements of the Phong Lighting Model Specular Reflection For a shiny surface, most light is reflected around vector r, corresponding to a reflected angle equal to the angle of incidence. If  is the angle between v and r, Adding a distance term, and expressing the cosine in terms of a dot product: n l r v p Viewer

21 The Complete Lighting Model Distance (Attenuation) Ambient DiffuseSpecular Emission Global Ambient n l r v p Viewer

22 Calculation of Normals Normal exists at every point for mathematically defined smooth surfaces. Exists for each polygon in surface defined by flat polygons. What about the shared lines/points? Polygonal Case: p0 p1 p2 n

23 Calculation of Normals Mathematically Defined Surface: Sphere Sphere is defined implicitly by equation:

24 Specification of the Normal in OpenGL Specification of the current normal is modal and associated with vertices. glNormal3f(nx,ny,nz); glNormal3fv(pointer_to_normal_vector); You have to calculate the normals yourself.

25 Shading in OpenGL: Flat Shading OpenGL uses the normal of the first vertex of a single polygon to determine the color. Requested by: glShadeModel(GL_FLAT); Flat shading exaggerates the visual effect of the boundaries between polygons due to the Mach band effect.

26 Mach Band Effect (Mach, 1865) Color Science, Concepts and Methods Wyszecki, Stiles

27 Mach Band Effect

28 Mach Band Effect

29 Shading in OpenGL: Smooth Shading Requested by: glShadeModel(GL_SMOOTH); Lighting calculations will be done at each vertex using the material properties, and the vectors v and l calculated for that vertex. Bilinear interpolation is used to determine color values in the interior of the polygon. Gouraud Shading: Normal at a vertex is the normalized average of the normals of the polygons that share that vertex.

30 Gouraud Shading

31 Lighting Implementation in OpenGL

32 Lighted Sphere Demo and Source Code

33 Enable Depth Buffer For Hidden Surface Removal //before enabling while (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT); draw_3d_object_A(); draw_3d_object_B(); } //after enabling glutInitDisplayMode (GLUT_DEPTH |.... ); glEnable(GL_DEPTH_TEST);... while (1) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); get_viewing_point_from_mouse_position(); draw_3d_object_A(); draw_3d_object_B(); }

34 Specifying Light Sources in OpenGL General form: glLightf(source, parameter, value); glLightfv(source, parameter, *array); source is one of at least eight lights: GL_LIGHT i Parameters: GL_AMBIENT contains four values that specify the ambient RGBA intensity of the light. Default is (0.0, 0.0, 0.0, 1.0). GL_DIFFUSE contains four values that specify the diffuse RGBA intensity of the light. Default is (1.0, 1.0, 1.0, 1.0). GL_SPECULAR contains four values that specify the secular RGBA intensity of the light. Default is (1.0, 1.0, 1.0, 1.0).

35 Specifying Light Sources in OpenGL General form: glLightf(source, parameter, value); glLightfv(source, parameter, *array); GL_POSITION specifies the position of the light in homogeneous object coordinates. If the w component is zero, the light is treated as a directional source. GL_SPOT_DIRECTION specifies the direction of the light in homogeneous object coordinates. Default is (0.0, 0.0, -1.0) GL_SPOT_EXPONENT spotlight exponent, default 0.0 GL_SPOT_CUTOFF spot cutoff angle in [0,90] or (default) 180.  l s

36 Specifying Light Sources in OpenGL General form: glLightf(source, parameter, value); glLightfv(source, parameter, *array); GL_CONSTANT_ATTENUATION constant atten. factor, default 1.0 GL_LINEAR_ATTENUATION linear atten. factor, default 0.0 GL_QUADRATIC_ATTENUATION quadratic atten. factor, default 0.0 n l r v p Viewer

37 Enabling the Lights and Lighting Enabling a specific light source: glEnable(GL_LIGHT i ); Enabling the lighting model: glEnable (GL_LIGHTING);

38 Specifying Materials in OpenGL General form: glMaterialf(face, parameter,value); glMaterialfv(face, parameter,*array); face is GL_FRONT, GL_BACK, GL_FRONT_AND_BACK parameter is: GL_AMBIENT four values that specify the ambient RGBA reflectance of the material. (0.2,0.2,0.2,1.0) GL_DIFFUSE four values that specify the diffuse RGBA reflectance of the material. (0.8,0.8,0.8,1.0) GL_SPECULAR four values that specify the ambient RGBA reflectance of the material. (0.0,0.0,0.0,1.0) GL_SHININESS specifies the specular reflectance exponent of the material. 0.0

39 1. Ambient light in a scene with 3 spheres. 2. Diffuse light hitting the surface of 3 spheres. Notice, the spheres look matte and almost plastic like. 3. The three spheres illuminated by specular light. Imagine an extremely shiny billiard ball and the sheen it creates Types of Light and effects

40 Demo Rotating light

41 Creating Light Sources in OpenGL void glLight{if}(GLenum light, GLenum pname, TYPE param); void glLight{if}v(GLenum light, GLenum pname, TYPE *param);

42 Light Sources in OpenGL (cont.) Color for ambient, diffuse, and specular light GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0}; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

43 Light Sources in OpenGL (cont.) Directional light GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position); Positional light -when w is nonzero, (x, y, z) specify the location

44 Light Sources in OpenGL (cont.) Attenuation factor -d = distance between the light’s position and the vertex -k c = GL_CONSTANT_ATTENUATION -k l = GL_LINEAR_ATTENUATION -k q = GL_QUADRATIC_ATTENUATION

45 Light Sources in OpenGL (cont.) Spotlights -GL_SPOT_CUTOFF : the angle between the axis of the cone and a ray along the edge of the cone -GL_SPOT_DIRECTION : the axis of the cone of light -GL_SPOT_EXPONENT : to control how concentrated the light is.

46 Selecting a Lighting Model in OpenGL void glLightModel{if}(GLenum pname, TYPE param); void glLightModel{if}v(GLenum pname, TYPE *param);

47 Lighting Model in OpenGL (cont.) Global ambient light -Ambient light that’s not from any particular light source GLfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0}; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); Local or infinite viewpoint -With infinite viewpoint, the direction between it and any vertex remains constant -A local viewpoint yields more realistic results, but decreased performance glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);

48 Lighting Model in OpenGL (cont.) Two-sided lighting glLightModel(LIGHT_MODEL_TWO_SIDE, GL_TRUE); Enabling lighting glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDisable(GL_LIGHTING);

49 Multiple Lights in OpenGL At least, 8 lights - GL_LIGHT0, GL_LIGHT1,..., GL_LIGHT7 Increasing the number of lights adversely affects performance

50 Defining Material Properties in OpenGL void glMaterial{if}(GLenum face, GLenum pname, TYPE param); void glMaterial{if}v(GLenum face, GLenum pname, TYPE *param);

51 Material Properties in OpenGL (Cont.) Diffuse and ambient reflection -Affect the color of the diffuse and ambient light reflected by an object -Ambient term = ambient light *ambient material -Diffuse term = -For real-world objects, diffuse and ambient reflectance are normally the same color GLfloat mat_amb_diff[] = {0.1, 0.5, 0.8, 1.0}; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);

52 Example GLfloat a[] = {0.1, 0.5, 0.8, 1.0}; GLfloat b[] = {0.1, 0.5, 0.8, 1.0}; GLfloat c[] = {0.1, 0.5, 0.8, 1.0}; GLfloat d[] = {0.0, 0.0, 0.0, 1.0}; glMaterialfv(GL_FRONT, GL_AMBIENT, a); glMaterialfv(GL_FRONT, GL_DIFFUSE, b); glMaterialfv(GL_FRONT, GL_SPECULAR, c); glMaterialfv(GL_FRONT, GL_SHININESS, 50); glMaterialfv(GL_FRONT, GL_EMISSION, d); glutSolidSphere(1.0, 16, 16);

53 Material Properties in OpenGL (Cont.) Specular reflection -GL_SPECULAR specifies the color -GL_SHININESS controls the size and brightness of the highlight -Specular term = Emission -An object appears to give off light -GL_EMISSION specifies the color

54 Material Properties in OpenGL (Cont.) Changing material properties -When only a single property is changed, use glColorMaterial(), instead of glMaterial*() for performance glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_DIFFUSE); glColor3f(0.2, 0.5, 0.8); // draw some object here glColorMaterial(GL_FRONT, GL_SPECULAR); glColor3f(0.9, 0.0, 0.2); // draw other objects here glDisable(GL_COLOR_MATERIAL);

55 End of Lighting