@ 2017 by Jim X. Chen George Mason University

Slides:



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

2 COEN Computer Graphics I Evening’s Goals n Discuss the fundamentals of lighting in computer graphics n Analyze OpenGL’s lighting model n Show.
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.
Computer Graphics - Class 10
OpenGL Son of the Survival Guide. Last Time on OpenGL Windowing … glut Rendering Primatives Transformations Projections State Management.
Shading in OpenGL CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
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.
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 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Chapter 6: Shading Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
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.
OpenGL - Lighting, Shading and Material Properties
Computer Graphics I, Fall 2010 Shading in OpenGL.
Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
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.
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.
Lecture Fall 2001 Illumination and Shading in OpenGL Light Sources Empirical Illumination Shading Transforming Normals Tong-Yee Lee.
11/5/2002 (c) University of Wisconsin, CS 559 Last Time Local Shading –Diffuse term –Specular term –All together –OpenGL brief overview.
Where We Stand So far we know how to: –Transform between spaces –Rasterize –Decide what’s in front Next –Deciding its intensity and color.
Lighting and Shading Part 2. Global Ambient Light There are at least 8 OpenGL lights and 1 Global Ambient Setting the Global Ambient globalAmbient[] =
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 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.
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
CS418 Computer Graphics John C. Hart
© University of Wisconsin, CS559 Spring 2004
Shading To determine the correct shades of color on the surface of graphical objects.
Illumination and Shading
Open GL: Colors and Lighting
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Vectors, Normals, & Shading
Shading in OpenGL Ed Angel
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
ייצוג בעולם 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:

@ 2017 by Jim X. Chen George Mason University COLOR IN OPENGL RGB Mode and Index Mode If each pixel value in the frame buffer is an RGB vector, RGB mode. Each pixel value an index into a color look-up table called a colormap, index mode. The pixel color is specified in the colormap instead of the frame buffer. @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University RGBA versus Color-Index Mode Color-index mode a memory trade-off except: If you’re porting an existing application Tricks as color-map animation and drawing in layers. @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Color Interpolation glShadeModel(GL_FLAT), only one color in a primitive (the last vertex in a primitive) glShadeModel(GL_SMOOTH), color interpolation For a line, the vertex colors are linearly interpolated along the pixels between the two end vertices. a line has 5 pixels, and the end point colors are (0,0,0) and (0,0,1), then, after the interpolation, the 5 pixel colors will be (0,0,0), (0,0,1/4), (0,0,2/4), (0,0,3/4), and (0,0,1). @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Discrete interpolation J3_1_Shading each RGB component is interpolated separately Given end point intensities (Il1 and Il2) and the number of pixels (N), the intensity increment of the linear interpolation is: DIl= (Il2 – Il1) / (N-1) For a polygon, OpenGL first interpolates along the edges, and then along the horizontal scan-lines during scan-conversion. @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Continuous interpolation each RGB component is interpolated separately Given end point intensities (Il1 and Il2) and the distance to the end point is 0 <= a <= 1, the intensity of the linear interpolation is: Il= (1-a)*Il1 + a* Il2 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University GLSL Vertex colors are sent to vertex shaders After primitive assembly, colors are interpolated along edges and then the horizontal lines Each pixel will have a (different) color, which will be sent to the corresponding fragment shader @ 2017 by Jim X. Chen George Mason University

