Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 7: OpenGL Shading

Slides:



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

2 COEN Computer Graphics I Evening’s Goals n Discuss the fundamentals of lighting in computer graphics n Analyze OpenGL’s lighting model n Show.
Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
1 Graphics CSCI 343, Fall 2013 Lecture 18 Lighting and Shading.
Computer Graphics (Fall 2008) COMS 4160, Lecture 18: Illumination and Shading 1
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
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.
Rendering (彩現 渲染).
IMGD 1001: Illumination by Mark Claypool
Computer Graphics (Fall 2005) COMS 4160, Lecture 16: Illumination and Shading 1
Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 11: OpenGL 3
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.
Computer Graphics (Spring 2008) COMS 4160, Lecture 15: Illumination and Shading
1 CSCE 641: Computer Graphics Lighting Jinxiang Chai.
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. Illumination (Lighting) Model the interaction of light with surface points to determine their final color and brightness OpenGL.
Lighting & 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.
SET09115 Intro Graphics Programming
Computer Graphics Lighting.
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.
Shading in OpenGL.
19/17/ :25 UML Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera Real Light Synthetic.
Shading and Illumination. OpenGL Shading Without ShadingWith Shading.
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.
Real-Time High Quality Rendering CSE 291 [Winter 2015], Lecture 4 Brief Intro to Programmable Shaders
Rendering Overview CSE 3541 Matt Boggus. Rendering Algorithmically generating a 2D image from 3D models Raster graphics.
Taku KomuraComputer Graphics Local Illumination and Shading Computer Graphics – Lecture 10 Taku Komura Institute for Perception, Action.
Advanced Computer Graphics Advanced Shaders CO2409 Computer Graphics Week 16.
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.
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.
11/5/2002 (c) University of Wisconsin, CS 559 Last Time Local Shading –Diffuse term –Specular term –All together –OpenGL brief overview.
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.
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.
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.
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.
David Luebke3/16/2016 CS 551 / 645: Introductory Computer Graphics David Luebke
1 CSCE 441: Computer Graphics Lighting Jinxiang Chai.
Computer Graphics Ken-Yi Lee National Taiwan University (the slides are adapted from Bing-Yi Chen and Yung-Yu Chuang)
Computer Graphics (Fall 2006) COMS 4160, Lecture 16: Illumination and Shading 1
CSE 167 [Win 17], Lecture 7: OpenGL Shading Ravi Ramamoorthi
Shaders, part 2 alexandri zavodny.
© University of Wisconsin, CS559 Spring 2004
Shading To determine the correct shades of color on the surface of graphical objects.
Shading in OpenGL Ed Angel
CSE 470 Introduction to Computer Graphics Arizona State University
Last Time Liang-Barsky Details Weiler-Atherton clipping algorithm
Computer Graphics Material Colours and Lighting
Computer Graphics (Fall 2003)
Computer Graphics Shading in OpenGL
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Presentation transcript:

Foundations of Computer Graphics (Fall 2012) CS 184, Lecture 7: OpenGL Shading

To Do  This week’s lectures have all info for HW 2  Start thinking about HW 4

Methodology for Lecture  Lecture deals with lighting (DEMO for HW 2)  Briefly explain shaders used for mytest3  Do this before explaining code fully so you can start HW 2  Primarily explain with reference to source code  More formal look at lighting and shading later in class  Based on physical units and radiometry

Demo for mytest3  Lighting on teapot  Blue, red highlights  Diffuse shading  Texture on floor  Update as we move

Importance of Lighting  Important to bring out 3D appearance (compare teapot now to in previous demo)  Important for correct shading under lights  The way shading is done also important  Flat: Entire face has single color (normal) from one vertex  Gouraud or smooth: Colors at each vertex, interpolate glShadeModel(GL_FLAT) glShadeModel(GL_SMOOTH )

Brief primer on Color  Red, Green, Blue primary colors  Can be thought of as vertices of a color cube  R+G = Yellow, B+G = Cyan, B+R = Magenta, R+G+B = White  Each color channel (R,G,B) treated separately  RGBA 32 bit mode (8 bits per channel) often used  A is for alpha for transparency if you need it  Colors normalized to 0 to 1 range in OpenGL  Often represented as 0 to 255 in terms of pixel intensities  Also, color index mode (not so important)  More next week

