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

Similar documents
Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

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

Lecture 15: Shading-I. CITS3003 Graphics & Animation

Introduction to Computer Graphics with WebGL

CS 4600 Fall Utah School of Computing

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

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

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

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

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

Comp 410/510 Computer Graphics. Spring Shading

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

Why we need shading?

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

Illumination Models & Shading

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

Illumination and Shading

Introduction to Computer Graphics 7. Shading

Shading and Illumination

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

CS 432 Interactive Computer Graphics

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

Deferred Rendering Due: Wednesday November 15 at 10pm

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

Pipeline Operations. CS 4620 Lecture 14

Visualisatie BMT. Rendering. Arjan Kok

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

Shading II. CITS3003 Graphics & Animation

Pipeline Operations. CS 4620 Lecture 10

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

Illumination & Shading

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

CS5620 Intro to Computer Graphics

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

CS452/552; EE465/505. Intro to Lighting

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

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

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

CEng 477 Introduction to Computer Graphics Fall

Computer Graphics. Illumination and Shading

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

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 Coursework 1

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

5.2 Shading in OpenGL

Shading 1: basics Christian Miller CS Fall 2011

The Rasterization Pipeline

Computer Graphics I Lecture 11

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

Shading I Computer Graphics I, Fall 2008

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

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

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

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

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

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

CS Illumination and Shading. Slide 1

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

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

Illumination and Shading

Shading. Brian Curless CSE 457 Spring 2017

CPSC / Illumination and Shading

Illumination & Shading I

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

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

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

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

Illumination & Shading: Part 1

9. Illumination and Shading

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

CPSC 314 LIGHTING AND SHADING

University of Victoria CSC 305 Shading. Brian Wyvill 2016

Display Issues Week 5

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

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

CS 130 Final. Fall 2015

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

Illumination and Shading

CS559 Computer Graphics Fall 2015

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

Lab 9 - Metal and Glass

INF3320 Computer Graphics and Discrete Geometry

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

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

Level of Details in Computer Rendering

Transcription:

Objectives Shading in OpenGL Introduce the OpenGL shading methods - per vertex shading vs per fragment shading - Where to carry out Discuss polygonal shading - Flat - Smooth - Gouraud CITS3003 Graphics & Animation 2 OpenGL shading Front and Back Faces To compute the shading values, we need to know onormal vectors (denoted by n) omaterial properties olight 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. 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 3 4 1

Vertex Normals Complex surfaces are often represented by a triangular mesh. Consider 5 adjacent triangles sharing a common vertex p. owe can compute the normals n 1, n 2,, n 5 of the 5 triangles owe can then average these normals to get the normal n at p: n 4 n n 3 5 n = i=1 n i then n n/ n othis allows us to have normals defined at vertices owe 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 modelview 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 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 pre-defined constants. 8 2

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 φ p 1 is illuminated by the light source as it falls inside the cone but p 2 is not. p 1 φ p 2 Large e values attenuate faster Small e values attenuate slower φ θ 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. 0 φ e = 1 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 3

Polygonal Shading Methods There are 3 methods for shading a polygonal mesh: oflat shading osmooth shading (also known as Gouraud shading) ophong shading Recall that OpenGL handles only triangles. So the terms polygon and polygonal from here onward should be interpreted as triangle and triangular. 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. n 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. 13 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, othe normal vector at each vertex of the polygon is firstly computed; othe shading value at each vertex is then computed using the light vector l, normal vector n, and view vector v at that vertex; oshading values of interior points n 2 of the polygon are obtained by interpolating the shading values at the vertices. n 1 p 2 oas 1 shading calculation is required for each vertex, it is also known as per-vertex shading. p 1 compute the shading values at p 1, p 2, and p 3. 16 p 3 n 3 4

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); 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. 17 18 Phong Shading 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: onormals along the edges of the polygon are computed by interpolating the normals at the end-point vertices; onormals at interior points of the polygon are obtained by interpolating normals on the edges. normals are then interpolated for E. Angel and D. Shreiner: Interactive Computer Graphics 6E Addison-Wesley each interior 2012 point of the polygon. 19 n 1 n 2 n 3 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. May only be done in off-line processing. Not suitable for interactive graphics. However, it is reported that the latest graphics card can do Phong shading in real-time. Recall that a fragment is a potential pixel which carries additional information such 20 E. as Angel depth and D. value, Shreiner: colour Interactive value, Computer opacity, Graphics etc. 6E Addison-Wesley 2012 5

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 E. Angel a flat and impression D. Shreiner: Interactive in the region. Computer Graphics 6E Addison-Wesley 2012 21 Example 1: Vertex Shader for the Gouraud Shading method // vertex shader in vec4 vposition; in vec3 vnormal; out vec4 color; //vertex shade // 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. In the Example 1 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. 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 24 d I d (l n) + k s I s n h β + k a I a 6

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. 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. This is Phong shading or per-fragment shading. // output values that will be interpolated per-fragment out vec3 fn; These out variables will be interpolated by out vec3 fe; the rasterizer further down the pipeline. out vec3 fl; uniform mat4 ModelView; uniform vec4 LightPosition; uniform mat4 Projection; 25 26 Example 2: Vertex Shader for Phong Shading (cont.) Example 2: Fragment Shader for Phong Shading (cont.) void main() { fn = vnormal; fe = vposition.xyz; fl = LightPosition.xyz; if( LightPosition.w!= 0.0 ) { fl = LightPosition.xyz - vposition.xyz; gl_position = Projection*ModelView*vPosition; 27 // 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 7

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): 30 I = k d I d (l n) + k s I s n h β + k a I a Exercises References 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 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 8