Book Image

WebGL Beginner's Guide

Book Image

WebGL Beginner's Guide

Overview of this book

WebGL is a new web technology that brings hardware-accelerated 3D graphics to the browser without installing additional software. As WebGL is based on OpenGL and brings in a new concept of 3D graphics programming to web development, it may seem unfamiliar to even experienced Web developers.Packed with many examples, this book shows how WebGL can be easy to learn despite its unfriendly appearance. Each chapter addresses one of the important aspects of 3D graphics programming and presents different alternatives for its implementation. The topics are always associated with exercises that will allow the reader to put the concepts to the test in an immediate manner.WebGL Beginner's Guide presents a clear road map to learning WebGL. Each chapter starts with a summary of the learning goals for the chapter, followed by a detailed description of each topic. The book offers example-rich, up-to-date introductions to a wide range of essential WebGL topics, including drawing, color, texture, transformations, framebuffers, light, surfaces, geometry, and more. With each chapter, you will "level up"ù your 3D graphics programming skills. This book will become your trustworthy companion filled with the information required to develop cool-looking 3D web applications with WebGL and JavaScript.
Table of Contents (18 chapters)
WebGL Beginner's Guide
Credits
About the Authors
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – accessing the WebGL context


We are going to modify the previous example to add a JavaScript function that is going to check the WebGL availability in your browser (trying to get a handle). This function is going to be called when the page is loaded. For this, we will use the standard DOM onLoad event.

  1. Open the file ch1_Canvas.html in your favorite text editor (a text editor that highlight HTML/JavaScript syntax is ideal).

  2. Add the following code right below the </style> tag:

    <script>
    var gl = null;
    function getGLContext(){
    var canvas = document.getElementById("canvas-element-id");
       if (canvas == null){
          alert("there is no canvas on this page");
          return;
       }
    
    var names = ["webgl", 
                 "experimental-webgl", 
                 "webkit-3d", 
                 "moz-webgl"];
    
       for (var i = 0; i < names.length; ++i) {
           try {
              gl = canvas.getContext(names[i]);
           } 
           catch(e) {}
           if (gl) break;
       }
    
       if (gl == null){
          alert("WebGL is not available");
       }
       else{
          alert("Hooray! You got a WebGL context");
       }
    }
       </script>
  3. We need to call this function on the onLoad event. Modify your body tag so it looks like the following:

    <body onLoad ="getGLContext()">
  4. Save the file as ch1_GL_Context.html.

  5. Open the file ch1_GL_Context.html using one of the WebGL supported browsers.

  6. If you can run WebGL you will see a dialog similar to the following:

What just happened?

Using a JavaScript variable (gl), we obtained a reference to a WebGL context. Let's go back and check the code that allows accessing WebGL:

var names = ["webgl", 
             "experimental-webgl", 
             "webkit-3d", 
             "moz-webgl"];
   
for (var i = 0; i < names.length; ++i) {
       try {
          gl = canvas.getContext(names[i]);
       } 
       catch(e) {}
       if (gl) break;
}

The canvas getContext method gives us access to WebGL. All we need to specify a context name that currently can vary from vendor to vendor. Therefore we have grouped them in the possible context names in the names array. It is imperative to check on the WebGL specification (you will find it online) for any updates regarding the naming convention.

getContext also provides access to the HTML5 2D graphics library when using 2d as the context name. Unlike WebGL, this naming convention is standard. The HTML5 2D graphics API is completely independent from WebGL and is beyond the scope of this book.