Illumination and Shading Angel, Chapter 6 slides from AW, etc.

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.
Illumination and Shading
1. What is Lighting? 2 Example 1. Find the cubic polynomial or that passes through the four points and satisfies 1.As a photon Metal Insulator.
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
Shading in OpenGL CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
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.
7M836 Animation & Rendering
Objectives Learn to shade objects so their images appear three- dimensional Learn to shade objects so their images appear three- dimensional Introduce.
CS5500 Computer Graphics March 26, Shading Reference: Ed Angel’s book.
Computer Graphics (Spring 2008) COMS 4160, Lecture 14: OpenGL 3
University of British Columbia CPSC 414 Computer Graphics © Tamara Munzner 1 Shading Week 5, Wed 1 Oct 2003 recap: lighting shading.
Illumination and Shading
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Shading I Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Course Website: Computer Graphics 16: Illumination.
CS 480/680 Computer Graphics Shading I Dr. Frederick C Harris, Jr.
Fundamentals of Computer Graphics Part 6 Shading prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.:
CS 445 / 645: Introductory Computer Graphics
Computer Graphics Lighting.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
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.
COMPUTER GRAPHICS CS 482 – FALL 2014 AUGUST 27, 2014 FIXED-FUNCTION 3D GRAPHICS MESH SPECIFICATION LIGHTING SPECIFICATION REFLECTION SHADING HIERARCHICAL.
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.
Illumination and Shading Angel, Chapter 5 slides from AW, etc.
CSC418 Computer Graphics n Illumination n Lights n Lightinging models.
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.
David Luebke 1 10/26/2015 Lighting CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
Steve Sterley. Real World Lighting Physical objects tend to interact with light in three ways: Absorption (black body) Reflection (mirror) Transmission.
CG Summary: OpenGL Shading andTextures Angel, Chapters 5, 7; “Red Book” slides from AW, red book, etc. CSCI 6360/4360.
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.
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S Andries van Dam October 23, 2003 Illumination Models 1/18 CIS 736 Computer Graphics Illumination.
Illumination and Shading
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Photo-realistic Rendering and Global Illumination in Computer Graphics Spring 2012 Material Representation K. H. Ko School of Mechatronics Gwangju Institute.
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.
Local Illumination and Shading
Where We Stand So far we know how to: –Transform between spaces –Rasterize –Decide what’s in front Next –Deciding its intensity and color.
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
David Luebke3/16/2016 CS 551 / 645: Introductory Computer Graphics David Luebke
Illumination Models. Introduction 1 Illumination model: Given a point on a surface, what is the perceived color and intensity? Known as Lighting Model,
CS552: Computer Graphics Lecture 33: Illumination and Shading.
Computer Graphics: Illumination
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.
© University of Wisconsin, CS559 Spring 2004
Shading To determine the correct shades of color on the surface of graphical objects.
CSC461: Lecture 23 Shading Computation
Vectors, Normals, & Shading
Shading in OpenGL Ed Angel
Lighting and Materials
CS5500 Computer Graphics April 10, 2006.
Isaac Gang University of Mary Hardin-Baylor
Chapter 14 Shading Models.
CS 480/680 Computer Graphics Shading.
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Presentation transcript:

Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Overview Photorealism and complexity Rendering equation Light interactions with a solid Rendering equation “infinite scattering and absorption of light” Local vs. global illumination Local techniques Flat, Gouraud, Phong An illumination model “describes the inputs, assumptions, and outputs that we will use to calculate illumination of surface elements” Light Reflection characteristics of surfaces Diffuse reflection, Lambert’s law, specular reflection Phong approximation – interpolated vector shading

Angel on Speed for Interactivity “… our goal is to create realistic shading effects in as close to real time as possible, rather than trying to model the BRDF as accurately as possible. Hence, we use tricks with a local model to simulate effects that can be global in nature.” P. 298 “Because we cannot solve the full rendering equation, we instead use various tricks in an attempt to obtain realistic renderings, …” P. 307 Good enough … for “fast” (interactive) graphics Things change with time Moore’s law

Photorealism and Complexity Recall, from 1st lecture … examples below exhibit range of “realism” In general, trade off realism for speed – interactive computer graphics Wireframe – just the outline Local illumination models, polygon based Flat shading – same illumination value for all of each polygon Smooth shading (Gouraud and Phong) – different values across polygons Global illlumination models E.g., Raytracing – consider “all” interactions of light with object Wireframe Polygons – Flat shading Polygons - Smooth shading Ray tracing

