Lecture 11 Shaders and WebGL. October 8, 2015

Similar documents
WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

CPSC 436D Video Game Programming

OPENGL RENDERING PIPELINE

WebGL. Creating Interactive Content with WebGL. Media #WWDC14. Session 509 Dean Jackson and Brady Eidson WebKit Engineers

CS452/552; EE465/505. Review & Examples

WebGL: Hands On. DevCon5 NYC Kenneth Russell Software Engineer, Google, Inc. Chair, WebGL Working Group

Introduction to OpenGL/GLSL and WebGL GLSL

COMP371 COMPUTER GRAPHICS

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

CS559 Computer Graphics Fall 2015

Introductory Seminar

2D graphics with WebGL

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

12.2 Programmable Graphics Hardware

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

Programmable Graphics Hardware

CS 432 Interactive Computer Graphics

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

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

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

JavaScript. Interpolated points

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

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

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

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

CS452/552; EE465/505. Image Formation

The Graphics Pipeline

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

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

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

The Graphics Pipeline

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

CS452/552; EE465/505. Image Processing Frame Buffer Objects

CSE 167. Discussion 03 ft. Glynn 10/16/2017

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

OpenGL shaders and programming models that provide object persistence

Mining the Rendering Power in Web Browsers. Jianxia Xue Jan. 28, 2014

Supplement to Lecture 22

Converts geometric primitives into images Is split into several independent stages Those are usually executed concurrently

Computergraphics Exercise 15/ Shading & Texturing

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

Mobile Application Programing: Android. OpenGL Operation

CS452/552; EE465/505. Transformations

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

Programming with OpenGL Complete Programs Objectives Build a complete first program

Geometry Shaders. And how to use them

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

Comp4422. Computer Graphics. Lab 02: WebGL API Prof. George Baciu. PQ838 x7272.

Introduction to Computer Graphics with WebGL

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

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

Programming shaders & GPUs Christian Miller CS Fall 2011

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

Introduction to the OpenGL Shading Language (GLSL)

Mobile Application Programing: Android. OpenGL Operation

Rasterization-based pipeline

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

Tutorial 04. Harshavardhan Kode. September 14, 2015

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

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

Modern OpenGL Guide. Alexander Overvoorde. May Introduction 3 Credits... 3 Prerequisites... 4

Mobile Application Programming: Android. OpenGL ES 3D

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

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

CS 4620 Midterm, October 23, 2018 SOLUTION

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

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

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

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

Programming Graphics Hardware

Particle Systems with Compute & Geometry Shader. Institute of Computer Graphics and Algorithms Vienna University of Technology

Mobile graphics API Overview

BCA611 Video Oyunları için 3B Grafik

Practical Texturing (WebGL) CS559 Fall 2016 Lecture 20 November 7th 2016

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

Introduction to Shaders.

INTRODUCTION TO OPENGL PIPELINE

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

Computação Gráfica. Computer Graphics Engenharia Informática (11569) 3º ano, 2º semestre. Chap. 4 Windows and Viewports

Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

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

More frames per second. Alex Kan and Jean-François Roy GPU Software

Mobile Application Programming: Android. OpenGL Operation

Sign up for crits! Announcments

An Overview GLUT GLSL GLEW

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/C++.

Computer Graphics Seminar

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Computer Graphics (CS 4731) & 2D Graphics Systems

Introduction to Computer Graphics with WebGL

Transcription:

Lecture 11 Shaders and WebGL October 8, 2015

Review Graphics Pipeline (all the machinery) Program Vertex and Fragment Shaders WebGL to set things up

Key Shader Concepts Fragment Processing and Vertex Processing Each does their own step in the pipeline

Vertex Shader Process each vertex independently Inputs: attribute variables (from buffers) uniform variables (constants from app) Outputs: gl_position (special magic variable) varying variables (interpolated for fragment shaders)

Fragment Shader Process each fragment (pixel) independently Inputs: varying variables (outputs from vertex shader) uniform variables (constants from app) Outputs: gl_fragcolor (magic variable) Other things for fragment tests

GLSL A special language for shaders Compiler built into graphics driver Used to write both kinds of shaders

GLSL Basics main function always the entry point to a shader no arguments no return value inputs through variables outputs through variables

GLSL Type System Strongly and Strictly Typed floats and ints are different need to be explicit about all conversions Useful math types Short vectors: vec2 vec3 vec4 Small matrices mat3 mat4

Vector operations vec3 v; float a = v.x; // access a component vec2 b = v.xy; vec2 c = v.yz; // any subset vec3 d = v.zyx; vec3 e = v.xxx; // any order (swizzle) // even repeats

Assembling vectors with type operators vec2 a,b; vec3 c; vec3 f = vec3(1.0,2.0,3.0); vec3 g = vec3(1,2,3); // rare times an integer works vec3 h = vec3(a,1); vec3 j = vec3(1,a); vec4 k = vec4(a,b); vec4 l = vec4(a,c.xy);

