Lab 9 - Metal and Glass

Similar documents
Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Complex Shading Algorithms

Computer Graphics. Illumination and Shading

CS452/552; EE465/505. Intro to Lighting

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

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

CPSC 314 LIGHTING AND SHADING

Local Reflection Models

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

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Recollection. Models Pixels. Model transformation Viewport transformation Clipping Rasterization Texturing + Lights & shadows

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

Introduction to Visualization and Computer Graphics

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

w Foley, Section16.1 Reading

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

CS5620 Intro to Computer Graphics

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

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

Illumination and Shading

Simple Lighting/Illumination Models

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

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

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

The Rasterization Pipeline

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

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

CS 5625 Lec 2: Shading Models

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

So far, we have considered only local models of illumination; they only account for incident light coming directly from the light sources.

Illumination and Shading

Visual cues to 3D geometry. Light Reflection and Advanced Shading. Shading. Recognizing materials. size (perspective) occlusion shading

Comp 410/510 Computer Graphics. Spring Shading

CS 325 Computer Graphics

Pipeline Operations. CS 4620 Lecture 14

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

Shading. Brian Curless CSE 557 Autumn 2017

Lighting and Shading

Illumination & Shading: Part 1

CMSC427 Advanced shading getting global illumination by local methods. Credit: slides Prof. Zwicker

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

Reflection models and radiometry Advanced Graphics

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

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

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

Reflection and Shading

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

Introduction to Computer Graphics 7. Shading

CSE 681 Illumination and Phong Shading

CS 4600 Fall Utah School of Computing

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

Shading I Computer Graphics I, Fall 2008

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

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

Illumination & Shading

Surface Reflection Models

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

1 of 5 9/10/ :11 PM

Real-time Graphics 6. Reflections, Refractions

Computer Vision Systems. Viewing Systems Projections Illuminations Rendering Culling and Clipping Implementations

Ø Sampling Theory" Ø Fourier Analysis Ø Anti-aliasing Ø Supersampling Strategies" Ø The Hall illumination model. Ø Original ray tracing paper

Computer Graphics Coursework 1

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

Illumination Models & Shading

Lecture 15: Shading-I. CITS3003 Graphics & Animation

Deferred Rendering Due: Wednesday November 15 at 10pm

INF3320 Computer Graphics and Discrete Geometry

Shading. Brian Curless CSE 457 Spring 2017

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

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

CMSC427 Shading Intro. Credit: slides from Dr. Zwicker

Shading 1: basics Christian Miller CS Fall 2011

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

Programming shaders & GPUs Christian Miller CS Fall 2011

Problem Set 4 Part 1 CMSC 427 Distributed: Thursday, November 1, 2007 Due: Tuesday, November 20, 2007

BRDF Computer Graphics (Spring 2008)

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

Materials & Shadows. Steve Rotenberg CSE168: Rendering Algorithms UCSD, Winter 2017

Photorealism: Ray Tracing

Pipeline Operations. CS 4620 Lecture 10

Raytracing. COSC 4328/5327 Scott A. King

Lab 2-3D Transformations and Vertex Shaders

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

Game Technology. Lecture Physically Based Rendering. Dipl-Inform. Robert Konrad Polona Caserman, M.Sc.

Visualisatie BMT. Rendering. Arjan Kok

Shading, Advanced Rendering. Week 7, Wed Feb 28

CS452/552; EE465/505. Lighting & Shading

Computer Graphics (CS 543) Lecture 8 (Part 1): Physically-Based Lighting Models

Lecture 4: Reflection Models

Introduction. Lighting model Light reflection model Local illumination model Reflectance model BRDF

Sung-Eui Yoon ( 윤성의 )

Lighting and Materials

Shading II. CITS3003 Graphics & Animation

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

Computer Graphics. Illumination and Shading

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

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

03 RENDERING PART TWO

Ray Tracing: shading

Transcription:

Lab 9 - Metal and Glass Let the form of an object be what it may, light, shade, and perspective will always make it beautiful. -John Constable Prologue Support code: /course/cs1230/src/labs/lab09 This lab is entirely focused on shaders and GLSL. The Blinn-Phong lighting used in the projects is often insufficient for creating convincing replicas of materials such as glass, water, metal, skin, or ice. This is partially due to the fact that the Phong lighting model has no physical foundation -- it just looks okay. Shaders allow for much more control over lighting and shading. You will be writing two material shaders - shaders which mimic the properties and appearance of various materials. Through this, you will learn to approximate physical models to render more convincing computer graphics. Intro If you haven't already guessed, you will be writing a metal shader and a glass shader! Both of these shaders use physically-based models to calculate illumination. In particular, they mimic Fresnel reflectance and refractance, and will use the physically-based Cook-Torrance model to determine specular reflection. There s a lot of support code, but you only need to worry about metal.frag and glass.frag. Fig. 1: Metal and glass shaders applied to the dragon.obj model

