Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

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

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

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

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

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

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

CS452/552; EE465/505. Lighting & Shading

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

Introduction to Computer Graphics with WebGL

Lecture 15: Shading-I. CITS3003 Graphics & Animation

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

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

CS 4600 Fall Utah School of Computing

CS Surface Rendering

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

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

Objectives. Shading II. Distance Terms. The Phong Reflection Model

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

Computer Graphics. Illumination and Shading

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

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

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

Why we need shading?

Comp 410/510 Computer Graphics. Spring Shading

Illumination and Shading

CS 432 Interactive Computer Graphics

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

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

Deferred Rendering Due: Wednesday November 15 at 10pm

Shading and Illumination

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

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

Illumination Models & Shading

Introduction to Computer Graphics 7. Shading

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

Visualisatie BMT. Rendering. Arjan Kok

Shading II. CITS3003 Graphics & Animation

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

Pipeline Operations. CS 4620 Lecture 14

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

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

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

Pipeline Operations. CS 4620 Lecture 10

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

CS452/552; EE465/505. Intro to Lighting

Illumination & Shading

CS5620 Intro to Computer Graphics

Computer Graphics Coursework 1

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

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

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

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

CEng 477 Introduction to Computer Graphics Fall

Computer Graphics. Illumination and Shading

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

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

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

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

Illumination and Shading

5.2 Shading in OpenGL

Mach band effect. The Mach band effect increases the visual unpleasant representation of curved surface using flat shading.

The Rasterization Pipeline

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Computer Graphics I Lecture 11

Illumination & Shading I

Shading 1: basics Christian Miller CS Fall 2011

CS Illumination and Shading. Slide 1

surface: reflectance transparency, opacity, translucency orientation illumination: location intensity wavelength point-source, diffuse source

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

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

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

Shading I Computer Graphics I, Fall 2008

Shading. Brian Curless CSE 457 Spring 2017

CPSC 314 LIGHTING AND SHADING

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

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

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

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

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

Illumination and Shading

Illumination and Shading

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

CPSC / Illumination and Shading

9. Illumination and Shading

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

CS 130 Final. Fall 2015

Display Issues Week 5

Illumination & Shading: Part 1

INF3320 Computer Graphics and Discrete Geometry

CS 325 Computer Graphics

Introduction to Visualization and Computer Graphics

Shading Intro. Shading & Lighting. Light and Matter. Light and Matter

University of Victoria CSC 305 Shading. Brian Wyvill 2016

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

Lab 9 - Metal and Glass

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

Lighting and Shading. Slides: Tamar Shinar, Victor Zordon

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

Transcription:

Lecture 17: Shading in OpenGL CITS3003 Graphics & Animation E. Angel and D. Shreiner: Interactive Computer Graphics 6E Addison-Wesley 2012

Objectives Introduce the OpenGL shading methods - per vertex shading - per fragment shading - where to carry out Discuss polygonal shading - Flat Shading - Smooth Shading Gouraud shading Phong shading 2

OpenGL Shading To compute the shading values, we need to know o normal vectors (denoted by n) o material properties o light vectors (denoted by l) The cosine terms in lighting calculations (see the previous lecture) can be computed using dot product. Using unit length vectors simplifies calculation. GLSL has a built-in normalization function for normalizing vectors to unit length. 3

Front and Back Faces Every surface has a front and back face For many objects, we never see the back face so we don t care how or if it is rendered If it matters, we can handle that in the shader back faces not visible back faces visible 4

Vertex Normals Complex surfaces are often represented by a triangular mesh. Consider 5 adjacent triangles sharing a common vertex p. o We can compute the normals n 1, n 2,, n 5 of the 5 triangles o We can then average these normals to get the normal n at p: 5 n = i=1 n i then n n/ n n 4 n n 3 o This allows us to have normals defined at vertices o We call n the vertex normal at p. n 5 p n 1 n 2 5

Moving Light Sources Light sources are geometric objects whose positions or directions are affected by the model-view matrix Depending on where we place the position (direction) setting function, we can - move the light source(s) with the object(s) - fix the object(s) and move the light source(s) - fix the light source(s) and move the object(s) - move the light source(s) and object(s) independently In interactive graphics, users should be able to do all of the above. 6

Specifying a Point Light Source For each light source, we can set the RGBA values for the diffuse, specular, and ambient components, and for the light source position: x y z w vec4 light0_pos = vec4(1.0, 2.0, 3.0, 1.0); 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); Red (R) Green (G) Blue (B) Opacity (A or alpha) 7

Distance and Direction of a Point Light Source The light source color is specified in RGBA The light source position is given in homogeneous coordinates: - If w = 1.0, we are specifying a finite location - If w = 0.0, we are specifying a parallel source with the given direction vector Recall from lecture 15, we can have a distance-attenuation coefficient so objects further from the light source receive less light. The distance-attenuation coefficient is usually inversely proportional to the square of the distance d: 1/(a + bd + cd 2 ), where a, b, and c are user-defined constants. 8

