Illumination and Shading ECE 567

Similar documents
Illumination & Shading: Part 1

Illumination and Shading

Topic 9: Lighting & Reflection models 9/10/2016. Spot the differences. Terminology. Two Components of Illumination. Ambient Light Source

Topic 9: Lighting & Reflection models. Lighting & reflection The Phong reflection model diffuse component ambient component specular component

Computer Graphics. Illumination and Shading

Overview. Shading. Shading. Why we need shading. Shading Light-material interactions Phong model Shading polygons Shading in OpenGL

Illumination & Shading

Sung-Eui Yoon ( 윤성의 )

Shading and Illumination

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

Lighting. CSC 7443: Scientific Information Visualization

Why we need shading?

INF3320 Computer Graphics and Discrete Geometry

Reading. Shading. Introduction. An abundance of photons. Required: Angel , Optional: OpenGL red book, chapter 5.

Today s class. Simple shadows Shading Lighting in OpenGL. Informationsteknologi. Wednesday, November 21, 2007 Computer Graphics - Class 10 1

Reading. Shading. An abundance of photons. Introduction. Required: Angel , 6.5, Optional: Angel 6.4 OpenGL red book, chapter 5.

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

CEng 477 Introduction to Computer Graphics Fall

Illumination Model. The governing principles for computing the. Apply the lighting model at a set of points across the entire surface.

Illumination and Shading

Illumination & Shading

Light Transport Baoquan Chen 2017

Today. Global illumination. Shading. Interactive applications. Rendering pipeline. Computergrafik. Shading Introduction Local shading models

CSE 167: Introduction to Computer Graphics Lecture #6: Lights. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2016

Comp 410/510 Computer Graphics. Spring Shading

Illumination & Shading I

w Foley, Section16.1 Reading

Shading. Brian Curless CSE 457 Spring 2015

Objectives. Introduce Phong model Introduce modified Phong model Consider computation of required vectors Discuss polygonal shading.

Shading. CSE 457 Winter 2015

CPSC 314 LIGHTING AND SHADING

Today. Global illumination. Shading. Interactive applications. Rendering pipeline. Computergrafik. Shading Introduction Local shading models

Ambient reflection. Jacobs University Visualization and Computer Graphics Lab : Graphics and Visualization 407

Computer Graphics (CS 543) Lecture 7b: Intro to lighting, Shading and Materials + Phong Lighting Model

CS 4600 Fall Utah School of Computing

Introduction to Computer Graphics 7. Shading

Computer Graphics (CS 4731) Lecture 16: Lighting, Shading and Materials (Part 1)

CS Surface Rendering

Computer Graphics. Shading. Based on slides by Dianna Xu, Bryn Mawr College

Illumination in Computer Graphics

Graphics and Visualization

Ligh%ng and Shading. Ligh%ng and Shading. q Is this a plate or a ball? q What color should I set for each pixel?

Light Sources. Spotlight model

Reflection and Shading

Shading. Why we need shading. Scattering. Shading. Objectives

WHY WE NEED SHADING. Suppose we build a model of a sphere using many polygons and color it with glcolor. We get something like.

Simple Lighting/Illumination Models

Today we will start to look at illumination models in computer graphics

CMSC427 Shading Intro. Credit: slides from Dr. Zwicker

Lighting/Shading III. Week 7, Wed Mar 3

Display Issues Week 5

Shading , Fall 2004 Nancy Pollard Mark Tomczak

CS5620 Intro to Computer Graphics

Virtual Reality for Human Computer Interaction

Shading. Brian Curless CSE 557 Autumn 2017

Shading Intro. Shading & Lighting. Light and Matter. Light and Matter

C O M P U T E R G R A P H I C S. Computer Graphics. Three-Dimensional Graphics V. Guoying Zhao 1 / 65

Shading I Computer Graphics I, Fall 2008

University of British Columbia CPSC 314 Computer Graphics Sep-Dec Tamara Munzner (guest lecturing) Lighting/Shading

Lecture 15: Shading-I. CITS3003 Graphics & Animation

Lessons Learned from HW4. Shading. Objectives. Why we need shading. Shading. Scattering

Illumination Models & Shading

CS130 : Computer Graphics Lecture 8: Lighting and Shading. Tamar Shinar Computer Science & Engineering UC Riverside

Illumination and Shading