Shading So far, just used OpenGL pipeline for vertices Polygons have all had constant color glColor(_) Not “realistic” – or computationally complex Of course, OpenGL can (efficiently) provide more realistic images Light-material interactions cause each point to have a different color or shade Need to consider : Light sources Material properties Location of viewer Surface orientation Terminology “Lighting” modeling light sources, surfaces, and their interaction “Shading” how lighting is done with polygon

Rendering Equation Light travels … Light strikes A Some scattered, some absorbed, … Some of scattered light strikes B Some of this scattered light strikes A And so on … Infinite scattering and absorption of light can be described by rendering equation Bidirectional reflection distribution function Cannot be solved in general Ray tracing is a special case for perfectly reflecting surfaces Rendering equation is global, includes: Shadows Multiple scattering from object to object … and everything translucent surface shadow multiple reflection

Elements of Global Illumination Simulating what happens when other objects effect light reaching a surface element e.g., ray tracing Lights and Shadows Most light comes directly light sources Light from a source may be blocked by other objects In “shadow” from that light source, so darker All Non – global can’t do shadows Inter-Object Reflection Light strikes other objects, bounces toward surface element When that light reaches surface element from other surface elements, brightens surface element (indirect illumination) Expensive to Compute Many objects in scene affect light reaching surface elements But, necessary for some applications translucent surface shadow multiple reflection

Global Illumination – Ray Tracing Image formed from all light reaching view Ray tracing (or casting) Basically, “running things backwards, constrained by pixels …” Follow rays from center of projection until they either are absorbed by objects or go off to infinity Can handle global effects Multiple reflections Translucent objects Slow Must have whole data base available at all times Radiosity Energy based approach Very slow

Light Interactions with a Solid Will focus on surface properties and light-material interactions But, there are complexities in modeling light … but good enough is good enough… Below from Watt, “3d Computer Graphics”

Light – Material Interactions for CG (quick look) Will examine each of these in detail Diffuse surface Matte, dull finish Light “scattered” in all directions Specular surface Shiny surface Light reflected (scattered) in narrow range of directions Translucent surface Light penetrates surface and emerges in another location on object

Physical Model: BRDF Bidirectional Reflectance Distribution Function Light energy can arrive at any point on a surface from any direction and be reflected and leave from any direction For every pair of input and output directions, will be a value that is the fraction of incoming light that is reflected and leave in output direction BDRF – Bidirectional reflectance distribution function Describes reflectance, absorption, and transmission of light at surface of material BDRF is a function of Frequency of light 2 angles to describe light in 2 angles to describe light out

Physical Model: BRDF Light energy can arrive at any point on a surface from any direction and be reflected and leave from any direction For every pair of input and output directions, will be a value that is the fraction of incoming light that is reflected and leave in output direction Obtaining BDRF’s empirically … and how many points are there to consider???

“Surface Elements” for Interactive CG (a big idea) A computer graphics issue/orientation: Consider everything or just “sampling a scene”? Again, global view considers all light coming to viewer: From each point on each surface in scene - object precision Points are smallest units of scene Can think of points having no area or infinitesimal area i.e., there are an infinite number of visible points. Of course, computationally intractable Alternatively, consider surface elements Finite number of differential pieces of surface E.g., polygon Figure out how much light comes to viewer from each of these pieces of surface Often, relatively few (vs. infinite) is enough Reduction of computation through use of surface elements is at core of tractable/interactive cg

Surface Elements and Illumination, 1 Tangent Plane Approximation for Objects Most surfaces are curved: not flat Surface element is area on that surface Imagine breaking up into very small pieces Each of those pieces is still curved, but if we make the pieces small enough, then we can make them arbitrarily close to being flat Can approximate this small area with a tiny flat area Surface Normals Each surface element lies in a plane. To describe plane, need a point and a normal Area around each of these vertices is a surface element where we calculate “illumination” Illumination

Surface Elements and Illumination, 2 Tangent Plane Approximation for Objects Surface Normals Illumination Again, light rays coming from rest of scene strike surface element and head out in different directions Light that goes in direction of viewer from that surface element If viewer moves, light will change This is “illumination” of that surface element Will see model for cg later

