Skip to main content
Engineering LibreTexts

6: Introduction to WebGL

  • Page ID
    13744
  • In this chapter, we turn to WebGL, the version of OpenGL for the Web. Three.js, which was covered in the previous chapter, uses WebGL for 3D graphics. Of course, it is more difficult to use WebGL directly, but doing so gives you full control over the graphics hardware. And learning it will be a good introduction to modern graphics programming.

    There have been many versions of OpenGL. WebGL 1.0 is based on OpenGL ES 2.0, a version designed for use on embedded systems such as smart phones and tablets. OpenGL ES 1.0 was very similar to OpenGL 1.1, which we studied in Chapter 3 and Chapter 4. However, the 2.0 version of OpenGL ES introduced major changes. It is actually a smaller, simpler API that puts more responsibility on the programmer. For example, functions for working with transformations, such as glRotatef and glPushMatrix, were eliminated from the API, making the programmer responsible for keeping track of transformations. WebGL does not use glBegin/glEnd to generate geometry, and it doesn’t use function such as glColor* or glNormal* to specify attributes of vertices. Nevertheless, it will turn out that much of what you learned in previous chapters will carry over to WebGL.

    There are two sides to any WebGL program. Part of the program is written in JavaScript, the programming language for the web. The second part is written in GLSL, a language for writing “shader” programs that run on the GPU. I will try to always be clear about which language I am talking about.

    For this introductory chapter about WebGL, we will stick to basic 2D graphics. You will learn about the structure of WebGL programs. You will learn most of the JavaScript side of the API, and you learn how to write and use simple shaders. In the next chapter, we will move on to 3D graphics, and you will learn a great deal more about GLSL.