Computer Graphics. Illumination Models and Surface-Rendering Methods. Somsak Walairacht, Computer Engineering, KMITL

5.2 Shading in OpenGL

Color and Light CSCI 4229/5229 Computer Graphics Fall 2016

Introduction to Computer Graphics. Farhana Bandukwala, PhD Lecture 14: Light Interacting with Surfaces

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

LIGHTING AND SHADING

Chapter 10. Surface-Rendering Methods. Somsak Walairacht, Computer Engineering, KMITL

CSE 167: Lecture #7: Color and Shading. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

Shading in OpenGL. Outline. Defining and Maintaining Normals. Normalization. Enabling Lighting and Lights. Outline

Lights, Colour and Materials In OpenGL

CSE 167: Lecture #8: Lighting. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

CSE 681 Illumination and Phong Shading

Shading. Reading. Pinhole camera. Basic 3D graphics. Brian Curless CSE 557 Fall Required: Shirley, Chapter 10

Methodology for Lecture. Importance of Lighting. Outline. Shading Models. Brief primer on Color. Foundations of Computer Graphics (Spring 2010)

CSCI 4620/8626. Computer Graphics Illumination Models and Surface Rendering Methods (Chapter 17)

CS452/552; EE465/505. Lighting & Shading

Lighting and Shading II. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

OpenGl Pipeline. triangles, lines, points, images. Per-vertex ops. Primitive assembly. Texturing. Rasterization. Per-fragment ops.

Lighting Models. CS116B Chris Pollett Mar 21, 2004.

Introduction. Lighting model Light reflection model Local illumination model Reflectance model BRDF

S U N G - E U I YO O N, K A I S T R E N D E R I N G F R E E LY A VA I L A B L E O N T H E I N T E R N E T

CS Illumination and Shading. Slide 1

Lighting/Shading II. Week 7, Mon Mar 1

Color and Light. CSCI 4229/5229 Computer Graphics Summer 2008

Virtual Reality for Human Computer Interaction

Local Illumination. CMPT 361 Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller

Shading. Brian Curless CSE 457 Spring 2017

Surface Rendering Methods

CS 148, Summer 2012 Introduction to Computer Graphics and Imaging

CS580: Ray Tracing. Sung-Eui Yoon ( 윤성의 ) Course URL:

Rendering. Illumination Model. Wireframe rendering simple, ambiguous Color filling flat without any 3D information

CS452/552; EE465/505. Intro to Lighting

Lighting affects appearance

CSE 167: Introduction to Computer Graphics Lecture #6: Colors. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2013

Computer Graphics Lighting

Transcription:

Illumination and Shading ECE 567

Overview Lighting Models Ambient light Diffuse light Specular light Shading Models Flat shading Gouraud shading Phong shading OpenGL 2

Introduction To add realism to drawings of 3D scenes need to simulate the lighting of the surfaces in the scene To examine ways to determine how light reflects or scatters from object surfaces surfaces are shaded based on the position, orientation, and characteristics of the surfaces Position and orientation of the light sources 3

How to shade surfaces Illumination models express the factors determining a surface s color at given point. consists of equation for light reflected from all points on object Shading model attempts to model how light that emanates from light sources would interact with objects in a scene some shading models invoke an illumination model for every pixel in the image others invoke an illumination model for only some pixels and shade the remaining pixels by interpolation. 4

How to shade surfaces cont. When light sources shine on various surfaces of the objects, and the incident light interacts with the surface in three different ways: 1. Some is absorbed by the surface and is converted to heat 2. Some is reflected from the surface 3. Some is transmitted into the interior of the object, as in the case of a piece of glass 5

Definition Illumination the transport luminous flux (energy) from light sources between points via direct and indirect paths Lighting the process of computing the luminous intensity reflected from a specified 3-D 3 D point Shading the process of assigning a colors to a pixels 6

Illumination Models Empirical simple formulations that approximate observed phenomenon a model that looks good enough for application and can be calculated efficiently with the resources that are available Physically-based models based on the actual physics of light's interactions with matter takes a long time to compute illumination 7

Components of Illumination Light Sources Emittance Spectrum color of the light Geometry position and direction Directional Attenuation Surface Properties Reflectance Spectrum color of the surface Geometry position, orientation, and micro-structure Absorption 8