In Sum: Local vs. Global Rendering Correct shading requires a global calculation involving all objects and light sources Recall “rendering equation” infinite scattering and absorption of light Incompatible with pipeline model which shades each polygon independently (local rendering) However, in computer graphics, especially real time graphics, happy if things “look right” Exist many techniques for approximating global effects Will see several

Light-Material Interaction, 1 Light that strikes an object is partially absorbed and partially scattered (reflected) Amount reflected determines color and brightness of object Surface appears red under white light because the red component of light is reflected and rest is absorbed Can specify both light and surface colors rough surface Livingstone, “Vision and Art”

Light-Material Interaction, 2 Reflected light is scattered in a manner that depends on the smoothness and orientation of surface to light source Diffuse surfaces Rough (flat, matte) surface scatters light in all directions Appear same from different viewing angles Specular surfaces Smoother surfaces, more reflected light is concentrated in direction a perfect mirror would reflected the light Light emerges at single angle … to varying degrees – Phong shading will model

Light Sources General light sources are difficult to work with because must integrate light coming from all points on the source Use “simple” light sources Point source Model with position and color Distant source = infinite distance away (parallel) Spotlight Restrict light from ideal point source Ambient light A real convenience – recall, “rendering equation” – but real nice Same amount of light everywhere in scene Can model (in a good enough way) contribution of many sources and reflecting surfaces

Overview: Local Rendering Techniques Will consider Illumination (light) models focusing on following elements: Ambient Diffuse Attenuation Specular Reflection Interpolated shading models: Flat, Gouraud, Phong, modified/interpolated Phong (Blinn-Phong)

About (Local) Polygon Mesh Shading (it’s all in how many polygons there are) Angel example of approximation of a sphere… Recall, any surface can be illuminated/shaded/lighted (in principle) by: 1. calculating surface normal at each visible point and 2. applying illumination model … or, recall surface model of cg! Again, where efficiency is consideration, e.g., for interactivity (vs. photorealism) approximations are used Fine, because polygons themselves are approximation And just as a circle can be considered as being made of “an infinite number of line segments”, so, it’s all in how many polygons there are!

About (Local) Polygon Mesh Shading (interpolation) Interpolation of illumination values are widely used for speed And can be applied using any illumination model Will see three methods - each treats a single polygon independently of others (non-global) Constant (flat) Gouraud (intensity interpolation) Interpolated Phong (normal-vector interpolation) Each uses interpolation differently

Flat/Constant Shading, About Single illumination value per polygon Illumination model evaluated just once for each polygon 1 value for all polygon, Which is as fast as it gets! As “sampling” value of illumination equation (at just 1 point) Right is flat vs. smooth (Gouraud) shading If polygon mesh is an approximation to curved surface, faceted look is a problem Also, facets exaggerated by mach band effect For fast, can (and do) store normal with each surface Or can, of course, compute from vertices But, interestingly, approach is valid, if: Light source is at infinity (is constant on polygon) Viewer is at infinity (is constant on polygon) Polygon represents actual surface being modeled (is not an approximation)!

Flat/Constant Shading, Light Source In cg lighting, often don’t account for angle of rays Approach is valid, if … Light source is at infinity (is constant on polygon) Viewer is at infinity (is constant on polygon) Polygon represents actual surface being modeled (is not an approximation) Consider point light sources at right Close to surface: L1 <> L2 <> L3 Farther from surface: L1 <> L2 <> L3, but closer -------------------- At “infinity” can consider: L1 = L2 = L3! – same for V!, so and are constant on polygon

But, … Mach Banding May or may not be apparent here …

Mach Banding (Chevreul illusion)

Mach Banding Mach banding In fact, physiological cause Exaggerated differences in perceived intensities At adjacent edges of differing gintensities Non-intuitive and striking An “illusion” in sense that perception not veridical (true) In fact, physiological cause Actual and perceived intensities due to cellular bilateral inhibition sensation (response of retinal cells) depends on how cell neighbors are stimulated Eye’s photoreceptors responds to light according to intensity of light falling on it minus the activation of its neighbors Great for human edge detection A challenge for computer graphics Ernst Mach

Mach Bands and Receptor Fields Point where uniform area meets luminance ramp, bright band is perceived Another way, appear where abrupt change in 1st derivative of brightness profile Particularly a problem for uniformly shaded polygons in computer graphics Hence, various methods of smoothing are applied … any number of “unintended” things can happen with perception

Simultaneous Brightness Contrast fyi Gray patch on a dark background looks lighter than the same patch on a light background

