EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 3: Lighting and Materials.

Slides:



Advertisements
Similar presentations
1 Graphics CSCI 343, Fall 2013 Lecture 18 Lighting and Shading.
Advertisements

Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive.
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.
Computer Graphics - Class 10
Rendering (彩現 渲染).
IMGD 1001: Illumination by Mark Claypool
3/23/2005 © Dr. Zachary Wartell 1 Illumination Models and Surface- Rendering Methods.
1 CSCE 641: Computer Graphics Lighting Jinxiang Chai.
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.
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
Course Website: Computer Graphics 16: Illumination.
LIGHTING Part One - Theory based on Chapter 6. Lights in the real world Lights bounce off surfaces and reflect colors, scattering light in many directions.
LIGHTING JEFF CHASTINE 1. WHAT IS LIGHT? A very complex process Find a dark area – how is it being lit? Light bounces (mirrors, shiny objects) Light refracts.
SET09115 Intro Graphics Programming
Illumination.
Lecture 5: 3D Rendering Pipeline (II) Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology.
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.
Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 7: OpenGL Shading
19/17/ :25 UML Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera Real Light Synthetic.
CSC418 Computer Graphics n Illumination n Lights n Lightinging models.
Computer Graphics I, Fall 2010 Shading in OpenGL.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Basic Rendering Pipeline and Shading Spring 2012.
CSE 381 – Advanced Game Programming GLSL Lighting.
Lighting Review & Example Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, November 17, 2003.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
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.
CS 450: COMPUTER GRAPHICS BASIC ILLUMINATION SPRING 2015 DR. MICHAEL J. REALE.
Lecture Fall 2001 Illumination and Shading in OpenGL Light Sources Empirical Illumination Shading Transforming Normals Tong-Yee Lee.
UV Mapping After a 3D object has been modeled it must be prepared for texturing. 3D surfaces can be “unwrapped” into a 2D representation with. This process.
Local Illumination and Shading
Day 06 Vertex Shader for Ambient-Diffuse-Specular Lighting.
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[] =
1 CSCE 441: Computer Graphics Lighting Jinxiang Chai.
UW EXTENSION CERTIFICATE PROGRAM IN GAME DEVELOPMENT 2 ND QUARTER: ADVANCED GRAPHICS Lighting.
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.
Illumination and Shading Sang Il Park Sejong University.
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
1 CSCE 441: Computer Graphics Lighting Jinxiang Chai.
Shading NOTE: Some of these slides are from Ed Angel’s presentation at SIGGRAPH February 27, 2008.
David Luebke3/16/2016 CS 551 / 645: Introductory Computer Graphics David Luebke
Computer Graphics Lecture 30 Mathematics of Lighting and Shading - IV Taqdees A. Siddiqi
1 CSCE 441: Computer Graphics Lighting Jinxiang Chai.
Computer Graphics: Illumination
Illumination Models and Surface-Rendering Methods CEng 477 Introduction to Computer Graphics.
Computer Graphics Lecture 26 Mathematics of Lighting and Shading Part II Taqdees A. Siddiqi
Illumination and Shading. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
Chapter 5. Lighting Computer Graphics (spring, 2009) School of Computer Science University of Seoul.
1 Dr. Scott Schaefer Lighting. 2/49 Lighting/Illumination Color is a function of how light reflects from surfaces to the eye Global illumination accounts.
© University of Wisconsin, CS559 Spring 2004
@ 2017 by Jim X. Chen George Mason University
Illumination and Shading
Lighting – Light Sources
Chapter IX Lighting.
Last Time Liang-Barsky Details Weiler-Atherton clipping algorithm
Computer Graphics Material Colours and Lighting
Illumination Model 고려대학교 컴퓨터 그래픽스 연구실.
CS 480/680 Computer Graphics Shading.
CS 480/680 Computer Graphics Shading.
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 3: Lighting and Materials

Classic Lighting Model Adds up set of independently computed lighting components to get total lighting effect Three components: ambient, diffuse, and specular light

Three Types of Light (in more detail) Ambient light – doesn’t come from a particular direction. Is constant throughout the scene. Diffuse light – light that is scattered by the surface equally in all directions for a particular light source. Computation depends on direction of surface normal and direction of light source. Specular highlighting – light reflected directly by the surface. Refers to how much the surface material acts like a mirror. Strength of specular highlighting depends on angle and is known as shininess. Computation depends on direction of surface normal, direction of light source, and direction of eye. Might or might not incorporate surface color.

