To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Foundations of Computer Graphics (Spring 2012)

Similar documents
To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Computer Graphics

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

Computer Graphics. Illumination and Shading

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

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

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

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

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

The Rasterization Pipeline

CS 4600 Fall Utah School of Computing

Shading and Illumination

Computer Graphics (CS 543) Lecture 8a: Per-Vertex lighting, Shading and Per-Fragment lighting

Illumination & Shading I

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

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

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

Lets assume each object has a defined colour. Hence our illumination model is looks unrealistic.

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

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

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Shading 1: basics Christian Miller CS Fall 2011

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Light Sources. Spotlight model

Why we need shading?

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

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

CS452/552; EE465/505. Lighting & Shading

INF3320 Computer Graphics and Discrete Geometry

CS5620 Intro to Computer Graphics

Pipeline Operations. CS 4620 Lecture 14

Lab 9 - Metal and Glass

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

Color and Light CSCI 4229/5229 Computer Graphics Fall 2016

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

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

-=Bui Tuong Phong's Lighting=- University of Utah, but with shaders. Anton Gerdelan Trinity College Dublin

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

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

Illumination Models & Shading

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

CS 5625 Lec 2: Shading Models

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

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

Computer Graphics with OpenGL ES (J. Han) Chapter 6 Fragment shader

CMSC427 Shading Intro. Credit: slides from Dr. Zwicker

w Foley, Section16.1 Reading

Illumination & Shading: Part 1

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

1 of 5 9/10/ :11 PM

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

Spring 2012 Final. CS184 - Foundations of Computer Graphics. University of California at Berkeley

Shading. Brian Curless CSE 457 Spring 2017

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

Objectives. Introduce the OpenGL shading Methods 1) Light and material functions on MV.js 2) per vertex vs per fragment shading 3) Where to carry out

CS 130 Final. Fall 2015

Sign up for crits! Announcments

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

Illumination and Shading

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

CPSC 314 LIGHTING AND SHADING

CS Surface Rendering

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Lecture 15: Shading-I. CITS3003 Graphics & Animation

Illumination and Shading

Lighting/Shading III. Week 7, Wed Mar 3

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

CS452/552; EE465/505. Intro to Lighting

Introduction Rasterization Z-buffering Shading. Graphics 2012/2013, 4th quarter. Lecture 09: graphics pipeline (rasterization and shading)

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

Shading. Brian Curless CSE 557 Autumn 2017

Comp 410/510 Computer Graphics. Spring Shading

CS 184: Assignment 2 Scene Viewer

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

CS 418: Interactive Computer Graphics. Basic Shading in WebGL. Eric Shaffer

Shading. Brian Curless CSE 457 Spring 2015

CEng 477 Introduction to Computer Graphics Fall

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Shading. CSE 457 Winter 2015

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

OpenGL Lighting Computer Graphics Spring Frank Palermo

CGT520 Lighting. Lighting. T-vertices. Normal vector. Color of an object can be specified 1) Explicitly as a color buffer

Introduction to 3D Graphics

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

LIGHTING AND SHADING

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Programmable Graphics Hardware

Shading. Slides by Ulf Assarsson and Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology

Virtual Reality for Human Computer Interaction

Pipeline Operations. CS 4620 Lecture 10

Introduction to Visualization and Computer Graphics

12.2 Programmable Graphics Hardware

Phong Lighting & Materials. Some slides modified from: David Kabala Others from: Andries Van Damm, Brown Univ.

CS230 : Computer Graphics Lighting and Shading. Tamar Shinar Computer Science & Engineering UC Riverside

Transcription:

Foundations of Computer Graphics (Spring 2012) CS 184, Lecture 7: OpenGL Shading http://inst.eecs.berkeley.edu/~cs184 To Do Submit HW 1 (due tomorrow) Must include partners for HW 2 (speak with TA to find partner if needed). HW 2 is more difficult This week s lectures should have all information Start thinking about HW 3 (also groups of 2) 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 Lighting on teapot Blue, red highlights Diffuse shading Texture on floor Update as we move Demo for mytest3 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 1

Outline 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 ) y 1 y s y 2 Gouraud Shading Details I 2 Ia I 1 I p I ( y y ) I ( y ys) I a I b 1 s 2 2 1 y1 y2 I ( y y ) I ( y ys) I b 1 s 3 3 1 y1 y3 Ia( xb xp) Ib( xp xa) Ip xb xa Scan line 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. y 3 Actual implementation efficient: difference equations while scan converting I 3 I 1 = 0 I 2 = 0 area of desired highlight Phong Illumination Model 2 Phongs make a Highlight 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 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 = 0 I 2 = 0 Roughness 2

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 ; http://blog.cryos.net/categories/15-avogadro/p3.html http://blenderartists.org/forum/showthread.php?11430-games-amp-tutorials-(updated-jan-5-2011) 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 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) 1 atten k kd k d c l q 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 2 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 3

Emissive Term I Emission material 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 I Ambient Diffuse Term Rough matte (technically Lambertian) surfaces Light reflects equally in all directions -L N I N L Diffuse Term Rough matte (technically Lambertian) surfaces Light reflects equally in all directions -L N I N L n I intensitylight i * diffusematerial * atteni *[max( L N,0)] i 0 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 4

Idea of Phong Illumination Find a simple way to create highlights that are viewdependent 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 -L Phong Formula I ( R E) p R? R L 2( L N) N R E Alternative: Half-Angle (Blinn( Blinn-Phong) I ( N H) p N H Demo in mytest3 What happens when we make surface less shiny? n I intensity * specular * atten *[max( N H,0)] i 0 light i material i shininess Diffuse and specular components for most materials Outline # version 120 Fragment Shader Setup // 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. 5

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 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) ; 6

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 0 0 0 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 Shader Mappings in init 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. 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") ; 7