Simultaneous Brightness Contrast fyi Background removed! (honest, no change in foreground)

Gouraud Shading, About Recall, for flat/constant shading, single illumination value per polygon Gouraud (or smooth, or interpolated intensity) shading overcomes problem of discontinuity at edge exacerbated by Mach banding “Smooths” where polygons meet H. Gouraud, "Continuous shading of curved surfaces," IEEE Transactions on Computers, 20(6):623–628, 1971. Linearly interpolate intensity along scan lines Eliminates intensity discontinuities at polygon edges Still have gradient discontinuities, So mach banding is improved, but not eliminated Must differentiate desired creases from tessellation artifacts (edges of a cube vs. edges on tesselated sphere)

Gouraud Shading, About To find illumination intensity, need intensity of illumination and angle of reflection Flat shading uses 1 angle Gouraud estimates …. Interpolates 1. Use polygon surface normals to calculate “approximation” to vertex normals Average of surrounding polygons’ normals Since neighboring polygons sharing vertices and edges are approximations to smoothly curved surfaces So, won’t have greatly differing surface normals Approximation is reasonable one 2. Interpolate intensity along polygon edges 3. Interpolate along scan lines i.e,, find: Ia, as interpolated value between I1 and I2 Ib, as interpolated value between I1 and I3 Ip, as interpolated value between Ia and Ib formulaically, next slide

(S) Gouraud Shading, Formulaically What goes in (firmware) - straightforward 3: interpolate along scan lines e.g., find: Ia, as interpolated value between I1 and I2 Ib, as interpolated value between I1 and I3 Ip, as interpolated value between Ia and Ib i.e., formulaically: or

(S) Gouraud Shading Can also interpolate color Though less straightforward Summary: Interpolating intensity Gouraud shading Gouraud versus constant shading integrates nicely with scan line algorithm: constant along polygon edge:

What Gouraud Shading Misses Misses specular highlights in specular objects because interpolates vertex colors instead of vertex normals interpolating normal comes closer to what actual normal of surface being “polygonally” approximated would be Illumination model following, and its implementation in Phong shading, does handle Below: Flat/constant, Gouraud/interpolated intensity, Phong

Illumination Model, Describing Light, 1 Will be looking at model of illumination for cg Why are things relatively brighter or darker depending on orientation to light source and viewer? Start with light … Units of light light incident on a surface and exiting from a surface is measured in specific terms defined later for now, consider the ratio: Light exiting surface from the viewer Light incident on surface from light Another way to conceptualize Quick take ( note diff angle, theta, at right): Just not as much “light” (energy) per surface area unit – for non “straight on” light dA “shorter” (less) on left, than right And can describe quantitatively

Describing Light, 2 Factors in computing “light exiting surface” (how bright) physical properties of the surface (material) geometric relationship of surface with respect to viewer geometric relationship of surface with respect to lights light incident on the surface (color and intensity of lights in the scene) polarization, fluorescence, phosphorescence Difficult to define some of these inputs not sure what all categories of physical properties are …, and the effect of physical properties on light is not totally understood polarization of light, fluorescene, phosphorescene difficult to keep track of Light exiting surface toward viewer Light incident on surface from lights

Simple Illumination Model One of first models of illumination that “looked good” and could be calculated efficiently simple, non-physical, non-global illumination model describes some observable reflection characteristics of surfaces came out of work done at the University of Utah in the early 1970’s still used today, as it is easy to do in software and can be optimized in hardware Later, will put all together with normal interpolation Components of a simple model Reflection characteristics of surfaces Diffuse Reflection Ambient Reflection Specular Reflection Model not physically-based, and does not attempt to accurately calculate global illumination does attempt to simulate some of important observable effects of common light interactions can be computed quickly and efficiently

Reflection Characteristics of Surfaces, Diffuse Reflection (1/7) Diffuse (Lambertian) reflection typical of dull, matte surfaces e.g. carpet, chalk plastic independent of viewer position dependent on light source position (in this case a point source, again a non-physical abstraction) Vecs L, N used to determine reflection Value from Lambert’s cosine law … next slide

Reflection Characteristics of Surfaces, Lambert’s Law (2/7) Lambert’s cosine law: Specifies how much energy/light reflects toward some point Computational form used in equation for illumination model Now, have intensity (I) calculated from: Intensity from point source Diffuse reflection coefficient (arbitrary!) With cos-theta calculated using normalized vectors N and V For computational efficiency Again:

