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

Similar documents
Programming with OpenGL Complete Programs Objectives Build a complete first program

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

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

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

CS452/552; EE465/505. Image Formation

CS452/552; EE465/505. Overview of Computer Graphics

Introduction to Computer Graphics with WebGL

Introduction to Computer Graphics with WebGL

We assume that you are familiar with the following:

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

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

Introduction to Computer Graphics with WebGL

2D graphics with WebGL

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

BCA611 Video Oyunları için 3B Grafik

CS452/552; EE465/505. Review & Examples

CS 432 Interactive Computer Graphics

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

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

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

We will use WebGL 1.0. WebGL 2.0 is now being supported by most browsers but requires a better GPU so may not run on older computers or on most cell

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

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

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

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

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

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

Shaders. Slide credit to Prof. Zwicker

Over the past 15 years, OpenGL has become

Introduce the basic input devices Input, Interaction and Animation

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

Introduction to OpenGL/GLSL and WebGL GLSL

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

Mobile Application Programing: Android. OpenGL Operation

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

CS475/CS675 - Computer Graphics. OpenGL Drawing

Ciril Bohak. - INTRODUCTION TO WEBGL

Geometry Shaders. And how to use them

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

OPENGL AND GLSL. Computer Graphics

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

WebGL (Web Graphics Library) is the new standard for 3D graphics on the Web, designed for rendering 2D graphics and interactive 3D graphics.

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

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

OPENGL RENDERING PIPELINE

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Mobile Application Programing: Android. OpenGL Operation

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

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

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

Rasterization-based pipeline

Tutorial 04. Harshavardhan Kode. September 14, 2015

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

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

OUTLINE. Hierarchical Models Z-Fighting Primitives Besides Triangles Back Face Culling

Mobile Application Programming: Android. OpenGL Operation

Three Main Themes of Computer Graphics

CS 432 Interactive Computer Graphics

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

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

COMP371 COMPUTER GRAPHICS

WebGL. Mike Bailey. Oregon State University. What is WebGL?

WebGL. Announcements. WebGL for Graphics Developers. WebGL for Web Developers. Homework 5 due Monday, 04/16. Final on Tuesday, 05/01

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

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

CS452/552; EE465/505. Transformations

Introduction to Computer Graphics with WebGL

Advanced Graphics. OpenGL and Shaders I. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

Computer Graphics (CS 4731) OpenGL/GLUT (Part 2)

Lecture 11 Shaders and WebGL. October 8, 2015

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

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

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

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

last time put back pipeline figure today will be very codey OpenGL API library of routines to control graphics calls to compile and load shaders

CSE Real Time Rendering Week 3 & 4

Transformation, perspective projection, and LookAT in. Ming Ouhyoung, September 2018

CS452/552; EE465/505. Models & Viewing

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

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Starting out with OpenGL ES 3.0. Jon Kirkham, Senior Software Engineer, ARM

6.S096 Lecture 9 Visualization

2D Drawing Primitives

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

Mali & OpenGL ES 3.0. Dave Shreiner Jon Kirkham ARM. Game Developers Conference 27 March 2013

Rendering Objects. Need to transform all geometry then

Lecture 4 of 41. Lab 1a: OpenGL Basics

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

Introduction to 2D Graphics

CS130 : Computer Graphics. Tamar Shinar Computer Science & Engineering UC Riverside

CS 548: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE

Lecture 13: OpenGL Shading Language (GLSL)

Advanced Graphics. OpenGL and Shaders I. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

10/24/2017. WebGL. What is WebGL? What are the Important Things to Know about OpenGL-ES 2.0? What are the Important Things to Know about WebGL?

Introduction to Computer Graphics. Hardware Acceleration Review

WebGL. WebGL. Bring 3D to the Masses. WebGL. The web has text, images, and video. We want to support. Put it in on a webpage

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

CSC 8470 Computer Graphics. What is Computer Graphics?

From system point of view, a graphics application handles the user input, changes the internal state, called the virtual world by modeling or

Transcription:

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

Interactive Computer Graphics Graphics library / package is intermediary between application and display hardware Application program maps / renders objects / models to images by calling on the graphics library User interaction allows image and / or model modification [van Dam] 2

Graphics Library Examples: OpenGL, RenderMan, DirectX, Windows Presentation Foundation (WPF), HTML5 + WebGL, Primitives : characters, points, lines, triangles, Attributes : color, line /polygon style, Transformations : rotation, scaling, Light sources Viewing 3

WebGL Web Graphics Library JavaScript API Operating system and windows system independent! Rendering interactive 2D and 3D Computer Graphics Using local hardware Within any compatible web browser No plug-ins necessary! Complete integration GPU Web page canvas (HTML5) Mixing / compositing with other HTML elements Integrates with standard Web packages and apps 4

WebGL Ed Angel s simple examples [Angel / Shreiner] 5

WebGL Execution in browser [Angel/Shreiner] 6

WebGL Execution in browser Run WebGL on any recent browser Chrome / Edge / Firefox / IE / Safari Code written in JavaScript JavaScript runs within browser Use local resources 7

WebGL Programs Control code CPU JavaScript Send data to the GPU Render function Static application : execute once Dynamic application : redraw after trigger events Shader code GPU GLSL C / C++ like Where the action happens! 8

WebGL Programs HTML file Describes Web page: structure, style and contents Includes JS utilities and the JS application Includes shader programs JavaScript files Graphics Modeling Simulation 9

JavaScript JS is the language of the Web Interpreted OO language JS code executed in all browsers Interaction with the DOM Is JS slow? JS engines are getting much faster Not a key issue for graphics! GPU handles the graphics data Use only a JS sub-set! 10

