1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.

Slides:



Advertisements
Similar presentations
 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance.
Advertisements

Open Graphics Library (OpenGL)
Michael Robertson Yuta Takayama. WebGL is OpenGL on a web browser. OpenGL is a low-level 3D graphics API Basically, WebGL is a 3D graphics API that generates.
CSE Real Time Rendering. TBT (Not So) Real Time Rendering.
WebGL: Hands On Zhenyao Mo Software Engineer, Google, Inc. Chrome GPU Team.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
CS 418: Interactive Computer Graphics Introduction to WebGL: HelloTriangle.html Eric Shaffer.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Angel and Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Introduction to Computer Graphics Ed Angel Professor Emeritus of Computer.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 2 Introduction to HTML, JavaScript and WebGL.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
User Input and Animation. For Further Reading Angel 7 th Ed: –Chapter 3: Input and Animation. –Chapter 4: Rotation and other transformation. Beginning.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in WebGL.
1 Graphics CSCI 343, Fall 2015 Lecture 3 Introduction to WebGL.
Vertex Buffer Objects and Shader Attributes. For Further Reading Angel 7 th Ed: –Most parts of Chapter 2. Beginning WebGL: –Chapter 1: vertex Buffer Objects,
Programming with OpenGL Part 0: 3D API. For Further Reading Angel 7 th Ed: –2.2: JavaScript –2.3: OpenGL & WebGL –2.8: fragment & vertex shaders Beginning.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Mouse Input. For Further Reading Learning WebGL, Lesson 11: 2.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
CGS 3066: Web Programming and Design Spring 2016 Introduction to JavaScript.
1 Introduction to Computer Graphics with WebGL ADOPTED from Ed Angel Chapter 2 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
The Basics: HTML5, Drawing, and Source Code Organization
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 2: Complete Programs
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
CS 4722 Computer Graphics and Multimedia Spring 2018
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Presentation transcript:

1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science Laboratory University of New Mexico Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Example: Draw a triangle ­Each application consists of (at least) two files ­HTML file and a JavaScript file HTML ­describes page ­includes utilities ­includes shaders JavaScript ­contains the graphics 2 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

3 Coding in WebGL WebGL => not a toy any more (stable) Can run WebGL on any recent browser ­Chrome ­Firefox ­Safari ­IE Code written in JavaScript (JS) JS runs within browser ­Use local resources Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Example: triangle.html 4 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Debugging WebGL applications 5 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015 One can use Chrome’s WebGL Inspector (extension) to debug WebGL code. inspector/ogkcjmbhnfmlnielkjhedpcjomeaghda?hl=enhttps://chrome.google.com/webstore/detail/webgl- inspector/ogkcjmbhnfmlnielkjhedpcjomeaghda?hl=en WebGL API reference: ­ Quick reference card :

Debugging WebGL applications 6 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015 Demonstration : triangle.html + triangle.jstriangle.htmltriangle.js (take the time to show all the feature of the debugger…) Right-click on triangle Select « Inspecter l’élément » Click on « Sources » ­Set breakpoint by clicking on a line number (second click removes breakpoint) ­Step (global execute) one line => F10 ­Step (detailled execute) one line => F11 ­Reload and run => F5 ­Continue => F8 ­To observe a variable => put the mouse over ­Scope Variables… ­Watch Expression

Example Code triangle.html + triangle.js triangle.htmltriangle.js 7 attribute vec4 vPosition; void main(){ gl_Position = vPosition; } precision mediump float; void main(){ gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); } Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

HTML File (cont) 8 Oops... your browser doesn't support the HTML5 canvas element Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

JS File 9 var gl; var points; window.onload = function init(){ var canvas = document.getElementById( "gl-canvas" ); gl = WebGLUtils.setupWebGL( canvas ); if ( !gl ) { alert( "WebGL isn't available" ); } // Three Vertices var vertices = [ vec2( -1, -1 ), vec2( 0, 1 ), vec2( 1, -1 ) ]; Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

JS File (cont) 10 // Configure WebGL // gl.viewport( 0, 0, canvas.width, canvas.height ); gl.clearColor( 1.0, 1.0, 1.0, 1.0 ); // Load shaders and initialize attribute buffers var program = initShaders( gl, "vertex-shader", "fragment-shader" ); gl.useProgram( program ); // locate vPosition in the shaders var vPosition = gl.getAttribLocation(program, "vPosition"); Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

JS File (cont) 11 // 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 ); // Now render gl.enableVertexAttribArray(vPosition); // activate vertex array for vPosition // gl.bindBuffer(gl.ARRAY_BUFFER, bufferId); // select buffer to be used // The previous line is not necessary because the correct buffer is already selected gl.vertexAttribPointer(vPosition, 2, gl.FLOAT, false, 0, 0); // define data format render(); } Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

JS File (cont) 12 function render() { gl.clear( gl.COLOR_BUFFER_BIT ); gl.drawArrays( gl.TRIANGLES, 0, 3 ); } Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Exercise Run triangle.html from the class website Load the triangle.html and triangle.js to your computer and run them from there Edit the two files to change the color and display more than one triangle (see 2trianglesa.html + 2trianglesa.js)2trianglesa.html2trianglesa.js 13 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

JavaScript Notes JavaScript (JS) is the language of the Web ­All browsers will execute JS code ­JavaScript is an interpreted object-oriented language References ­Flanagan, JavaScript: The Definitive Guide, O’Reilly ­Crockford, JavaScript, The Good Parts, O’Reilly ­Many Web tutorials 14 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

JS Notes Is JS slow? ­JS engines in browsers are getting much faster ­Not a key issues for graphics since once we get the data to the GPU it doesn’t matter how we got the data there JS is a (too) big language ­We don’t need to use it all ­Choose parts we want to use ­Don’t try to make your code look like C or Java 15 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

JS Notes Very few native types: ­numbers ­strings ­booleans Only one numerical type: 32 bit float ­var x = 1; ­var x = 1.0; // same ­potential issue in loops ­two operators for equality == and === Dynamic typing 16 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Scoping Different from other languages (C, C++,..) Function scope : ­a variable can only have one name… variables are hoisted within a functionhoisted ­can use a variable before it is declared Note functions are first class objects in JS ­A “first class object” is an entity that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have. 17 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

JS Arrays JS arrays are objects ­inherit methods ­var a = [1, 2, 3]; is not the same as in C++ or Java ­a.length // 3 ­a.push(4); // length now 4 ­a.pop(); // 4 ­avoids use of many loops and indexing ­It is a problem for WebGL which expects C-style arrays 18 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Typed Arrays JS has “typed arrays” that are like C arrays var a = new Float32Array(3) var b = new Uint8Array(3) Generally, we prefer to work with standard JS arrays and convert to “typed arrays” only when we need to send data to the GPU with the flatten function in MV.js 19 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Additional advices Do not give a variable a name identical to a function… Ex.: var cube; cube = cube(1.0); function cube(side){ } 20 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

A Minimalist Approach We will use only core JS and HTML ­no extras or variants No additional packages ­CSS ­JQuery Focus on graphics ­examples may lack beauty You are welcome to use other variants as long as I can run them from your URL 21 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015