Illumination and Shading with GLSL/WebGL

Similar documents
CS 418: Interactive Computer Graphics. Basic Shading in WebGL. Eric Shaffer

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

Illumination & Shading I

Computer Graphics Coursework 1

CS452/552; EE465/505. Lighting & Shading

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

Illumination and Shading

CS 148, Summer 2012 Introduction to Computer Graphics and Imaging

5.2 Shading in OpenGL

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

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

CS 432 Interactive Computer Graphics

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Introduction to Computer Graphics with WebGL

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

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

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

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

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

Illumination and Shading

CS 4600 Fall Utah School of Computing

CS Computer Graphics: Illumination and Shading I

CS Computer Graphics: Illumination and Shading I

INSTANCE VARIABLES (1/2) P. 1

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

Shading and Illumination

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

INFOGR Computer Graphics

CS559 Computer Graphics Fall 2015

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

Mobile Application Programing: Android. OpenGL Operation

Game Graphics & Real-time Rendering

INFOGR Computer Graphics

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

Ambient and Diffuse Light

Lighting. CSC 7443: Scientific Information Visualization

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

Pipeline Operations. CS 4620 Lecture 14

REALISTIC RENDERING IN WEBGL

Mobile Application Programing: Android. OpenGL Operation

CS 130 Final. Fall 2015

BCA611 Video Oyunları için 3B Grafik

Graphics Processing Units (GPUs) V1.2 (January 2010) Outline. High-Level Pipeline. 1. GPU Pipeline:

CS452/552; EE465/505. Intro to Lighting

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

Lecture 10: Shading Languages. Kayvon Fatahalian CMU : Graphics and Imaging Architectures (Fall 2011)

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

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

Shading Language Update

Introduction to Programmable GPUs CPSC 314. Real Time Graphics

Shading , Fall 2004 Nancy Pollard Mark Tomczak

CS 4620 Midterm, October 23, 2018 SOLUTION

ECS 175 COMPUTER GRAPHICS. Ken Joy.! Winter 2014

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

Mobile Application Programming: Android. OpenGL Operation

Illumination and Shading

Appendix D: POL A Binary File Format for Polygonal Models

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

Lab 9 - Metal and Glass

Supplement to Lecture 22

CS5620 Intro to Computer Graphics

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

1 of 5 9/10/ :11 PM

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

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

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

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

Graphics Processing Units (GPUs) V2.2 (January 2013) Jan Kautz, Anthony Steed, Tim Weyrich

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

CG Surfaces: Materials, Shading, and Texturing

OpenGL Lighting Computer Graphics Spring Frank Palermo

OPENGL RENDERING PIPELINE

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

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

INF3320 Computer Graphics and Discrete Geometry

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

Interactive Real-Time Raycasting

Introduction to Computer Graphics. Hardware Acceleration Review

Introduction to Computer Graphics with WebGL

Shading. Brian Curless CSE 457 Spring 2017

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

Ligh%ng in OpenGL. The Phong Illumina%on Model. Vector Background

Introduction to Programmable GPUs CPSC 314. Introduction to GPU Programming CS314 Gordon Wetzstein, 09/03/09

CS GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

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

Introduction to Computer Graphics with WebGL

Ligh%ng and Shading. Ligh%ng and Shading. q Is this a plate or a ball? q What color should I set for each pixel?

Game Graphics & Real-time Rendering

SUMMARY. CS380: Introduction to Computer Graphics Projection Chapter 10. Min H. Kim KAIST School of Computing 18/04/12. Smooth Interpolation

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

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

To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Foundations of Computer Graphics (Spring 2012)

Deferred Rendering Due: Wednesday November 15 at 10pm

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

University of Victoria CSC 305 Shading. Brian Wyvill 2016

Shaders. Slide credit to Prof. Zwicker

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

Transcription:

Illumination and Shading with GLSL/WebGL

Illumination with Shaders! Remember the local illumination model (Phong illumination) Illumination = ambient + diffuse + specular = Ka x Ia + Kd x Id x (N.L) + Ks x Is x (R.V) n Ambient Diffuse Specular

What you need to do in your WebGL code! Define the light(s ) position and its ambient, diffuse, and specular intensities (Ia, Id, Is in R,G,B)! Define the vertex normals (create a VBO)! Define object material properties (ambient, diffuse, and specular reflectance coefficients Ka, Kd, Ks)! Compute the normal transformation matrix (inverse-transpose of modelview matrix)

Uniforms or Attributes?! The light properties are definitely defined as uniforms (do not change per vertex)! The normals are attributes each vertex has its own normal! The material properties can be attributes or uniforms depends on whether you want to change them (Ka, Kd, Ks) per vertex

