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

Similar documents
Computer Graphics (CS 4731) Lecture 18: Lighting, Shading and Materials (Part 3)

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

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

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

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

CS 4731: Computer Graphics Lecture 16: Phong Illumination and Shading. Emmanuel Agu

Illumination and Shading

CS452/552; EE465/505. Lighting & Shading

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

Introduction to Computer Graphics with WebGL

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

Illumination and Shading

CS 4600 Fall Utah School of Computing

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

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

Illumination & Shading I

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

Illumination Models & Shading

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

CS5620 Intro to Computer Graphics

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

CS Surface Rendering

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

CS 432 Interactive Computer Graphics

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

Recall: Indexing into Cube Map

Computer Graphics. Illumination and Shading

Shading and Illumination

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

Shadows. Prof. George Wolberg Dept. of Computer Science City College of New York

Shading. Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller/Fuhrmann

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

CS Illumination and Shading. Slide 1

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

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

9. Illumination and Shading

Why we need shading?

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

Computer Graphics Coursework 1

CEng 477 Introduction to Computer Graphics Fall

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

Shading , Fall 2004 Nancy Pollard Mark Tomczak

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

INF3320 Computer Graphics and Discrete Geometry

Computer Graphics (CS 543) Lecture 8c: Environment Mapping (Reflections and Refractions)

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

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

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

The Rasterization Pipeline

CPSC 314 LIGHTING AND SHADING

Shading 1: basics Christian Miller CS Fall 2011

Pipeline Operations. CS 4620 Lecture 14

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

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

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

How do we draw a picture?

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

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

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

Illumination and Shading

[175 points] The purpose of this assignment is to give you practice with shaders in OpenGL.

Computer Graphics. Illumination and Shading

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Lab 9 - Metal and Glass

Shading. Brian Curless CSE 457 Spring 2017

CS452/552; EE465/505. Intro to Lighting

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

Illumination and Shading

CS559 Computer Graphics Fall 2015

CS 325 Computer Graphics

w Foley, Section16.1 Reading

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

Chapter 7 - Light, Materials, Appearance

Painter s HSR Algorithm

5.2 Shading in OpenGL

Light Sources. Spotlight model

Shading. CSE 457 Winter 2015

INFOGR Computer Graphics

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

CSE Real Time Rendering Week 10

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

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

Pipeline Operations. CS 4620 Lecture 10

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?

Visualisatie BMT. Rendering. Arjan Kok

Lighting/Shading III. Week 7, Wed Mar 3

CPSC / Illumination and Shading

蔡侑庭 (Yu-Ting Tsai) National Chiao Tung University, Taiwan. Prof. Wen-Chieh Lin s CG Slides OpenGL 2.1 Specification

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

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

Shading. Brian Curless CSE 457 Spring 2015

Lecture 15: Shading-I. CITS3003 Graphics & Animation

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

Lighting and Shading Computer Graphics I Lecture 7. Light Sources Phong Illumination Model Normal Vectors [Angel, Ch

CS 432 Interactive Computer Graphics

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

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

Transcription:

Computer Graphics (CS 543) Lecture 8a: Per-Vertex lighting, Shading and Per-Fragment lighting Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI)

Computation of Vectors To calculate lighting at vertex P Need l, n, r and v vectors at vertex P User specifies: Light position Viewer (camera) position Vertex (mesh position) l: Light position vertex position v: Viewer position vertex position n: Newell method Normalize all vectors!

Specifying a Point Light Source For each light source component, set RGBA alpha = transparency Red Green Blue Alpha vec4 diffuse0 =vec4(1.0, 0.0, 0.0, 1.0); vec4 ambient0 = vec4(1.0, 0.0, 0.0, 1.0); vec4 specular0 = vec4(1.0, 0.0, 0.0, 1.0); vec4 light0_pos =vec4(1.0, 2.0, 3,0, 1.0); x y z w Light position is in homogeneous coordinates vec4 light0_pos =vec4(1.0, 2.0, 3,0, 1.0); x y z w

Recall: Mirror Direction Vector r Can compute r from l and n l, n and r are co-planar r = 2 (l n ) n - l

Finding Normal, n Normal calculation in application. E.g. Newell method Passed to vertex shader OpenGL Application Calculates n n vertex Shader

Material Shininess (alpha in specular) Material Properties OpenGL Normal, material, shading functions deprecated (glnormal, glmaterial, gllight) deprecated Specify material properties of scene object ambient, diffuse, specular (RGBA) w component gives opacity (transparency) Default? all surfaces are opaque Red Green Blue Opacity vec4 ambient = vec4(0.2, 0.2, 0.2, 1.0); vec4 diffuse = vec4(1.0, 0.8, 0.0, 1.0); vec4 specular = vec4(1.0, 1.0, 1.0, 1.0); GLfloat shine = 100.0

