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

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

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

Computer Graphics Coursework 1

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

INFOGR Computer Graphics

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

INFOGR Computer Graphics

Illumination and 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

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

CS452/552; EE465/505. Intro to Lighting

Computer Graphics. Illumination and Shading

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

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

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

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

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

Lab 9 - Metal and Glass

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

Illumination & Shading I

Lecture 15: Shading-I. CITS3003 Graphics & Animation

CS559 Computer Graphics Fall 2015

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

CS 432 Interactive Computer Graphics

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

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

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

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

The Rasterization Pipeline

Programming Graphics Hardware

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

Illumination and Shading with GLSL/WebGL

CS452/552; EE465/505. Lighting & Shading

Illumination & Shading: Part 1

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

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

Lighting. Figure 10.1

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

Comp 410/510 Computer Graphics. Spring Shading

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

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

Phong Lighting & Materials. Some slides modified from: David Kabala Others from: Andries Van Damm, Brown Univ.

Shading I Computer Graphics I, Fall 2008

1 of 5 9/10/ :11 PM

Ambient reflection. Jacobs University Visualization and Computer Graphics Lab : Graphics and Visualization 407

Ambient Occlusion. Ambient Occlusion (AO) "shadowing of ambient light "darkening of the ambient shading contribution

INF3320 Computer Graphics and Discrete Geometry

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

CSE 681 Illumination and Phong Shading

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

OpenGL Performances and Flexibility

Today we will start to look at illumination models in computer graphics

Deferred Rendering Due: Wednesday November 15 at 10pm

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

CS 4620 Midterm, October 23, 2018 SOLUTION

Mobile Application Programing: Android. OpenGL Operation

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

Programmable Graphics Hardware

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

Texture Mapping. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

OpenGL Performances and Flexibility. Visual Computing Laboratory ISTI CNR, Italy

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

[175 points] The purpose of this assignment is to give you practice with shaders in OpenGL.

Illumination Models & Shading

Shading Language Update

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

12.2 Programmable Graphics Hardware

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

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

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

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

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

Lighting and Shading. Slides: Tamar Shinar, Victor Zordon

Illumination in Computer Graphics

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

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

Illumination & Shading

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

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Why we need shading?

Sign up for crits! Announcments

CS 4600 Fall Utah School of Computing

Three-Dimensional Graphics V. Guoying Zhao 1 / 55

w Foley, Section16.1 Reading

OPENGL RENDERING PIPELINE

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Computer Graphics with OpenGL ES (J. Han) Chapter XI Normal Mapping

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

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

Computer Graphics and GPGPU Programming

OUTLINE. Implementing Texturing What Can Go Wrong and How to Fix It Mipmapping Filtering Perspective Correction

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

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

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

OpenGL Programmable Shaders

Computergraphics Exercise 15/ Shading & Texturing

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

Transcription:

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

Vertex and Fragment Shaders The inputs to the fragment shader Varyings: The per-vertex output variables produced by the vertex shader are interpolated to determine the per-fragment ones. Uniforms to be used by the fragment shader. Samplers = Textures. The fragment color stored in gl_fragcolor is passed to the output merger. Computer Graphics with OpenGL ES (J. Han) 6-2

Fragment Shader A simplest fragment shader uniform sampler2d s_tex0; varying vec2 v_texcoord; void main() { gl_fragcolor = texture2d(s_tex0, v_texcoord); } The above fragment shader declares a uniform variable, s_tex0, of type sampler2d, which represents a texture. Recall that our vertex shader simply output 2D texture coordinates without modification. The per-vertex texture coordinates were interpolated so that the per-fragment texture coordinates, v_texcoord, are now passed to the fragment shader and used for fetching the texture, s_tex0. The fragment shader invokes the built-in function texture2d, which returns a color. It is output to the built-in variable gl_fragcolor. Computer Graphics with OpenGL ES (J. Han) 6-3

