WebGL

7
WebGL by Gyuri Horak

Transcript of WebGL

Page 1: WebGL

WebGLby Gyuri Horak

Page 2: WebGL

What is WebGl?

● JavaScript extension● based on OpenGL ES 2.0● Khronos Group (WebGL main site)● Firefox 4+, Chrome 9+, Safari 5.1+, Opera

12+● a bit ugly (C+JS)● tons of helper libraries

Page 4: WebGL

Issues with numbers

● Matrix operations (glUtils.js, sylvester.js)● JavaScript (Number class) => OpenGL (lot

of types)var vertices = [ 1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, -1.0, 0.0];var squareVerticesBuffer = gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesBuffer);gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

● example2

Page 5: WebGL

Shaders

● standard OpenGL shader language● from plain text● <script type="x-shader/x-fragment" id="fShader">...

</script>

● passing attributes to shaders:vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");gl.enableVertexAttribArray(vertexColorAttribute);

● example3

Page 7: WebGL

Tree.js

● the most popular lib built on top of WebGL currently

● source on github (mrdoob/three.js)● many examples, tutorials● easy to use, cutomizable boilerplate● default boilerplate, previous example in

three.js