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

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

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

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

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

INF3320 Computer Graphics and Discrete Geometry

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

Shading, lighting, & BRDF Theory. Cliff Lindsay, PHD

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

Shading 1: basics Christian Miller CS Fall 2011

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

Virtual Reality for Human Computer Interaction

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

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

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

CS452/552; EE465/505. Intro to Lighting

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

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

CS452/552; EE465/505. Lighting & Shading

Why we need shading?

Basic GPU techniques Josef Pelikán CGG MFF UK Praha.

CEng 477 Introduction to Computer Graphics Fall

CPSC 314 LIGHTING AND SHADING

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

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

Virtual Reality for Human Computer Interaction

Introduction to Visualization and Computer Graphics

Comp 410/510 Computer Graphics. Spring Shading

CS 4600 Fall Utah School of Computing

Shading and Illumination

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

Computer Graphics MTAT Raimond Tunnel

Computer Graphics. Illumination and Shading

Visualisatie BMT. Rendering. Arjan Kok

CS Surface Rendering

Light Sources. Spotlight model

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

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

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

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

Computer Graphics Coursework 1

CS5620 Intro to Computer Graphics

Illumination & Shading: Part 1

Illumination & Shading

Illumination in Computer Graphics

Local Illumination. CMPT 361 Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller

Lecture 15: Shading-I. CITS3003 Graphics & Animation

Illumination and Shading

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

The Rasterization Pipeline

Today. Global illumination. Shading. Interactive applications. Rendering pipeline. Computergrafik. Shading Introduction Local 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

Computer Graphics. Illumination and Shading

From Graphics to Visualization

Illumination and Shading ECE 567

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

Illumination Models & Shading

Pipeline Operations. CS 4620 Lecture 14

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

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

Recall: Indexing into Cube Map

9. Illumination and Shading

Shading I Computer Graphics I, Fall 2008

Graphics and Visualization

Introduction to Computer Graphics 7. Shading

Illumination and Shading

Illumination & Shading I

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

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

Pipeline Operations. CS 4620 Lecture 10

Complex Shading Algorithms

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

INFOGR Computer Graphics. J. Bikker - April-July Lecture 10: Shading Models. Welcome!

Computergraphics Exercise 15/ Shading & Texturing

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

Shading. Brian Curless CSE 457 Spring 2015

Lights, Colour and Materials In OpenGL

Shading. CSE 457 Winter 2015

CS 432 Interactive Computer Graphics

CS 498 VR. Lecture 19-4/9/18. go.illinois.edu/vrlect19

CPSC / Illumination and Shading

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

CS451Real-time Rendering Pipeline

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

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

CSCI 4620/8626. Computer Graphics Illumination Models and Surface Rendering Methods (Chapter 17)

w Foley, Section16.1 Reading

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

CENG 477 Introduction to Computer Graphics. Ray Tracing: Shading

Shading. Why we need shading. Scattering. Shading. Objectives

Sung-Eui Yoon ( 윤성의 )

Orthogonal Projection Matrices. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

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

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

CMSC427 Shading Intro. Credit: slides from Dr. Zwicker

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

Transcription:

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

Overview of today s lecture l A simple most basic real-time lighting model Also, OpenGL s old fixed pipeline lighting model l Fog l Gamma correction l Transparency and alpha

Compute lighting at vertices, then interpolate over triangle light blue Rasterizer Geometry red green l How compute lighting? l We could set colors per vertex manually l For a little more realism, compute lighting from Light sources Material properties Geometrical relationships

A basic lighting model Light: Ambient (r,g,b,a) Diffuse (r,g,b,a) Specular (r,g,b,a) Material: Ambient (r,g,b,a) Diffuse (r,g,b,a) Specular (r,g,b,a) DIFFUSE SPECULAR AMBIENT EMISSION SHININESS Base color Highlight Color Low-light Color Glow Color Surface Smoothness Emission (r,g,b,a) = självlysande färg Tomas Added Akenine-Mőller by Ulf Assarsson, 2002 2004

Ambient component: iamb l Ad-hoc tries to account for light coming from other surfaces l Just add a constant color: i amb = m amb s amb i.e., (i r, i g, i b, i a ) = (m r, m g, m b, m a ) (l r, l g, l b, l a ) Old: gllightmodelfv(gl_light_model_ambient, global_ambient) Modified Tomas Akenine-Mőller by Ulf Assarsson, 2002 2006

Diffuse component : idiff l i=iamb+idiff+ispec l Diffuse is Lambert s law: i diff = n l l Photons are scattered equally in all directions i diff = ( n l) m diff s diff = cosφ n and l are assumed being unit vectors

