0%

Book Description

If you’re a JavaScript developer who wants to take the plunge into 3D web development, this is the perfect primer. From a basic understanding of WebGL structure to creating realistic 3D scenes, everything you need is here.

  • Dive headfirst into 3D web application development using WebGL and JavaScript.
  • Each chapter is loaded with code examples and exercises that allow the reader to quickly learn the various concepts associated with 3D web development
  • The only software that the reader needs to run the examples is an HTML5 enabled modern web browser. No additional tools needed.
  • A practical beginner's guide with a fast paced but friendly and engaging approach towards 3D web development

In Detail

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

  1. WebGL Beginner's Guide
    1. WebGL Beginner's Guide
    2. Credits
    3. About the Authors
    4. Acknowledgement
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Time for action — heading
        1. What just happened?
        2. Have a go hero — heading
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Getting Started with WebGL
      1. System requirements
      2. What kind of rendering does WebGL offer?
      3. Structure of a WebGL application
      4. Creating an HTML5 canvas
      5. Time for action — creating an HTML5 canvas
        1. What just happened?
        2. Defining a CSS style for the border
        3. Understanding canvas attributes
        4. What if the canvas is not supported?
      6. Accessing a WebGL context
      7. Time for action — accessing the WebGL context
        1. What just happened?
      8. WebGL is a state machine
      9. Time for action — setting up WebGL context attributes
        1. What just happened?
        2. Using the context to access the WebGL API
      10. Loading a 3D scene
        1. Virtual car showroom
      11. Time for action — visualizing a finished scene
        1. What just happened?
      12. Summary
    9. 2. Rendering Geometry
      1. Vertices and Indices
      2. Overview of WebGL's rendering pipeline
        1. Vertex Buffer Objects (VBOs)
        2. Vertex shader
        3. Fragment shader
        4. Framebuffer
        5. Attributes, uniforms, and varyings
      3. Rendering geometry in WebGL
        1. Defining a geometry using JavaScript arrays
        2. Creating WebGL buffers
          1. Operations to manipulate WebGL buffers
        3. Associating attributes to VBOs
          1. Binding a VBO
          2. Pointing an attribute to the currently bound VBO
          3. Enabling the attribute
        4. Rendering
          1. The drawArrays and drawElements functions
            1. Using drawArrays
            2. Using drawElements
      4. Putting everything together
      5. Time for action — rendering a square
        1. What just happened?
        2. Have a go hero — changing the square color
      6. Rendering modes
      7. Time for action — rendering modes
        1. What just happened?
      8. WebGL as a state machine: buffer manipulation
      9. Time for action — enquiring on the state of buffers
        1. What just happened?
        2. Have a go hero — add one validation
      10. Advanced geometry loading techniques: JavaScript Object Notation (JSON) and AJAX
        1. Introduction to JSON - JavaScript Object Notation
          1. Defining JSON-based 3D models
          2. JSON encoding and decoding
      11. Time for action — JSON encoding and decoding
        1. What just happened?
        2. Asynchronous loading with AJAX
          1. Setting up a web server
          2. Working around the web server requirement
      12. Time for action — loading a cone with AJAX + JSON
        1. What just happened?
        2. Have a go hero — loading a Nissan GTX
      13. Summary
    10. 3. Lights!
      1. Lights, normals, and materials
        1. Lights
        2. Normals
        3. Materials
      2. Using lights, normals, and materials in the pipeline
        1. Parallelism and the difference between attributes and uniforms
      3. Shading methods and light reflection models
        1. Shading/interpolation methods
          1. Goraud interpolation
          2. Phong interpolation
        2. Light reflection models
          1. Lambertian reflection model
          2. Phong reflection model
      4. ESSL — OpenGL ES Shading Language
        1. Storage qualifier
        2. Types
        3. Vector components
        4. Operators and functions
        5. Vertex attributes
        6. Uniforms
        7. Varyings
        8. Vertex shader
        9. Fragment shader
      5. Writing ESSL programs
        1. Goraud shading with Lambertian reflections
      6. Time for action — updating uniforms in real time
        1. What just happened?
        2. Have a go hero — moving light
        3. Goraud shading with Phong reflections
      7. Time for action — Goraud shading
        1. What just happened?
        2. Phong shading
      8. Time for action — Phong shading with Phong lighting
        1. What just happened?
      9. Back to WebGL
        1. Creating a program
        2. Initializing attributes and uniforms
      10. Bridging the gap between WebGL and ESSL
      11. Time for action — working on the wall
        1. What just happened?
      12. More on lights: positional lights
      13. Time for action — positional lights in action
        1. What just happened?
        2. Nissan GTS example
      14. Summary
    11. 4. Camera
      1. WebGL does not have cameras
      2. Vertex transformations
        1. Homogeneous coordinates
        2. Model transform
        3. View transform
        4. Projection transform
        5. Perspective division
        6. Viewport transform
      3. Normal transformations
        1. Calculating the Normal matrix
      4. WebGL implementation
        1. JavaScript matrices
        2. Mapping JavaScript matrices to ESSL uniforms
        3. Working with matrices in ESSL
      5. The Model-View matrix
        1. Spatial encoding of the world
          1. Rotation matrix
          2. Translation vector
          3. The mysterious fourth row
      6. The Camera matrix
        1. Camera translation
      7. Time for action — exploring translations: world space versus camera space
        1. What just happened?
        2. Camera rotation
      8. Time for action — exploring rotations: world space versus camera space
        1. What just happened?
        2. Have a go hero — combining rotations and translations
        3. The Camera matrix is the inverse of the Model-View matrix
        4. Thinking about matrix multiplications in WebGL
      9. Basic camera types
        1. Orbiting camera
        2. Tracking camera
          1. Rotating the camera around its location
          2. Translating the camera in the line of sight
          3. Camera model
      10. Time for action — exploring the Nissan GTX
        1. What just happened?
        2. Have a go hero — updating light positions
      11. The Perspective matrix
        1. Field of view
        2. Perspective or orthogonal projection
      12. Time for action — orthographic and perspective projections
        1. What just happened?
        2. Have a go hero — integrating the Model-view and the projective transform
      13. Structure of the WebGL examples
        1. WebGLApp
        2. Supporting objects
        3. Life-cycle functions
          1. Configure
          2. Load
          3. Draw
        4. Matrix handling functions
          1. initTransforms
          2. updateTransforms
          3. setMatrixUniforms
      14. Summary
    12. 5. Action
      1. Matrix stacks
      2. Animating a 3D scene
        1. requestAnimFrame function
        2. JavaScript timers
      3. Timing strategies
        1. Animation strategy
        2. Simulation strategy
        3. Combined approach: animation and simulation
        4. Web Workers: Real multithreading in JavaScript
      4. Architectural updates
        1. WebGLApp review
        2. Adding support for matrix stacks
        3. Configuring the rendering rate
        4. Creating an animation timer
      5. Connecting matrix stacks and JavaScript timers
      6. Time for action — simple animation
        1. What just happened?
        2. Have a go hero — simulating dropped and frozen frames
      7. Parametric curves
        1. Initialization steps
        2. Setting up the animation timer
        3. Running the animation
        4. Drawing each ball in its current position
      8. Time for action — bouncing ball
        1. What just happened?
      9. Optimization strategies
        1. Optimizing batch performance
        2. Performing translations in the vertex shader
      10. Interpolation
        1. Linear interpolation
        2. Polynomial interpolation
        3. B-Splines
      11. Time for action — interpolation
        1. What just happened?
      12. Summary
    13. 6. Colors, Depth Testing, and Alpha Blending
      1. Using colors in WebGL
      2. Use of color in objects
        1. Constant coloring
        2. Per-vertex coloring
        3. Per-fragment coloring
      3. Time for action — coloring the cube
        1. What just happened?
      4. Use of color in lights
        1. Using multiple lights and the scalability problem
          1. How many uniforms can we use?
          2. Simplifying the problem
      5. Architectural updates
        1. Adding support for light objects
          1. Improving how we pass uniforms to the program
      6. Time for action — adding a blue light to a scene
        1. What just happened?
        2. Have a go hero — adding interactivity with JQuery UI
        3. Using uniform arrays to handle multiple lights
          1. Uniform array declaration
          2. JavaScript array mapping
      7. Time for action — adding a white light to a scene
      8. Time for action — directional point lights
        1. What just happened?
      9. Use of color in the scene
        1. Transparency
        2. Updated rendering pipeline
      10. Depth testing
        1. Depth function
      11. Alpha blending
        1. Blending function
        2. Separate blending functions
        3. Blend equation
        4. Blend color
        5. WebGL alpha blending API
        6. Alpha blending modes
          1. Additive blending
          2. Subtractive blending
          3. Multiplicative blending
          4. Interpolative blending
      12. Time for action — blending workbench
        1. What just happened?
      13. Creating transparent objects
      14. Time for action — culling
        1. What just happened?
      15. Time for action — creating a transparent wall
        1. What just happened?
      16. Summary
    14. 7. Textures
      1. What is texture mapping?
      2. Creating and uploading a texture
      3. Using texture coordinates
      4. Using textures in a shader
      5. Time for action — texturing the cube
        1. What just happened?
        2. Have a go hero — try a different texture
      6. Texture filter modes
      7. Time for action — trying different filter modes
        1. What just happened?
          1. NEAREST
          2. LINEAR
        2. Mipmapping
          1. NEAREST_MIPMAP_NEAREST
          2. LINEAR_MIPMAP_NEAREST
          3. NEAREST_MIPMAP_LINEAR
          4. LINEAR_MIPMAP_LINEAR
        3. Generating mipmaps
      8. Texture wrapping
      9. Time for action — trying different wrap modes
        1. What just happened?
          1. CLAMP_TO_EDGE
          2. REPEAT
          3. MIRRORED_REPEAT
      10. Using multiple textures
      11. Time for action — using multitexturing
        1. What just happened?
        2. Have a go hero — moving beyond multiply
      12. Cube maps
      13. Time for action — trying out cube maps
        1. What just happened?
        2. Have a go hero — shiny logo
      14. Summary
    15. 8. Picking
      1. Picking
      2. Setting up an offscreen framebuffer
        1. Creating a texture to store colors
        2. Creating a Renderbuffer to store depth information
        3. Creating a framebuffer for offscreen rendering
      3. Assigning one color per object in the scene
      4. Rendering to an offscreen framebuffer
      5. Clicking on the canvas
      6. Reading pixels from the offscreen framebuffer
      7. Looking for hits
      8. Processing hits
      9. Architectural updates
      10. Time for action — picking
        1. What just happened?
        2. Picker architecture
      11. Implementing unique object labels
      12. Time for action — unique object labels
        1. What just happened?
        2. Have a go hero — clearing the scene
      13. Summary
    16. 09. Putting It All Together
      1. Creating a WebGL application
      2. Architectural review
      3. Virtual Car Showroom application
        1. Complexity of the models
        2. Shader quality
        3. Network delays and bandwidth consumption
      4. Defining what the GUI will look like
        1. Adding WebGL support
      5. Implementing the shaders
      6. Setting up the scene
      7. Configuring some WebGL properties
        1. Setting up the camera
        2. Creating the Camera Interactor
        3. The SceneTransforms object
        4. Creating the lights
        5. Mapping the Program attributes and uniforms
        6. Uniform initialization
      8. Loading the cars
        1. Exporting the Blender models
        2. Understanding the OBJ format
        3. Parsing the OBJ files
        4. Load cars into our WebGL scene
      9. Rendering
      10. Time for action — customizing the application
        1. What just happened?
        2. Have a go hero — flying through the scene
      11. Summary
    17. 10. Advanced Techniques
      1. Post-processing
        1. Creating the framebuffer
        2. Creating the geometry
        3. Setting up the shader
      2. Architectural updates
      3. Time for action — testing some post-process effects
        1. What just happened?
        2. Have a go hero — funhouse mirror effect
      4. Point sprites
      5. Time for action — using point sprites to create a fountain of sparks
        1. What just happened?
        2. Have a go hero — bubbles!
      6. Normal mapping
      7. Time for action — normal mapping in action
        1. What just happened?
      8. Ray tracing in fragment shaders
      9. Time for action — examining the ray traced scene
        1. What just happened?
        2. Have a go hero — multiple spheres
      10. Summary
18.218.61.16