2D graphics with WebGL

Similar documents
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

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

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

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

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

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

Supplement to Lecture 22

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

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

OPENGL RENDERING PIPELINE

CS 432 Interactive Computer Graphics

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

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

Programming with OpenGL Complete Programs Objectives Build a complete first program

The Graphics Pipeline

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

The Graphics Pipeline

12.2 Programmable Graphics Hardware

Programmable Graphics Hardware

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

CS452/552; EE465/505. Image Formation

Objectives. Open GL Shading Language (GLSL)

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

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

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

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

Introduction to Computer Graphics with WebGL

Sign up for crits! Announcments

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

CS452/552; EE465/505. Review & Examples

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

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

Computer Graphics (CS 4731) & 2D Graphics Systems

Lecture 11 Shaders and WebGL. October 8, 2015

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

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

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

Mobile Application Programing: Android. OpenGL Operation

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

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

CPSC 436D Video Game Programming

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

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

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

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

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

BCA611 Video Oyunları için 3B Grafik

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

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

Mobile Application Programing: Android. OpenGL Operation

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

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

OPEN GL BACKGROUND. Objectives. Early History of APIs. SGI and GL. PHIGS and X. Modified from Angel 6e book slides

Objectives. Programming with WebGL Part 1: Background. Retained vs. Immediate Mode Graphics. Early History of APIs. PHIGS and X.

Programming with WebGL Part 1: Background. CS 432 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

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

Shaders. Slide credit to Prof. Zwicker

Introduction to the OpenGL Shading Language (GLSL)

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

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

CS179: GPU Programming

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

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

Introduction to Computer Graphics with WebGL

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Content. Building Geometry Appearance Lights Model Loaders

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

Overview. By end of the week:

CS559 Computer Graphics Fall 2015

Mobile graphics API Overview

Introduction to GLSL. Announcements. Agenda. Course Contents. Patrick Cozzi University of Pennsylvania CIS Fall 2012

Introduction to Computer Graphics. Hardware Acceleration Review

COMP371 COMPUTER GRAPHICS

Computergraphics Exercise 15/ Shading & Texturing

Programming shaders & GPUs Christian Miller CS Fall 2011

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

CS 432 Interactive Computer Graphics

Lecture 09: Shaders (Part 1)

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

CS452/552; EE465/505. Texture Mapping in WebGL

Mobile Application Programming: Android. OpenGL Operation

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

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

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

Lecture 9(B): GPUs & GPGPU

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

Game Graphics & Real-time Rendering

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

Programmable Graphics Hardware

INTRODUCTION TO OPENGL PIPELINE

Buffers. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

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

Shading Language Update

CSE Real Time Rendering Week 3 & 4

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

We assume that you are familiar with the following:

Transcription:

2D graphics with WebGL Some material contained here is adapted from the book s slides. September 7, 2015 (Dr. Mihail) 2D graphics September 7, 2015 1 / 22

Graphics Pipeline (Dr. Mihail) 2D graphics September 7, 2015 2 / 22

Shaders So far we ve seen trivial vertex and fragment shaders: 1 <script id=" vertex - shader " type="x- shader /x- vertex "> 2 attribute vec4 vposition ; 3 void main () 4 { 5 gl_pointsize = 1.0; 6 gl_position = vposition ; 7 } 8 </ script > 9 10 11 <script id=" fragment - shader " type="x- shader /x- fragment "> 12 precision mediump float ; 13 void main () 14 { 15 gl_fragcolor = vec4 ( 1.0, 0.0, 0.0, 1.0 ); 16 } 17 </ script > This vertex shader simply passes the vertex position attribute (from the application) through the pipeline. The fragment shader hardcodes every fragment red. (Dr. Mihail) 2D graphics September 7, 2015 3 / 22

Attributes Vertex Attributes The only mandatory attribute for a vertex is its position: 1 <script id=" vertex - shader " type="x- shader /x- vertex "> 2 attribute vec4 vposition ; 3 void main () 4 { 5 gl_pointsize = 1.0; 6 gl_position = vposition ; 7 } 8 </ script > We will add others: Color Normal vector (the vector perpendicular to the plane to which the vertex belongs to) Texture coordinate (Dr. Mihail) 2D graphics September 7, 2015 4 / 22

Attributes Vertex Attributes Let s add color: 1 <script id=" vertex - shader " type="x- shader /x- vertex "> 2 attribute vec4 vposition ; 3 attribute vec4 vcolor ; // in: the attribute is what the application sends to the shader 4 varying vec4 fcolor ; // out : varying qualifier interpolates the color between the vertices 5 void main () 6 { 7 gl_pointsize = 1.0; 8 gl_position = vposition ; // pass - through 9 fcolor = vcolor ; // pass - through 10 } 11 </ script > 12 13 <script id=" fragment - shader " type="x- shader /x- fragment "> 14 precision mediump float ; 15 16 varying vec4 fcolor ; 17 18 void main () 19 { 20 gl_fragcolor = fcolor ; // pass - through 21 } 22 </ script > (Dr. Mihail) 2D graphics September 7, 2015 5 / 22