Simplifications Most computer graphics systems apply simplifications Only the direct illumination from the emitters to the reflectors of the scene Ignore the geometry of light emitters, and consider only the geometry of reflectors 9

Light Sources Point Light Sources Light bulb Directional Light Sources Sun 10

Point Light Sources rays emitted from a point light radially diverge from the source. rays emit uniformly in all direction. A point light source for a scene defined by giving its position and the color of the emitted light A point light source is a fair approximation to a local light source such as a light bulb. p 11

Point Light Sources Cont. The direction of the light to each point on a surface changes when a point light source is used. p l The object s brightness varies from one part to another depends on the direction of and distance to the light sources a normalized vector to the light emitter is computed for each point that is illuminated d = Intensity attenuates in proportion to distance from source p l p l 12

Directional Light Sources All of the rays from a directional light source have a common direction no point of origin. Directional light source has parallel rays the light source was infinitely far away from the surface that it is illuminating. Sunlight is an example of an infinite light source. 13

Directional Light Sources Cont. The direction from a surface to a light source is important for computing the light reflected from the surface. Attenuation proportional to cosine of angle between normal and light direction No attenuation based on distance from source since energy does not spread out. With a directional light source this direction is a constant for every surface. A directional light source can be colored. 14

Other Light Sources Spotlights Point source whose intensity falls off away from a given direction Requires a color, a point, a direction, parameters that control the rate of fall off Area Light Sources Light source occupies a 2-D 2 D area (usually a polygon or disk) Generates soft shadows Example: fluorescent light panels Extended Light Sources Spherical Light Source Generates soft shadows 15

Lighting Models The light incident on a surface and exiting from a surface is measured in specific terms Ambient Lambert/Diffuse Angle between surface normal and light Phong/Specular Surface normal, light, and viewpoint 16

