Computer Graphics Shading in OpenGL

Slides:



Advertisements
Similar presentations
Computer Graphics Shading Lecture 13 John Shearer
Advertisements

Polygon Rendering Flat Rendering Goraud Rendering Uses Phong Reflectance Phong Rendering.
2 COEN Computer Graphics I Evening’s Goals n Discuss the fundamentals of lighting in computer graphics n Analyze OpenGL’s lighting model n Show.
CSPC 352: Computer Graphics
Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive.
1. What is Lighting? 2 Example 1. Find the cubic polynomial or that passes through the four points and satisfies 1.As a photon Metal Insulator.
Based on slides created by Edward Angel
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Shading I.
Computer Graphics - Class 10
Shading in OpenGL CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
Shading in OpenGL Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
CS5500 Computer Graphics March 26, Shading Reference: Ed Angel’s book.
CS 480/680 Computer Graphics Shading I Dr. Frederick C Harris, Jr.
Computer Graphics Lighting.
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Chapter 6: Shading Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Shading (introduction to rendering). Rendering  We know how to specify the geometry but how is the color calculated.
COMPUTER GRAPHICS CS 482 – FALL 2014 AUGUST 27, 2014 FIXED-FUNCTION 3D GRAPHICS MESH SPECIFICATION LIGHTING SPECIFICATION REFLECTION SHADING HIERARCHICAL.
Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL.
Shading in OpenGL.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Chapter 6 Shading. 2 Objectives Learn to shade objects so their images appear three-dimensional Introduce the types of light-material interactions Build.
19/17/ :25 UML Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera Real Light Synthetic.
Computer Graphics I, Fall 2010 Shading in OpenGL.
Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CSE 381 – Advanced Game Programming GLSL Lighting.
CG Summary: OpenGL Shading andTextures Angel, Chapters 5, 7; “Red Book” slides from AW, red book, etc. CSCI 6360/4360.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
11/04/04© University of Wisconsin, CS559 Fall 2004 Last Time Visibility –Z-Buffer and transparency –A-buffer –Area subdivision –BSP Trees –Exact Cell-Portal.
Illumination and Shading
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
1 Graphics CSCI 343, Fall 2015 Lecture 21 Lighting and Shading III.
Lecture Fall 2001 Illumination and Shading in OpenGL Light Sources Empirical Illumination Shading Transforming Normals Tong-Yee Lee.
Shading. For Further Reading Angel 7 th Ed: ­Chapter 6 2.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Day 06 Vertex Shader for Ambient-Diffuse-Specular Lighting.
Lighting and Shading Part 2. Global Ambient Light There are at least 8 OpenGL lights and 1 Global Ambient Setting the Global Ambient globalAmbient[] =
Render methods. Contents Levels of rendering Wireframe Plain shadow Gouraud Phong Comparison Gouraud-Phong.
Lighting and Reflection Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Advanced Texture Mapping Bump Mapping & Environment Mapping (Reflection)
Chapter 5. Lighting Computer Graphics (spring, 2009) School of Computer Science University of Seoul.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 55 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 55 Computer Graphics Three-Dimensional Graphics V.
© University of Wisconsin, CS559 Spring 2004
Shading To determine the correct shades of color on the surface of graphical objects.
Introduction to Computer Graphics with WebGL
CS 480/680 Computer Graphics Shading.
School of Computer Science
CSC461: Lecture 23 Shading Computation
Vectors, Normals, & Shading
Shading in OpenGL Ed Angel
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Lighting Phong's Lighting Model normals
CSE 470 Introduction to Computer Graphics Arizona State University
Illumination and Shading
Lighting and Materials
Introduction to Computer Graphics with WebGL
CS5500 Computer Graphics April 10, 2006.
Isaac Gang University of Mary Hardin-Baylor
Introduction to Computer Graphics with WebGL
ICG 2018 Fall Homework1 Guidance
Last Time Liang-Barsky Details Weiler-Atherton clipping algorithm
Computer Graphics Material Colours and Lighting
CS 480/680 Computer Graphics Shading.
CS 480/680 Computer Graphics Shading.
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science
Presentation transcript:

Computer Graphics Shading in OpenGL Dr. Frederick C Harris, Jr.

Objectives Introduce the OpenGL shading methods per vertex vs per fragment shading Where to carry out Discuss polygonal shading Flat Smooth Gouraud

