Using Shaders for Lighting

Similar documents
Using Shaders for Lighting

Introduction to the OpenGL Shading Language (GLSL)

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Illumination and Shading

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

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

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

Computer Graphics Lighting

Computer Graphics Lighting. Why Do We Care About Lighting?

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

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Illumination Models & Shading

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

Computer Graphics. Illumination and Shading

INFOGR Computer Graphics

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

CS5620 Intro to Computer Graphics

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

Virtual Reality for Human Computer Interaction

CPSC 314 LIGHTING AND SHADING

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

w Foley, Section16.1 Reading

Lab 9 - Metal and Glass

INFOGR Computer Graphics

Models and Architectures

CS Illumination and Shading. Slide 1

The Rasterization Pipeline

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

CPSC / Illumination and Shading

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

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

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

COMP environment mapping Mar. 12, r = 2n(n v) v

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

Computer Graphics Coursework 1

CS452/552; EE465/505. Intro to Lighting

Visualisatie BMT. Rendering. Arjan Kok

Illumination & Shading: Part 1

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

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

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

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

Shading. Brian Curless CSE 457 Spring 2017

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

Light Sources. Spotlight model

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

Illumination & Shading I

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

CEng 477 Introduction to Computer Graphics Fall

Shading and Illumination

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

OpenGL Lighting Computer Graphics Spring Frank Palermo

Programmable Graphics Hardware

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

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

9. Illumination and Shading

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

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

Computer Graphics. Illumination and Shading

Ligh%ng in OpenGL. The Phong Illumina%on Model. Vector Background

CS 4620 Program 3: Pipeline

Shading. Shading = find color values at pixels of screen (when rendering a virtual 3D scene).

Lighting/Shading III. Week 7, Wed Mar 3

Shading I Computer Graphics I, Fall 2008

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

AGDC Per-Pixel Shading. Sim Dietrich

Stripes, Rings, and Dots!

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

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

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

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

Comp 410/510 Computer Graphics. Spring Shading

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

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

SUMMARY. CS380: Introduction to Computer Graphics Texture Mapping Chapter 15. Min H. Kim KAIST School of Computing 18/05/03.

CS452/552; EE465/505. Lighting & Shading

How do we draw a picture?

Illumination and Shading

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

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

CS559 Computer Graphics Fall 2015

Shading. Brian Curless CSE 457 Spring 2015

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

Introduction to the OpenGL Shading Language

INF3320 Computer Graphics and Discrete Geometry

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

Shading. CSE 457 Winter 2015

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

Pipeline Operations. CS 4620 Lecture 14

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

Illumination and Shading

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

Homework 3: Programmable Shaders

Lecture 15: Shading-I. CITS3003 Graphics & Animation

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

12.2 Programmable Graphics Hardware

Shading Languages. Ari Silvennoinen Apri 12, 2004

Transcription:

Using Shaders for Lighting Mike Bailey mjb@cs.oregonstate.edu

Lighting Definitions L N R E N = Normal L = Light vector E = Eye vector R = Light reflection vector ER = Eye reflection vector Color = LightColor * MaterialColor ER Ambient = Light intensity that is everywhere Diffuse = Light intensity proportional to cos(θ) Specular = Light intensity proportional to cos S (Φ) A-D-S = Lighting model that includes Ambient, Diffuse, and Specular Flat Interpolation = Use a single polygon normal to compute one A-D-S for the entire polygon Per-vertex lighting= Compute A-D-S using each vertex normal and then interpolate the sum over the entire polygon Per-fragment lighting = Interpolate the vertex normals across the entire polygon and compute A-D-S at each fragment CubeMap Reflection = Using the Eye Reflection Vector (ER) to look-up the reflection of a wall texture

A-D-S Lighting Ambient: K a Diffuse: K d *cosθ Specular: K s *cos n φ

Ambient-only Diffuse-only Specular-only ADS Shininess=50 ADS Shininess=1000 ADS Shininess=1000 -- Flat

A-D-S Lighting with Flat Interpolation Each facet has a single lighting value applied to every pixel within it. N = Normal L = Light vector E = Eye vector R = Light reflection vector ER = Eye reflection vector Color = LightColor * MaterialColor Vertex Shader Fragment Shader vec3 ambient = Color.rgb; diffuse = max( dot(l,n), 0. ) * Color.rgb; vec3 R = normalize( reflect( -L, N ) ); vec3 spec = LightColor * pow( max( dot( R, E), 0. ), Shininess ); Flat Rasterize ambient, diffuse, spec gl_fragcolor.rgb = Ka*ambient + Kd*diffuse + Ks*spec;

What you see depends on the light color and the material color L R L G L B White Light What the light can produce E R E G E B What the eye sees M R M G M B What the material can reflect Green Light E R E G E B = L R * M R = L G * M G = L B * M B

