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

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Computer Graphics Lighting

Computer Graphics Lighting. Why Do We Care About Lighting?

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

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

Stripes, Rings, and Dots!

-=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

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

INFOGR Computer Graphics

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

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

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

Bump Mapping. Mike Bailey. What is Bump-Mapping?

Visualisatie BMT. Rendering. Arjan Kok

Illumination & Shading: Part 1

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


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

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

Shading. Brian Curless CSE 457 Spring 2017

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

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)!

CEng 477 Introduction to Computer Graphics Fall

Illumination & Shading I

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

OpenGL Lighting Computer Graphics Spring Frank Palermo

Shading and Illumination

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

Programmable Graphics Hardware

INFOGR Computer Graphics

9. Illumination and Shading

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

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

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

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

Computer Graphics. Illumination and Shading

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

GLSL Geometry Shaders

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

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

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

Comp 410/510 Computer Graphics. Spring Shading

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

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

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

How do we draw a picture?

Illumination and Shading

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

CS452/552; EE465/505. Lighting & Shading

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

Shading. Brian Curless CSE 457 Spring 2015

CS559 Computer Graphics Fall 2015

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

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

Introduction to the OpenGL Shading Language

INF3320 Computer Graphics and Discrete Geometry

Shading. CSE 457 Winter 2015

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

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

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

Transcription:

1 Using Shaders for Lighting This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License Mike Bailey mjb@cs.oregonstate.edu lighting.pptx Lighting Definitions 2 L N R E 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 1

A-D-S Lighting 3 Ambient: K a Diffuse: K d *cosθ Specular: K s *cos n φ 4 Ambient-only Diffuse-only Specular-only ADS Shininess=50 ADS Shininess=1000 ADS Shininess=1000 -- Flat 2

A-D-S Lighting with Flat Interpolation 5 Each facet has a single lighting value applied to every pixel within it. Vertex Shader 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, specular gl_fragcolor.rgb = Ka*ambient + Kd*diffuse + Ks*spec; What you see depends on the light color and the material color 6 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 3

A-D-S Lighting with Smooth Interpolation 7 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. Vertex Shader 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 8 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. Smooth-rasterize N, L, E 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; 4

The Difference Between Per-Vertex Lighting and Per-Fragment Lighting 9 Per-vertex Per-fragment The Difference Between Per-Vertex Lighting and Per-Fragment Lighting 10 Per-vertex Per-fragment 5

Flat shading 11 Normal interpolation A-D-S Lighting with Normal Interpolation and a CubeMap Reflection 12 Note: A cube map reflection is blended in, given a stronger impression that the surface is shiny. Smooth-rasterize N, L, E 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; 6

A-D-S Anisotropic Lighting with Normal Interpolation 13 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. 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 14 Flat Normal Anisotropic Smooth Reflection 7

#version 330 compatibility 15 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 ); Ns = Nf; Lf = eyelightposition - ECposition.xyz; Ls = Lf; Ef = vec3( 0., 0., 0. ) - ECposition.xyz; Es = Ef ; // surface normal vector // vector from the point // to the light position // vector from the point // to the eye position gl_position = umodelviewprojectionmatrix * avertex; Computer } Graphics #version 330 compatibility 16 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); 8

17 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. ); 9