ILLUMINATION (LIGHTING) Robot Arm LIGHTING MODELS FOR A POINT Have different illumination models (shading models, lighting models); the models have no grounding in theory, but work well. SHADING MODELS FOR A POLYGON/MODEL Shade surfaces based on the position, orientation, and characteristics of the surfaces and the light sources illuminating them. Light & Material Approximation methods: Interpolation @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University NORMAL VECTORS A normal vector (or normal) points in a direction perpendicular to a surface (in general). An object’s normal vectors define its orientation relative to light sources. These vectors are used by OpenGL to determine how much light the object receives at its vertices. Lighting for vertices is calculated after MODELVIEW transformation before PROJECTION transformation (vertex shading) In GLSL, an array of normals corresponding to the array of vertices are sent to the vertex shaders. glBegin (GL_POLYGON); glNormal3fv(n0); glVertex3fv(v0); glNormal3fv(n1); glVertex3fv(v1); glNormal3fv(n2); glVertex3fv(v2); glNormal3fv(n3); glVertex3fv(v3); glEnd(); @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Transforming Normals A point v=(x, y, z, w)T is on a plane p=(a, b, c, d) if ax + by + cz + d = 0 pv = 0 if v is transformed by matrix M, then pM-1 Mv=0 So Mv lies in the plane pM-1. The normal n=p. The transformed normal N along with v is: N = nM-1 that is NT = (M-1)TnT Conclusion: normals are transformed by the inverse transpose of the matrix that transforms the points. glEnable(GL_REESCALE_NORMAL); // rescale back to unit. // Normals are affected by scale only glEnable(GL_NORNALIZE); // automatically normalize @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Lighting Components J3_2_Emission Emissive Component Light & Material I = M l e l emission where l is an RGB component or A (alpha), and Mlemission is the material’s emission property. Each color component is calculated independently. In OpenGL, emission is a material property that is neither dependent on any light source nor considered a light source. Emissive material does not emit light, it displays its own color. The vertex’s corresponding surface has two sides, the front and the back, which can be specified with different material properties. @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University J3_3_Ambient Ambient Component Multiple reflections of light from the environment ambient light impinges equally on all surfaces from all directions. I = L M l a l a l a where Lla represents the light source’s ambient intensity and Mla is the material’s ambient property. Light & Material @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Diffuse Component J3_4_Diffuse Diffuse color is the reflection from a dull surface material that appears equally bright from all viewing directions. I = L M (n · L) l d l d l d where Lld is the light source’s diffuse intensity, Mld is the material’s diffuse property, L is the light source direction, and n is the surface normal direction cosq is between 0 and 1 when q is between 0o and 90o. When q is greater than 90o, the diffuse intensity is set to zero Light & Material @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University = L M (n · L) l d l d l d we can call glEnable(GL_NORMALIZE), which enables the OpenGL system to normalize each normal before calculating the lighting. If a point light source is sufficiently distant from the objects being shaded, it makes essentially the same angle with all surfaces sharing the same surface normal. L q L q N N @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University the normals are n1=v1+v3, n2=v2+v3, and n3=n1+n2. void drawConeSide(float *v1, float *v2, float *v3) { float v11[3], v22[3], v33[3]; int i; for (i=0; i<3; i++) { v11[i] = v1[i] + v3[i]; // normal for cone vertex 1 v22[i] = v2[i] + v3[i]; // normal for cone vertex 2 v33[i] = v11[i] + v22[i]; // normal for cone vertex 3 } glBegin(GL_TRIANGLES); glNormal3fv(v11); glVertex3fv(v1); glNormal3fv(v22); glVertex3fv(v2); glNormal3fv(v33); glVertex3fv(v3); glEnd(); @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University J3_5_Specular Specular Component Highlight reflection from a smooth-surface material that depends on th reflection direction R (which is L reflected along the normal) and the viewing point direction V. shininess æ ö __________ N L V · ( ) + I = L M l s l s l s è ø L V + where Lls is the light source’s specular intensity, Mls is the material’s specular property, and shininess is the material’s shininess property. n L q ( normal ) light source R V view point a cos = __________ N L V · ( ) + L V + Light & Material @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University The view point, as we discussed in the viewing transformation, is at the origin (facing the negative z axis). We use glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE) to specify the view point at (0, 0, 0). OpenGL allows us to specify the view point at infinity. Since this assumption is only used to simplify lighting calculations, the view point is not changed for other graphics calculations, such as projection. n L q ( normal ) light source R V view point a cos = N L V __________ · ( ) + L V + Light & Material @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Lighting Model J3_6_Materials I = I + I + I + I l l e a l d l s l = I + I l e l L While ambient, diffuse, and specular intensities depend on the light source, emissive intensity does not. @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Movable Light Source Lighting is calculated according to the transformed position. To simulate a visible light source, we can specify the light source and draw an object at the same position. Lighting is calculated after ModelView transforamtion. J3_7_MoveLight 18 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Spot Light Effect A real light source may not generate equal intensity in all directions: I = I + f I l l e spot l L where fspot is called the spot light effect factor. In OpenGL, it is calculated as follows: spotExp f = ( ) – L · D spot spot where (-L) is a unit vector pointing from the light source to the vertex pixel, Dspot is the direction of the light source, and spotExp is a specified constant. 19 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University = I + f I l l e spot l L spotExp f = ( ) – L · D spot spot where (-L) is a unit vector pointing from the light source to the vertex pixel, Dspot is the direction of the light source, and spotExp is a specified constant. J3_8_SpotLight 20 @ 2017 by Jim X. Chen George Mason University

Light Source Attenuation J3_9_AttLight The intensity from a point light source to a vertex pixel can be attenuated by the distance dL the light travels: I = I + f f I l l e att spot l L where fatt is called the light source attenuation factor. In OpenGL, fatt is calculated as follows: ________________ 1 f = att 2 A + A d + A d c l L q L 21 @ 2017 by Jim X. Chen George Mason University