Outline  Gouraud and Phong shading (vertex vs fragment)  Types of lighting, materials and shading  Lights: Point and Directional  Shading: Ambient, Diffuse, Emissive, Specular  Fragment shader for mytest3  HW 2 requires a more general version of this  Source code in display routine

Vertex vs Fragment Shaders  Can use vertex or fragment shaders for lighting  Vertex computations interpolated by rasterizing  Gouraud (smooth) shading, as in mytest1  Flat shading: no interpolation (single color of polygon)  Either compute colors at vertices, interpolate  This is standard in old-style OpenGL  Can be implemented with vertex shaders  Or interpolate normals etc. at vertices  And then shade at each pixel in fragment shader  Phong shading (different from Phong illumination)  More accurate  Wireframe: glPolygonMode (GL_FRONT, GL_LINE)  Also, polygon offsets to superimpose wireframe  Hidden line elimination? (polygons in black…)

Gouraud Shading – Details Scan line Actual implementation efficient: difference equations while scan converting

Gouraud and Errors  I 1 = 0 because (N dot E) is negative.  I 2 = 0 because (N dot L) is negative.  Any interpolation of I 1 and I 2 will be 0. I 1 = 0I 2 = 0 area of desired highlight

Phong Illumination Model  Specular or glossy materials: highlights  Polished floors, glossy paint, whiteboards  For plastics highlight is color of light source (not object)  For metals, highlight depends on surface color  Really, (blurred) reflections of light source Roughness

2 Phongs make a Highlight  Besides the Phong Illumination or Reflectance model, there is a Phong Shading model.  Phong Shading: Instead of interpolating the intensities between vertices, interpolate the normals.  The entire lighting calculation is performed for each pixel, based on the interpolated normal. (Old OpenGL doesn’t do this, but you can and will with current fragment shaders) I 1 = 0I 2 = 0

Examples and Color Plates See OpenGL color plates 1-8 and glsl book

Simple Vertex Shader in mytest3 # version 120 // Mine is an old machine. For version 130 or higher, do // out vec4 color ; // out vec4 mynormal ; // out vec4 myvertex ; // That is certainly more modern varying vec4 color ; varying vec3 mynormal ; varying vec4 myvertex ; void main() { gl_TexCoord[0] = gl_MultiTexCoord0 ; gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex ; color = gl_Color ; mynormal = gl_Normal ; myvertex = gl_Vertex ; }

Outline  Gouraud and Phong shading (vertex vs fragment)  Types of lighting, materials and shading  Lights: Point and Directional  Shading: Ambient, Diffuse, Emissive, Specular  Fragment shader for mytest3  HW 2 requires a more general version of this  Source code in display routine

Lighting and Shading  Rest of this lecture considers lighting  In real world, complex lighting, materials interact  We study this more formally later in class  For now some basic approximations to capture key effects in lighting and shading  Inspired by old OpenGL fixed function pipeline  But remember that’s not physically based

Types of Light Sources  Point  Position, Color  Attenuation (quadratic model)  Attenuation  Usually assume no attenuation (not physically correct)  Quadratic inverse square falloff for point sources  Linear falloff for line sources (tube lights). Why?  No falloff for distant (directional) sources. Why?  Directional (w=0, infinite far away, no attenuation)  Spotlights (not considered in homework)  Spot exponent  Spot cutoff

Material Properties  Need normals (to calculate how much diffuse, specular, find reflected direction and so on)  Usually specify at each vertex, interpolate  GLUT does it automatically for teapots etc  Can do manually for parametric surfaces  Average face normals for more complex shapes  Four terms: Ambient, Diffuse, Specular, Emissive

Emissive Term Only relevant for light sources when looking directly at them Gotcha: must create geometry to actually see light Emission does not in itself affect other lighting calculations

Ambient Term  Hack to simulate multiple bounces, scattering of light  Assume light equally from all directions  Global constant  Never have black pixels

