ebook img

Beginning WebGL for HTML5 PDF

348 Pages·2·10.969 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Beginning WebGL for HTML5

www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.it-ebooks.info Contents at a Glance About the Author ..........................................................................................................xv About the Technical Reviewer .....................................................................................xvi Acknowledgments ......................................................................................................xvii Introduction ...............................................................................................................xviii ■ Chapter 1: Setting the Scene .......................................................................................1 ■ Chapter 2: Shaders 101 .............................................................................................33 ■ Chapter 3: Textures and Lighting ...............................................................................57 ■ Chapter 4: Increasing Realism ..................................................................................85 ■ Chapter 5: Physics ...................................................................................................115 ■ Chapter 6: Fractals, Height Maps, and Particle Systems .........................................139 ■ Chapter 7: Three.js Framework ...............................................................................173 ■ Chapter 8: Productivity Tools ..................................................................................205 ■ Chapter 9: Debugging and Performance ..................................................................233 ■ Chapter 10: Effects, Tips, and Tricks .......................................................................267 ■ Afterword: The Future of WebGL ..............................................................................299 ■ Appendix A: Essential HTML5 and JavaScript .........................................................303 ■ Appendix B: Graphics Refresher ..............................................................................309 ■ Appendix C: WebGL Spec. Odds and Ends ................................................................315 ■ Appendix D: Additional Resources ..........................................................................317 ■ Index........................................................................................................................323 iv www.it-ebooks.info Introduction WebGL (Web-based Graphics Language) is a wonderful and exciting new technology that lets you create powerful 3D graphics within a web browser. The way that this is achieved is by using a JavaScript API that interacts with the Graphics Processing Unit (GPU). This book will quickly get you on your way to demystify shaders and render realistic scenes. To ensure enjoyable development, we will show how to use debugging tools and survey libraries which can maximize productivity. Audience Beginning WebGL for HTML5 is aimed at graphics enthusiasts with a basic knowledge of computer graphics techniques. A knowledge of OpenGL, especially a version that uses the programmable pipeline, such as OpenGL ES is beneficial, but not essential. We will go through all the relevant material. A JavaScript background will certainly help. When writing a book of this nature, we unfortunately cannot cover all the prerequisite material. Baseline assumptions about the reader need to be made. The assumptions that I have made are that the reader has a basic knowledge of 2D and 3D computer graphics concepts such as pixels, colors, primitives, and transforms. Appendix B quickly refreshes these concepts. It is also assumed that the reader is familiar (though need not be an expert) with HTML, CSS, and JavaScript. Although much of the book makes use of plain “vanilla” JavaScript, we will use some jQuery. Appendix A discusses newer HTML5 concepts and a quick jQuery crash course that will be essential for properly understanding the text. Appendix D provides a complete reference for further reading on topics that are presented throughout the book. What You Will Learn This book presents theory when necessary and examples whenever possible. You will get a good overview of what you can do with WebGL. What you will learn includes the following: • Understanding the model view matrix and setting up a scene • Rendering and manipulating primitives • Understanding shaders and loving their power and flexibility • Exploring techniques to create realistic scenes • Using basic physics to simulate interaction • Using mathematics models to render particle systems, terrain, and fractals • Getting productive with existing models, shaders, and libraries xviii www.it-ebooks.info ■ INTrOduCTION • Using the Three.js framework • Learning about GLGE and philoGL frameworks and a survey of other frameworks available • Debugging and performance tips • Understanding other shader uses, such as image processing and nonphotorealistic rendering • Using an alternate framebuffer to implement picking and shadowmaps • Learning about current browser and mobile support and the future of WebGL Book Structure It is recommended that you start by reading the first two chapters before moving on to other areas of the book. Even though the book does follow a fairly natural progression, you may choose to read the book in order or skip around as desired. For example, the debugging section of Chapter 9 is not strictly essential, but is very useful information to know as soon as possible. Chapter 1: Setting the Scene We go through all the steps to render an image with WebGL, including testing for browser support and setting up the WebGL environment, using vertex buffer objects (VBOs), and basic shaders. We start with creating a one color static 2D image, and by the end of the chapter have a moving 3D mesh with multiple colors. Chapter 2: Shaders 101 Shaders are covered in depth. We show an overview of graphics pipelines (fixed and programmable), give a background of the GL Shading Language (GLSL), and explain the roles of vertex and fragment shaders. Next we go over the primitive types and language details of GLSL and how our WebGL application will interact with our shaders. Finally, we show several examples of GLSL usage. Chapter 3: Textures and Lighting We show how to apply texture and simple lighting. We explain texture objects and how to set up and configure them and combine texture lookups with a lighting model in our shader. Chapter 4: Increasing Realism A more realistic lighting model—Phong illumination—is explained and implemented. We discuss the difference between flat and smooth shading and vertex and fragment calculations. We show how to add fog and blend objects; and discuss shadows, global illumination, and reflection and refraction. Chapter 5: Physics This chapter shows how to model gravity, elasticity, and friction. We detect and react to collisions, model projectiles and explore both the conservation of momentum and potential and kinetic energy. xix www.it-ebooks.info ■ INTrOduCTION Chapter 6: Fractals, Height Maps, and Particle Systems In this chapter we show how to paint directly with the GPU, discuss fractals, and model the Mandlebrot and Julia sets. We also show how to produce a height map from a texture and generate terrain. We also explore particle systems. Chapter 7: Three.js Framework The Three.js WebGL framework is introduced. We provide a background and sample usage of the library, including how to fall back to the 2D rendering context if necessary, API calls to easily create cameras, objects, and lighting. We compare earlier book examples to the equivalent Three.js API calls and introduce tQuery, a library that combines Three.js and jQuery selectors. Chapter 8: Productivity Tools We discuss the benefits of using frameworks and the merit of learning core WebGL first. Several available frameworks are discussed and the GLGE and philoGL frameworks are given examples. We show how to load existing meshes and find other resources. We list available physics libraries and end the chapter with an example using the physi.js library. Chapter 9: Debugging and Performance An important chapter to help identify and fix erroneous code and improve performance by following known WebGL best practices. Chapter 10: Effects, Tips, and Tricks Image processing and nonphotorealistic shaders are discussed and implemented. We show how to use offscreen framebuffers that enable us to pick objects from the canvas and implement shadow maps. Afterword: The Future of WebGL In the afterword, we will speculate on the bright future of WebGL, the current adoption of it within the browser, and mobile devices and what features will be added next. Appendix A: Essential HTML5 and JavaScript We cover some of the changes between HTML 4 and 5, such as shorter tags, added semantic document structure, the <canvas> element, and basic JavaScript and jQuery usage. Appendix B: Graphics Refresher This appendix is a graphics refresher covering coordinate systems, elementary transformations and other essential topics. xx www.it-ebooks.info ■ INTrOduCTION Appendix C: WebGL Specification Odds and Ends Contains part of the WebGL specification, available at http://www.khronos.org/registry/webgl/specs/latest/, which were not covered in the book, but are nonetheless important. Appendix D: Additional Resources A list of references for further reading about topics presented in the book such as HTML5, WebGL, WebGLSL, JavaScript, jQuery, server stacks, frameworks, demos, and much more. WebGL Origins The origin of WebGL starts 20 years ago, when version 1.0 of OpenGL was released as a nonproprietary alternative to Silicon Graphics’ Iris GL. Up until 2004, OpenGL used a fixed functionality pipeline (which is explained in Chapter 2). Version 2.0 of OpenGL was released that year and introduced the GL Shading Language (GLSL) which lets you program the vertex and fragment shading portions of the pipeline. The current version of OpenGL is 4.2, however WebGL is based off of OpenGL Embedded Systems (ES) 2.0, which was released in 2007 and is a trimmer version of OpenGL 2.0. Because OpenGL ES is built for use in embedded devices like mobile phones, which have lower processing power and fewer capabilities than a desktop computer, it is more restrictive and has a smaller API than OpenGL. For example, with OpenGL you can draw vertices using both a glBegin...glEnd section or VBOs. OpenGL ES only uses VBOs, which are the most performance-friendly option. Most things that can be done in OpenGL can be done in OpenGL ES. In 2006, Vladimar Vukic´evic´ worked on a Canvas 3D prototype that used OpenGL for the web. In 2009, the Khronos group created the WebGL working group and developed a central specification that helps to ensure that implementations across browsers are close to one another. The 3D context was modified to WebGL, and version 1.0 of the specification was completed in spring 2011. Development of the WebGL specification is under active development, and the latest revision can be found at http://www.khronos.org/registry/webgl/specs/latest/. How Does WebGL work? WebGL is a JavaScript API binding from the CPU to the GPU of a computer’s graphics card. The API context is obtained from the HTML5 <canvas> element, which means that no browser plugin is required. The shader program uses GLSL, which is a C++ like language, and is compiled at runtime. Without a framework, setting up a WebGL scene does require quite a bit of work: handling the WebGL context, setting buffers, interacting with the shaders, loading textures, and so on. The payoff of using WebGL is that it is much faster than the 2D canvas context and offers the ability to produce a degree of realism and configurability that is not possible outside of using WebGL. Uses Some uses of WebGL are viewing and manipulating models and designs, virtual tours, mapping, gaming, art, data visualization, creating videos, manipulating and processing of data and images. xxi www.it-ebooks.info ■ INTrOduCTION Demonstrations There are many demos of WebGL, including these: • http://www.chromeexperiments.com/webgl • https://code.google.com/p/webglsamples/ • http://aleksandarrodic.com/p/jellyfish/ • Google Body (now http://www.zygotebody.com), parts of Google Maps, and Google Earth • http://www.ro.me/tech/ • http://alteredqualia.com/ Supported Environments Does your browser support WebGL? It is important to know that WebGL is not currently supported by all browsers, computers and/or operating systems (OS). Browser support is the easiest requirement to meet and can be done simply by upgrading to a newer version of your browser or switching to a different browser that does support WebGL if necessary. The minimum requirements are as follows: • Firefox 4+ • Safari 5.1+ (OS X only) • Chrome 9+ • Opera 12alpha+ • Internet Explorer (IE)—no native support Although IE currently has no built in support, plugins are available; for example, JebGL (available at http://code.google.com/p/jebgl/), Chrome Frame (available at http://www.google.com/chromeframe), and IEWebGL (http://iewebgl.com/). JebGL converts WebGL to a Java applet for deficient browsers; Chrome Frame allows WebGL usage on IE, but requires that the user have it installed on the client side. Similarly, IEWebGL is an IE plugin. In addition to a current browser, you need a supported OS and newer graphics card. There are also several graphics card and OS combinations that have known security vulnerabilities or are highly prone to a severe system crash and so are blacklisted by browsers by default. Chrome supports WebGL on the following operating systems (according to Google Chrome Help (http://www.google.com/support/chrome/bin/answer.py?answer=1220892): • Windows Vista and Windows 7 (recommended) with no driver older than 2009–01 • Mac OS 10.5 and Mac OS 10.6 (recommended) • Linux Often, updating your graphics driver to the latest version will enable WebGL usage. Recall that OpenGL ES 2.0 is based on OpenGL 2.0, so this is the version of OpenGL that your graphics card should support for WebGL usage. There is also a project called ANGLE (Almost Native Graphics Layer Engine) that ironically uses Microsoft Direct X to enhance a graphics driver to support OpenGL ES 2.0 API calls through conversions to Direct X 9 API calls. The result is that graphics cards that only support OpenGL 1.5 (OpenGL ES 1.0) can still run WebGL. Of course, support for WebGL should improve drastically over the next couple of years. xxii www.it-ebooks.info ■ INTrOduCTION Testing for WebGL Support To check for browser support of WebGL. there are several websites such as http://get.webgl.org/, which displays a spinning cube on success; and http://doesmybrowsersupportwebgl.com/, which gives a large “Yay” or “Nay” and specific details if the webgl context is supported. We can also programmatically check for WebGL support using modernizr (http://www.modernizr.com). Companion Site Along with the Apress webpage at http://www.apress.com/9781430239963, this book has a companion website at http://www.beginningwebgl.com. This site demonstrates the examples found in the book, and offers an area to make comments and add suggestions directly to the author. Your constructive feedback is both welcome and appreciated. Downloading the code The code for the examples shown in this book is available on the Apress website, http://www.apress.com. A link can be found on the book’s information page, http://www.apress.com/9781430239963, under the Source Code/ Downloads tab. This tab is located underneath the Related Titles section of the page. Updated code will also be hosted on github at https://github.com/bdanchilla/beginningwebgl. Contacting the Author If you have any questions or comments—or even spot a mistake you think I should know about—you can contact the author directly at [email protected] or on the contact form at http://www.beginningwebgl.com/contact. xxiii www.it-ebooks.info Chapter 1 Setting the Scene In this chapter we will go through all the steps of creating a scene rendered with WebGL. We will show you how to • obtain a WebGL context • create different primitive types in WebGL • understand and create vertex buffer objects (VBOs) and attributes • do static two-dimensional rendering • create a program and shaders • set up the view matrices • add animation and movement • render a three-dimensional model A Blank Canvas Let’s start by creating a HTML5 document with a single <canvas> element (see Listing 1-1). Listing 1-1. A basic blank canvas <!doctype html> <html> <head> <title>A blank canvas</title> <style> body{ background-color: grey; } canvas{ background-color: white; } </style> </head> <body> <canvas id="my-canvas" width="400" height="300"> Your browser does not support the HTML5 canvas element. </canvas> </body> </html> The HTML5 document in Listing 1-1 uses the shorter <!doctype html> and <html> declaration available in HTML5. In the <head> section, we set the browser title bar contents and then add some basic styling that will 1 www.it-ebooks.info

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.