Multiple Light Sources We can also specify K multiple light sources: K 1 – å I = I + f f I l l e atti spoti l Li i 0 = J3_10_Lights 22 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University OpenGL 4.x/GLSL vertex parameters sent to the vertex shader Vertex positions, normals, light source positions, material properties Light source position is transformed by ModelView matrix before sent to the vertex shader ModelView matrix, Inverse-transpose of ModelView Matrix, Projection Matrix For transforming the vertex as well as the normal Lighting is calculated afterwards accordingly @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Old OpenGL Lighting Defining Light Source Properties void glLight{if}[v](GLenum light, GLenum pname, TYPE param) light can be GL_LIGHT0, GL_LIGHT1, ... , or GL_LIGHT7. pname specifies a named parameter. param indicates the values to which the pname characteristic is set; 2424 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Light & Material Parameter Name Default Value Meaning =================================================== GL_AMBIENT (0.0, 0.0, 0.0, 1.0) ambient RGBA GL_DIFFUSE (1.0, 1.0, 1.0, 1.0) diffuse RGBA GL_SPECULAR (1.0, 1.0, 1.0, 1.0) specular RGBA GL_POSITION (0.0, 0.0, 1.0, 0.0) position of light GL_SPOT_DIRECTION (0.0, 0.0, -1.0) direction of spotlight GL_SPOT_EXPONENT 0.0 spotlight exponent GL_SPOT_CUTOFF 180.0 spotlight cutoff angle GL_CONSTANT_ATTENUATION 1.0 constant attenuation factor GL_LINEAR_ATTENUATION 0.0 linear attenuation factor GL_QUADRATIC_ATTENUATION 0.0 quadratic attenuation factor (Max{Vd, 0})GL_SPOT_EXPONENT V is the normalized direction of the vertex from GL_SPOT_POSITION GL_SPOT_CUTOFF GL_SPOT_DIRECTION (d) V 2525 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Directional and Positional light source GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position); If w is zero, the corresponding light source is directional (at infinite), & the (x, y, z) values describe its direction. This direction is transformed by the modelview matrix. If the w value is nonzero, the light is positional, and the (x, y, z) values specify the location of the light in homogeneous object coordinates. This location is transformed by the modelview matrix and stored in eye coordinates. Light Lab. 2626 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Attenuation factor = 1/(kc+kl*d+kq*d2) where d = distance between the light’s position and the vertex k c = GL_CONSTANT_ATTENUATION l = GL_LINEAR_ATTENUATION q = GL_QUADRATIC_ATTENUATION glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION, 2.0); glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION, 1.0); glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION, 0.5); The ambient, diffuse, and specular values are all attenuated. Only the emission and global ambient values aren’t attenuated. Emitted light: it originates from an object and is unaffected by any light sources. 2727 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Light & Material Selecting an OpenGL Lighting Model Global Ambient Light (not from any particular source) GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1. }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); Local or Infinite Viewpoint A local viewpoint tends to more realistic results (specular highlights), but overall performance is decreased: glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); This call places the viewpoint at (0, 0, 0) in eye coordinates. Two-sided Lighting glLightModeli(LIGHT_MODEL_TWO_SIDE, GL_TRUE); both front-facing polygons and back-facing polygons are lit. OpenGL reverses the normals to calculate the lighting for back-facing polygons. 2828 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Defining Material Properties void glMaterial{if}[v](GLenum face, GLenum pname, TYPEparam); face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK. Material properties are similar to creating light sources GL_SHININESS; GL_EMISSION; GL_AMBIENT_AND_DIFFUSE; Color Material Mode void glColorMaterial(Glenum face, Glenum mode); face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK. mode: GL_AMBIENT, GL_DIFFUSE, GL_EMISSION; etc. Need to call glEnable(GL_COLOR_MATERIAL), then you can change the current color using glColor*() to change the material properties. 2929 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University The Mathematics of Lighting Spotlight Effect Evaluates to one of 3 possible values: ·1 if it isn’t a spotlight (GL_SPOT_CUTOFF is 180.0). ·0 if the vertex lies outside the cone of illumination. ·(max {v (dot) d, 0 } ) GL_SPOT_EXPONENT where: v is unit from the spotlight (GL_POSITION) to the vertex; d is the spotlight’s direction (GL_SPOT_DIRECTION) GL_SPOT_CUTOFF GL_SPOT_DIRECTION (d) V 3030 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Specular Term (max { s (dot) n , 0}) shininess * specular light material ·s = L + V if GL_LIGHT_MODEL_LOCAL_VIEWER is not true, V = (0, 0, 1) ·However, if L (dot) n = 0, the specular term is 0. Together a vertex color = emission + ambient light_model *ambient n-1 S { ((1/(k c + k l *d + k q *d 2 )) i *(spotlight effect) * i=0 [ (max{L dot n, 0})*diffuse *diffuse (max{s dot n ,0}) *specular ] Light & Material 3131 @ 2017 by Jim X. Chen George Mason University

@ 2017 by Jim X. Chen George Mason University Lighting Equation I l e f atti spoti Li i 0 K 1 – å + = L/|L| = f att 1 A c l d L q 2 + ________________ = (max{(-L D ), 0} f spot · spotExp = I lL l a d s + = I l a L M = I l d L M max{(n · L/|L|), 0} = __________ æ ö è ø + ( ) I l s L M n L V · L V shininess = 32 @ 2017 by Jim X. Chen George Mason University