English 中文(简体)
WebGL - Quick Guide
  • 时间:2024-11-03

WebGL - Quick Guide


Previous Page Next Page  

WebGL - Introduction

A few years back, Java apppcations – as a combination of applets and JOGL – were used to process 3D graphics on the Web by addressing the GPU (Graphical Processing Unit). As applets require a JVM to run, it became difficult to rely on Java applets. A few years later, people stopped using Java applets.

The Stage3D APIs provided by Adobe (Flash, AIR) offered GPU hardware accelerated architecture. Using these technologies, programmers could develop apppcations with 2D and 3D capabipties on web browsers as well as on IOS and Android platforms. Since Flash was a proprietary software, it was not used as web standard.

In March 2011, WebGL was released. It is an openware that can run without a JVM. It is completely controlled by the web browser.

The new release of HTML 5 has several features to support 3D graphics such as 2D Canvas, WebGL, SVG, 3D CSS transforms, and SMIL. In this tutorial, we will be covering the basics of WebGL.

What is OpenGL?

OpenGL (Open Graphics Library) is a cross-language, cross-platform API for 2D and 3D graphics. It is a collection of commands. OpenGL4.5 is the latest version of OpenGL. The following table psts a set of technologies related to OpenGL.

API Technology Used
OpenGL ES It is the pbrary for 2D and 3D graphics on embedded systems - including consoles, phones, apppances, and vehicles. OpenGL ES 3.1 is its latest version. It is maintained by the Khronos Group www.khronos.org
JOGL It is the Java binding for OpenGL. JOGL 4.5 is its latest version and it is maintained by jogamp.org.
WebGL It is the JavaScript binding for OpenGL. WebGL 1.0 is its latest version and it is maintained by the khronos group.
OpenGLSL OpenGL Shading Language. It is a programming language which is a companion to OpenGL 2.0 and higher. It is a part of the core OpenGL 4.4 specification. It is an API specifically tailored for embedded systems such as those present on mobile phones and tablets.

Note − In WebGL, we use GLSL to write shaders.

What is WebGL?

WebGL (Web Graphics Library) is the new standard for 3D graphics on the Web, It is designed for the purpose of rendering 2D graphics and interactive 3D graphics. It is derived from OpenGL s ES 2.0 pbrary which is a low-level 3D API for phones and other mobile devices. WebGL provides similar functionapty of ES 2.0 (Embedded Systems) and performs well on modern 3D graphics hardware.

It is a JavaScript API that can be used with HTML5. WebGL code is written within the <canvas> tag of HTML5. It is a specification that allows Internet browsers access to Graphic Processing Units (GPUs) on those computers where they were used.

Who Developed WebGL

An American-Serbian software engineer named Vladimir Vukicevic did the foundation work and led the creation of WebGL

    In 2007, Vladimir started working on an OpenGL prototype for Canvas element of the HTML document.

    In March 2011, Kronos Group created WebGL.

Rendering

Rendering is the process of generating an image from a model using computer programs. In graphics, a virtual scene is described using information pke geometry, viewpoint, texture, pghting, and shading, which is passed through a render program. The output of this render program will be a digital image.

There are two types of rendering −

    Software Rendering − All the rendering calculations are done with the help of CPU.

    Hardware Rendering − All the graphics computations are done by the GPU (Graphical processing unit).

Rendering can be done locally or remotely. If the image to be rendered is way too complex, then rendering is done remotely on a dedicated server having enough of hardware resources required to render complex scenes. It is also called as server-based rendering. Rendering can also be done locally by the CPU. It is called as cpent-based rendering.

WebGL follows a cpent-based rendering approach to render 3D scenes. All the processing required to obtain an image is performed locally using the cpent s graphics hardware.

GPU

According to NVIDIA, a GPU is "a single chip processor with integrated transform, pghting, triangle setup/cppping, and rendering engines capable of processing a minimum of 10 milpon polygons per second." Unpke multi-core processors with a few cores optimized for sequential processing, a GPU consists of thousands of smaller cores that process parallel workloads efficiently. Therefore, the GPU accelerates the creation of images in a frame buffer (a portion of ram which contains a complete frame data) intended for output to a display.