Vertex and Fragment Shaders: No Lighting No lighting: Just draw objects with color unmodulated by lighting effects. Base to bulid on. Example 7.1: Setting Final Color Values with No Lighting // Vertex shader with no lighting #version 330 core uniform mat4 MVPMatrix; // model-view-projection transform in vec4 VertexColor; // sent from the application, includes alpha in vec4 VertexPosition; // pre-transformed position out vec4 Color; // sent to the rasterizer for interpolation void main() { Color = VertexColor; gl_Position = MVPMatrix * VertexPosition; }

Example 7.1 (continued) // Fragment shader with no lighting #version 330 core in vec4 Color;// interpolated between vertices out vec4 FragColor;// color result for this fragment void main() { FragColor = Color; }

Vertex and Fragment Shaders: Ambient Lighting Example 7.2: Ambient Lightint // Vertex shader for ambient light #version 330 core uniform mat4 MVPMatrix; in vec4 VertexColor; in vec4 VertexPosition; out vec4 Color; void main() { Color = VertexColor; gl_Position = MVPMatrix * VertexPosition; // the light is considered constant // throughout the scene }

Example 7.2 (continued) // Fragment shader for global ambient lighting #version 330 core uniform vec4 Ambient;// sets lighting level, same across many vertices in vec4 Color; out vec4 FragColor; void main() { vec4 scatterLight = Ambient;// this is the only light // modulate surface color with light, but saturate at white FragColor = min(Color * scatterLight, vec4(1.0)); }

Dealing with the alpha value If you are using a vec4 for color, set alpha = 1.0 (as the default value). Alternatively, if you want to use vec3 for color: vec3 scatteredLight = vec3(Ambient); // this is the only light vec3 rgb = min(Color.rgb * scatteredLight, vec3(1.0)); FragColor = vec4(rgb, Color.a); // convert to vec4 for fragment color

Directional Light Source Lighting Directional Light Source Lighting: Far away light Same direction from every point on surface Keep calculations in the range [-90 o, 90 o ] Example 7.3: Directional Light Source Lighting // Vertex shader for a directional light computed in the fragment shader #version330 core uniform mat4 MVPMatrix; uniform mat3 NormalMatrix; // to transform normal vectors, pre-perspective in vec4 VertexColor; // surface normal is now needed in vec3 VertexNormal; out vec4 Color; out vec3 Normal; // interpolate the normalized surface normal