Reflection Characteristics of Surfaces, Energy Density Falloff (3/7) Less light as things are farther away from light source Reflection - Energy Density Falloff Should also model inverse square law energy density falloff Formula often creates harsh effects However, this makes surfaces with equal differ in appearance ¾ important if two surfaces overlap Do not often see objects illuminated by point lights Can instead use formula at right Experimentally-defined constants Heuristic

Reflection Characteristics of Surfaces, Ambient Reflection (4/7) Diffuse surfaces reflect light Some light goes to eye, some to scene Light bounces off of other objects and eventually reaches this surface element This is expensive to keep track of accurately Instead, we use another heuristic Ambient reflection Independent of object and viewer position Again, a constant – “experimentally determined” Exists in most environments some light hits surface from all directions Approx. indirect lighting/global illumination A total convenience but images without some form of ambient lighting look stark, they have too much contrast Light Intensity = Ambient + Attenuation*Diffuse

Reflection Characteristics of Surfaces, Color (5/7) Colored Lights and Surfaces Write separate equation for each component of color model Lambda - wavelength represent an object’s diffuse color by one value of for each component e.g., in RGB are reflected in proportion to e.g., for the red component Wavelength dependent equation Evaluating the illumination equation at only 3 points in the spectrum is wrong, but often yields acceptable pictures. To avoid restricting ourselves to one color sampling space, indicate wavelength dependence with (lambda).

Reflection Characteristics of Surfaces, Specular Reflection (6/7) Directed reflection from shiny surfaces typical of bright, shiny surfaces, e.g. metals color depends on material and how it scatters light energy in plastics: color of point source, in metal: color of metal in others: combine color of light and material color dependent on light source position and viewer position Early model by Phong neglected effect of material color on specular highlight made all surfaces look plastic for perfect reflector, see light iff for real reflector, reflected light falls off as increases Below, “shiny spot” size < as angle view >

Reflection Characteristics of Surfaces, Specular Reflection (7a/7) Phong Approximation Again, non-physical, but works Deals with differential “glossiness” in a computationally efficient manner Below shows increasing n, left to right “Tightness” of specular highlight n in formula below (k, etc., next slide)

Reflection Characteristics of Surfaces, Specular Reflection (7b/7) Yet again, constant, k, for specular component Vectors R and V express viewing angle and so amount of illumination n is exponent to which viewing angle raised Measure of how “tight”/small specular highlight is

Putting it all together: A Simple Illumination Model Non-Physical Lighting Equation Energy from a single light reflected by a single surface element For multiple point lights, simply sum contributions An easy-to-evaluate equation that gives useful results It is used in most graphics systems, but it has no basis in theory and does not model reflections correctly!

Interpolated Vector Shading/Model Calculating normal at each point is computationally expensive Can interpolate normal As interpolated vertices with Gouraud shading Interpolated Vector Model: Rather than recalculate normal at each at each step, interpolate normal for calculation Much more computationally efficient Bui Tuong Phong, "Illumination for Computer Generated Images," Comm. ACM, Vol 18(6):311-317, June 1975

Phong Shading Normal vector interpolation interpolate N rather than I especially important with specular reflection computationally expensive at each pixel to recompute must normalize, requiring expensive square root Looks much better than Gouraud and done in commodity hardware OpenGL default Bui Tuong Phong, "Illumination for Computer Generated Images," Comm. ACM, Vol 18(6):311-317, June 1975

Demo Program Calculates Phong shading at each point on sphere

End .

OpenGL Shading Functions Polygonal shading Flat Smooth Gouraud Steps in OpenGL shading Enable shading and select model Specify normals Specify material properties Specify lights

Normals Normal – vector perpendicular to a surface Recall, typically set by application program for each vertex or polygon 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 Length can be affected by transformations Note that scaling does not preserved length glEnable(GL_NORMALIZE) allows for autonormalization at a performance penalty

FYI - Normal for Triangle p0 p1 p2 n p plane n ·(p - p0 ) = 0 n = (p2 - p0 ) ×(p1 - p0 ) normalize n  n/ |n| Note that right-hand rule determines outward face

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….. Can choose light model parameters: glLightModeli(parameter, GL_TRUE) GL_LIGHT_MODEL_LOCAL_VIEWER do not use simplifying distant viewer assumption in calculation GL_LIGHT_MODEL_TWO_SIDED shades both sides of polygons independently

