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 – JSON encoding and decoding


Let's create a simple model: a 3D line. Here we will be focusing on how we do JSON encoding and decoding. Follow the given steps:

  1. Go to your Internet browser and open the interactive JavaScript console. Use the following table for assistance:

    Web browser

    Menu option

    Shortcut keys (PC / Mac)

    Firefox

    Tools | Web Developer | Web Console

    Ctrl + Shift + K / Command + Alt + K

    Safari

    Develop | Show Web Inspector

    Ctrl + Shift + C / Command + Alt + C

    Chrome

    Tools | JavaScript Console

    Ctrl + Shift + J / Command + Alt + J

  2. Create a JSON object by typing:

    var model = {"vertices":[0,0,0,1,1,1], "indices":[0,1]};
  3. Verify that the model is an object by writing:

    typeof(model)
  4. Now, let's print the model attributes. Write this in the console (press Enter at the end of each line):

    model.vertices
    model.indices
  5. Now, let's create a JSON text:

    var text = JSON.stringify(model)
    alert(text)
  6. What happens when you type text.vertices?

    As you can see, you get an...