Diffuse Term  Rough matte (technically Lambertian) surfaces  Light reflects equally in all directions N -L

Diffuse Term  Rough matte (technically Lambertian) surfaces  Light reflects equally in all directions N -L

Specular Term  Glossy objects, specular reflections  Light reflects close to mirror direction

Phong Illumination Model  Specular or glossy materials: highlights  Polished floors, glossy paint, whiteboards  For plastics highlight is color of light source (not object)  For metals, highlight depends on surface color  Really, (blurred) reflections of light source Roughness

Idea of Phong Illumination  Find a simple way to create highlights that are view- dependent and happen at about the right place  Not physically based  Use dot product (cosine) of eye and reflection of light direction about surface normal  Alternatively, dot product of half angle and normal  Has greater physical backing. We use this form  Raise cosine lobe to some power to control sharpness or roughness

Phong Formula -L R E

Alternative: Half-Angle (Blinn-Phong)  Diffuse and specular components for most materials H N

Demo in mytest3  What happens when we make surface less shiny?

Outline  Gouraud and Phong shading (vertex vs fragment)  Types of lighting, materials and shading  Lights: Point and Directional  Shading: Ambient, Diffuse, Emissive, Specular  Fragment shader for mytest3  HW 2 requires a more general version of this  Source code in display routine

Fragment Shader Setup # version 120 // Mine is an old machine. For version 130 or higher, do // in vec4 color ; // in vec4 mynormal ; // in vec4 myvertex ; // That is certainly more modern attribute vec4 color ; attribute vec3 mynormal ; attribute vec4 myvertex ; uniform sampler2D tex ; uniform int istex ; uniform int islight ; // are we lighting.

Fragment Shader Variables // Assume light 0 is directional, light 1 is a point light. // Actual light values are passed from the main OpenGL program. // This could of course be fancier. Illustrates a simple idea. uniform vec3 light0dirn ; uniform vec4 light0color ; uniform vec4 light1posn ; uniform vec4 light1color ; // Now, set the material parameters. These could be varying or // bound to a buffer. But for now, I'll just make them uniform. // I use ambient, diffuse, specular, shininess as in OpenGL. // But, ambient is just additive and doesn't multiply the lights. uniform vec4 ambient ; uniform vec4 diffuse ; uniform vec4 specular ; uniform float shininess ;

Fragment Shader Compute Lighting vec4 ComputeLight (const in vec3 direction, const in vec4 lightcolor, const in vec3 normal, const in vec3 halfvec, const in vec4 mydiffuse, const in vec4 myspecular, const in float myshininess) { float nDotL = dot(normal, direction) ; vec4 lambert = mydiffuse * lightcolor * max (nDotL, 0.0) ; float nDotH = dot(normal, halfvec) ; vec4 phong = myspecular * lightcolor * pow (max(nDotH, 0.0), myshininess) ; vec4 retval = lambert + phong ; return retval ; }

