230 likes | 436 Views
Introduction to WebGL. Greg Balek. Structure. What is WebGL Introduction to Graphics Simple Example 3 rd Party Dev Tools. Overview. WebGL is a JavaScript API for rendering interactive 3D and 2D graphics Supported on all current browsers GPU accelerated Integrates with HTML5 Canvas.
E N D
Introduction to WebGL Greg Balek
Structure • What is WebGL • Introduction to Graphics • Simple Example • 3rd Party Dev Tools
Overview • WebGL is a JavaScript API for rendering interactive 3D and 2D graphics • Supported on all current browsers • GPU accelerated • Integrates with HTML5 Canvas
Graphics & Rendering • Every image on an electronic screen is just a 2D array of pixels • Rendering to 3D means coloring the pixels to produce a convincing illusion
GPU & Rasterization • Can offload the computationally intensive rendering process to the GPU • Rasterizer draws all images triangle by triangle, creating a mesh • WebGL calls this a “TRIANGLE_STRIP” 5 4 3 2 1
Rendering Pipeline 5 4 3 2 1
Pipeline Details • 3: Scan Converted Triangle • Convert the triangle to pixels • 1: Normalized Clip Space Transformation • 4 dimensional (X, Y, Z, Visibility Extents) • 2: Window Transformation • Scale triangle to relative to canvas element’s dimensions • 4: Fragment Processing • Applies color transformations
Shaders • User defined algorithms to create special effects on the triangle fragments • Run during the fragment processing stage of the pipeline • Offloaded to the GPU
WebGL example • Looks can be deceiving
The HTML • WebGL acts on the HTML5 canvas element • No need to point to external JavaScript file
Program Flow • Four helper functions to push the image through the pipeline
initGL() Method • Purpose is to set the “context” of the program • A specific set of state used for rendering. Context is like a large C-style struct that contains a large number of fields that can be accessed. If you were to create multiple windows for rendering, each one would have its own.
initShaders() Method • Loads the shader algorithms
Vertex Shader • Defines the clipping space of the object after transformation • 4D Vector (X, Y, Z, Clipping)
Fragment Shader • Applies color to each triangle fragment • 4D Vector (R, G, B, A)
initBuffers() Method • Pushes the vertex data onto the GPU 3 2 1 1 2 3
3rd Part Dev Tools • Free, open source content creation editors • Mozilla’s shader editor