Cube Environment Mapping Notice that both of these materials interact with the scene by reflecting and/or refracting it. For now, simulating this perfectly is too hard, so we'll approximate using a technique called environment mapping. Environment mapping encodes all data that can be reflected or refracted into a texture. For example, to simulate reflection, we simply figure out where in the scene the reflected data should come from and sample the texture (the environment map ) correspondingly. Though computationally cheap, this technique only allows objects to reflect the environment: objects do not show reflections of each other. Later in this course you'll write a raytracer, which will be able to handle inter-object reflection among other things! In this lab you will encode the environment in a cube map. Cube maps are a collection of six texture maps that form a cube, where each inside face of the cube is covered with a 2D texture: Fig. 2: Example of an environment cube map, unrolled into a single image Each texture map is chosen so that the sides of the cube come together to form an entire scene. The object we're shading is surrounded by a skybox that uses the cube map textures. This way, the object being rendered appears to be inside the environment and the material shader can index into the environment cube map to determine what data to reflect and/or refract. Since the skybox and the shader will use the same cube map, it will appear as though the object is reflecting and refracting the scene. Remember to use the same cube map for the skybox and the shader! Otherwise, your object will be reflecting a different scene than the one it's in.

The skybox has already been written for you, but the shaders that index into the cube map will be up to you. 1 Fresnel Equations The Fresnel equations model how light interacts with objects. Schlick's approximation (explained in the next section) mimics Fresnel equations, and you will be implementing Schlick's approximation because the Fresnel equations are too expensive to compute for real-time graphics applications. Fig. 3: Diagram of light with a surface The Fresnel equations describe the behavior of light as it moves between media with different indices of refraction. When light passes from one medium into the next, it may be reflected (Q) or refracted (S), and the Fresnel equations can be used to compute the fraction of light being reflected and refracted. This will be useful in both the glass and metal shaders in order to properly model the behaviors of each material. Shlick's Approximation In your metal and glass shaders, you will use Schlick's approximation to calculate the reflectance F and the transmittance T. The reflectance (or reflection coefficient) is the fraction of light that is reflected, and the transmittance (or transmission coefficient) is the fraction of light that is refracted. Under our model all light is either reflected or transmitted, so T = 1 F. 1 pronounced FRAY-nell

We determine F by Schlick's approximation as follows: In this equation, the vertex. is zero). R 0 θ i 5 F R 0 + (1 R 0)(1 cos θ i ) is the angle between the surface normal and the vector from the camera to is the reflectance at normal incidence (i.e. the fraction of light reflected when θ i The Phong Illumination Model The Phong and Blinn-Phong illumination models were the first illumination models to try mimicking specular reflection phenomena. However, these models are not based on reality. Recall that the Phong model splits the lighting into three different terms: ambient, diffuse, and specular. In the Phong model, k spec = cos n (E, R) where n is the specular coefficient, E points in the direction of the viewer, and R is the reflection of the incoming light vector about the normal vector. Larger values of brighter, more localized specular highlights. n produce It is relatively easy to implement Phong in shader code. Below are examples for the vertex and fragment shaders: // Example Phong vertex shader varying vec3 normal, light, camera ; // Will be passed to fragment shader void main() { // Transform normal vector into camera coordinates normal = normalize( gl_normalmatrix * gl_normal ).xyz; // Transform position into camera coordinates vec3 vertex = (gl_modelviewmatrix * gl_vertex).xyz; // Get the vector from the vertex to the light source light = normalize( gl_lightsource[0].position.xyz - vertex ) ; // The vector from the vertex to the camera is ((0, 0, 0) - vertex) // since the vertex is in camera coordinates camera = -vertex ;

} // Project the normal and position into screen coordinates gl_position = ftransform() ; // Example Phong fragment shader uniform vec4 color ; // User-supplied material color... uniform float spec ; //.. and specular power coefficient varying vec3 normal, light, camera ; // Computed in vertex shader void main() { // Re-normalize interpolated values vec3 n = normalize( normal ) ; vec3 l = normalize( light ) ; vec3 e = normalize( camera ) ; // Compute diffuse coefficient float intensity = dot( n, l ) ; // Add specular coefficient vec3 r = reflect( - l, n ) ; intensity = intensity + pow(dot( e, r ), spec ) ; } gl_fragcolor = color * intensity ; Note that the normal vector is normalized in both the vertex shader and the fragment shader. In general, if you need a vector normalized within a fragment shader, it should also be normalized in the vertex shader. Also notice that the vertex shader uses several different matrices to calculate N, E, and ( gl_modelviewmatrix and gl_normalmatrix ). Since OpenGL lighting is mostly done in camera coordinate space, we need to transform object coordinates into camera coordinates using these matrices. L Remember that: Object coordinates are transformed by the ModelView matrix to produce camera coordinates