Lambertian Surfaces Perfectly diffuse reflector Light scattered equally in all directions Highly reflective surface (specular) Fully diffuse surface (Lambertian)

Lighting Specular component : ispec l Diffuse is dull (left) l Specular: simulates a highlight

Specular component: Phong l Phong specular highlight model l Reflect l around n: r r spec = l + 2(n l)n i = ( r v) = (cos ρ) m shi m shi ( n l) n -l n n must be unit vector l n l i spec = ((n l) < 0)? 0 : max(0,(r v)) m shi m s spec spec l Next: Blinns highlight formula: (n. h) m

Halfway Vector Blinn proposed replacing v r by n h where h = (l+v)/ l + v (l+v)/2 is halfway between l and v If n, l, and v are coplanar: ψ = φ/2 Must then adjust exponent so that (n h) e (r v) e (e 4e)

Efficiency The Blinn rendering model is less efficient than pure Phong shading in most cases, since it contains a square root calculation. While the original Phong model only needs a simple vector reflection, this modified form takes more into consideration. However, as many CPUs and GPUs contain single and double precision square root functions (as standard features) and other instructions that can be used to speed up rendering -- the time penalty for this kind of shader will not be noticed in most implementations. However, Blinn-Phong will be faster in the case where the viewer and light are treated to be at infinity. This is the case for directional lights. In this case, the half-angle vector is independent of position and surface curvature. It can be computed once for each light and then used for the entire frame, or indeed while light and viewpoint remain in the same relative position. The same is not true with Phong's original reflected light vector which depends on the surface curvature and must be recalculated for each pixel of the image (or for each vertex of the model in the case of vertex lighting). In most cases where lights are not treated to be at infinity, for instance when using point lights, the original Phong model will be faster.

Lighting i=iamb+idiff+ispec + + = l This is just a hack! l Has little to do with how reality works!

Additions to the lighting equation l Depends on distance: 1/(a+bt+ct ) l Can have more lights: just sum their respective contributions l Different light types: 2

Clarifications Energy is emitted at equal proportions in all directions from a spherical radiator. Due to energy conservation, the intensity is proportional to the spherical area at distance r from the light center. A = 4πr 2 Thus, the intensity scales ~ 1/r 2 r But with 8-bit colors, linear decay works better in practice. 14

OpenGL Example (fixed pipeline) Sets constant (no) attenuation: gllight(gl_light0, GL_CONSTANT_ATTENUATION, 1.0); gllight(gl_light0, GL_LINEAR_ATTENUATION, 0.0); gllight(gl_light0, GL_QUADRATIC_ATTENUATION, 0.0); Attenuation often looks unnatural in OpenGL, due to only 8-bit colors (too small intensity range)

Shading l Shading: do lighting (at e.g. vertices) and determine pixel s colors from these l Three common types of shading: Flat, Goraud, and Phong Old: glshademodel(gl_flat / GL_SMOOTH);

Shading l Three common types of shading: l Flat, Goraud, and Phong In standard Gouraud shading the lighting is computed per triangle vertex and for each pixel, the color is interpolated from the colors at the vertices. l In Phong Shading the lighting is not per vertex. Instead the normal is interpolated per pixel from the normals defined at the vertices and full lighting is computed per pixel using this normal. This is of course more expensive but looks better. Flat Gouraud Phong

// Vertex Shader #version 130 Gouraud Shading Code in vec3 vertex; in vec3 normal; uniform vec4 mtrlamb, mtrldiffuse, mtrlspec, mtrlemission; uniform vec4 lightamb, lightdiffuse, lightspec, lightemission; uniform float shininess; uniform mat4 modelviewprojectionmatrix, normalmatrix, modelviewmatrix; uniform vec4 lightpos; // in view space out vec3 outcolor; For one light source // Fragment Shader: #version 130 in vec3 outcolor; out vec4 fragcolor; void main() { gl_position = modelviewprojectionmatrix*vec4(vertex,1); // ambient outcolor = lightamb * mtrlamb; void main() { } fragcolor = vec4(outcolor,1); // diffuse vertex = vec3(modelviewmatrix * vec4(vertex,1)); normal = normalize(normalmatrix * normal); vec3 lightdirection = normalize(lightpos vertex.xyz); float intensity=max(0, dot(normal, lightdirection) outcolor += lightdiffuse*mtrldiffuse*intensity; } // specular vec3 viewvec = -vertex.xyz; // because we are in view space vec3 reflvec = -lightdirection + normal*(2*dot(normal*lightdirection)) r = l + 2(n l)n intensity=pow(max(0,(dot(reflvec, viewvec)), shininess)); outcolor += lightspec * mtrlspec * max(0,intensity); i spec = ((n l) < 0)? 0 : max(0,(r v)) m shi m spec s spec

