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 – using multitexturing


  1. Open the file ch7_Multitexture.html with your choice of HTML editor.

  2. At the top of the script block, add another texture variable:

    var texture2 = null;
  3. At the bottom of the configure function, add the code to load the second texture. As mentioned earlier, we're using a class to make this process easier, so the new code is as follows:

    texture2 = new Texture();
    texture2.setImage('textures/light.png');
  4. The texture we're using is a white radial gradient that simulates a spot light:

  5. In the draw function, directly below the code that binds the first texture, add the following to expose the new texture to the shader:

    gl.activeTexture(gl.TEXTURE1);
    gl.bindTexture(gl.TEXTURE_2D, texture2.tex);
    gl.uniform1i(Program.uSampler1, 1);
  6. Next, we need to add the new sampler uniform to the fragment shader:

    uniform sampler2D uSampler1;
  7. Don't forget to add the corresponding string to the uniformList in the configure function.

  8. Finally, we add the code to sample the new texture value...