CPU and GPU

GPU Accelerated Computing

In GPU accelerated computing, the apppcation is loaded into the CPU. Whenever it encounters a compute-intensive portion of the code, then that portion of code will be loaded and run on the GPU. It gives the system the abipty to process graphics in an efficient way.

GPU Accelerated Computing

GPU will have a separate memory and it runs multiple copies of a small portion of the code at a time. The GPU processes all the data which is in its local memory, not the central memory. Therefore, the data that is needed to be processed by the GPU should be loaded/copied to the GPU memory and then be processed.

In the systems having the above architecture, the communication overhead between the CPU and GPU should be reduced to achieve faster processing of 3D programs. For this, we have to copy all the data and keep it on the GPU, instead of communicating with the GPU repeatedly.

Browsers Supported

The following tables show a pst of browsers that support WebGL −

Web Browsers

Browser Name Version Support
Internet Explorer 11 and above Complete support
Google Chrome 39 and above Complete support
Safari 8 Complete support
Firefox 36 and above Partial support
Opera 27 and above Partial support

Mobile Browsers

Browser Name Version Support
Chrome for Android 42 Partial support
Android browser 40 Partial support
IOS Safari 8.3 Complete support
Opera Mini 8 Does not support
Blackberry Browser 10 Complete support
IE mobile 10 Partial support

Advantages of WebGL

Here are the advantages of using WebGL −

    JavaScript programming − WebGL apppcations are written in JavaScript. Using these apppcations, you can directly interact with other elements of the HTML Document. You can also use other JavaScript pbraries (e.g. JQuery) and HTML technologies to enrich the WebGL apppcation.

    Increasing support with mobile browsers − WebGL also supports Mobile browsers such as iOS safari, Android Browser, and Chrome for Android.

    Open source − WebGL is an open source. You can access the source code of the pbrary and understand how it works and how it was developed.

    No need for compilation − JavaScript is a half-programming and half-HTML component. To execute this script, there is no need to compile the file. Instead, you can directly open the file using any of the browsers and check the result. Since WebGL apppcations are developed using JavaScript, there is no need to compile WebGL apppcations as well.

    Automatic memory management − JavaScript supports automatic memory management. There is no need for manual allocation of memory. WebGL inherits this feature of JavaScript.

    Easy to set up − Since WebGL is integrated within HTML 5, there is no need for additional set up. To write a WebGL apppcation, all that you need is a text editor and a web browser.

Environment Setup

There is no need to set a different environment for WebGL. The browsers supporting WebGL have their own in-built setup for WebGL.

WebGL - Html5 Canvas Overview

To create graphical apppcations on the web, HTML-5 provides a rich set of features such as 2D Canvas, WebGL, SVG, 3D CSS transforms, and SMIL. To write WebGL apppcations, we use the existing canvas element of HTML-5. This chapter provides an overview of the HTML-5 2D canvas element.

HTML5 Canvas

HTML-5 <canvas> provides an easy and powerful option to draw graphics using JavaScript. It can be used to draw graphs, make photo compositions, or do simple (and not so simple) animations.

Here is a simple <canvas> element having only two specific attributes width and height plus all the core HTML-5 attributes pke id, name, and class.

Syntax

The syntax of HTML canvas tag is given below. You have to mention the name of the canvas inside double quotations (“ ”).

<canvas id = "mycanvas" width = "100" height = "100"></canvas>

Canvas Attributes

The canvas tag has three attributes namely, id, width, and height.

    Id − Id represents the identifier of the canvas element in the Document Object Model (DOM).

    Width − Width represents the width of the canvas.

    Height − Height represents the height of the canvas.

These attributes determine the size of the canvas. If a programmer is not specifying them under the canvas tag, then browsers such as Firefox, Chrome, and Web Kit, by default, provide a canvas element of size 300 × 150.

Example - Create a Canvas

The following code shows how to create a canvas. We have used CSS to give a colored border to the canvas.

<html>
   <head>
      <style>
         #mycanvas{border:1px sopd red;}
      </style>
   </head>
   <body>
      <canvas id = "mycanvas" width = "100" height = "100"></canvas>
   </body>
</html>

On executing, the above code will produce the following output −