Phong Lighting Model Illumination or lighting refers to the techniques handling the interaction between light sources and objects. In real-time applications, the most popular illumination method is based on the Phong model. Even though it is not physically correct, it is widely adopted in commercial games and lays foundations of various advanced lighting techniques. Computer Graphics with OpenGL ES (J. Han) 6-4

Phong Lighting Model - Diffuse Term Among various light sources (including point, area, spot, and directional light sources), for now let s take the simplest, the directional light source, where the light vector (l) connecting a surface point and the faraway light source is constant for the entire scene. The Phong model is composed of diffuse, specular, ambient, and emissive terms. For the sake of simplicity, let us consider the diffuse term only. The diffuse term is based on Lambert s law. Reflections from ideally diffuse surfaces (Lambertian surfaces) are scattered with equal intensity in all directions. The amount of reflection perceived by the eye is just proportional to the amount of incoming light, which is inversely proportional to the angle between l and the surface normal n. It is described as n l. In order to avoid negative reflection, it is modified into max(n l, 0). Computer Graphics with OpenGL ES (J. Han) 6-5

Phong Lighting Model - Diffuse Term (cont d) Suppose a white light (1,1,1). If an object lit by the light appears yellow, it means that the object reflects R and G and absorbs B. We can easily implement this kind of filtering through material parameter, i.e., if it is (1,1,0), then (1,1,1) (1,1,0)=(1,1,0) where is component-wise multiplication. R G B intensity 1 1 1 1x1=1 1x1=1 1x0=0 R G B intensity.5.5.5.5x.5=.25.5x1=.5.5x.5=.25 (1,1,0) (.5,1,.5) The diffuse term combines the intensity and color: max(n l, 0) s d m d where s d is the light source color and m d is the object material color. The texture provides m d whereas s d is given by the user. Computer Graphics with OpenGL ES (J. Han) 6-6

Per-fragment Lighting Let us show how to implement Phong lighting (with the diffuse term only) and texturing. The vertex shader shown below computes the clip-space vertex position, as usual, and also the world-space vertex position and normal of each vertex. In addition, it bypasses the texture coordinates. The output variables v_position, v_normal, and v_texcoord will be interpolated for fragments and they will be provided for the fragment shader. uniform mat4 viewprojmat; uniform mat4 worldmat; attribute vec3 position; attribute vec3 normal; attribute vec2 texcoord; varying vec3 v_position; varying vec3 v_normal; varying vec2 v_texcoord; void main() { gl_position = viewprojmat * worldmat * vec4(position, 1.0); v_position = (worldmat * vec4(position, 1.0)).xyz; v_normal = mat3(worldmat) * normal; v_texcoord = texcoord; } Computer Graphics with OpenGL ES (J. Han) 6-7

Per-fragment Lighting (cont d) The fragment shader shown below obtains the object material color (m d ) by filtering the texture and then asks the user-defined function phongdiffuse to compute the diffuse term. It uses uniform vec3 lightpos for calculating the light vector l (lightdirection) and simply implements max(n l, 0) s d m d. uniform sampler2d s_tex0; uniform vec3 lightpos; uniform vec3 Sd; varying vec3 v_position; varying vec3 v_normal; varying vec2 v_texcoord; vec3 phongdiffuse(vec3 Md) { // The diffuse term of Phong model per fragment vec3 lightdirection = normalize(lightpos - v_position); vec3 nor = normalize(v_normal); vec3 phongdiffuse(vec3 Md); // Forward declaration void main() { vec3 Md = texture2d(s_tex0, v_texcoord).xyz; vec3 phongcolor = phongdiffuse(md); } gl_fragcolor = vec4(phongcolor, 1.0); } //Diffuse term float Diffuse = max(dot(nor, lightdirection), 0.0); vec3 PhongDiffuse = Diffuse * Sd * Md; return PhongDiffuse; Computer Graphics with OpenGL ES (J. Han) 6-8