Fragment Shader Main Transforms void main (void) { if (istex > 0) gl_FragColor = texture2D(tex, gl_TexCoord[0].st) ; else if (islight == 0) gl_FragColor = color ; else { // They eye is always at (0,0,0) looking down -z axis // Also compute current fragment posn, direction to eye const vec3 eyepos = vec3(0,0,0) ; vec4 _mypos = gl_ModelViewMatrix * myvertex ; vec3 mypos = _mypos.xyz / _mypos.w ; // Dehomogenize vec3 eyedirn = normalize(eyepos - mypos) ; // Compute normal, needed for shading. // Simpler is vec3 normal = normalize(gl_NormalMatrix * mynormal) vec3 _normal = (gl_ModelViewMatrixInverseTranspose*vec4(mynormal,0.0)).xyz ; vec3 normal = normalize(_normal) ;

Fragment Shader Main Routine // Light 0, directional vec3 direction0 = normalize (light0dirn) ; vec3 half0 = normalize (direction0 + eyedirn) ; vec4 col0 = ComputeLight(direction0, light0color, normal, half0, diffuse, specular, shininess) ; // Light 1, point vec3 position = light1posn.xyz / light1posn.w ; vec3 direction1 = normalize (position - mypos) ; // no atten. vec3 half1 = normalize (direction1 + eyedirn) ; vec4 col1 = ComputeLight(direction1, light1color, normal, half1, diffuse, specular, shininess) ; gl_FragColor = ambient + col0 + col1 ; }

Outline  Gouraud and Phong shading (vertex vs fragment)  Types of lighting, materials and shading  Lights: Point and Directional  Shading: Ambient, Diffuse, Emissive, Specular  Fragment shader for mytest3  HW 2 requires a more general version of this  Source code in display routine

Light Set Up (in display) /* New for Demo 3; add lighting effects */ { const GLfloat one[] = {1,1,1,1} ; const GLfloat medium[] = {0.5, 0.5, 0.5, 1}; const GLfloat small[] = {0.2, 0.2, 0.2, 1}; const GLfloat high[] = {100} ; const GLfloat zero[] = {0.0, 0.0, 0.0, 1.0} ; const GLfloat light_specular[] = {1, 0.5, 0, 1}; const GLfloat light_specular1[] = {0, 0.5, 1, 1}; const GLfloat light_direction[] = {0.5, 0, 0, 0}; // Dir lt const GLfloat light_position1[] = {0, -0.5, 0, 1}; GLfloat light0[4], light1[4] ; // Set Light and Material properties for the teapot // Lights are transformed by current modelview matrix. // The shader can't do this globally. So we do so manually. transformvec(light_direction, light0) ; transformvec(light_position1, light1) ;

Moving a Light Source  Lights transform like other geometry  Only modelview matrix (not projection). The only real application where the distinction is important  Types of light motion  Stationary: set the transforms to identity before specifying it  Moving light: Push Matrix, move light, Pop Matrix  Moving light source with viewpoint (attached to camera). Can simply set light to so origin wrt eye coords (make modelview matrix identity before doing this)

Modelview Light Transform  Could also use GLM (but careful of conventions) /* New helper transformation function to transform vector by modelview */ void transformvec (const GLfloat input[4], GLfloat output[4]) { GLfloat modelview[16] ; // in column major order glGetFloatv(GL_MODELVIEW_MATRIX, modelview) ; for (int i = 0 ; i < 4 ; i++) { output[i] = 0 ; for (int j = 0 ; j < 4 ; j++) output[i] += modelview[4*j+i] * input[j] ; }

Set up Lighting for Teapot glUniform3fv(light0dirn, 1, light0) ; glUniform4fv(light0color, 1, light_specular) ; glUniform4fv(light1posn, 1, light1) ; glUniform4fv(light1color, 1, light_specular1) ; // glUniform4fv(light1color, 1, zero) ; glUniform4fv(ambient,1,small) ; glUniform4fv(diffuse,1,medium) ; glUniform4fv(specular,1,one) ; glUniform1fv(shininess,1,high) ; // Enable and Disable everything around the teapot // Generally, we would also need to define normals etc. // But glut already does this for us if (DEMO > 4) glUniform1i(islight,lighting) ; // lighting only teapot.

Shader Mappings in init vertexshader = initshaders(GL_VERTEX_SHADER, "shaders/light.vert") ; fragmentshader = initshaders(GL_FRAGMENT_SHADER, "shaders/light.frag") ; shaderprogram = initprogram(vertexshader, fragmentshader) ; // * NEW * Set up the shader parameter mappings properly for lighting. islight = glGetUniformLocation(shaderprogram,"islight") ; light0dirn = glGetUniformLocation(shaderprogram,"light0dirn") ; light0color = glGetUniformLocation(shaderprogram,"light0color") ; light1posn = glGetUniformLocation(shaderprogram,"light1posn") ; light1color = glGetUniformLocation(shaderprogram,"light1color") ; ambient = glGetUniformLocation(shaderprogram,"ambient") ; diffuse = glGetUniformLocation(shaderprogram,"diffuse") ; specular = glGetUniformLocation(shaderprogram,"specular") ; shininess = glGetUniformLocation(shaderprogram,"shininess") ;