Phong Shading Code For one light source // Vertex Shader #version 130 in vec3 vertex; in vec3 normal; uniform vec3 mtrlamb; uniform vec3 lightamb; uniform vec4 lightpos; uniform mat4 modelviewprojectionmatrix; uniform mat4 normalmatrix; uniform mat4 modelviewmatrix; out vec3 outcolor; out vec3 N; out vec3 viewvec; out vec3 lightdirection; void main() { gl_position = modelviewprojectionmatrix* vec4(vertex,1); } // ambient outcolor = lightamb * mtrlamb; N= normalize(normalmatrix * normal); lightdirection = normalize(lightpos vertex.xyz); viewvec=-vec3(modelviewmatrix*vec4(vertex,1)); // Fragment Shader: #version 130 in vec3 outcolor, lightdirection, N, pos; in vec3 viewvec; uniform vec3 mtrldiffuse, mtrlspec, mtrlemission; uniform vec3 lightdiffuse, lightspec, lightemission; uniform float shininess; out vec4 fragcolor; void main() { N = normalize(n); // renormalize due to the interpolation // diffuse float intensity=max(0, dot(n, lightdirection) outcolor += lightdiffuse*mtrldiffuse*intensity; } // specular vec3 reflvec = -lightdirection + N*(2*dot(N*lightDirection)); intensity=pow(max(0,(dot(reflvec, viewvec)), shininess)); outcolor += lightspec * mtrlspec * max(0,intensity); fragcolor = vec4(outcolor,1);

Fog l Simple atmospheric effect A little better realism Help in determining distances

l Color of fog: c c p = fc s + (1 f )c f l How to compute f? f color of surface: f [0,1] l 3 ways: linear, exponential, exponential-squared l Linear: f = z z end end z z p start cs

Fog The equation for GL_EXP fog is f = e (-(density x)) Rationale: Constant intensity decrease at greater distance due to out scattering and absorption. di = CIds I(0) ds I(s) s I(s) = I(0)e density*s The equation for GL_EXP2 fog is f = e ( (density c))^2

Fog example l Often just a matter of Choosing fog color Choosing fog model Old OpenGL just turn it on. New OpenGL program it yourself in the fragment shader

Fog in up-direction

Gamma correction l If input to gun is 0.5, then you don t get 0.5 as output in intensity l Instead, gamma correct that signal: gives linear relationship

Gamma correction I = a( V +ε) γ l I=intensity on screen l V=input voltage (electron gun) l a,ε, and γ are constants for each system l Common gamma values: 2.2-2.6 l Assuming ε=0, gamma correction is: c = c i ( 1/ γ )

Why is it important to care about gamma correction? l Portability across platforms l Image quality Texturing Anti-aliasing l One solution is to put gamma correction in hardware l srgb asumes gamma=2.2 l Can use EXT_framebuffer_sRGB to render with gamma correction directly to frame buffer

Gamma correction today l Standard is 2.2 l Happens to give more efficient color space when compressing intensity from 32-bit floats to 8-bits. Thus, still somewhat motivated. better distribution for humans Gamma of 2.2 Truth On most displays (those with gamma of about 2.2), one can observe that the linear intensity output (bottom) has a large jump in perceived brightness between the intensity values 0.0 and 0.1, while the steps at the higher end of the scale are hardly perceptible. The linear input (upper), which has a nonlinearly-increasing intensity, will show much more even steps in perceived brightness.

Transparency and alpha l Transparency Very simple in real-time contexts l The tool: alpha blending (mix two colors) l Alpha (α) is another component in the frame buffer, or material for a triangle Represents the opacity 1.0 is totally opaque 0.0 is totally transparent l The over operator: c = αc + ( 1 α) c o s d Rendered object

Transparency c = αc + ( 1 α) c o Rendered object s Background d l Need to sort the transparent objects Render back to front (blending is order dep.) l Lots of different other blending modes l Can store RGBα in textures as well So the texture rectangle does not hide objects behind Ulf Assarsson 2007

Blending c = αc + ( 1 α) c o s d l Used for Transparency l glblendfunc(gl_src_alpha, GL_ONE_MINUS_SRC_ALPHA) Effects (shadows, reflections) Complex materials l Quake3 used up to 10 rendering passes, blending toghether contributions such as: Diffuse lighting (for hard shadows) Bump maps Base texture Specular and emissive lighting Volumetric/atmospheric effects Enable with glenable(gl_blend) 31 Ulf Assarsson 2003