Linear Algebra is Built In vec4 x; vec3 p; mat4 m; vec4 y = M * x; vec4 z = M * vec4(p,1); float a = dot(x, vec4(p,0);

Yellow (simplest) shdr.bkcore

Yellow (vertex) precision highp float; attribute vec3 position; uniform mat4 modelviewmatrix; uniform mat4 projectionmatrix; void main() { vec4 pos = modelviewmatrix * vec4(position, 1.0); gl_position = projectionmatrix * pos; }

Yellow (fragment) precision highp float; void main() { gl_fragcolor = vec4(1,1,0, 1.0); }

Yellow Diffuse The P4 shader (part of P5 as well) Sortof: this does the lighting in the fragment shader shdr.bkcore

Yellow Diffuse (vertex) precision highp float; attribute vec3 position; attribute vec3 normal; uniform mat3 normalmatrix; uniform mat4 modelviewmatrix; uniform mat4 projectionmatrix; varying vec3 fnormal; void main() { fnormal = normalize(normalmatrix * normal); vec4 pos = modelviewmatrix * vec4(position, 1.0); gl_position = projectionmatrix * pos; }

Note the inputs The application program sets the attributes and uniforms We need to use the same names Here the "application" is shdr.bkcore

Yellow Diffuse (fragment) precision highp float; varying vec3 fnormal; void main() { vec3 dir = vec3(0,1,0); // high noon vec3 color = vec3(1,1,0); // yellow } float diffuse =.5 + dot(fnormal,dir); gl_fragcolor = vec4(diffuse * color, 1.0);

Vertex Colors Compute the colors in the vertex shader Pass to Fragment shader shdr.bkcore

Vertex Colors (Fragment) precision highp float; varying vec3 vcolor; void main() { gl_fragcolor = vec4(vcolor, 1.0); }

Vertex Colors (Vertex) precision highp float; attribute vec3 position; uniform mat4 modelviewmatrix; uniform mat4 projectionmatrix; varying vec3 vcolor; void main() { vec4 pos = modelviewmatrix * vec4(position, 1.0); gl_position = projectionmatrix * pos; } vcolor = vec3(0,.7,1);

Something different... Color the right side of the screen is different GLSL built in variables (reference) Warning: gl_fragcoord is special (in pixels) Warning: resolution comes from shdr.bkcore http://goo.gl/hy9ir6

precision highp float; uniform vec2 resolution; void main() { vec3 color; // gl_fragcoord is in pixels - so convert... float ndcx = (gl_fragcoord.x / resolution.x) - 1.0; } if (ndcx > 0.0) { color = vec3(1,1,0); } else { color = vec3(1,0,1); } gl_fragcolor = vec4(color, 1.0);

Control structures If-then-else if (ndcx > 0.0) { color = vec3(1,1,0); } else { color = vec3(1,0,1); } Step color = mix(vec3(1,1,0), vec3(1,0,1), step(ndcx,0.0) );

Step and Smoothstep color = mix(vec3(1,1,0), vec3(1,0,1), step(0.0, ncdx) ); color = mix(vec3(1,1,0), vec3(1,0,1), smoothstep(-0.1, 0.1, ncdx) );

use 3D positions for colors shdr.bkcore What coordinates system to use position? World coordinates? Local coordinates? Stripes Checkers

Making cool shaders Is actually hard in shdr.bkcore stuck with their attributes stuck with their uniforms they do give time Siren consult the help

Sifakis Shader Sifakis Shader

Connecting to the program

Vertex Shader: ins and outs attribute vec3 a1; attribute vec4 a2; uniform float u1; uniform float u2; varying vec3 v1; varying vec3 v2; main() { gl_position =... v1 =... v2 =... }

Fragment Shader: ins and outs uniform float u1; uniform float u2; varying vec3 v1; varying vec3 v2; // same as vertex shader // same as vertex shader main() { gl_fragcolor =... discard(); }

Set up this shader // first compile the vertex shader var vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader,vertexsource); gl.compileshader(vertexshader); This creates a shader object

Hook 2 shaders together var shaderprogram = gl.createprogram(); gl.attachshader(shaderprogram, vertexshader); gl.attachshader(shaderprogram, fragmentshader); gl.linkprogram(shaderprogram); This creates a shader program object (draw wiring diagram)

Find an attribute location var posloc = gl.getattriblocation(shaderprogram, "pos"); gl.enablevertexattribarray(posloc); This gives an integer (which is used in enable)

Buffers There are many kinds of buffers For now, we are just using attribute arrays An array with one value per vertex values can be points (1D, 2D 3D) called the stride

Make a buffer and fill var mybuf = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, mybuf); gl.bufferdata(gl.array_buffer, new Float32Array(vertexPos), gl.static_draw); creates a WebGL Buffer Object note magic constants (gl.arraybuffer, gl.staticdraw) note the we need a Float32Array

Using Two Buffers Assume we have buf1 & buf2, and loc1 & loc2 gl.bindbuffer(gl.array_buffer, buf1); gl.vertexattribpointer(attr1loc, 3 /*itemsize*/, gl.float, false, 0, 0); gl.bindbuffer(gl.array_buffer, buf2); gl.vertexattribpointer(attr2loc, 3 /*itemsize*/, gl.float, false, 0, 0); gl.drawarrays(gl.triangles, 0, numitems); Draw triangles (every 3 vertices = 1 triangle)

Other ways to send data triangle stips, fans indexed arrays interleaved arrays (and many more)