Defining a Point Light Source For each light source, can set an RGBA (A for alpha channel) For diffuse, specular, and ambient components, and For the position Code below from Angel, other ways to do it (of course) GL float diffuse0[]={1.0, 0.0, 0.0, 1.0}; GL float ambient0[]={1.0, 0.0, 0.0, 1.0}; GL float specular0[]={1.0, 0.0, 0.0, 1.0}; Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightv(GL_LIGHT0, GL_POSITION, light0_pos); glLightv(GL_LIGHT0, GL_AMBIENT, ambient0); glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0); glLightv(GL_LIGHT0, GL_SPECULAR, specular0);

Distance and Direction Position is given in homogeneous coordinates If w =1.0, we are specifying a finite location If w =0.0, we are specifying a parallel source with the given direction vector Coefficients in distance terms are by default a=1.0 (constant terms), b=c=0.0 (linear and quadratic terms) Change by: a= 0.80; glLightf(GL_LIGHT0, GLCONSTANT_ATTENUATION, a); GL float diffuse0[]={1.0, 0.0, 0.0, 1.0}; GL float ambient0[]={1.0, 0.0, 0.0, 1.0}; GL float specular0[]={1.0, 0.0, 0.0, 1.0}; Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};

Spotlights f Use glLightv to set q -q Direction GL_SPOT_DIRECTION Cutoff GL_SPOT_CUTOFF Attenuation GL_SPOT_EXPONENT Proportional to cosaf f -q q

Global Ambient Light Ambient light depends on color of light sources A red light in a white room will cause a red ambient term that disappears when the light is turned off OpenGL also allows a global ambient term that is often helpful for testing glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient)

Moving Light Sources Light sources are geometric objects whose positions or directions are affected by the model-view matrix Depending on where we place the position (direction) setting function, we can Move the light source(s) with the object(s) Fix the object(s) and move the light source(s) Fix the light source(s) and move the object(s) Move the light source(s) and object(s) independently

Material Properties Material properties are also part of the OpenGL state and match the terms in the modified Phong model Set by glMaterialv() 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);

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 in glMaterialf back faces not visible back faces visible

Emissive Term We can simulate a light source in OpenGL by giving a material an emissive component This component is unaffected by any sources or transformations GLfloat emission[] = 0.0, 0.3, 0.3, 1.0); glMaterialf(GL_FRONT, GL_EMISSION, emission);

Transparency Material properties are specified as RGBA values The A value can be used to make the surface translucent The default is that all surfaces are opaque regardless of A Later we will enable blending and use this feature

Efficiency Because material properties are part of the state, if we change materials for many surfaces, we can affect performance We can make the code cleaner by defining a material structure and setting all materials during initialization typedef struct materialStruct { GLfloat ambient[4]; GLfloat diffuse[4]; GLfloat specular[4]; GLfloat shineness; } MaterialStruct; We can then select a material by a pointer

Polygonal Shading Shading calculations are done for each vertex Vertex colors become vertex shades By default, vertex shades are interpolated across the polygon glShadeModel(GL_SMOOTH); If we use glShadeModel(GL_FLAT); the color at the first vertex will determine the shade of the whole polygon

Polygon Normals Polygons have a single normal Shades at the vertices as computed by the Phong model can be almost same Identical for a distant viewer (default) or if there is no specular component Consider model of sphere Want different normals at each vertex even though this concept is not quite correct mathematically

Smooth Shading We can set a new normal at each vertex Easy for sphere model If centered at origin n = p Now smooth shading works Note silhouette edge

Mesh Shading The previous example is not general because we knew the normal at each vertex analytically For polygonal models, Gouraud proposed we use the average of the normals around a mesh vertex n = (n1+n2+n3+n4)/ |n1+n2+n3+n4|

Gouraud and Phong Shading Gouraud Shading Find average normal at each vertex (vertex normals) Apply modified Phong model at each vertex Interpolate vertex shades across each polygon Phong shading Find vertex normals Interpolate vertex normals across edges Interpolate edge normals across polygon Apply modified Phong model at each fragment

Comparison If the polygon mesh approximates surfaces with a high curvatures, Phong shading may look smooth while Gouraud shading may show edges Phong shading requires much more work than Gouraud shading Until recently not available in real time systems Now can be done using fragment shaders (see Chapter 9) Both need data structures to represent meshes so we can obtain vertex normals

End .