Object coordinates are the raw coordinates you submit to OpenGL with a call to glvertex*(). They represent the coordinates of your object or other geometry you want to render. Camera coordinates are transformed by the Projection matrix to produce clip coordinates Clip coordinates X, Y, and Z are divided by W to produce normalized device coordinates Normalized device coordinates are scaled and translated by the viewport parameters to produce window coordinates A Better Illumination Model: Cook-Torrance There are many other illumination techniques which better model the distribution of microfacets on a surface. One of these is the physically-based Cook-Torrance model. You will be using this model to calculate the specular term. In particular, k spec = DF G V N D is the Beckmann distribution factor, D = 2 2 exp( tan (α)/m ) 4m2 4 cos (α), α = arccos (n h) Where n is the normal vector and h is the half angle vector, named because it is halfway between the eye vector (which goes from the vertex to the viewer) and the light vector (which goes from the vertex to the light). If u is the vector from the vertex to the light and v is the vector from the vertex to the eye, then we find h by adding u + v and then normalizing. This is pictured in the diagram below. m [ 0, 1 ] controls the roughness of the surface.

F is the Fresnel coefficient (calculated using Schlick's approximation), and G attenuation term caused by self-shadowing of the microfacets in the surface, is the geometric 2(h n)(v n) 2(h n)(u n) G = min (1, v h, v h ) In the above formulas, v is the vector from the vertex to the eye, h is the half angle vector, u is the vector to the light source, and Sampling a Cube Map Recall texture sampling from lab 3: n is the normal vector. uniform sampler2d tex ; void main() { gl_fragcolor = texture( tex, gl_texcoord[0].st ) ; } Sampling a cube map works similarly, except OpenGL uses a 3D vector to index into the cube instead of 2D texcoords. OpenGL will do all the work of figuring out which face to sample and which pixels on that face to use. For example, the input (0,-1,0) would sample the center of the bottom face, while (1,1,1) would sample the corner where the +X, +Y, and +Z faces meet. The GLSL for sampling a texture cube is: uniform samplercube tex ; void main() { gl_fragcolor = texture( tex, myvec3 ) ; }

When using cube environment maps, the vec3 you pass to texture() will always be a normalized direction vector pointing where in the environment you want to read a color. We want to simulate reflection by doing the following: Using the normal and the eye direction vector, compute the incident direction vector. You may find the GLSL reflect() function useful for this. Don t forget: If you want to sample from the cube map correctly you need to calculate your reflected vector from the world coordinates. You ll need to find a way to transform from camera coordinates to world coordinates for this one step. The incident vector obtained may be pointing towards the point being rendered. Reverse it so that it points into the environment, away from the object being rendered. Sample the environment cube map using that vector. Getting Started The support code is located in /course/cs1230/src/labs/lab09. Note that there is also a demo version of the lab that can be run by inputting /course/cs1230/bin/cs1230_lab09_demo into your terminal. Metal Shader Metals and other shiny materials have large specular reflections. These specular highlights are important visual cues, helping viewers determine the shape and location of an object as well as the location of light sources acting on the object. Real materials don't have perfect specular reflections. Even surfaces that appear smooth are not perfectly so: their surfaces are covered in very tiny facets ( microfacets ), each of which is a perfect specular reflector. The material appears smooth because these microfacets don't differ greatly in their orientations. However, the material will still appear somewhat rough because the microfacets are not aligned perfectly. Task 1: Copy the stencil code from /course/cs1230/src/labs/lab09. Open metal.vert and metal.frag. To implement the metal shader, follow these high-level steps: Determine the diffuse and ambient terms as you would in a Phong shader. For the specular term, k s, use the Cook-Torrance model. Clamp it above 0, so that negative values are ignored

Compute the object color (without the reflection component) as a mbient + l ambertt erm d iffuse + k s s pecular. Determine the reflection color. Calculate the reflected direction using cameratovertex, the surface normal, and the GLSL reflect function. Keep in mind that the camera is the eye in the above diagrams. Convert the reflected direction to world space Sample the cube map Calculate the reflectance F by Schlick's approximation Get the final color by blending the reflection color and the object color Use the formula o bjectcolor ( 1 F ) + r ef lectioncolor F You might consider using GLSL's mix() function for this The final product should look like this:

Glass Shader When you look at a material like glass, you see a mixture of reflected and refracted (or transmitted) light. You already did reflection in the previous shader; now it is time to implement refraction too. When light is refracted at the interface between two media, its direction changes according to Snell's law :

sin(θ i ) sin(θ t ) n = 2 n1 The variables in this equation correspond to figure 3 in the Fresnel Equations section ; n 2 are the indices of refraction (IOR) of medium 1 and medium 2, respectively. Fortunately, GLSL has a function will come in handy so we do not have to directly implement Snell s law ourselves. Our strategy for refraction will be similar to reflection: using the GLSL refract() function, compute the direction of the incident vector that refracts in the direction of the eye vector, and then sample the cube map to simulate the incoming light. The following refraction shader samples a refracted ray from the surrounding cube environment map: // Vertex shader varying vec3 normal ; varying vec3 vertex ; void main() { vertex = gl_modelviewmatrix * gl_vertex; normal = normalize( gl_normalmatrix * gl_normal ) ; gl_position = ftransform() ; } n 1 and // Fragment shader uniform samplercube envmap ; varying vec3 normal ; varying vec3 vertex ; float eta =.77; // ratio of IORs (n2/n1 in Snell's law) void main() { vec3 n = normalize( normal ) ; vec3 e = normalize( vertex ) ; vec3 t = gl_modelviewmatrixinverse * vec4(refract( e, n, eta ), 0.0 ) ; gl_fragcolor = vec4(texture( envmap, t ).rgb, 1.0 ) ; } However, this is not very realistic. Recall the Fresnel equations described above. When light hits an interface between two media, some is reflected and some is refracted. Furthermore, different wavelengths of light get refracted different amounts.

Task 2: Open glass.vert and glass.frag. The vertex shader is already complete, so you just need to fill in the fragment shader. You will need Schlick's approximation to determine how much light is reflected and how much is refracted. The ratio of reflected to refracted light should be the Fresnel term, like in the metal shader. Then, we need to refract light by different amounts for each color channel (R, G, and B). To do this, change your shader to calculate three refraction vectors (one for each color channel), each of which is refracted by a slightly different amount (use the values in the uniform vec3 eta declared for you in the stencil code). To summarize, here are the high-level steps which should be performed by glass.frag : Sample the cube map to determine the reflection color. For each of the R, G, and B channels, determine the direction from which refracted light originated. For example use refract(cameratovertex, normal, eta.r) to get the refracted direction for R Use these direction vectors to respectively sample the R, G, and B channels in the environment map. Don t forget to convert the vectors to world space first Calculate the reflectance F by Schlick's approximation Get the final color by blending the refraction color and the reflection color Use the formula r ef ractioncolor ( 1 F ) + ref lectioncolor F Try changing around the You might consider using GLSL's mix() function for this r 0 product should look like this: value to see the mix of reflectance and refraction change. The final

End Once you see both the glass and metal shaders working, ask a TA to get checked off for lab. Now you have the basis to implement shaders for other reflective/refractive materials, and you can reuse these shaders in final project, too! Be prepared to answer one or more of the following: What is the purpose of Schlick's approximation? What are the coefficients we approximate with it?

What is environment mapping and how do we use it in our material shaders? How does the Cook-Torrance model differ from the Phong model? What is the difference in the implementation of the metal and glass shaders to allow the object to look like it has different material properties? Food for thought Review the Phong reflection model: http://en.wikipedia.org/wiki/phong_shading#phong_reflection_model More about the Blinn-Phong shading model; https://en.wikipedia.org/wiki/blinn%e2%80%93phong_shading_model Learn more about the Fresnel equations: http://en.wikipedia.org/wiki/fresnel_equations OpenGL FAQ: https://www.khronos.org/opengl/wiki/faq