Attributes From Application to GPU The process of sending information to the GPU consists of: Creating a buffer Binding it (sets the buffer currently worked on). Remember WebGL is a state machine. Populating it Associating the buffer with shader variable and enabling it 1 var vertices = [ new vec2 (0, 1), new vec2 (1, 1), new vec2 (1, -1), new vec2 (-1, 1), new vec2 (-1, -1), new vec2 (0, -1) ]; 2 // Create buffer 3 var bufferid = gl. createbuffer (); 4 // Bind it 5 gl. bindbuffer ( gl. ARRAY_BUFFER, bufferid ); 6 // Populate it with data 7 gl. bufferdata ( gl. ARRAY_BUFFER, flatten ( vertices ), gl. STATIC_DRAW ); 8 // Associate our shader variables with our data buffer 9 var vposition = gl. getattriblocation ( program, " vposition " ); 10 gl. vertexattribpointer ( vposition, 2, gl.float, false, 0, 0 ); 11 gl. enablevertexattribarray ( vposition ); Color is just another attribute, we follow the same process to send it to the GPU. (Dr. Mihail) 2D graphics September 7, 2015 6 / 22

Attributes Color Attribute to GPU 1 var colors = [ new vec4 (1, 0, 0, 1), new vec4 (0, 1, 0, 1), new vec4 (0, 0, 1, 1), new vec4 (0, 0, 0, 1), new vec4 (1, 1, 1, 1), new vec4 (0, 0, 1, 1) ]; 2 // Create buffer that will hold color data 3 var colorbufferid = gl. createbuffer (); 4 gl. bindbuffer ( gl. ARRAY_BUFFER, colorbufferid ); 5 // Populate it 6 gl. bufferdata ( gl. ARRAY_BUFFER, flatten ( colors ), gl. STATIC_DRAW ); 7 // Associate our shader variables with our data buffer 8 var vcolor = gl. getattriblocation ( program, " vcolor " ); 9 gl. vertexattribpointer ( vcolor, 4, gl.float, false, 0, 0 ); 10 gl. enablevertexattribarray ( vcolor ); (Dr. Mihail) 2D graphics September 7, 2015 7 / 22

Notice how colors are interpolated. This example is available to download on the course web page. (Dr. Mihail) 2D graphics September 7, 2015 8 / 22 Attributes Example: Two colored triangles

Attributes Vertex Attributes Texture Part of an image mapped onto a triangle. We need to: Load the image from the a file on the web server. Send it to the GPU. Instruct the GPU which part of the image gets mapped onto the triangle. (Dr. Mihail) 2D graphics September 7, 2015 9 / 22

