1 Graphics CSCI 343, Fall 2015 Lecture 21 Lighting and Shading III.

Slides:



Advertisements
Similar presentations
Computer Graphics I, Fall 2010 Shading II.
Advertisements

1 Graphics CSCI 343, Fall 2013 Lecture 18 Lighting and Shading.
CS 480/680 Computer Graphics Shading 2 Dr. Frederick C Harris, Jr.
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.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner Lighting/Shading III Week.
Shading in OpenGL CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Shading II CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Computer Graphics (Fall 2005) COMS 4160, Lecture 16: Illumination and Shading 1
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.
6.1 Vis_04 Data Visualization Lecture 6 - A Rough Guide to Rendering.
Illumination and Shading
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.
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.
Polygon Shading. Assigning color to a shape to make graphical scenes look realistic, or artistic, or whatever effect we’re attempting to achieve But first.
COMPUTER GRAPHICS CS 482 – FALL 2014 AUGUST 27, 2014 FIXED-FUNCTION 3D GRAPHICS MESH SPECIFICATION LIGHTING SPECIFICATION REFLECTION SHADING HIERARCHICAL.
Shading in OpenGL.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Computer Graphics I, Fall 2010 Shading in OpenGL.
1 Illumination and Shading Day 6, 2013 © Jeff Parker.
Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Basic Rendering Pipeline and Shading Spring 2012.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Illumination and Shading
Course Introduction to virtual engineering Óbuda University John von Neumann Faculty of Informatics Institute of Applied Mathematics Lecture and laboratory.
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 325 Introduction to Computer Graphics 03 / 29 / 2010 Instructor: Michael Eckmann.
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in WebGL.
Introduction to Computer Graphics with WebGL
Shading. For Further Reading Angel 7 th Ed: ­Chapter 6 2.
Specular Reflection Lecture 27 Mon, Nov 10, 2003.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
Day 06 Vertex Shader for Ambient-Diffuse-Specular Lighting.
Lighting and Shading Part 2. Global Ambient Light There are at least 8 OpenGL lights and 1 Global Ambient Setting the Global Ambient globalAmbient[] =
Illumination and Shading Sang Il Park Sejong University.
Lighting and Reflection Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Advanced Texture Mapping Bump Mapping & Environment Mapping (Reflection)
Illumination Models. Introduction 1 Illumination model: Given a point on a surface, what is the perceived color and intensity? Known as Lighting Model,
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.
Introduction to Computer Graphics with WebGL
Tips for Shading Your Terrain
Unit-7 Lighting and Shading
CSC461: Lecture 23 Shading Computation
Vectors, Normals, & Shading
Shading in OpenGL Ed Angel
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
CSE 470 Introduction to Computer Graphics Arizona State University
Lighting – Light Sources
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Last Time Liang-Barsky Details Weiler-Atherton clipping algorithm
Advanced Texture Mapping
CS 480/680 Computer Graphics Shading.
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Introduction to Computer Graphics with WebGL
Lighting Calculations
Presentation transcript:

1 Graphics CSCI 343, Fall 2015 Lecture 21 Lighting and Shading III

2 The halfway vector Computing r takes a lot of computation. We can approximate the angle between r and v by computing the halfway vector. l n r v ii  h  The angle between n and h, , is 1/2 the angle between r and v, . What is  in terms of  ? We can use  instead of  in our calculation of specular reflection: We adjust  ' to account for the fact that  <  2  = 

3 Example of computing h Compute h from the following l and v:

4 The Phong reflection model and OpenGL The Phong reflection model: In webGL we must specify: 1) Lighting properties: Ld, Ls and La 2) Light position (or direction): l 3) Material reflectance properties: kd, ks, ka,  4) Surface normals

5 Flat Shading In older versions of OpenGL, it was more efficient to use flat shading, which specifies a single color for each polygonal face of an object. In flat shading, the light source is modeled as a distant source. Each point on each plane has the same light hitting it and so each plane is a uniform color. In this case, we would specify each surface normal once, and associate it with the vertices of a corresponding polygon. With vertex shaders in the newer versions of OpenGL and WebGL, we now can do the shading computation much more quickly with each vertex, so flat shading is not necessary.

6 Problem with Flat Shading Flat shading models a distant viewer and a distant light source. If the polygon is flat, then n, l, and v are constant over the entire polygon. Older versions of OpenGL used the surface normal associated with the first vertex to specify the normals for the entire polygon. Problem: The changes in lighting look too abrupt. Sometimes see light and dark lines at edges (Mach bands), because of the way the visual system processes the intensity changes. l l l v v v Sharp Edge I x I x IntensityAppearance

7 Specify Light properties Specify values of the ambient, diffuse and specular light parameters as well as the light position in the application (javaScript) file: var lightAmbient = vec4(0.2, 0.2, 0.2, 1.0 ); var lightDiffuse = vec4( 1.0, 1.0, 1.0, 1.0 ); var lightSpecular = vec4( 1.0, 1.0, 1.0, 1.0 ); var lightPosition = vec4(1.0, 1.0, 1.0, 0.0 );