A-D-S Lighting with Smooth Interpolation Note: The light intensity is computed at each vertex and interpolated throughout the facet. This creates artifacts such Mach Banding and the fact that the bright spot is not circular. You can do this in stock OpenGL or in a shader. N = Normal L = Light vector E = Eye vector R = Light reflection vector ER = Eye reflection vector Color = LightColor * MaterialColor Vertex Shader Fragment Shader vec3 ambient = Color.rgb; diffuse = max( dot(l,n), 0. ) * Color.rgb; vec3 R = normalize( reflect( -L, N ) ); vec3 spec = LightColor * pow( max( dot( R, E), 0. ), Shininess ); Smooth Rasterize ambient, diffuse, spec gl_fragcolor.rgb = Ka*ambient + Kd*diffuse + Ks*spec;

A-D-S Lighting with Normal Interpolation Note: The normal is interpolated throughout the facet. The light intensity is computed at each fragment. This avoids Mach Banding and makes the bright spot circular. You can only do this in a shader. N = Normal L = Light vector E = Eye vector R = Light reflection vector ER = Eye reflection vector Color = LightColor * MaterialColor Fragment Shader Smooth Rasterize N, L, E vec3 ambient = Color.rgb; diffuse = max( dot(l,n), 0. ) * Color.rgb; vec3 R = normalize( reflect( -L, N ) ); vec3 spec = LightColor * pow( max( dot( R, E ), 0. ), Shininess ); gl_fragcolor.rgb = Ka*ambient + Kd*diffuse + Ks*spec;

The Difference Between Per-Vertex Lighting and Per-Fragment Lighting Per-vertex Per-fragment

The Difference Between Per-Vertex Lighting and Per-Fragment Lighting Per-vertex Per-fragment

Flat shading Normal interpolation

A-D-S Lighting with Normal Interpolation and a CubeMap Reflection Note: A cube map reflection is blended in, given a stronger impression that the surface is shiny. N = Normal L = Light vector E = Eye vector R = Light reflection vector ER = Eye reflection vector Color = LightColor * MaterialColor Fragment Shader Smooth Rasterize N, L, E vec3 ambient = Color.rgb; diffuse = max( dot(l,n), 0. ) * Color.rgb; vec3 R = normalize( reflect( -L, N ) ); vec3 spec = LightColor * pow( max( dot( R, E ), 0. ), Shininess ); vec3 reflcolor = texturecube( ReflectUnit, R ).rgb; gl_fragcolor.rgb = Ka*ambient + Kd*diffuse + Ks*spec + Kr*reflcolor.rgb;

A-D-S Anisotropic Lighting with Normal Interpolation Note: The bright spot is not circular because the material has different properties in different directions. Materials such as fur, hair, and brushed metal behave this way. James Kajiya and Timothy Kay, Rendering Fur with Three Dimensional Textures, Proceedings of SIGGRAPH 1989, Volume 23, Number 3, July 1989, pp. 271-280. N = Normal L = Light vector E = Eye vector R = Light reflection vector ER = Eye reflection vector Color = LightColor * MaterialColor Fragment Shader vec3 ambient = Color.rgb; float dl = dot( T, L ); vec3 diffuse = sqrt( 1. - dl*dl ) * Color.rgb; float de = dot( T, E ); vec3 spec = LightColor * pow( dl * de + sqrt( 1. - dl*dl ) * sqrt( 1. - de*de ), Shininess ); gl_fragcolor.rgb = Ka*ambient + Kd*diffuse + Ks*spec;

Summary Flat Normal Anisotropic Smooth Reflection

#version 330 compatibility uniform float ulightx, ulighty, ulightz; flat out vec3 vnf; out vec3 vns; flat out vec3 vlf; out vec3 vls; flat out vec3 vef; out vec3 ves; vec3 eyelightposition = vec3( ulightx, ulighty, ulightz ); void main( ) { vec4 ECposition = umodelviewmatrix * avertex; Nf = normalize( unormalmatrix * anormal ); // surface normal vector Ns = Nf; Lf = eyelightposition - ECposition.xyz; Ls = Lf; Ef = vec3( 0., 0., 0. ) - ECposition.xyz; Es = Ef ; // vector from the point // to the light position // vector from the point // to the eye position } gl_position = umodelviewprojectionmatrix * avertex;

#version 330 compatibility uniform float uka, ukd, uks; uniform vec4 ucolor; uniform vec4 uspecularcolor; uniform float ushininess; uniform bool uflat, uhalf; flat in vec3 vnf; in vec3v Ns; flat in vec3 vlf; in vec3v Ls; flat in vec3 vef; in vec3 ves; out vec4 ffragcolor; void main( ) { vec3 Normal; vec3 Light; vec3 Eye; if( uflat ) { } else { } Normal = normalize(vnf); Light = normalize(vlf); Eye = normalize(vef); Normal = normalize(vns); Light = normalize(vls); Eye = normalize(ves);

vec4 ambient = uka * ucolor; float d = max( dot(normal,light), 0. ); vec4 diffuse = ukd * d * ucolor; float s = 0.; if( dot(normal,light) > 0. ) // only do specular if the light can see the point { vec3 ref = normalize( 2. * Normal * dot(normal,light) - Light ); s = pow( max( dot(eye,ref),0. ), ushininess ); } vec4 specular = uks * s * uspecularcolor; } ffragcolor = vec4( ambient.rgb + diffuse.rgb + specular.rgb, 1. );