Define and Pass Lights var light_ambient = [0,0,0,1]; var light_diffuse= [.8,.8,.8,1]; var light_specular = [1,1,1,1]; var light_pos = [10,10,0,1]; Variables defined in your shaders var l1 = gl.getuniformlocation(programobject, "light_ambient"); var l2 = gl.getuniformlocation(programobject, "light_diffuse"); var l3 = gl.getuniformlocation(programobject, "light_specular"); var lp = gl.getuniformlocation(programobject, "light_pos"); gl.uniform4f(l1, light_ambient[0], light_ambient[1], light_ambient[2], 1.0); gl.uniform4f(l2, light_diffuse[0], light_diffuse[1], light_diffuse[2], 1.0); gl.uniform4f(l3, light_specular[0], light_specular[1], light_specular[2],1.0); gl.uniform4f(lp, light_pos[0], light_pos[1], light_pos[2], light_pos[3]);

Define and Pass Materials Here I am setting materials as uniforms (not per vertex) var mat_ambient = [0, 0, 0, 1]; var mat_diffuse= [1, 1, 0, 1]; var mat_specular = [.9,.9,.9,1]; Var mat_shine = [50]; Variables defined in your shaders var d1 = gl.getuniformlocation(programobject, "ambient_coef"); var d2 = gl.getuniformlocation(programobject, "diffuse_coef"); var d3 = gl.getuniformlocation(programobject, "specular_coef"); var d4 = gl.getuniformlocation(programobject, "mat_shininess"); gl.uniform4f(d1, mat_ambient[0], mat_ambient[1], mat_ambient[2], 1.0); gl.uniform4f(d2, mat_diffuse[0], mat_diffuse[1], mat_diffuse[2], 1.0); gl.uniform4f(d3, mat_specular[0], mat_specular[1], mat_specular[2],1.0); gl.uniform1f(d4, mat_shine[0]);

Define a normal for each vertex and pass with the VBO /// Define the Vertex attributes location[4]; normal[4]; color[4]; v2 Create VBOs for normals as well n v1 The vertex normal n = v 1 cross product v2 Note:make the fourth component of the normal Normal[3] = 0.0

Where do I calculate lighting! You need to calculate lighting in eye space! Lighting cannot be calculated in clip space because the shape of the object, therefore all the angles, are distorted! This means that you need to pass the modelview matrix to the shader as uniforms! Why? Modelview matrix transforms your vertices from local space to eye space

Normals need to be transformed too! Like you vertex positions need to be transformed to eye space, the normals need to be transformed too! The normal [u,v,w] will have the form [u,v,w,0] in the homogeneous space! The matrix to transform the normal is the inverse transpose of the modelview matrix normal matrix = (modelview -1 ) T normal_matrix = mat4.inverse(modelview); normal_matrix = mat4.transpose(normal_matrix);

Pass the matrices var m1 = gl.getuniformlocation(programobject, ummatrix"); // model matrix var m2 = gl.getuniformlocation(programobject, uvmatrix"); // view matrix var m3 = gl.getuniformlocation(programobject, upmatrix"); // projection matrix var m4 = gl.getuniformlocation(programobject, unmatrix); // normal matrix gl.uniformmatrix4fv(m1, false, mmatrix); gl.uniformmatrix4fv(m2, false, vmatrix); gl.uniformmatrix4fv(m3, false, pmatrix); gl.uniformmatrix4fv(m4, false, nmatrix); Variables defined in your javascript

Vertex Shader precision mediump float; attribute vec3 avertexposition; attribute vec3 avertexnormal; attribute vec4 avertexcolor; uniform mat4 ummatrix; uniform mat4 uvmatrix; uniform mat4 upmatrix; uniform mat4 unmatrix; uniform vec4 ambient_coef; uniform vec4 diffuse_coef; uniform vec4 specular_coef; uniform float mat_shininess; uniform vec4 light_pos; uniform vec4 light_ambient; uniform vec4 light_diffuse; uniform vec4 light_specular; varying vec4 eye_pos; //vertex position in eye space varying vec3 v_normal; // vertex normal main(){ // will discuss the implementation in class }

Fragment Shader precision mediump float; uniform mat4 ummatrix; uniform mat4 uvmatrix; uniform mat4 upmatrix; uniform mat4 unmatrix; uniform vec4 ambient_coef; uniform vec4 diffuse_coef; uniform vec4 specular_coef; uniform float mat_shininess; uniform vec4 light_pos; uniform vec4 light_ambient; uniform vec4 light_diffuse; uniform vec4 light_specular; varying vec4 eye_pos; //vertex position in eye space varying vec3 v_normal; // vertex normal main(){ // will discuss the implementation in class }