OpenGL shading Need Normals material properties Lights State-based shading functions have been deprecated (glNormal, glMaterial, glLight) Get computer in application or send attributes to shaders

Normalization Length can be affected by transformations Cosine terms in lighting calculations can be computed using dot product Unit length vectors simplify calculation Usually we want to set the magnitudes to have unit length but Length can be affected by transformations Note that scaling does not preserved length GLSL has a normalization function

Normal for Triangle p1 n p2 plane n ·(p - p0 ) = 0 n = (p2 - p0 ) ×(p1 - p0 ) p normalize n  n/ |n| p0 Note that right-hand rule determines outward face

Specifying a Point Light Source For each light source, we can set an RGBA for the diffuse, specular, and ambient components, and for the position 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); vec4 light0_pos =vec4(1.0, 2.0, 3,0, 1.0);

Distance and Direction The source colors are specified in RGBA The 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 The coefficients in distance terms are usually quadratic (1/(a+b*d+c*d*d)) where d is the distance from the point being rendered to the light source

Spotlights f Derive from point source Direction Cutoff Attenuation Proportional to cosaf f -q q

Global Ambient Light Ambient light depends on color of light sources A red light in a white room will cause a red ambient term that disappears when the light is turned off A global ambient term that is often helpful for testing

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

Material Properties Material properties should match the terms in the light model Reflectivities w component gives opacity vec4 ambient = vec4(0.2, 0.2, 0.2, 1.0); vec4 diffuse = vec4(1.0, 0.8, 0.0, 1.0); vec4 specular = vec4(1.0, 1.0, 1.0, 1.0); GLfloat shine = 100.0

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

Emissive Term We can simulate a light source in OpenGL by giving a material an emissive component This component is unaffected by any sources or transformations

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 regardless of A Later we will enable blending and use this feature

Polygonal Shading In per vertex shading, shading calculations are done for each vertex Vertex colors become vertex shades and can be sent to the vertex shader as a vertex attribute Alternately, we can send the parameters to the vertex shader and have it compute the shade By default, vertex shades are interpolated across an object if passed to the fragment shader as a varying variable (smooth shading) We can also use uniform variables to shade with a single shade (flat shading)

Polygon Normals Triangles have a single normal Shades at the vertices as computed by the Phong model can be almost same Identical for a distant viewer (default) or if there is no specular component Consider model of sphere Want different normals at each vertex even though this concept is not quite correct mathematically

Smooth Shading We can set a new normal at each vertex Easy for sphere model If centered at origin n = p Now smooth shading works Note silhouette edge

Mesh Shading The previous example is not general because we knew the normal at each vertex analytically For polygonal models, Gouraud proposed we use the average of the normals around a mesh vertex n = (n1+n2+n3+n4)/ |n1+n2+n3+n4|

Gouraud and Phong Shading Gouraud Shading Find average normal at each vertex (vertex normals) Apply modified Phong model at each vertex Interpolate vertex shades across each polygon Phong shading Find vertex normals Interpolate vertex normals across edges Interpolate edge normals across polygon Apply modified Phong model at each fragment

Comparison Until recently not available in real time systems If the polygon mesh approximates surfaces with a high curvatures, Phong shading may look smooth while Gouraud shading may show edges Phong shading requires much more work than Gouraud shading Until recently not available in real time systems Now can be done using fragment shaders Both need data structures to represent meshes so we can obtain vertex normals

Vertex Lighting Shaders I // 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;

Vertex Lighting Shaders II 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 ); // Transform vertex normal into eye coordinates vec3 N = normalize( ModelView*vec4(vNormal, 0.0) ).xyz;

Vertex Lighting Shaders III // 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; }

Vertex Lighting Shaders IV // fragment shader in vec4 color; void main() { gl_FragColor = color; }

Fragment Lighting Shaders I // vertex shader in vec4 vPosition; in vec3 vNormal; // output values that will be interpolatated per-fragment out vec3 fN; out vec3 fE; out vec3 fL; uniform mat4 ModelView; uniform vec4 LightPosition; uniform mat4 Projection;

Fragment Lighting Shaders II 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;

Fragment Lighting Shaders III // 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;

Fragment Lighting Shaders IV 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;

Fragment Lighting Shaders V 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; }