Spotlights Spotlights are derived from point sources. Each spotlight thus has a distance and a colour. In addition, it has - A direction - A cutoff value, θ - An attenuation function, usually defined by cos e φ Large e values attenuate faster Small e values attenuate slower p 1 is illuminated by the light source as it falls inside the cone but p 2 is not. p 1 φ p 2 θ 0 θ φ If θ = 180 0 then the spotlight becomes a point source 9

Spotlights The exponent term α in cos e φ determines how rapidly the light intensity drops off. e = 1 0 φ e > 1 e much larger than 1 Note: the term e here is only a coefficient. It is not equal to exp(1). It is a poor choice of symbol from Angel & Shreiner. 10

Material Properties Material properties should match the terms in the light source model Material properties are specified via the ambient, diffuse, and specular reflectivity coefficients (k a, k d, and k s ) The A value gives opacity, e.g., vec4 ambient = vec4(0.2, 0.2, 0.2, 1.0); vec4 diffuse = vec4(0.8, 0.8, 0.0, 1.0); vec4 specular = vec4(1.0, 1.0, 1.0, 1.0); GLfloat shine = 100.0; R G B A (opacity) This is a yellow (not fully saturated though) object. Is it a smooth (shiny) object? 11

Transparency Material properties are specified as RGBA values. The A value can be used to make the surface translucent. The default is that all surfaces are opaque. Later (if there s time) we will see how to enable blending and use this feature. 12

Polygonal Shading Methods There are 3 methods for shading a polygonal mesh: 1. Flat shading 2. Smooth shading 1. Gouraud shading 2. Phong shading Gouraud shading and Phong shading are both smooth shading techniques. The difference will be clear in the coming slides. Recall that OpenGL handles only triangles. So the terms polygon and polygonal from here onward should be interpreted as triangle and triangular. 13

Flat Shading Recall that to compute the shading value at a point, we need to know the light vector l, the normal vector n, and the view vector v. As we move from one point to the next point inside the polygon, l and v should vary. In the flat shading method, they are assumed to be constant. a single shading calculation is performed for each polygon. the entire polygon has a single shading value. This technique is thus known as flat or constant shading. n 14

Flat Shading Advantage: computationally cheap. Disadvantage: boundary edges of polygons may show up in the rendered output. This shading method is suitable when the viewer and/or light source is far away from the polygon. In OpenGL, we specify flat shading as follows: glshademodel(gl_flat); An example of rendering a polygonal mesh using flat shading 15

Smooth Shading or Gouraud Shading In the Gouraud shading method, o the normal vector at each vertex of the polygon is firstly computed; o the shading value at each vertex is then computed using the light vector l, normal vector n, and view vector v at that vertex; o shading values of interior points of the polygon are obtained by interpolating the shading values at the vertices. oas one shading calculation is required for each vertex, it is also known as per-vertex shading. n 1 p 1 compute the shading values at p 1, p 2, and p 3. n 2 p 2 n 3 p 3 16

Smooth Shading or Gouraud Shading As 1 shading calculation is required for each vertex, it is also known as per-vertex shading and sometimes referred to as per-vertex lighting. Advantage: Compared to flat shading, the smooth shading method gives much better, smoother rendering results Disadvantage: Compared to flat shading, smooth shading is more computationally intensive; however, compared to Phong shading (see later), smooth shading is computationally cheap. Smooth shading (or Gouraud shading) is the default implementation in OpenGL. You can also explicitly set the shading mode as follows: glshademodel(gl_smooth); 17

Phong Shading Even Gouraud shading gives smooth shading, the appearance of Mach bands may still be found, especially at specular highlight regions of the scene Phong proposed that - Instead of interpolating vertex intensities to get the intensities of the interior of each polygon - We interpolate the normals of the vertices of each polygon to get the normal across the interior of each polygon. - We then use the interpolated normal at each fragment to compute the intensity of the fragment. 18

Phong Shading Similar to Gouraud shading, the Phong shading method firstly computes the normal vector at each vertex of the polygon. The differences are then: o normals along the edges of the polygon are computed by interpolating the normals at the end-point vertices; n 2 o normals at interior points of the polygon are obtained by interpolating normals on the edges. n 1 normals are then interpolated for each interior point of the polygon. n 3 19

Phong Shading The differences are then: o shading value for each point of the polygon is finally computed using the normal, light vector, and view vector at that point. As 1 shading calculation is performed for each fragment, Phong shading is also known as per-fragment shading or per-fragment lighting. Advantage: Very good rendering results, especially at the highlight regions of highly specular (shiny) surfaces Disadvantage: Too computationally expensive. Only done in off-line processing. Not suitable for interactive graphics. However, it is reported that the latest graphics cards perform Phong shading in real-time. Recall that a fragment is a potential pixel which carries additional information such as depth value, colour value, opacity, etc. 20