Attributes Loading the texture The texture is loaded asynchronously, so mechanics are more complicated. 1 function inittexture () { 2 texture = gl. createtexture (); 3 gl. bindtexture ( gl. TEXTURE_2D, texture ); 4 gl. pixelstorei (gl. UNPACK_FLIP_Y_WEBGL, true ); 5 texture. image = new Image (); 6 texture. image. src = " trip. jpg "; 7 texture. image. onload = function () { 8 gl. bindtexture (gl. TEXTURE_2D, texture ); // set the texture we re currently working on 9 gl. pixelstorei (gl. UNPACK_FLIP_Y_WEBGL, true ); // turn upside down 10 gl. teximage2d (gl. TEXTURE_2D, 0, gl.rgba, gl.rgba, gl. UNSIGNED_BYTE, this ); // upload to GPU 11 gl. texparameteri (gl. TEXTURE_2D, gl. TEXTURE_MAG_FILTER, gl. LINEAR ); 12 gl. texparameteri (gl. TEXTURE_2D, gl. TEXTURE_MIN_FILTER, gl. LINEAR ); 13 } 14 } Textures must be square, sized power of two! (Dr. Mihail) 2D graphics September 7, 2015 10 / 22

Attributes Texture Coordinates Triangles are textured from a unit area. Images retrieved from: http://blogs.msdn.com/b/danlehen/archive/2005/11/06/489627.aspx (Dr. Mihail) 2D graphics September 7, 2015 11 / 22

Attributes Texture Coordinate Attributes a a See textured triangles example on course web page for full source code. 1 // for reference, the vertices are as follows 2 var vertices = [ new vec2 (-1, 1), new vec2 (0, 1), new vec2 (-1, -1), new vec2 (0, 1), new vec2 (1, 1), new vec2 (1, -1), new vec2 (0, 0.8), new vec2 ( -0.9, -1), new vec2 (0.9, -1) ]; 3 4 5 var texcoords = [ new vec2 (0, 0), new vec2 (0.5, 0), new vec2 (0, 1), new vec2 (0.5, 0), new vec2 (1, 0), new vec2 (1, 1), new vec2 (0.5, 0), new vec2 (0, 1), new vec2 (1, 1) ]; 6 7 var texcoordbufferid = gl. createbuffer (); 8 gl. bindbuffer ( gl. ARRAY_BUFFER, texcoordbufferid ); 9 gl. bufferdata ( gl. ARRAY_BUFFER, flatten ( texcoords ), gl. STATIC_DRAW ); 10 // Associate our shader variables with our data buffer 11 var vtexturecoord = gl. getattriblocation ( program, " vtexturecoord " ); 12 gl. vertexattribpointer ( vtexturecoord, 2, gl.float, false, 0, 0 ); 13 gl. enablevertexattribarray ( vtexturecoord ); (Dr. Mihail) 2D graphics September 7, 2015 12 / 22

Attributes Shaders 1 <script id=" vertex - shader " type="x- shader /x- vertex "> 2 attribute vec4 vposition ; 3 attribute vec2 vtexturecoord ; // in 4 varying vec2 ftexturecoord ; // out 5 void main () 6 { 7 gl_pointsize = 1.0; 8 gl_position = vposition ; // pass - through 9 ftexturecoord = vtexturecoord ; // pass - through 10 11 } 12 </ script > 13 14 <script id=" fragment - shader " type="x- shader /x- fragment "> 15 precision mediump float ; 16 17 varying vec2 ftexturecoord ; 18 uniform sampler2d texture ; 19 20 21 void main () 22 { 23 gl_fragcolor = texture2d ( texture, ftexturecoord ); // sample from the image 24 } 25 </ script > (Dr. Mihail) 2D graphics September 7, 2015 13 / 22

Attributes Example: Three textured triangles (Dr. Mihail) 2D graphics September 7, 2015 14 / 22

GLSL Data Types C types: int, float, bool Vectors: float types: vec2, vec3, vec4 int types: ivec, and boolean: bvec Matrices: mat2, mat3, mat4. They are stored by columns and referenced m[row][column]. C++ style constructors and casting: vec3 a = vec3(1.0, 2.0, 3.0); vec2 b = vec2(a); (Dr. Mihail) 2D graphics September 7, 2015 15 / 22

GLSL No Pointers There are no pointers in GLSL. Can use C structs that are populated from the application. Since matrices and vectors are basic types, they can be passed into and out-of GLSL functions. E.g.: mat3 foobar(mat3 a). Variables are passed by copying (value) (Dr. Mihail) 2D graphics September 7, 2015 16 / 22

GLSL Qualifiers GLSL has many of the same qualifiers as C++, such as const Need others due to the nature of execution model Variables can change: Once per primitive Once per vertex Once per fragment At any time in the application Vertex attributes are interpolated by the rasterizer into fragment attributes. (Dr. Mihail) 2D graphics September 7, 2015 17 / 22

GLSL Attribute Qualifier Attribute-qualified variables can change at most once per vertex There are a few built-in variables such as gl Position, but most have been deprecated Most are user-defined, in the application attribute float temperature; attribute vec3 velocity; (Dr. Mihail) 2D graphics September 7, 2015 18 / 22

GLSL Uniform qualified Variables are constant for an entire primitive Can be changed in application and sent to shaders Most are user-defined, in the application Cannot be changed in shader Used to pass information to shader such as time, a bounding box or transformation matrices (Dr. Mihail) 2D graphics September 7, 2015 19 / 22

GLSL Varying qualified Variables that are passed from vertex shader to fragment shader Automatically interpolated by the rasterizer With WebGL, GLSL uses the varying qualifier in both shaders varying vec4 color; More recent versions of WebGL use out in vertex shader and in in the fragment shader out vec4 color; //vertex shader in vec4 color; // fragment shader (Dr. Mihail) 2D graphics September 7, 2015 20 / 22

GLSL Variable Naming Attributes passed to vertex shader have names beginning with v (v Position, vcolor) in both the application and the shader. Note that these are different entities with the same name. Variable variables must have the same name in both shaders Uniform variables can have the same name in both shaders, not required. (Dr. Mihail) 2D graphics September 7, 2015 21 / 22

GLSL Selection Can refer to array elements using [] or selection (.) operator with: x, y, z, w r, g, b, a s, t, p, q a[2], a.b, a.z, a.p are the same (Dr. Mihail) 2D graphics September 7, 2015 22 / 22

GLSL Swizzling Allows us to manipulate components, for example: vec4 a, b; a.yz = vec2(1.0, 2.0); b = a.yxzw; More details later in the course (Dr. Mihail) 2D graphics September 7, 2015 23 / 22