Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Real-Time 3D Graphics with WebGL 2

You're reading from   Real-Time 3D Graphics with WebGL 2 Build interactive 3D applications with JavaScript and WebGL 2 (OpenGL ES 3.0)

Arrow left icon
Product type Paperback
Published in Oct 2018
Publisher Packt
ISBN-13 9781788629690
Length 500 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Farhad Ghayour Farhad Ghayour
Author Profile Icon Farhad Ghayour
Farhad Ghayour
Diego Cantor Diego Cantor
Author Profile Icon Diego Cantor
Diego Cantor
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Getting Started FREE CHAPTER 2. Rendering 3. Lights 4. Cameras 5. Animations 6. Colors, Depth Testing, and Alpha Blending 7. Textures 8. Picking 9. Putting It All Together 10. Advanced Techniques 11. WebGL 2 Highlights 12. Journey Ahead 13. Other Books You May Enjoy

Architecture Updates

As we progress through chapters, we will encounter common functionality (for example, design patterns, utility functions, helpers, and data structures) that we can build upon. Not only this will serve us in writing DRY code, but it will also provide a useful architecture to support an advanced 3D WebGL application by the end of this book.

DRY

Don't Repeat Yourself (DRY) is a software development principle, the main aim of which is to reduce repetition of code. Write Everything Twice (WET) is a cheeky abbreviation to mean the opposite— code that doesn't adhere to the DRY principle.

Let's cover some changes that we will use in future chapters:

  1. Open common/js/utils.js in your editor to see the following code.
  2. We will use utils to include many of the utility functions to serve us in building our 3D application. The two methods, getCanvas and getGLContent, inside of utils are similar to the code we've implemented earlier in this chapter:
'use strict';

// A set of utility functions for /common operations across our
// application
const utils = {

// Find and return a DOM element given an ID
getCanvas(id) {
const canvas = document.getElementById(id);

if (!canvas) {
console.error(`There is no canvas with id ${id} on this
page.`
);
return null;
}

return canvas;
},

// Given a canvas element, return the WebGL2 context
getGLContext(canvas) {
return canvas.getContext('webgl2') || console.error('WebGL2 is
not available in your browser.'
);
}

};
  1. getCanvas returns the canvas element with the provided id as the argument.
  2. getGLContext returns a WebGL 2 context for a given canvas element.
  3. Open up ch01_05_attributes-final.html in your editor to see the following changes.
  4. We've included <link rel="stylesheet" href="/common/lib/normalize.css"> in the <head> of our document that resets many of the inconsistencies across browsers. This is an external library to help us normalize CSS styling across browsers.
  5. We've included <script type="text/javascript" src="/common/js/utils.js"></script>.
  1. Scroll to the init function where the necessary changes were made to use the utils.getCanvas and utils.getGLContext functions:
function init() {
const canvas = utils.getCanvas('webgl-canvas');
gl = utils.getGLContext(canvas);
window.onkeydown = checkKey;
}
  1. Open up ch01_05_attributes-final.html in a browser to see these changes in action.
Example Code Structure

All example code has been structured so that common functionality is at the root of the directory ( common/), while examples for each chapter are categorized under chapter directories (for example, ch01/, ch02/, and ch03/). That being said, to view these examples in your browser, you will need to start a server at the root of the directory to load all required assets for each example. Please refer to the Preface of this book for more details.
You have been reading a chapter from
Real-Time 3D Graphics with WebGL 2 - Second Edition
Published in: Oct 2018
Publisher: Packt
ISBN-13: 9781788629690
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image