Recall: CTM Matrix passed into Shader Recall: CTM matrix concatenated in application mat4 ctm = ctm * LookAt(vec4 eye, vec4 at, vec4 up); CTM matrix passed in contains object transform + Camera Connected to matrix ModelView in shader OpenGL Application Builds CTM CTM vertex Shader in vec4 vposition; Uniform mat4 ModelView ; CTM passed in main( ) { // Transform vertex position into eye coordinates vec3 pos = (ModelView * vposition).xyz;.. }

Per-Vertex Lighting: Declare Variables Note: Phong lighting calculated at EACH VERTEX!! // vertex shader in vec4 vposition; in vec3 vnormal; out vec4 color; //vertex shade Ambient, diffuse, specular (light * reflectivity) specified by user // light and material properties uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct; uniform mat4 ModelView; uniform mat4 Projection; uniform vec4 LightPosition; k a I a k d I d k s I s uniform float Shininess; exponent of specular term

Per-Vertex Lighting: Compute Vectors CTM transforms vertex position into eye coordinates Eye coordinates? Object, light distances measured from eye void main( ) { // Transform vertex position into eye coordinates vec3 pos = (ModelView * vposition).xyz; vec3 L = normalize( LightPosition.xyz - pos ); // light Vector vec3 E = normalize( -pos ); // view Vector vec3 H = normalize( L + E ); // halfway Vector // Transform vertex normal into eye coordinates vec3 N = normalize( ModelView*vec4(vNormal, 0.0) ).xyz; GLSL normalize function Why not 1.0?

Per-Vertex Lighting: Calculate Components // Compute terms in the illumination equation vec4 ambient = AmbientProduct; k a I a float cos_theta = max( dot(l, N), 0.0 ); vec4 diffuse = cos_theta * DiffuseProduct; k d I d l n float cos_phi = pow( max(dot(n, H), 0.0), Shininess ); vec4 specular = cos_phi * SpecularProduct; k s I s (n h ) b if( dot(l, N) < 0.0 ) specular = vec4(0.0, 0.0, 0.0, 1.0); gl_position = Projection * ModelView * vposition; } color = ambient + diffuse + specular; color.a = 1.0; I = k a I a + k d I d l n + k s I s (n h ) b

Per-Vertex Lighting Shaders IV // in vertex shader, we declared color as out, set it. color = ambient + diffuse + specular; color.a = 1.0; } // in fragment shader ( in vec4 color; void main() { gl_fragcolor = color; } color set in vertex shader Graphics Hardware color used in fragment shader

Spotlights Derive from point source Direction I (of lobe center) Cutoff: No light outside Attenuation: Proportional to cos a f - f

Shading

Shading? After triangle is rasterized/drawn Per-vertex lighting calculation means we know color of pixels at vertices (red dots) Shading determines color of interior surface pixels Shading I = k d I d l n + k s I s (n h ) b + k a I a Lighting calculation at vertices (in vertex shader)

Shading? Two types of shading Assume linear change => interpolate (Smooth shading) No interpolation (Flat shading) Shading I = k d I d l n + k s I s (n h ) b + k a I a Lighting calculation at vertices (in vertex shader) Hardware unit between vertex and fragment units does shading

Flat Shading compute lighting once for each face, assign color to whole face Benefit: Fast!!

Flat shading Used when: Polygon is small enough Light source is far away (why?) Eye is very far away (why?) Previous OpenGL command: glshademodel(gl_flat) deprecated!

Mach Band Effect Flat shading suffers from mach band effect Mach band effect human eyes amplify discontinuity at the boundary perceived intensity Side view of a polygonal surface

Smooth shading Fix mach band effect remove edge discontinuity Compute lighting for more points on each face 2 popular methods: Gouraud shading (or per vertex lighting) Phong shading (or per pixel lighting) Flat shading Smooth shading

Gouraud Shading Lighting calculated for each polygon vertex Colors are interpolated for interior pixels Interpolation? Assume linear change across face Gouraud shading (interpolation) is OpenGL default

Flat Shading Implementation Default is smooth shading Colors set in vertex shader interpolated Flat shading? Prevent color interpolation In vertex shader, add keyword flat to output color flat out vec4 color; //vertex shade color = ambient + diffuse + specular; color.a = 1.0;

Flat Shading Implementation Also, in fragment shader, add keyword flat to color received from vertex shader flat in vec4 color; void main() { gl_fragcolor = color; }

Gouraud Shading Compute vertex color in vertex shader Shade interior pixels: vertex color interpolation C1 Ca = lerp(c1, C2) Cb = lerp(c1, C3) for all scanlines C2 C3 Lerp(Ca, Cb) * lerp: linear interpolation

Linear interpolation Example v1 a b x If a = 60, b = 40 RGB color at v1 = (0.1, 0.4, 0.2) RGB color at v2 = (0.15, 0.3, 0.5) Red value of v1 = 0.1, red value of v2 = 0.15 v2 60 40 0.1 x 0.15 Red value of x = 40 /100 * 0.1 + 60/100 * 0.15 = 0.04 + 0.09 = 0.13 Similar calculations for Green and Blue values

Gouraud Shading Interpolate triangle color 1. Interpolate using y distance of end points (green dots) to get color of two end points in scanline (red dots) 2. Interpolate using x distance of two ends of scanline (red dots) to get color of pixel (blue dot) Interpolate using y values Interpolate using x values

Gouraud Shading Function (Pg. 433 of Hill) for(int y = y bott ; y < y top ; y++) // for each scan line { find x left and x right } find color left and color right color inc = (color right color left )/ (x right x left ) for(int x = x left, c = color left ; x < x right ; x++, c+ = color inc ) { put c into the pixel at (x, y) } y top x left,color left x right,color right y bott

Gouraud Shading Implemenation Vertex lighting interpolated across entire face pixels if passed to fragment shader in following way 1. Vertex shader: Calculate output color in vertex shader, Declare output vertex color as out I = k d I d l n + k s I s (n h ) b + k a I a 2. Fragment shader: Declare color as in, use it, already interpolated!!

Calculating Normals for Meshes For meshes, already know how to calculate face normals (e.g. Using Newell method) For polygonal models, Gouraud proposed using average of normals around a mesh vertex n = (n 1 +n 2 +n 3 +n 4 )/ n 1 +n 2 +n 3 +n 4

Gouraud Shading Problem Assumes linear change across face If polygon mesh surfaces have high curvatures, Gouraud shading in polygon interior can be inaccurate Phong shading fixes, this, look smooth

Phong Shading Phong shading computes lighting in fragment shader Need vectors n, l, v, r for each pixels not provided by user Instead of interpolating vertex color Interpolate vertex normal and vectors Use pixel vertex normal and vectors to calculate Phong lighting at pixel (per pixel lighting)

Phong Shading (Per Fragment) Normal interpolation (also interpolate l,v) n1 na = lerp(n1, n2) nb = lerp(n1, n3) lerp(na, nb) n2 n3 At each pixel, need to interpolate Normals (n) and vectors v and l

Gouraud Vs Phong Shading Comparison Phong shading: Set up vectors (l,n,v,h) in vertex shader Move lighting calculation to fragment shaders a. Gouraud Shading Set Vectors (l,n,v,h) Calculate vertex colors Hardware Interpolates Vertex color Read/set fragment color (Already interpolated) I = k d I d l n + k s I s (n h ) b + k a I a b. Phong Shading Set Vectors (l,n,v,h) Hardware Interpolates Vectors (l,n,v,h) Read in vectors (l,n,v,h) (interpolated) Calculate fragment lighting I = k d I d l n + k s I s (n h ) b + k a I a

Per-Fragment Lighting Shaders I // vertex shader in vec4 vposition; in vec3 vnormal; // output values that will be interpolatated per-fragment out vec3 fn; out vec3 fe; out vec3 fl; uniform mat4 ModelView; uniform vec4 LightPosition; uniform mat4 Projection; Declare variables n, v, l as out in vertex shader

Per-Fragment Lighting Shaders II void main() { fn = vnormal; fe = -vposition.xyz; fl = LightPosition.xyz; Set variables n, v, l in vertex shader if( LightPosition.w!= 0.0 ) { fl = LightPosition.xyz - vposition.xyz; } } gl_position = Projection*ModelView*vPosition;

Per-Fragment Lighting Shaders III // fragment shader // per-fragment interpolated values from the vertex shader in vec3 fn; in vec3 fl; Declare vectors n, v, l as in in fragment shader (Hardware interpolates these vectors) in vec3 fe; uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct; uniform mat4 ModelView; uniform vec4 LightPosition; uniform float Shininess;

Per=Fragment Lighting Shaders IV void main() { // Normalize the input lighting vectors vec3 N = normalize(fn); vec3 E = normalize(fe); vec3 L = normalize(fl); Use interpolated variables n, v, l in fragment shader vec3 H = normalize( L + E ); vec4 ambient = AmbientProduct; I = k d I d l n + k s I s (n h ) b + k a I a

Per-Fragment Lighting Shaders V float Kd = max(dot(l, N), 0.0); vec4 diffuse = Kd*DiffuseProduct; Use interpolated variables n, v, l in fragment shader float Ks = pow(max(dot(n, H), 0.0), Shininess); vec4 specular = Ks*SpecularProduct; // discard the specular highlight if the light's behind the vertex if( dot(l, N) < 0.0 ) specular = vec4(0.0, 0.0, 0.0, 1.0); } gl_fragcolor = ambient + diffuse + specular; gl_fragcolor.a = 1.0; I = k d I d l n + k s I s (n h ) b + k a I a

Toon (or Cel) Shading Non-Photorealistic (NPR) effect Shade in bands of color

Toon (or Cel) Shading How? Consider (l n) diffuse term (or cos Θ) term I = k d I d l n + k s I s (n h ) b + k a I a Clamp values to min value of ranges to get toon shading effect l n Value used Between 0.75 and 1 0.75 Between 0.5 and 0.75 0.5 Between 0.25 and 0.5 0.25 Between 0.0 and 0.25 0.0

References Interactive Computer Graphics (6 th edition), Angel and Shreiner Computer Graphics using OpenGL (3 rd edition), Hill and Kelley