Ambient Light Even though an object in a scene is not directly lit it will still be visible. because light is reflected indirectly from nearby objects. ambient light source is commonly used to model this indirect illumination. ambient light has no spatial or directional characteristics (independent of the object's position or orientation) the amount of ambient light incident on each object is a constant for all surfaces in the scene. ambient light can have a color. 17

Ambient Light Cont. Ambient light is model as following: reflected is the resulting intensity ambient is the intensity of the ambient light assume to be constant for all objects ambient is the ambient-reflection coefficient ranges from 0 to 1 k ambient is a material property characterizing the material from which the surface is made (surface property) I reflected I ambient k ambient I = k reflected ambient I ambient 18

Ambient Light Cont. Exists in most environments Some light hits surface from all directions An approximation of indirect light/global illumination A scene with an ambient light source: 19

Point Light Sources Cont. Using an ambient and a point light source: 20

Directional Light Sources Cont. The same scene with a directional and an ambient light source 21

Types of reflection Two types of reflection of incident light: Diffuse reflections some of the incident light penetrates the surface slightly re-radiated radiated uniformly in all directions Specular reflections incident light does not penetrate the object reflected directly from its outer surface highly directional More mirror like 22

Types of reflection Cont. Most surfaces produce some combination of the two types of reflection depends on the characteristics of the surfaces roughness type of material from which the surface is made. 23

Diffuse Reflection Object s brightness only depends on: direction of light source distance from light source when light falls from a point source onto one side of a facet (a small piece of a surface). a fraction of light is reradiated diffusely in all directions from that side. the surfaces appear equally bright from all viewing angles the surfaces reflect light with equal intensity in all directions incident intensity proportional to cosine of incident angle 24

Diffuse Reflection Cont. Ideal diffuse reflection An ideal diffuse surface at the microscopic level a very rough surface. Chalk is a good approximation to an ideal diffuse surface. Because the microscopic variations in the surface, an incoming ray of light is equally likely to be reflected in any direction over the hemisphere. A surface that is oriented nearly perpendicular to the illumination ion direction receives more light from the source than a surface that t is tilted at an oblique angle to the direction of the of incoming lightl 25

Lambert's Cosine law Lambert's law states that the reflected energy from a small surface area in a particular direction is proportional to cosine of the angle between that direction and the surface normal. 26

Lambert's Cosine law cont. Ideal diffuse reflectors (Lambertian( reflectors) reflect light according to Lambert's cosine law Lambert's law determines how much of the incoming light energy is reflected. the amount energy that is reflected in any one direction is constant in this model. the reflected intensity is independent of the viewing direction. the intensity depends on the light source's orientation relative to the surface 27

Lambert's Cosine law cont. The angle between the surface normal and the incoming light ray is called the angle of incidence an intensity of the light can be expressed in terms of this angle In practice, vector analysis was used to compute cosine term indirectly. the normal vector and the incoming light vector are normalized (unit length) diffuse shading can be computed as follows: r r I = k I ( n l ) diffuse d light 28

Lambert's Cosine law cont. The I light represents the intensity of the incoming light at the particular wavelength the wavelength determines the light's color The k d represents the diffuse reflectivity of the surface at that wavelength. Diffuse-reflection coefficient To determines the fraction of the incident light that is to be scattered as diffuse reflections A constant value between 0 and 1 A highly reflective surface has a value near 1 To simulate a surface that absorbs most of the incident light, k d is set to a value near 0 Varies from on material to another wood has a higher k d 29

Diffuse Lighting Examples A Lambertian sphere seen at several different lighting angles: These spheres illustrate the diffuse-reflection model as I light =1.0 and k d = 0.4,0.55,0.7,0.85,1.0 30

Ambient and Diffuse Lighting These spheres illustrate the ambient and diffuse-reflection model as I ambient = I light =1.0 and k d = 0.4, k a = 0.0, 0.15, 0.3, 0.45, 0.6 31

Distance Attenuation If two surfaces with the same optical parameters project to overlapping positions, they would be indistinguishable from one another N N 32

Distance Attenuation Cont. As radiant energy from a light source travels outwards through space, its amplitude at any distance d light from the source is attenuated: 1 f = att 2 d light d light, is the distance the light travels from the point light source to the surface Meaning A surface close to the light source receives a higher incident light intensity from that source that a more distant surface 33

Distance Attenuation Cont. To produce realistic lighting effects, the intensity attenuation need take into account. f att models distance from light I diffuse = f att k d I light ( n l) 34

Distance Attenuation Cont. Simplification f att 1 = min( 2 c + c d + c d 1 2 light 2 light c 1, c 2 and c 3 are user-defined constants associated with the light source c 1 prevents blowup due to very close light sources c 2 models linear falloff due to large area light sources c 3 models quadratic falloff due to small, bright sources min prevents f att (d light ) from becoming greater than 1,1) 35

Specular Reflection A second surface type is called a specular reflector. Specular reflection can be observed on any shiny surface Light reflected unequally in different directions Example: highlight on an apple Specular reflection is view dependent. Real objects do not scatter light uniformly in all directions By looking at a shiny surface (polished metal or a glossy car finish) a highlight, or bright spot can be seen bright spot appears on the surface is a function of where the surface is seen from. 36

Specular Reflection Cont. At the microscopic level a specular reflecting surface is very smooth these microscopic surface elements are oriented in the same direction as the surface itself. Specular reflection is merely the mirror reflection of the light source in a surface. it is viewer dependent if you stood in front of a mirror and placed your finger over the reflection of a light, you would expect that you could reposition your head to look around your finger and see the light again. An ideal mirror is a purely specular reflector. 37

Specular Reflection Cont. This model is NOT physically-based does Not attempt to accurately calculate global illumination It does attempt to simulate some of the most important observable effects of common light interactions It can be computed quickly and efficiently still in use today in graphics software and especially in hardware renders N^ ^ L^ R θ θ V ^ φ 38

Snell's Law In order to model specular reflection we need to understand the physics of reflection. Reflection behaves according to Snell's laws which state: The incoming ray, the surface normal, and the reflected ray all lie in a common plane. The angle that the reflected ray forms with the surface normal is i determined by the angle that the incoming ray forms with the surface normal, the relative speeds of light of the mediums in which the incident and reflected rays propagate according to the following expression. n l sin θ = l n r sinθ r 39

Non-ideal Reflectors Specular Reflection is a very special case of Snell's Law incident light's medium and the reflected rays medium is the same. simplify the expression θ = θ l r Snell's law applies only to ideal mirror reflectors. Real materials, other than mirrors and chrome tend to deviate significantly from ideal reflectors. For example: apple is a nonperfect reflectors 40

Non-ideal Reflectors Cont. An empirical model (a crude approximation) is introduced most of the reflected light to travel in the direction of the ideal ray. some of the light to be reflected just slightly offset from the ideal reflected ray (microscopic surface variations) as move farther and farther, in the angular sense, from the reflected ected ray, it expects to see less light reflected. 41

Phong Illumination One function that approximates this fall off is called the Phong Illumination model. this model has no physical basis one of the most commonly used illumination models in computer graphics. I = k I (cosφ) specular s light n shiney the cos φ term maximum when the surface is viewed from the mirror direction falls off to 0 when viewed at 90 degrees away from it. the n shiny controls the rate of this fall off. For shiny surface has large n shiny than a dull surface k s is the material s specular-reflection reflection coefficient Ranges between 0 and 1 42

Effect of n shiney The diagram below shows the how the Phong reflectance drops off based on the viewers angle from the reflected ray for various values of n shiny. As n shiny increases, highlight is more concentrated, surface appears glossier. 43

Computing Phong Illumination Cont. The cos φ term of Phong's specular illumination using the following relationship. I = k I ( Vˆ Rˆ ) specular s The V vector is the unit vector in the direction of the viewer the R vector is the mirror reflectance direction. The vector R can be computed from the incoming light direction and the surface normal as below light n shiny Rˆ = (2( Nˆ Lˆ)) Nˆ Lˆ 44

Computing Phong Illumination Cont. The following figure illustrates this relationship. Rˆ + Lˆ = (2( Nˆ Lˆ)) Nˆ L^ ^ R + L^ L^ ^N ^R ^N L^ 45

Blinn & Torrance Variation Jim Blinn introduced another approach for computing Phong-like illumination based on the work of Ken Torrance. His illumination function uses the following equation: I = k I ( Nˆ Hˆ ) specular s light n shiney In this equation the angle of specular dispersion is computed by how far the surface's normal is from a vector bisecting the incoming light direction and the viewing direction. 46

Blinn & Torrance Variation Cont. The halfway vector Ĥ is given by Hˆ = Lˆ + Vˆ Lˆ + Vˆ L^ N^ ^ H φ ^R V ^ 47

Phong Examples These spheres illustrate the Phong model as L(direction of light source) and n shiny are varied: 48

Phong Lighting Model Intensity from a single light reflected by a single surface element I total = k ambient I ambient + I( k d ( Nˆ Lˆ) + k s ( Vˆ Rˆ) n shiney ) For multiple point lights, simply sum contributions I total = k ambient I ambient + lights i= 1 I i ( k d ( Nˆ Lˆ) + k s ( Vˆ Rˆ) n shiney ) Reflectance coefficients, k a, k d,and k s may or may not vary with the color component 49

Phong Lighting: Intensity Plots 50

Phong Lighting Model(color) Light sources have three types of color Ambient reflection coefficients k ar, k ag, k ab Diffuse reflection coefficients k dr, k dg, k db Specular reflection coefficients k sr, k sg, k sb I I I r g b = = = k k k ar ag ab I I I ar ag ab + + + I I r I b ( k g ( k ( k dr db ( Nˆ dg ( Nˆ ( Nˆ Lˆ) + Lˆ) + Lˆ) + k k sr k sb ( Vˆ sg ( Vˆ ( Vˆ Rˆ) n Rˆ) Rˆ) shiney n n shiney ) shiney ) ) 51

Shading Models Shading a surface Using normal at each surface point, calculate illumination at that point. Need to render each point! Shade polygons using their normals and illumination model Three types of shading models is used to simplify the process Flat Shading Gouraud Shading intensity interpolated across polygon Phong Shading normal vector interpolated across polygon 52

Flat Shading The simplest shading method applies only one illumination calculation for each primitive. This approach applies an illumination model once to determine a single intensity value that is then used to shade an entire polygon (primitive) This approach is valid if several assumptions are true: 1. The light source is at infinity 2. The viewer is infinity 3. The polygon represents the actual surface being modeled and is not an approximation to a curved surface Issues: For point light sources the direction to the light source varies over the facet For specular reflections the direction to the eye varies over the facet 53

Facet Shading Even when the illumination equation is applied at each point of the faceted nature of the polygonal nature is still apparent. To overcome this limitation, normals are introduced at each vertex. Usually different than the polygon normal Used only for shading (not backface culling or other geometric computations) Better approximates the "real" surface Assumes the polygons were a piecewise approximation of the real surface (C 0 ) Normals provide information about the tangent plane at each point (C 1 ) 54

Vertex Normals If vertex normals are not provided they can often be approximated by averaging the normals of the facets which share the vertex. This only works if the polygons reasonably approximate the underlying surface. A better approximation can be found using a clustering analysis of the normals on the unit sphere. 55

Gouraud Shading The Gouraud Shading shading method applies the illumination model on a subset of surface points and interpolates the intensity of the remaining points on the surface. Gouraud shading also called intensity interpolation shading or color interpolation shading, eliminates intensity discontinuities. In the case of a polygonal mesh the illumination model is usually applied at each vertex and the colors in the triangles interior are linearly interpolated from these vertex values. 56

Gouraud Shading Cont. The linear interpolation can be accomplished using the plane equation method. To compute the intensity at a vertex shared by more than one polygon ˆN 1 Nˆ v Nˆ v = Nˆ Nˆ i i ˆN 4 ˆN 2 ˆN 3 57

58 Gouraud Shading Cont. Once the intensities at the vertices are obtained, linear Once the intensities at the vertices are obtained, linear interpolation of vertex intensities provides the shade of interpolation of vertex intensities provides the shade of each polygon each polygon y A D B C x scan line I 1 I 2 I p 1 1 2 1 1 2 2 1 ) ( ) )( ( ) ( ) )( ( ) ( ) )( ( I x x x x I I I I y y y y I I I I y y y y I I I p p D C D S D D C A B A S A A B + = + = + = y A y S y B

Phong Shading Phong shading, also known as normal-vector interpolation shading, interpolates the surface normal vector, rather than the intensity. In Phong shading (not to be confused with Phong's illumination model), the surface normal is linearly interpolated across polygonal facets, and the Illumination model is applied at every point. The surface normal at each vertex find firstly Then blends this normal over the face of the polygon. It next compares the normal with light vector at each point, computing a new shade for each pixel location. 59

Phong Shading Cont. A Phong shader assumes the same input as a Gouraud shader,, which means that it expects a normal for every vertex. Phong shading will usually result in a very smooth appearance 60

Phong Shading Cont. Interpolation across edges first Normals then interpolated across each scan line Illumination model computed for each point in scan line B C Interpolated normal along xy Interpolated normal along CD y Average normal at each vertex of polygon D x A 61

Comparison of different Shading 62

OpenGL OpenGL approximates light and lighting as if light can be broken into red, green, and blue components. The color of light sources is characterized the amount of red, green, and blue light they emit, The material of surfaces is characterized the percentage of the incoming red, green, and blue components that are reflected in various directions. The OpenGL lighting equations are just an approximation works fairly well computes relatively quickly. 63

Defining a light source OpenGL lighting model divides the lighting into four independent components Emitted it originates from an object and is unaffected by any light sources Ambient Comes from all directions strikes a surface, it's scattered equally in all directions. Diffuse comes from one direction scattered equally in all directions specular comes from a particular direction bounce off the surface in a preferred direction All four components are computed independently, and then added together. t 64

Defining Light Sources Up to 8 different lights can be specified GL_LIGHT0, GL_LIGHT1, etc Each source has various properties Such as color, position, and direction gllight*() is used to specify all properties of lights General form: gllightfv(glenum light, GLenum pname,, TYPE *param) gllight(glenum light, GLenum pname,, TYPE param) pname defines characteristic of the light 65

Default Values for pname Parameter of gllight*() Parameter Name GL_AMBIENT GL_DIFFUSE GL_SPECULAR GL_POSITION GL_SPOT_DIRECTION GL_SPOT_EXPONENT GL_SPOT_CUTOFF GL_CONSTANT_ATTENUATION GL_LINEAR_ATTENUATION GL_QUADRATIC_ATTENUATION Default Value (0.0, 0.0, 0.0, 1.0) (1.0, 1.0, 1.0, 1.0) (1.0, 1.0, 1.0, 1.0) (0.0, 0.0, 1.0, 0.0) (0.0, 0.0, -1.0) 0.0 180.0 1.0 0.0 0.0 Meaning ambient RGBA intensity of light diffuse RGBA intensity of light specular RGBA intensity of light (x, y, z, w) position of light (x, y, z) direction of spotlight spotlight exponent spotlight cutoff angle constant attenuation factor linear attenuation factor quadratic attenuation factor 66

Pname Syntax Parameters to be defined for each source 1. Ambient intensity (r,g,b,a) 2. Diffuse intensity (r,g,b,a) 3. Specular color (r,g,b,a) 4. Position (x,y,z,w) 5. Spotlights: spot direction, exponent & cutoff 67

Creating Light Sources Enable lighting glenable(gl_lighting); glenable(gl_light0); // enable this particular light Disable lighting If lighting isn't enabled, the current color is simply mapped onto the current vertex no calculations concerning normals,, light sources, the lighting model, and material properties are performed. gldisable(gl_lighting) 68

Different color-related parameters Ambient component: GLfloat global_ambient[]={.1,.1,.1,1.0} GLfloat ambient[]={1.0,0.0,0.0,1.0} gllightfv(gl_light0,gl_ambient,ambient) gllightfv(gl_light_model_ambient,global_ambient) Diffuse component GLfloat diffuse[]={0.0,1.0,0.0,1.0} gllightfv(gl_light0,gl_diffuse,diffuse) Specular component GLfloat specular[] = { 0.0,1.0,1.0,1.0} gllightfv(gl_light0,gl_specular, (GL_LIGHT0,GL_SPECULAR,specular) 69

Position And Attenuation Position positional light source: w=1.0 directional light source: w=0.0 GLfloat light_pos[]={1.0,2.0,3.0,1.0} gllightfv(gl_light0,gl_position,light_pos) Attenuation The intensity of light decreases as distance from the light increases. a directional light is infinitely far away, attenuation is disabled for a directional light. a positional light OpenGL attenuates a light source by multiplying the contribution of that source by an attenuation factor: 70

Position And Attenuation Cont. By default, k c is 1.0 and both k l and k q are zero gllightf(gl_light0, GL_CONSTANT_ATTENUATION, 2.0) gllightf(gl_light0, GL_LINEAR_ATTENUATION, 1.0) gllightf(gl_light0, GL_QUADRATIC_ATTENUATION, 0.5) attenuation factor = k d k k k c l q c + k l 1 d + k = distance between the light's position and the vertex = GL_CONSTANT_ATTENUATION = GL_LINEAR_ATTENUATION = GL_QUADRATIC_ATTENUATION q d 2 71

Spotlights positional light source can act as a spotlight by restricting the shape of the light it emits to a cone light's intensity is highest in the center of the cone. attenuated toward the edges of the cone by the cosine of the angle between the direction of the light and the direction from the light to the vertex being lighted, raised to the power of the spot exponent. higher spot exponents result in a more focused light source. 72

Spotlights Cont. Spotlight parameters: GL_SPOT_DIRECTION,GL_SPOT_EXPONENT,GL_SPOT_CUTOFF sets the cutoff parameter to 45 degrees gllightf(gl_light0, GL_SPOT_CUTOFF, 45.0) specify a spotlight's direction, which determines the axis of the e cone of light GLfloat spot_direction[] = { -1.0, -1.0, 0.0 } gllightfv(gl_light0, GL_SPOT_DIRECTION, spot_direction) control the intensity distribution of the light within the cone set the attenuation factor set the GL_SPOT_EXPONENT parameter control how concentrated the light is 73

Multiple Lights two lights, one directional and one spotlight GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat light1_ambient[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light1_position[] = { -2.0, 2.0, 1.0, 1.0 }; GLfloat spot_direction[] = { -1.0, -1.0, 0.0 }; gllightfv(gl_light0, GL_POSITION, light_position); gllightfv(gl_light0, GL_SPOT_DIRECTION, spot_direction); gllightfv(gl_light1, GL_AMBIENT, light1_ambient); gllightfv(gl_light1, GL_DIFFUSE, light1_diffuse); gllightfv(gl_light1, GL_SPECULAR, light1_specular); gllightfv(gl_light1, GL_POSITION, light1_position); gllightf(gl_light1, GL_CONSTANT_ATTENUATION, 1.5); gllightf(gl_light1, GL_LINEAR_ATTENUATION, 0.5); gllightf(gl_light1, GL_QUADRATIC_ATTENUATION, 0.2); gllightf(gl_light1, GL_SPOT_CUTOFF, 45.0); gllightf(gl_light1, GL_SPOT_EXPONENT, 2.0); glenable(gl_light0); glenable(gl_light1); 74

Materials Colors OpenGL lighting model makes the approximation that a material's color depends on the percentages of the incoming red, green, and blue light it reflects Example a perfectly red ball reflects all the incoming red light and absorbs all the green and blue light that strikes it. If the ball is viewed in white light (composed of equal amounts of red, green, and blue light), all the red is reflected, a red ball is saw. If the ball is viewed in pure red light, it appears to be red. If the ball is viewed in pure green light, it appears black all the green is absorbed, and there's no incoming red, so 75 no light is reflected

Materials Colors Cont. Materials have different ambient, diffuse, and specular colors, which determine the ambient, diffuse, and specular reflectances of the material. A material's ambient reflectance is combined with the ambient component of each incoming light source Diffuse reflectance with the light's diffuse component Specular reflectance with the light's specular component. 76

Materials Colors Cont. Ambient and diffuse reflectances define the color of the material and are typically similar Specular reflectance is usually white or gray specular highlights end up being the color of the light source's specular intensity. Example: a white light shining on a shiny red plastic sphere, most of the sphere appears red, but the shiny highlight is white. 77

Materials in OpenGL Specify materials for front and back surfaces separately General form: glmaterialfv(glenum face, GLenum pname,, TYPE *param* param); glmaterialf(glenum face, GLenum pname,, TYPE param); face parameter GL_FRONT set the reflection coefficient for font faces GL_BACK set the reflection coefficient for back faces GL_FRONT_AND_BACK set the reflection coefficient for both front and back faces 78

Materials in OpenGL Cont. Default Values for pname Parameter of glmaterial*() Parameter Name GL_AMBIENT GL_DIFFUSE GL_AMBIENT_AND_DIFFUSE GL_SPECULAR GL_SHININESS GL_EMISSION GL_COLOR_INDEXES Default Value (0.2, 0.2, 0.2, 1.0) (0.8, 0.8, 0.8, 1.0) (0.0, 0.0, 0.0, 1.0) 0.0 (0.0, 0.0, 0.0, 1.0) (0,1,1) Meaning ambient color of material diffuse color of material ambient and diffuse color of material specular color of material specular exponent emissive color of material ambient, diffuse, and specular color indices 79

Materials in OpenGL Cont. Ambient component GLfloat ambient[] ={.2,.2,.2,1.0} glmaterialfv(gl_front_and_back,gl_ambient, ambient) Diffuse component GLfloat diffuse[]={1.0,0.8,0.0,1.0} glmaterialfv(gl_front,gl_diffuse,diffuse) Specular component control the size and brightness of the highlight (with GL_SHININESS) Glfloat specular[]={0.0,0.0,1.0,1.0} glmaterialfv(gl_front,gl_specular, (GL_FRONT,GL_SPECULAR,specular) 80

Materials in OpenGL Cont. Shininess the higher the value, the smaller and brighter (more focused) the highlight glmaterialf(gl_front,gl_shininess,80.0) Emission: make an object appear to be giving off light of that color independently of any light source Glfloat emission[] = {.5,0.0,0.0,1.0} glmaterialfv(gl_back,gl_emission,emission) 81

Shading a polygon Specify filling for front/back: glpolygonmode(gl_front_and_back, GL_FILL) Specify a shading model Flat glshademodel(gl_flat); Gouraud glshademodel(gl_smooth) Putting it together: 1. Specify lights 2. Specify shading model 3. Specify material properties for surface 4. Render polygons 82

Example Specify lights Specify shading model Specify material properties for surface Render polygons GLfloat light_position[] = {1.0,1.0,1.0,0.0); gllightfv(gl_light0, GL_POSITION, light_position); glenable(gl_lighting); glenable(gl_light0); glshademodel(gl_smooth); GLfloat mat_specular[] = {1.0,1.0,1.0,1.0}; GLfloat mat_shininess[] = {50.0}; glmaterialfv(gl_front, GL_SPECULAR, mat_specular); glmaterialfv(gl_front, GL_SHININESS, mat_shininess); glenable(gl_depth_test); glbegin(gl_polygon) glnormal3fv(n1); glvertex3fv(v1); glnormal3fv(n2); glvertex3fv(v2); glnormal3fv(n3); glvertex3fv(v3); glend(); 83