The Wayback Machine - https://web.archive.org/web/20240818203809/https://www.geeksforgeeks.org/webgl-introduction/
Open In App

WebGL Introduction

Last Updated : 11 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Creating interactive media content for the web traditionally required third-party apps or plug-ins installed in browsers. With the rise of open web standards and the need to support native graphics capabilities in the web browsers, WebGL was born to bring cross-browser compatibility and to be fully integrated with web standards.

What is WebGL?

WebGL (Web Graphics Library) is a rasterization API based on OpenGL ES developed by the Khronos Group, Inc. for rendering 2D and 3D graphics in a web browser. WebGL uses hardware acceleration which enables developers the ability to build high-performance real-time rendering interactive graphics in the web browser such as games, simulations, data visualization, animated videos, 3D modeling, molecular animations, and much more.

The efficiency of WebGL is perhaps one of the main advantages as it allows high-demanding power web applications to extract the full capabilities of the graphics card. Moreover, WebGL is plugin-free and is compatible with all major browsers, so users don’t need to worry about downloading or installing any external application to run a WebGL app.

How does WebGL work?

A WebGL program comprises codes written in OpenGL Shading Language (GLSL), JavaScript, and HTML5. GLSL is used to write the shaders, and special programs executed on the graphics hardware that computes attributes like vertices, textures, colors, lightning, and so on during the rendering of the object or scene. GLSL gives developers important control over the graphic pipeline. JavaScript and HTML are used primarily as binding languages and to provide a rendering context.

A WebGL program is commonly generated by multiple draws on the canvas HTML element that are carried out by the graphic processing unit (GPU) through a process called the rendering pipeline. 
To create high-fidelity graphics, WebGL may be quite complex to use as it is nearly low-level programming. 

There is, however, a variety of libraries available that abstract some of the difficulties of WebGL making them less verbose such as twgl, Three.js, PhiloGL, and J3D.

Rendering Pipeline:

The rendering pipeline consists of a sequence of operations required each time an object (the 2D/3D scene) is rendered on the screen. The objective is to draw the desired graphic model or scene from primitives (geometric shapes expressed by their Cartesian coordinates  X, Y, and Z). In computer graphics, each object is modeled from a collection of triangles. So, in the depicted diagram, we are rendering just one triangle and all these phases represent the rendering pipeline.

WebGL Rendering Pipeline

  1. Vertices (singular vertex): are attributes that describe the geometry of the object represented by its 2D or 3D space coordinates and are stored in a data structure like arrays. They can also specify other attributes like colors, texture, reflectance, normal vectors (used for lighting), etc.
  2. Vertex shader: is a function that generates clip space coordinates by applying a transformation to the vertex and helps to prepare data for use in the fragment shader.
  3. Primitive generation (assembler): connects vertices to assemble triangle primitives.
  4. Rasterization: is the process that projects the triangle described previously as a vector coordinate into small pieces called fragments (the data necessary to generate a screen pixel) and it also interpolates values across the primitives for each pixel.
  5. Fragment shader: is a function that calculates and assigns the color to pixels.
  6. Testing blending: takes the fragment shader output and combines it with colors in the current framebuffer to render transparency and translucency effects.
  7. Framebuffer: stores the frame of data in the GPU memory that represents the visible content of the screen

OpenGL (API, Used Technology): As mentioned earlier WebGL is based on OpenGL and its syntax is similar. There are, however, different versions and standards of OpenGL. Currently, there are three standards: OpenGL for desktop applications, OpenGL ES (Embedded Systems) used primarily in smartphones and tablets, and WebGL which is based on OpenGL ES specification for browsers.
WebGL 1.0 is equivalent to OpenGL ES 2.0
WebGL 2.0 is equivalent to OpenGL ES 3.0

Browsers Support:

WebGL 2.0 is widely supported in all major browsers on both desktop and mobile Apple (Safari), Google (Chrome), Microsoft (Edge), Opera (Opera web browser), and Mozilla (Firefox). However, as WebGL is running on the graphic card, its availability also requires hardware with OpenGL feature support.



Similar Reads