Example 7.3 (continued) void main () { Color = VertexColor; // transform the normal, without perspective, and normalize it Normal = normalize(NormalMatrix * VertexNormal); gl_Position = MVPMatrix * VertexPosition; } // Fragment shader computing lighting for a directional light #version 330 core uniform vec3 Ambient; uniform vec3 LightColor; uniform vec3 LightDirection;// direction toward the light uniform vec3 HalfVector;// surface orientation for shiniest spots uniform float Shininess;// exponent for sharping highlights uniform float Strength;// extra factor to adjust shininess in vec4 Color; in vec3 Normal;// surface normal, interpolated between vertices out vec4 FragColor;

Example 7.3 (continued) void main() { // compute cosine of the directions, using dot products to see how much light would be reflected float diffuse = max(0.0, dot(Normal, LightDirection)); float specular = max(0.0, dot(Normal, HalfVector)); // surfaces facing away from the light (negative dot products) won’t be lit by directional light if (diffuse == 0.0) specular = 0.0; else specular = pow(specular, Shininess);// sharpen the highlight vec3 scatteredLight = Ambient + LightColor * diffuse; vec3 reflectedLight = LightColor * specular * Strength; // don’t modulate the underlying color with reflected light – only with scattered light vec3 rgb = min(Color.rgb * scatteredLight + reflectedLight, vec3(1.0)); FragColor = vec4(rgb, Color.a); }

Point Light Sources Point light sources: Lights near or within scene For point lights, the direction of light is different for each point on the surface. Light received at surface is expected to decrease as the surface gets farther and farther from the light. Attenuation: Fading of reflected light based on increasing distance Light attenuates as the square of the distances.

Example 7.4: Point-Light Source Lighting // Vertex shader for a point-light (local) lighting source, with computation done in the // fragment shader #version 330 core uniform mat4 MVPMatrix; uniform matr4 MVMatrix;// now need the transform, minus perspective uniform mat3 NormalMatrix; in vec4 VertexColor; in vec3 VertexNormal; in vec4 VertexPosition; out vec4 Color; out vec3 Normal; out vec4 Position;// adding position, so we know where we are

Example 7.4: Point-Light Source Lighting (continued) void main() { Color = VertexColor; Normal = normalize(NormalMatrix * VertexNormal); Position = MVMatrix * VertexPosition;// pre-perspective space gl_Position = MVPMatrix * VertexPosition;// includes perspective } // Fragment shader computing a point-light (local) source light #version 330 core uniform vec3 Ambient; uniform vec3 LightColor; uniform vec3 LightPosition;// location of the light, eye space uniform float Shininess; uniform float Strength;

Example 7.4: Point-Light Source Lighting (continued) uniform vec3 EyeDirection; uniform float ConstantAttenuation;// attenuation coefficients uniform float LinearAttenuation; uniform float QuadraticAttenuation; in vec4 Color; in vec3 Normal; in vec4 Position; void main() { // find direction and distance of the light which changes fragmentwise for local light vec3 lightDirection = LightPosition – vec3(Position); float lightDistance = length(lightDirection);

Example 7.4: Point-Light Source Lighting (continued) // normalize the light direction vector so dot products yield cosines lightDirection = lightDirection / lightDistance; // model how much light is available for this fragment float attenuation = 1.0 / (ConstantAttenuation + LinearAttenuation * lightDistance + QuadraticAttenuation * lightDistance * lightDistance); // direction of maximum highlight changes per fragment vec3 halfVector = normalize(lightDirection + EyeDirection); float diffuse = max(0.0, dot(Normal, lightDirection)); float specular = max(0.0, dot(Normal, halfVector));

Example 7.4: Point-Light Source Lighting (continued) if (diffuse == 0.0) specular = 0.0; else specular = pow(specular, Shininess) * Strength; vec3 scatteredLight = Ambient + LightColor * diffuse * attenuation; vec3 reflectedLight = LightColor * specular * attenuation; vec3 rgb = min(Color.rgb * scatteredLight + reflectedLight), vec3(1.0)); FragColor = vec4(rgb, Color.a); } Note: Can leave out some of the attenuation terms. Can also attenuate the Ambient term. Attenuate per-light ambient colors. Could also attenuate global ambient color.

Spotlights Spotlights: project strong beam of light that illuminates a well-defined area. Produce a cone of light in a particular direction. Example 7.5: Spot Lighting // Vertex shader for spotlight computed in fragment shader #version 330 core uniform mat4 MVPMatrix; uniform mat4 MVMatrix; uniform mat3 NormalMatrix; in vec4 VertexColor; in vec3 VertexNormal; in vec4 VertexPosition;

Example 7.5 (continued) out vec4 Color; out vec3 Normal; out vec4 Position; void main() { Color = VertexColor; Normal = normalize(NormalMatrix * VertexNormal); Position = MVMatrix * VertexPosition; gl_Position = MVPMatrix * VertexPosition; }

Example 7.5 (continued) // Fragment shader computing a spotlight’s effect #version 330 core uniform vec3 Ambient; uniform vec3 LightColor; uniform vec3 LightPosition; uniform float Shininess; uniform float Strength; uniform vec3 EyeDirection; uniform float ConstantAttenuation; uniform float LinearAttenuation; uniform float QuadraticAttenuation;

Example 7.5 (continued) in vec4 Color; in vec3 Normal; in vec4 Position; out vec4 FragColor; void main() { vec3 lightDirection = LightPosition – vec3(Position); float lightDistance = length(lightDirection); lightDirection = lightDirection / lightDistance;

Example 7.5 (continued) float attenuation = 1.0 / (ConstantAttenuation + LinearAttenuation * lightDistance + QuadraticAttenuation * lightDistance * lightDistance); // how close are we to being in the splot? float spotCos = dot(lightDirection, -ConeDirection); // attenuate more, based on spot-relative position if (spotCos < spotCosCutoff) attenuation = 0.0; else attenuation *= pow(spotCos, SpotExponent);

Example 7.5 (continued) vec3 halfVector = normalize(lightDirection + EyeDirection); float diffuse = max(0.0, dot(Normal, lightDirection)); float specular = max(0.0, dot(Normal, halfVector)); if (diffuse = 0.0) specular = 0.0; else specular = pow(specular, Shininess) * Strength; vec3 scatteredLight = Ambient + LightColor * diffuse * attenuation; vec3 reflectedLight = LightColor * specular * attenuation; vec3 rgb = min(Color.rgb * scatteredLight + reflectedLight, vec3(1.0)); FragColor = vec4(rgb, Color.a); }

Omit: Example 7.6 – Moving calculations to vertex shader Note: There is flexibility as to whether the calculations appear in the vertex shader or the fragment shader.

Example 7.8: Multiple Mixed Light Sources // Vertex shader for multiple lights stays the same with all lighting done in the vertex shader #version 330 core uniform mat4 MVPMatrix; uniform mat4 MVMatrix; uniform mat3 NormalMatrix; in vec4 VertexColor; in vec3 VertexNormal; in vec4 VertexPosition; out vec4 Color; out vec3 Normal; out vec4 Position;

Example 7.8 (continued) void main() { Color = VertexColor; Normal = normalize(NormalMatrix * VertexNormal); Position = MVMatrix *VertexPosition; gl_Position = MVPMatrix * VertexPosition; } // Fragment shader for multiple lights #version 330 core

Example 7.8 (continued) struct LightProperties {// structure for holding light properties bool isEnabled; bool isLocal; bool isSpot; vec3 ambient; vec3 color; vec3 position; vec3 halfVector; vec3 coneDirection; float splotCosCutoff; float spotExponent; float constantAttenuation; float linearAttenuation; float quadraticAttenuation; };

Example 7.8 (continued) // the set of lights to apply, per invocation of this shader const int MaxLights = 10; uniform LightProperties Lights(MaxLights); uniform float Shininess; uniform float Strength; uniform vec3 EyeDirection; in vec4 Color; in vec3 Normal; in vec4 Position; out vec4 FragColor;

Example 7.8 (continued) void main() { vec3 scatteredLight = vec3(0.0);// or, to a global ambient light vec3 reflectedLight = vec3(0.0); // loop over all the lights for (int light = 0; light < MaxLights; ++light) { if (! Lights[light].isEnabled) continue; vec3 halfVector; vec3 lightDirection = Lights[light].position; float attenuation = 1.0;

Example 7.8 (continued) // for local lights, compute per-fragment direction, halfVector, // and attenuation if (Lights[light].isLocal) { lightDirection = lightDirection = vec3(Position); float lightDistance = length(lightDirection); lightDirection = lightDirection / lightDistance; attenuation = 1.0 / (Lights[light].constantAttenuation + Lights[light].linearAttenuation * lightDistance + Lights[light].quadraticAttenuation * lightDistance * lightDistance);

Example 7.8 (continued) if (Lights[light].isSpot) { float spotCos = dot(lightDirection, - Lights[light].coneDirection); if (spotCos < Lights[light].spotCosCutoff) attenuation = 0.0; else attenuation *= pow(spotCos, Lights[light].spotExponent); } halfVector = normalize(lightDirection + EyeDirection); } else { halfVector = Light[light].halfVector; }

Example 7.8 (continued) float diffuse = max(0.0, dot(Normal, lightDirection)); float specular = max(0.0, dot(Normal, halfVector)); if (diffuse == 0.0) specular = 0.0; else specular = pow(specular, Shininess) * Strength; // Accumulate all the lights’ effects scatteredLight += Lights[light].ambient * attenuation + Lights[light].color * diffuse * attenuation; reflectedLight += Lights[light].color * specular * attenuation; } vec3 rgb = min(Color.rgb * scatteredLight + reflectedLight, vec3(1.0)); FragColor = vec4(rgb, Color.a); }

Material Properties Different materials have differently sized specular highlights. Can also have material-specific modulation of the color of ambient, diffuse, and specular lighting. Types of material properties: emission, ambient, diffuse, specular, shininess Emission – the material emits light Ambient – how ambient light is reflected off the object Diffuse – how diffuse light is reflected off the object Specular – color of the highlight Shininess – specular exponent

Material Properties Example Example 7.10: Code Snippets for Using an Array of Material Properties // Fragment shader – snippets selecting what material to shade with multiple lights #version 330 core struct MaterialProperties { vec3 emission; vec3 ambient; vec3 diffuse; vec3 specular; float shininess; };

Example 7.10 (continued) // a set of materials to select between, per shader invocation Const int NumMaterials = 14; Uniform MaterialProperties Material[NumMaterials]; flat in int MatIndex;// input material index from vertex shader. void main() {.

Example 7.10 (continued) // Accumulate all the lights’ effects scatteredLight += Lights[light].ambient * Material[MatIndex].ambient * attenuation + Lights[light].color * Material[MatIndex].diffuse * diffuse * attenuation; reflectedLight += Lights[light].color * Material[MatIndex].specular * specular * attenuation; } Vec3 rgb = min(Material[MatIndex].emission + Color.rgb * scatteredLight + reflectedLight, vec3(1.0)); FragColor = vec4(rgb, Color.a); }

Advanced Topic: Two-Sighted Lighting See Example 7.11 for an example OpenGL code for this.