Book Image

WebGL Game Development

By : Sumeet Arora
Book Image

WebGL Game Development

By: Sumeet Arora

Overview of this book

<p>WebGL, the web implementation of Open GL, is a JavaScript API used to render interactive 3D graphics within any compatible web browser, without the need for plugins. It helps you create detailed, high-quality graphical 3D objects easily. WebGL elements can be mixed with other HTML elements and composites to create high-quality, interactive, creative, innovative graphical 3D objects.</p> <p>This book begins with collecting coins in Super Mario, killing soldiers in Contra, and then quickly evolves to working out strategies in World of Warcraft. You will be guided through creating animated characters, image processing, and adding effects as part of the web page canvas to the 2D/3D graphics. Pour life into your gaming characters and learn how to create special effects seen in the most powerful 3D games. Each chapter begins by showing you the underlying mathematics and its programmatic implementation, ending with the creation of a complete game scene to build a wonderful virtual world.</p>
Table of Contents (17 chapters)
WebGL Game Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Parsing UV coordinates from the JSON file


Now, we will walk you through the code to show the changes that we need to perform to load the JSON file with a texture. But, first let's understand the biggest challenge we are going to face to load the JSON file in order to have the minimum memory footprint.

The challenge and the algorithm

First, let's review the basic principles of WebGL texture mapping:

  • Each texture coordinate is mapped to a vertex and vice versa. So, if we have eight vertices defined in the vertex buffer, we need eight texture coordinates. If we have 16 vertices defined in the vertex buffer, we need 16 texture coordinates. Take a look at the previous code snippets of this chapter and you will notice we had four UV values for four vertices of the square. This applies to all vertex attributes, such as colors and normals, among others.

  • We used the index buffer so that we do not have to repeat vertices in the vertex buffer. We did this so that the memory we assign for the vertex buffer...