JavaScript Dynamic typing Scoping is different from most APIs Watch out for globals Comparison operators: == vs === and!= vs!=== JS arrays are objects! Not the same as in C / C++ / Java WebGL expects C-style arrays 11

JavaScript Arrays JS arrays are objects Attributes: length, Methods: pop(), push(), shift(), unshift(), Dynamic resizing WebGL expects C-style arrays JS typed arrays are like C arrays Work with standard JS arrays BUT, convert to typed arrays when sending data to the GPU Use a flatten() function to extract data from a JS array object 12

Minimalist approach Use only core JS and HTML No additional packages Focus on graphics If you want, you can be more ambitious! 13

WebGL Some features Lack of object-orientation! Multiple functions for the same logical function Example gl.uniform3f gl.uniform2i gl.uniform3dv 14

WebGL Function format [Angel] 15

WebGL General structure Describe page (HTML file) Request WebGL canvas Read in necessary files Define shaders (HTML file) Can be done with a separate file (browser dependent) Compute or specify data (JS file) Send data to GPU (JS file) Render data (JS file) 16

WebGL - Interaction Event-driven input uses callback functions or event listeners Define a callback function for each recognized event Browser enters an event loop and waits for an event Buttons / Menus / Keyboard / Mouse It responds to the events for which it has registered callbacks The callback function is executed when the event occurs 17

HTML onload event What happens after all files have been read? HTML + JS + GLSL Use the onload event to initiate execution of the WebGL initialization function onload event occurs when all files read 18

HTML Buttons <button id="button_1"> Change Color </button> Use HTML button tag for default style buttons id gives an identifier JS can use Text is displayed in the button Clicking on the button generates a click event Use CSS or jquery to get prettier buttons 19

JS Button-event listener Do not forget to define the listener Otherwise the event occurs and is ignored Two possibilities var mybutton = document.getelementbyid("button_1"); mybutton.addeventlistener("click", function() { }); document.getelementbyid("button_1").onclick = function() { }; 20

HTML Menus <select id="mymenu" size= 2"> <option value="0">item 1</option> <option value="1">item 2</option> </select> Use the HTML select element Each menu entry is an option element With an integer value returned by a click event 21

JS Menu listener var m = document.getelementbyid("mymenu"); m.addeventlistener("click", function() { switch (m.selectedindex) { case 0: break; case 1: break; } }); 22

WebGL General structure Describe page (HTML file) Request WebGL canvas Read in necessary files Define shaders (HTML file) Can be done with a separate file (browser dependent) Compute or specify data (JS file) Send data to GPU (JS file) Render data (JS file) 23

GLSL OpenGL Shading Language C / C++ like Matrix and vector types (2D, 3D and 4D) Overloaded operators C++ like constructors Code sent to shaders as source code WebGL Compile and link GLSL code Send information / data to shaders 24

The simplest Vertex Shader <script id="vertex-shader" type="x-shader/x-vertex"> attribute vec4 vposition; void main( void ) { gl_position = vposition; } </script> 25

The simplest Fragment Shader <script id="fragment-shader" type="x-shader/x-fragment"> precision mediump float; void main( void ) { gl_fragcolor = vec4( 1.0, 1.0, 1.0, 1.0 ); } </script> 26

Shaders Shaders are full programs Each shader has an id that can be used by JS code Shaders must set the two required built-in variables gl_position gl_fragcolor Must set precision in fragment shader 27

GLSL qualifiers Need qualifiers due to the nature of the execution model Variables can change (at most) Once per primitive uniform vec3 color; Once per vertex attribute vec4 vposition; Once per fragment varying vec3 fcolor; At any time in the application Vertex attributes are interpolated by the rasterizer into fragment attributes 28

Shaders // Load shaders var program = initshaders( gl, "vertex-shader", "fragment-shader" ); gl.useprogram( program ); // Load the data into the GPU var bufferid = gl.createbuffer(); gl.bindbuffer( gl.array_buffer, bufferid ); gl.bufferdata( gl.array_buffer, flatten( vertices ), gl.static_draw ); 29

Shaders // Associate out shader variables with our data buffer var vposition = gl.getattriblocation( program, "vposition" ); gl.vertexattribpointer( vposition, 4, gl.float, false, 0, 0 ); gl.enablevertexattribarray( vposition ); 30

Shaders initshaders used to load, compile and link shaders to form a program object Load data on the GPU by creating a vertex buffer object on the GPU Use flatten() to convert the JS array to an array of floats Connect JS variables with shader variables Need name, type and location in buffer 31

How to render? function render() { } gl.clear( gl.color_buffer_bit ); gl.drawarrays( gl.lines, 0, 4 ); Which primitive types? gl.points gl.lines, gl.line_strip, gl.line_loop gl.triangles, gl.triangle_strip, gl.triangle_fan 32

WebGL primitives [Angel] 33

Execution model [Angel] 34

Execution model [Angel] 35

Linking shaders with application Read shaders Compile shaders Check for errors! Create a program object Container for shaders Link everything togeher Check for errors! Link variables in application with variables in shaders Vertex attributes Uniform variables 36

WebGL programming Set up canvas to render onto Generate data in application Create shader programs Create buffer objects and load data into them Connect data locations with shader variables Render 37

WebGL Application organization Do not put all code into a single HTML file! Put the setup in an HTML file And the application in a separate JavaScript file 38

WebGL Application organization HTML file contains shaders brings in utilities and application JS file describes page elements: buttons, menus sets up canvas element JS file initializes WebGL context sets up VBOs contains listeners for interaction sets up required transformation matrices reads, compiles and links shaders triggers rendering 39