How to create 3D Geometries in webGL and p5.js ?
In this article, we will see how to draw 3D geometry (containing length, width, and height) in p5.js. The default rendering in p5.js is used for 2D images, and for the 3D images, we use WEBGL which enables 3D render by introducing the Z dimension to the geometry. Syntax: createCanvas( windowWidth, windowHeight, WEBGL ) Example 1: In this example, w
2 min read
Materials in WebGL and p5.js
In this article, we will learn how to apply different types of material in WebGL. Light reflects off objects differently depending on their angle of reflection as well as the material of the object. There are four types of materials in p5.js: Basic Material: It fills the geometry with the given color, but it is not affected by light. It is defined
3 min read
Lights in WebGL and p5.js
In this article, we will learn how to apply different types of lights in p5.js. Lighting is a simple but powerful way to provide depth and realism in p5.js sketches. There are three types of lights available in p5.js: Ambient Light: It is the simplest of the three lights. It provides even ambient lighting to objects drawn afterward. It takes a p5.C
2 min read
Getting Started with WebGL
WebGL is an open web standard that allows you to create highly efficient graphical applications to run in web browsers through hardware acceleration. This article will walk you through how to create a WebGL context and render a simple triangle (as shown in the picture below) using WebGL 2.0. Setting up WebGL: To develop and run a WebGL application
7 min read
Difference between WebGL and OpenGL
Both WebGL and OpenGL are graphics libraries and is developed by the same group i.e, Khronos Group in 2011 and 1992 respectively. Both libraries are used for making interactive 2D and 3D graphic images. WebGL is developed in JavaScript and is being used in HTML Canvas element so it is quite easy to use it in JavaScript and HTML and OpenGL is writte
3 min read
How to Handle Mouse and Keyboard Input in WebGL?
WebGL in web development is a JavaScript API that allows for the rendering of interactive 2D and 3D graphics within a web browser without the need for plugins. Handling mouse and keyboard input in WebGL involves capturing these inputs through event listeners and using them to manipulate the graphics rendered on the canvas. This interaction enables
4 min read
How to Implement WebGL Model View Projection?
It's an amazing tool (Web Graphics Library) that can help you create intricate 3D models and objects directly on your web browser with no need for any third-party plugins. It presents a JavaScript API, empowering you to craft and control 3D graphics within a web setting. In today’s rapidly evolving world of 3D visualization, gaming, and virtual rea
8 min read
How to Optimize WebGL Performance?
WebGL (Web Graphics Library) is a powerful JavaScript API used to render 3D and 2D graphics within any compatible web browser. However, achieving optimal performance with WebGL requires careful attention to various aspects of your code and rendering pipeline. Key strategies to optimize WebGL performance:1. Optimize Geometry and MeshesLevel of Detai
3 min read
How to Use WebGL Constants?
WebGL constants are used to configure and control various aspects of the rendering pipeline in WebGL. They define specific states, operations, and buffer bitmasks that influence how WebGL processes graphics. In this article, we will explore two practical examples of using WebGL Constants. Syntax:gl.<method>(<constant>, <value>);Pa
3 min read
How To Compile Shaders In WebGL?
Shaders in WebGL are small programs written in GLSL (OpenGL Shading Language) that run on the GPU to control the rendering of graphics. Compiling a shader means converting the GLSL code into a format that the GPU can execute, which involves creating a shader object, attaching source code, and processing it to check for errors. In this article, we w
6 min read
How to Create a WebGL Context?
WebGL Context is an essential component for rendering interactive 3D graphics in web browsers. It provides a JavaScript API for drawing graphics directly to the web page using the HTML <canvas> element. By establishing a WebGL Context, developers can use the GPU to create complex visual effects and animations. Using the getContext Method Dire
2 min read
How to Create a Basic 2D WebGL Animation?
2D WebGL animation is a technique for creating dynamic, graphical animations using the WebGL API or libraries like Three.js. It uses the power of the GPU to render and animate 2D shapes or objects on a canvas, allowing for smooth and performant visual effects directly in the browser. Below are the approaches to Creating a Basic 2D WebGL Animation:
4 min read
How to Load 3D Models in WebGL?
3D Models in WebGL are digital representations of objects within a 3D space, rendered directly in the browser. They can range from simple shapes to complex designs and are crucial for creating immersive and interactive graphics on the web. Below are the approaches to Creating a 3D Model in WebGL: Table of Content Using Plain WebGL for Loading a 3D
6 min read
How to Set Up a WebGL Project?
WebGL is the JavaScript API that allows you to render 3D and 2D graphics within a web browser without needing plugins. It provides a way to interact with the graphics hardware directly and is built on top of OpenGL ES. Setting up a WebGL project involves configuring your development environment, creating a project structure, and writing the necessa
3 min read
How to Use Shaders to Apply Color in WebGL?
Shaders in WebGL are small programs that run on the GPU to control the rendering pipeline. They allow you to manipulate the appearance and position of shapes on the screen. In this example, we use vertex and fragment shaders to apply a uniform color to a shape, making it possible to change the shape's color interactively. The color is set using a u
8 min read
How to Animate Objects with WebGL?
WebGL is the JavaScript API that allows the rendering 2D and 3D graphics directly within web browsers without the need for plugins. It provides low-level access to the GPU, enabling high-performance graphics and animations. By using WebGL, developers can create sophisticated animations and interactive visual effects that enhance user experiences on
4 min read
How To Set Up A Basic WebGL Rendering Loop?
WebGL Rendering Loop is important for creating dynamic, interactive graphics on the web. By continuously rendering frames, it allows for smooth animations and real-time updates. In this article, we will explore two different approaches to setting up a basic WebGL rendering loop : Table of Content Basic WebGL Rendering Loop with Vanilla JavaScriptBa
3 min read
How To Create And Use Buffers in WebGL?
Buffers in WebGL are important for storing and managing vertex and index data used in rendering. They allow for efficient transfer of data from JavaScript to the GPU, enabling smooth and flexible graphics rendering. By creating and binding buffers, you can manage vertex attributes and indices for various geometric shapes, optimizing performance and
4 min read
Introduction to React Native
If you want to build mobile apps for both Android and iOS. What should you learn? The individual native languages for each app i.e, Java for Android and Swift/Objective-C for iOS?, Actually NO. Native Android and iOS development are quite different and can be expensive – first, the language itself is quite different, and second, all the underlying
3 min read
Introduction to Object Oriented Programming in JavaScript
As JavaScript is widely used in Web Development, in this article we will explore some of the Object Oriented mechanisms supported by JavaScript to get the most out of it. Some of the common interview questions in JavaScript on OOPS include: How is Object-Oriented Programming implemented in JavaScript? How does it differ from other languages? Can yo
7 min read
PHP | MySQL Database Introduction
What is MySQL? MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation. The data in a MySQL database are stored in tables which consists of columns and rows.MySQL is a database system that runs on a server.MySQL
4 min read
Django Introduction | Set 2 (Creating a Project)
Note- This article is in continuation of Django introduction. Popularity of Django Django is used in many popular sites like as: Disqus, Instagram, Knight Foundation, MacArthur Foundation, Mozilla, National Geographic etc. There are more than 5k online sites based on Django framework. ( Source ) Sites like Hot Frameworks assess the popularity of a
3 min read
Introduction to Xamarin | A Software for Mobile App Development and App Creation
The entire world is now surrounded by billions and trillions of mobile Tech which is inevitable. The major share of the development of mobile apps is taken by the Google's Android, Apple's iOS, and Microsoft's Windows. Every new learner or newbie in Mobile Development Domain finds himself in the dilemma of choosing the platform to start with. They
9 min read
Introduction to Web Development and the Holy Trinity of it
Introduction and Holy Trinity of Web Development This article will give you an overview of the Front End Development, Back-End Development, and the Full Stack Development and will give you an idea about each of them in detail so that you can decide your field of interest for yourself. Starting from the basic book definition, ‘Web development refers
12 min read
Ajax Introduction
Introduction: The following tutorial will provide a short introduction to Ajax and its uses. Before understanding these terms see few practical examples to demonstrate the power of Ajax. Facebook, Instagram, Twitter etc are considered the situation when check news feed and if like someone post simply click the like button and the like count is adde
7 min read
Introduction to JavaScript Course - Learn how to build a task tracker using JavaScript
This is an introductory course about JavaScript that will help you learn about the basics of javascript, to begin with, the dynamic part of web development. You will learn the basics like understanding code structures, loops, objects, etc. What this course is about? In this course we will teach you about the basics of the scripting language i.e) Ja
4 min read
Introduction to Scripting Languages
All scripting languages are programming languages. The scripting language is basically a language where instructions are written for a run time environment. They do not require the compilation step and are rather interpreted. It brings new functions to applications and glue complex system together. A scripting language is a programming language des
3 min read
Introduction to React-Redux
The ones familiar with react will know that react is a component-based front end library tool that connects the various segments of the web page. In react, we use props (short for properties) in a component which allows the usage of non-static variables. With the help of props, we can pass these variables into various other components (child compon
5 min read
p5.js Introduction
p5.js is a JavaScript library used for creative coding. It is based on Processing which is a creative coding environment. The main focus of processing is to make it easy as possible for beginners to learn how to program interactive, graphical applications, to make a programming language more user-friendly by visualizing it. The advantage of using t
2 min read
PHP | Spreadsheet_Excel_Writer | Introduction
Introduction: Spreadsheet_Excel_Writer is a library written in PHP which helps to read from and write to different types of spreadsheet file formats with the help of given set of classes. Advantages: Easy and effective comparisons. Powerful analysis of large amounts of data. Splitting out large (or not so large) amounts of information in the form o
1 min read
Article Tags :
three90RightbarBannerImg