Comparing Gouraud Shading and Phong Shading If the polygon mesh approximates surfaces with a high curvatures, Phong shading may look smooth while Gouraud shading may show edges. Both need data structures to represent meshes so we can obtain vertex normals. Gouraud shading or smooth shading or per-vertex shading Phong shading or per-fragment shading Note the difference in the highlight region of the teapot. Gouraud shading gives a flat impression in the region. 21

Example 1: Vertex Shader for Gouraud Shading method // vertex shader in vec4 vposition; in vec3 vnormal; out vec4 color; //vertex shade Here, the vertex shade (or vertex colour) is computed in the vertex shader. The colour is then passed down the pipeline where the rasterizer carries out the interpolation of the vertex colour. This is Gouraud shading or per-vertex shading. // light and material properties uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct; uniform mat4 ModelView; uniform mat4 Projection; uniform vec4 LightPosition; uniform float Shininess; out variables will be interpolated by the rasterizer. These terms are computed in the application (the.cpp file) and passed to the vertex shader as uniform variable. 22

Example 1: Vertex Shader for Gouraud Shading (cont.) void main() { // Transform vertex position into eye coordinates vec3 pos = (ModelView * vposition).xyz; vec3 L = normalize( LightPosition.xyz - pos ); vec3 E = normalize( -pos ); vec3 H = normalize( L + E ); // the half-way vector // Transform vertex normal into eye coordinates vec3 N = normalize( ModelView*vec4(vNormal, 0.0) ).xyz; Built-in function that can be used in the vertex and fragment shaders. 23

Example 1: Vertex Shader for Gouraud Shading (cont.) // Compute terms in the illumination equation vec4 ambient = AmbientProduct; float Kd = max( dot(l, N), 0.0 ); β vec4 diffuse = Kd*DiffuseProduct; float Ks = pow( max(dot(n, H), 0.0), Shininess ); vec4 specular = Ks * SpecularProduct; 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; Recall that the total shading is (see previous lecture): I = k d I d (l n) + k s I s n h β + k a I a 24

Example 1: Fragment Shader for Gouraud Shading // fragment shader in vec4 color; void main() { gl_fragcolor = color; } All the hard work has been done by the application code and the vertex shader. The fragment shader simply takes the interpolated colour and assigns it to the fragment. 25

Example 2: Vertex Shader for the Phong Shading Method // vertex shader in vec4 vposition; in vec3 vnormal; In these examples, the vertex shader passes down fn, fe, and fl variables to the rasterizer to interpolate. The fragment colour is computed in the fragment shader using the interpolated normal, light, and view vectors. // output values that will be interpolated per-fragment out vec3 fn; These out variables will be interpolated by the out vec3 fe; rasterizer further down the pipeline. out vec3 fl; uniform mat4 ModelView; uniform vec4 LightPosition; uniform mat4 Projection; This is Phong shading or per-fragment shading. 26

Example 2: Vertex Shader for Phong Shading (cont.) void main() { fn = (ModelView * vnormal).xyz; fe = - (ModelView * vposition).xyz; fl = LightPosition.xyz (ModelView * vposition).xyz; } gl_position = Projection * ModelView * vposition; 27

Example 2: Fragment Shader for Phong Shading (cont.) // fragment shader // per-fragment interpolated values from the vertex shader in vec3 fn; in vec3 fl; in vec3 fe; uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct; uniform mat4 ModelView; uniform vec4 LightPosition; uniform float Shininess; 28

Example 2: Fragment Shader for Phong Shading (cont.) void main() { // Normalize the input lighting vectors vec3 N = normalize(fn); vec3 E = normalize(fe); vec3 L = normalize(fl); vec3 H = normalize( L + E ); vec4 ambient = AmbientProduct; 29

Example 2: Fragment Shader for Phong Shading (cont.) float Kd = max(dot(l, N), 0.0); vec4 diffuse = Kd*DiffuseProduct; 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; Recall that the total shading is (see previous lecture): I = k d I d (l n) + k s I s n h β + k a I a 30

Exercises Going back to the OpenGL pipeline architecture and the shaders (vertex and fragment) that we learned from previous lectures and what we learn here, you should be able to answer the following questions: If we want to do per-vertex shading - Which shader should compute the colour of each pixel? - What variables should be declared as varying in both shaders? If we want to do per-fragment shading - Which shader should compute the colour of each pixel? - What variables should be declared as varying in both shaders? Hint: the Rasterizer of the pipeline is responsible for interpolating the output variables from the vertex shader. The interpolated values are passed to the fragment shader. 31

Further Reading Interactive Computer Graphics A Top-Down Approach with Shader-Based OpenGL by Edward Angel and Dave Shreiner, 6 th Ed, 2012 Sec. 5.5. Polygonal Shading (including all subsections, covering Flat Shading, Smooth or Gouraud Shading, and Phong Shading) Appendix A.7 Per-Fragment Lighting of Sphere Model 32