8 Distant vs. Nearby light sources For distant light sources, we only need to specify the direction, because the light rays hitting the surface are all approximately parallel: postion = vec4(1.0, 1.0, 1.0, 0.0);//Note w is zero. For nearby light sources, we must specify position explicitly. postion = vec4(100.0, 100.0, 100.0, 1.0);//Note w is one. Distant sourceNearby source

9 Specifying Material Properties Specify the material properties (reflection of ambient, diffuse and specular light components, as well as the shininess) in the javaScript file: var materialAmbient = vec4( 1.0, 0.0, 1.0, 1.0 ); var materialDiffuse = vec4( 1.0, 0.8, 0.0, 1.0); var materialSpecular = vec4( 1.0, 0.8, 0.0, 1.0 ); var materialShininess = 100.0; Compute the product of the light and material properties: ambientProduct = mult(lightAmbient, materialAmbient); diffuseProduct = mult(lightDiffuse, materialDiffuse); specularProduct = mult(lightSpecular, materialSpecular);

10 Send lighting and Material information to the shaders gl.uniform4fv(gl.getUniformLocation(program, "ambientProduct"), flatten(ambientProduct)); gl.uniform4fv(gl.getUniformLocation(program, "diffuseProduct"), flatten(diffuseProduct) ); gl.uniform4fv(gl.getUniformLocation(program, "specularProduct"), flatten(specularProduct) ); gl.uniform4fv(gl.getUniformLocation(program, "lightPosition"), flatten(lightPosition) ); gl.uniform1f(gl.getUniformLocation(program, "shininess"),materialShininess);

11 Specifying the Normal vectors (-0.5, -0.5, 0.5) (0.5, 0.5, -0.5) The normals for a box with faces parallel to the x-y, y-z and z-x planes are the unit vectors along the x, y or z axes, in the positive or negative directions. n 1 = (1.0, 0.0, 0.0) n 4 = (0.0, 1.0, 0.0) n 0 = (0.0, 0.0, 1.0)

12 Set up vertices and normals var vertices = [ vec4( -0.5, -0.5, 0.5, 1.0 ), vec4( -0.5, 0.5, 0.5, 1.0 ), vec4( 0.5, 0.5, 0.5, 1.0 ), vec4( 0.5, -0.5, 0.5, 1.0 ), vec4( -0.5, -0.5, -0.5, 1.0 ),vec4( -0.5, 0.5, -0.5, 1.0 ), vec4( 0.5, 0.5, -0.5, 1.0 ), vec4( 0.5, -0.5, -0.5, 1.0 ) ]; var normals = [ vec3(0.0, 0.0, 1.0), vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, -1.0), vec3(-1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, -1.0, 0.0) ];

13 Calculating Surface Normals Not all surface normals are as easy to compute as for the cube. We can calculate surface normal for any flat surface from 3 of its vertices. p0p0 p1p1 p3p3 Calculating the cross product: (p 1 - p 0 ) x (p 3 - p 0 ) = ? vertices from cube

14 Code for computing the cross product /* Compute (b - a) x (c - a). Store the result in d. After computing the cross product, normalize the length of d to 1. */ void cross(point3 a, point3 b, point3 c, point3 d){ //we will complete this code in class. normalize(d);//Set length to one. Not technically // part of cross product computation } a b c

15 Using MV.js functions to compute normals We can use functions from MV.js from the textbook to compute normals given three vertices (a, b and c): function quad(a, b, c, d) { var t1 = subtract(vertices[b], vertices[a]); var t2 = subtract(vertices[c], vertices[b]); var normal = cross(t1, t2); var normal = vec3(normal); //continued next slide

16 Create an array of normals corresponding to vertices //quad( ) function continued pointsArray.push(vertices[a]); normalsArray.push(normal); pointsArray.push(vertices[b]); normalsArray.push(normal); pointsArray.push(vertices[c]); normalsArray.push(normal); pointsArray.push(vertices[a]); normalsArray.push(normal); pointsArray.push(vertices[c]); normalsArray.push(normal); pointsArray.push(vertices[d]); normalsArray.push(normal); } //end of quad( )

17 Sending the Normal Array to the Vertex Shader As with the colors, we set up a buffer for the normal array and attach it to our shader attribute, vNormal: var nBuffer = gl.createBuffer(); gl.bindBuffer( gl.ARRAY_BUFFER, nBuffer ); gl.bufferData( gl.ARRAY_BUFFER, flatten(normalsArray), gl.STATIC_DRAW ); var vNormal = gl.getAttribLocation( program, "vNormal" ); gl.vertexAttribPointer( vNormal, 3, gl.FLOAT, false, 0, 0 ); gl.enableVertexAttribArray( vNormal );

18 The Vertex Shader The vertex shader does the calculation of the fragment colors based on the lighting and normal information sent from the Javascript application. Next time: The vertex shader code.