What Is “Webgl”?

What Is “Webgl”?

10/24/2017 1 2 What is “WebGL”? WebGL From WikiPedia: http://cs.oregonstate.edu/~mjb/webgl WebGL (Web Graphics Library) is a JavaScript API for rendering interactive 3D graphics and 2D graphics within any compatible web browser without the use of plug-ins. WebGL is integrated completely into all the web standards of the browser allowing GPU accelerated usage of physics and image processing and effects as part of the web page canvas. WebGL elements can be mixed with other HTML elements and composited with other parts of the page or page background. WebGL programs consist of control code written in JavaScript and shader code that is executed on a computer's Graphics Processing Unit (GPU). WebGL is designed and maintained by the non-profit Khronos Group. [… of which OSU is a member] Mike Bailey [email protected] This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License Computer Graphics Computer Graphics webgl.pptx mjb – October 24, 2017 mjb – October 24, 2017 3 4 What are the Important Things to Know about WebGL? What are the Important Things to Know about OpenGL-ES 2.0? 1. WebGL programs are written in JavaScript. This means that the code is downloaded 1. WebGL uses OpenGL-ES 2.0 as its graphics API. and run by the browser. 2. There is no fixed-function pipeline -- you must use vertex and fragment shaders. 2. WebGL programs are written in JavaScript. This means that the code is interpreted, not compiled. This makes the code slower than the equivalent C/C++. 3. You can’t use glBegin…glEnd to draw things. You must use VBOs. 3. The JavaScript API for WebGL talks to the C-OpenGL library which in turn talks to the 4. You can’t use built-in transformation functions (e.g., glRotatef, glScalef, graphics hardware on the web browser client’s system. This gives you access to all glTranslatef, gluLookAt, glOrtho, gluPerspective). You must use your own of the speed and capability that the hardware provides. Use it! Especially use the matrices passed in as vertex shader attribute variables. vertex buffer object capability to store your displayable geometry on the graphics card, and be sure to bind your textures into GPU memory. Anything that you can put 5. Attribute variables for the vertex shader are declared as “attribute”, not “in”. on the GPU side minimizes the amount of code necessary to get it drawn. 6. Output variables from the vertex shader which are then rasterized to become input 4. WebGL uses the OpenGL-ES 2.0 as its graphics API. “ES” stands for “Embedded variables for the fragment shader are declared as “varying”, not “out” and “in”. Systems”. This is the same flavor of OpenGL that mobile devices use. 7. Fragment shaders must set the precision to be used, for example: precision highp float; Computer Graphics Computer Graphics mjb – October 24, 2017 mjb – October 24, 2017 5 6 Cube Example Cube Example 2 3 6 7 01 45 Cube Vertices = Cube Colors = { -1., -1., -1. }, { 0., 0., 0. }, Cube Indices = { 1., -1., -1. }, { 1., 0., 0. }, { 1, 0, 2, 3 }, { -1., 1., -1. }, { 0., 1., 0. }, { 4, 5, 7, 6 }, { 1., 1., -1. }, { 1., 1., 0. }, { 5, 1, 3, 7 }, { -1., -1., 1. }, { 0., 0., 1. }, { 0, 4, 6, 2 }, { 1., -1., 1. }, { 1., 0., 1. }, { 6, 7, 3, 2 }, { -1., 1., 1. }, { 0., 1., 1. }, { 0, 1, 5, 4 } { 1., 1., 1. } { 1., 1., 1. } Computer Graphics Computer Graphics mjb – October 24, 2017 mjb – October 24, 2017 1 10/24/2017 A Sample WebGL Program 7 The Vertex Shader Source Code in the HTML File 8 sample.html <script id="vertex-shader" type="x-shader/x-vertex"> Announces that this is a vertex shader Some things to note: uniform mat4 uModelViewMatrix; The name of the vertex shader uniform mat4 uProjectionMatrix; Uniform variables 1. This code displays a cube with 5 colored sides and one textured side. The attribute vec3 aVertex; cube is rotated in 3D by holding down the left mouse button and moving attribute vec3 aColor; Per-vertex attribute variables the mouse. It is scaled by using a jQuery-generated slider. The display is attribute vec2 aTexCoord0; toggled between perspective and orthographic with HTML buttons. Vertex shader-created output varying vec3 vColor; variables to be interpolated 2. This code is written to be clear-to-understand. It is not necessarily written varying vec2 vST; through the rasterizer varying float vZ; in the most efficient style using the very best practices. void 3. In computer graphics, vertex properties come in two flavors: (1) those that main( ) Set varying variables and are the same for that vertex no matter what face that vertex is on, and (2) { transform the vertex those that are different for that vertex depending on what face the vertex is vColor = aColor; on. This program demonstrates both. The colors are the first flavor. The vST = aTexCoord0; texture coordinates are the second. vZ = aVertex.z; gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aVertex,1.); } </script> Computer Graphics Computer Graphics mjb – October 24, 2017 mjb – October 24, 2017 The Fragment Shader Source Code in the HTML File 9 The General Format of the HTML File 10 sample.html sample.html <script id="fragment-shader" type="x-shader/x-fragment"> Announces that this is a fragment shader <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"> precision highp float; The name of the fragment shader <style>#slider { margin: 10px; } </style> <script src="http://code.jquery.com/jquery-1.8.3.js"></script> Must set the precision in OpenGL-ES <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> uniform sampler2D uTexture; Uniform variable varying vec3 vColor; varying vec2 vST; Fragment shader input variables varying float vZ; that were interpolated through the rasterizer Get the CSS style sheet and void get the jQuery user interface main( ) JavaScript code { if( vZ <= 0.99 ) Used to identify which face to texture { gl_FragColor = vec4( vColor, 1. ); } Decide to use the color or the else texture, based on the Z model { vec4 rgba = texture2D( uTexture, vST ); coordinate gl_FragColor = vec4( rgba.rgb, 1. ); } } </script> Computer Graphics Computer Graphics mjb – October 24, 2017 mjb – October 24, 2017 The General Format of the HTML File 11 What is in Webgl-Utils.js? 12 sample.html <button id = "PerspButton">Perspective</button> <script type="text/javascript" src="http://cs.oregonstate.edu/~mjb/WebGL/Webgl-Utils.js"></script> <button id = "OrthoButton">Orthographic</button> <p></p> Setup the buttons <div id="slider"> <script> Webgl-Utils.js is a Google-supplied set of Javascript to setup the WebGL $( "#slider" ).slider( ); window, canvas, and context. $( "#slider" ).slider( "option", "min", 0.1 ); $( "#slider" ).slider( "option", "max", 2.0 ); $( "#slider" ).slider( "option", "value", 1.0 ); $( "#slider" ).slider( "option", "step", 0.01 ); Setup the jQuery slider $( "#slider" ).slider( "option", "orientation", "horizontal" ); parameters $( "#slider" ).slider( "enable" ); </script> </div> Bring in all of the support <canvas id="gl-canvas" width="512"" height="512"> code and the main program Oops ... your browser doesn't support the HTML5 canvas element </canvas> <script type="text/javascript" src="http://cs.oregonstate.edu/~mjb/WebGL/Webgl-Utils.js"></script> <script type="text/javascript" src="http://cs.oregonstate.edu/~mjb/WebGL/InitShaders.js"></script> <script type="text/javascript" src="http://cs.oregonstate.edu/~mjb/WebGL/GlMatrix.js"></script> <script type="text/javascript" src="sampledata.js"></script> <script type="text/javascript" src="sample.js"></script> Computer Graphics Computer Graphics mjb – October 24, 2017 mjb – October 24, 2017 2 10/24/2017 What is in InitShaders.js? 13 What is in GlMatrix.js? 14 <script type="text/javascript" src="http://cs.oregonstate.edu/~mjb/WebGL/InitShaders.js"></script> <script type="text/javascript" src="http://cs.oregonstate.edu/~mjb/WebGL/GlMatrix.js"></script> InitShaders.js contains the calls to gl.createShader, gl.shaderSource, GlMatrix.js came from Brandon Jones and Ed Angel. It contains gl.compileShader, gl.createProgram, gl.attachShader, and vec2, vec3, vec4, mat2, mat3, and mat4 data types along with methods to create gl.linkProgram to create the shader program from the vertex and fragment transformation matrices to pass into vertex shaders as attribute variables. shader source. Basically, it acts as a Javascript GLM. The logic is exactly the same as it is in C, but written in Javascript. Take a look through it sometime. It is very readable. Computer Graphics Computer Graphics mjb – October 24, 2017 mjb – October 24, 2017 The General Format of the JavaScript File 15 The General Format of the JavaScript File 16 var canvas; sample.js var gl; sample.js Compiled and linked function DrawTriangle( i, a, b, c, sta, stb, stc ) var Program; shader program { var Vertices; VertexArray[i+0] = Vertices[a]; DrawQuad( ) calls this twice to var Color; OpenGL arrays for ColorArray[i+0] = Colors[a]; draw two triangles per quad vertices, colors, and TexArray[i+0] = sta; var NumPoints; texture coordinates var VertexArray; VertexArray[i+1] = Vertices[b]; var ColorArray; ColorArray[i+1] = Colors[b]; var TexArray; Mouse and transformation TexArray[i+1] = stb; information var MouseDown = false; VertexArray[i+2] = Vertices[c]; var LastMouseX; ColorArray[i+2] = Colors[c]; var LastMouseY; TexArray[i+2] = stc; var Left, Middle, Right; var Perspective; return i+3; var SaveScale = 1.; } Matrix and texture var MvMatrix = mat4.create( ); information var PMatrix = mat4.create( ); Call this to draw a var MvLoc;

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us