Over the past 15 years, OpenGL has become

Similar documents
Programming with OpenGL Complete Programs Objectives Build a complete first program

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

CS452/552; EE465/505. Image Formation

Programming with WebGL Part 1: Background. CS 432 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

CS452/552; EE465/505. Overview of Computer Graphics

Objectives. Programming with WebGL Part 1: Background. Retained vs. Immediate Mode Graphics. Early History of APIs. PHIGS and X.

Introduction to Computer Graphics with WebGL

Introduction to Computer Graphics with WebGL

CS452/552; EE465/505. Review & Examples

We will use WebGL 1.0. WebGL 2.0 is now being supported by most browsers but requires a better GPU so may not run on older computers or on most cell

API Background. Prof. George Wolberg Dept. of Computer Science City College of New York

We assume that you are familiar with the following:

CS452/552; EE465/505. Image Processing Frame Buffer Objects

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Introduction to Computer Graphics with WebGL

CS 432 Interactive Computer Graphics

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

CS452/552; EE465/505. Models & Viewing

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Computer Graphics. Bing-Yu Chen National Taiwan University

LECTURE 02 OPENGL API

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

Programming with OpenGL Part 1: Background

CS450/550. Pipeline Architecture. Adapted From: Angel and Shreiner: Interactive Computer Graphics6E Addison-Wesley 2012

Building Models. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Introduction to Computer Graphics with WebGL

Lecture 4 of 41. Lab 1a: OpenGL Basics

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

Introduction to OpenGL

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Introduction to Computer Graphics with WebGL

OpenGL refresher. Advanced Computer Graphics 2012

Programming using OpenGL: A first Introduction

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

CS475/CS675 - Computer Graphics. OpenGL Drawing

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Shaders. Slide credit to Prof. Zwicker

WebGL (Web Graphics Library) is the new standard for 3D graphics on the Web, designed for rendering 2D graphics and interactive 3D graphics.

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Early History of APIs. PHIGS and X. SGI and GL. Programming with OpenGL Part 1: Background. Objectives

Books, OpenGL, GLUT, CUDA, OpenCL, OpenCV, PointClouds, G3D, and Qt

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Introduction to Computer Graphics. Hardware Acceleration Review

Introduction to OpenGL/GLSL and WebGL GLSL

Introduction to Computer Graphics with WebGL

An Overview GLUT GLSL GLEW

WebGL. Creating Interactive Content with WebGL. Media #WWDC14. Session 509 Dean Jackson and Brady Eidson WebKit Engineers

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

OPENGL RENDERING PIPELINE

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Graphics Programming

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

CS452/552; EE465/505. Transformations

Lectures OpenGL Introduction

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

2D graphics with WebGL

Models and Architectures

CIS 441/541: Introduction to Computer Graphics Lecture 14: OpenGL Basics

Basic Graphics Programming

BCA611 Video Oyunları için 3B Grafik

Introduction to OpenGL Week 1

Objectives. Image Formation Revisited. Physical Approaches. The Programmer s Interface. Practical Approach. Introduction to OpenGL Week 1

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

Water Simulation on WebGL and Three.js

to OpenGL Introduction Pipeline Graphics pipeline OpenGL pipeline OpenGL syntax Modeling Arrays Conclusion 1 Introduction Introduction to OpenGL

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

Rendering Objects. Need to transform all geometry then

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

Sign up for crits! Announcments

3D computer graphics: geometric modeling of objects in the computer and rendering them

Precept 2 Aleksey Boyko February 18, 2011

WebGL. Announcements. WebGL for Graphics Developers. WebGL for Web Developers. Homework 5 due Monday, 04/16. Final on Tuesday, 05/01

Lecture 13: OpenGL Shading Language (GLSL)

Introduction to Computer Graphics with OpenGL/GLUT

Basic Graphics Programming

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

OpenGL shaders and programming models that provide object persistence

Announcements OpenGL. Computer Graphics. Spring CS4815

Mobile Application Programing: Android. OpenGL Operation

CSC 8470 Computer Graphics. What is Computer Graphics?

WebGL. WebGL. Bring 3D to the Masses. WebGL. The web has text, images, and video. We want to support. Put it in on a webpage

Game Graphics & Real-time Rendering

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Transcription:

Editors: Beatriz Sousa Santos and Ginger Alford The Case for Teaching Computer Graphics with WebGL: A 25-Year Perspective Ed Angel University of New Mexico Over the past 15 years, OpenGL has become the standard API for teaching computer graphics courses in computer science and engineering, mathematics, and various other areas in science and engineering. Whether the course focuses on the mathematical and algorithmic aspects of the field or on teaching students to build geometric models, the OpenGL API is a key part of the course. As graphics technology has evolved, the OpenGL API has also evolved to support advances in hardware and software. More recently, the JavaScript implementation of OpenGL ES 2.0, WebGL, has become an increasingly attractive API for both educational and professional practice. While the goals of a first course in computer graphics remain largely unchanged, changes to the API have been significant. From an instructor s perspective, keeping up with these changes can require significant changes to an established course. In this article, I examine three major forms of OpenGL (fixed-function OpenGL, -based OpenGL, WebGL) and three.js, a scene graph built on top of WebGL. I argue that almost all instructors should choose the last two, each of which has its own distinct audience. The Evolution of the Computer Graphics Curriculum Two seminal books essentially defined the modern computer graphics curriculum: Principles of Interactive Computer Graphics by William Newman and Robert Sproull 1 and Fundamentals of Interactive Computer Graphics by James Foley and Andries van Dam. 2 And for more than 30 years, virtually every introductory computer graphics course has been comprised of these topics: geometry and modeling, transformations, viewing, lighting and shading, texture mapping and pixel, and rasterization. How we teach these topics has changed dramatically, however. When this curriculum first became widespread, there was little hardware and software available to support courses, with the exception of the few institutions with research programs in the area. Instructors either taught a course without a programming component or used some sort of low-level local library. Consequently, most courses focused on algorithms and theory. Early efforts at 3D software such as Core Graphics and PHIGS were problematic. Only since the advent of OpenGL have we been able to develop courses with an API that was available on all standard systems (Windows, Linux, and Mac OS) and that supported the key course concepts. Table 1 lists the most important releases in the OpenGL family of APIs and dates. Teaching with OpenGL 1 Released in 1992, OpenGL 1.0 was based on immediate-mode graphics and a pipeline architecture (see Figure 1). The basic code to specify and display an object consisted of multiple instances that looked something like this: glbegin( ); glcolor( ); gltexcoord( ); glnormal( ); gl( ); glcolor( );.. glend(); 106 March/April 2017 Published by the IEEE Computer Society 0272-1716/17/$33.00 2017 IEEE

Vertices were assembled into the geometric objects specified by a parameter in glbegin once the glend was executed, and the result was passed through the pipeline for potential display. The information in the vertices was not kept, so any modification and redisplay required the reexecution of the code defining the geometry. Because the OpenGL API was defined by a pipeline with vertices flowing through fixed stages (transformations, clipping, rasterization, and hidden surface removal), there was very little a student could do to change the pipeline, other than vary certain parameters. In practice, OpenGL acted more like a state machine in which the state included the current color, current transformation matrices, and other values set by functions in the API. In the previous sample code fragment, only the gl function could cause any visible output, and the rest of the functions only altered the OpenGL state. One of the nice aspects of this early version of OpenGL was that there was a rich set of functions, including transformations, viewing, and lighting to set the state. I suspect that there are still many courses that use this version because it couples nicely with the classical concepts in computer graphics. In 1997, I published the first edition of Interactive Computer Graphics: A Top-Down Approach with OpenGL. 3 It was based on the premise that OpenGL would enhance the curriculum by allowing students to construct 3D applications that would let them explore key algorithms and to complete term projects using the concepts from the class. Crucial to my claim was that OpenGL was supported on all the standard operating systems. This approach soon became standard, and the first five editions of the textbook all used the fixedfunction pipeline, as did all the competing texts for a first course. Unfortunately, the fixed-function API is now far from modern graphics architectures, has at best limited support, and is not used in industry. Consequently, this early API is no longer a good choice for teaching. Teaching with OpenGL 2 Advances in graphics architectures eventually led to a new version of the OpenGL API that supported programmable s and introduced the OpenGL Shading Language (GLSL). OpenGL 2 still had the pipeline model, but it exposed the vertex and fragment s explicitly as well. Because the s had defaults equivalent to OpenGL 1, we could still use immediate mode and the fixedfunction pipeline. However, by using GLSL, we Table 1. Key API release dates. Release Year OpenGL 1.0 1992 OpenGL 2.0 2004 OpenGL 3.1 2009 OpenGL ES 2.0 2010 WebGL 1.0 2011 OpenGL 4.5 2014 Vulcan 1.0 2016 WebGL 2.0 2016 Application Geometry Pixel could also use alternative algorithms, especially for lighting, and compare the performance of using s on a GPU versus a pure CPU version of the same algorithm. The fourth edition of Interactive Computer Graphics included a chapter on GLSL. Teaching with OpenGL 3 Although new versions of the API had added functions for working with s and introduced GLSL, starting with OpenGL 3.1 the fixed-function pipeline and the functions that supported it were deprecated. Not only were most of the familiar functions no longer available, but every application had to provide its own s. In addition, because there was no longer an immediate mode, each application had to create and manipulate buffers for holding vertex properties (position, color, and texture coordinates). Figure 2 shows the OpenGL 3 architectural model. My coauthor, Dave Shreiner, and I decided that we would use this version of OpenGL for the sixth edition of our book. 4 Even though it would require instructors to make major changes to their courses, which was not the case with previous releases of OpenGL, we believed that -based APIs were key to the future of computer graphics. From a teaching perspective, OpenGL 3 forced major changes to the way most instructors taught computer graphics. In particular, because applications had to provide their own s, GPU architecture and GLSL had to be presented at the beginning of a course, thus delaying many other core topics. Frame buffer Figure 1. Simplified OpenGL pipeline. The application produces vertices that describe the geometry. The vertices are assembled into triangles, which are clipped out if they cannot be visible. The rasterizer produces a set of potential pixels called fragments that are processed through hidden removal and operations such as texture mapping that determine their color. IEEE Computer Graphics and Applications 107

CPU Application Rasterizer GPU data flow Vertices Vertices Fragments Pixels Fragment Frame buffer Fragment Figure 2. OpenGL 3 architecture. Beginning with this version, many familiar functions were no longer available and every application had to provide its own s. #include <GLUT/glut.h> void display(void) glclear(gl_color_buffer_bit); glbegin(gl_polygon); glcolor3f(1.0, 0.0, 0.0); gl2f(-1.0, -1.0); gl2f 0.0, 1.0); gl2f(1.0, -1.0); glend(); glflush(); int main(int argc, char** argv) glutinit(&argc,argv); glutcreatewindow( simple ); glutdisplayfunc(display); glutmainloop(); /* glut.h includes gl.h and glu.h*/ Figure 3. Simple WebGL example. This example, written in C, displays a red triangle on a black background. It relies on many of the OpenGL 1.0 default values. Less obvious at the time, this version also caused other issues because not all implementations supported the OpenGL 3.1 core profile. In addition, some of the standard libraries used in most courses, such as the OpenGL Utility Toolkit (GLUT), were incompatible with the new OpenGL. Although some of the libraries evolved, usually with limited functionality, assembling a set of working libraries for each architecture was a problem for students and instructors alike. An Alternative: WebGL Although these changes led to a stronger and more modern course, each new version of OpenGL was more complex than the previous one. For most instructors, it became increasingly difficult to get their classes off the ground. The beginning of every semester brought about an almost endless series of problems with getting a working set of libraries for each OS, dealing with 32- and 64-bit versions, and working in changes to the API into their lectures. It was also clear that future versions of OpenGL would add more complexity and were unlikely to improve the basic curriculum. The release of OpenGL ES 2.0 provided an interesting alternative. OpenGL ES 2.0 is based on OpenGL 3.1, but it is smaller and is intended to run on embedded systems and handheld devices with less memory and where power consumption is an issue. ES has been used extensively to develop smartphone apps. Despite its widespread use in industry, however, it has not significantly impacted computer graphics classes. Everything changed with WebGL though. WebGL 1.0 is a JavaScript implementation of OpenGL ES 2.0. From a teaching perspective, WebGL offers a number of attractive features: Runs in all browsers including those in smart phones Interpreted code aids development Browsers provide code development environments including debuggers Many tools available on the web Performance rivals compiled code WebGL JS API is more modern than OpenGL with C/C++ Easy integration with other Web APIs Teaching with WebGL Let s start with a simple example going back to OpenGL 1.0 using the GLUT library to open the window. The example in Figure 3 displays a red triangle on a black background and requires only 12 lines of code. It uses many defaults including the drawing color, background color, window size, and coordinate system. Still, it s so straightforward that it can be presented on the first day of class, and even without commenting, what each line of the code does is pretty clear. Changing most of the default values requires one line of code for each. 108 March/April 2017

This same example in OpenGL 3.1 would require almost 100 lines of code. Why? We must provide two s, even if these s do nothing other than pass on the data to the next stage in the pipeline. We need the code to read, compile, and link the s with the application code. We have to add vertex buffers in the application to send data to the s. Most importantly, the resulting program is not something that can be understood on the first day of class. In addition, multiple libraries are needed for interacting with the local window system and for interaction, which are different for every system. Whereas an instructor could get OpenGL 1.0 working on all the standard systems without difficulty, OpenGL 3.1 often created nightmares for instructors trying to get their students going. Many of these issues apply to WebGL, but as I will show, they are much easier to deal with in WebGL. I ll leave out the 100 lines of OpenGL 3 code and illustrate the example with WebGL, arguing that most of the problems are less difficult to solve. We will see many of the advantages of WebGL even with such a simple example. The full OpenGL 3 example and all the other code referred to in this article are available online at www.cs.unm.edu/~angel/cga. Figure 4 gives the example in WebGL 1.0, with an HTML file that includes the s and a JavaScript file. The first thing that we notice is that there are three programming languages in the Figure 4 example: JavaScript, HTML, and GLSL. As long as we are using a -based OpenGL/WebGL, we have to use GLSL. Because GLSL is a C-like language with some additional features such as a matrix and vectors as atomic types and operator overloading, it does not pose a problem for either instructors or students. The HTML file is necessary to access the needed resources and to open the canvas element to provide a drawing surface (see Figure 4a). This file is pretty much the same for every application and is simple enough that it doesn t require much class time to introduce. In addition, most students already have some familiarity with HTML. We include the s in the HTML file, but they could be in separate files, which would be preferable, although many browsers have security issues with the separate files if the application is not run from a server. The utility file webgl-utils.js one of many readily available libraries that is used in the application (WebGLUtils.setupWebGL) to create a WebGL context, a JavaScript object that contains all the functions and constants comprising the WebGL API. <!DOCTYPE html> <html> <script id= vertex- type= x-/x-vertex > attribute vec4 vposition; void main() gl_position = vposition; </script> <script id= fragment- type= x-/x-fragment > precision mediump float; void main() gl_fragcolor = vec4( 1.0, 0.0, 0.0, 1.0 ); </script> <script type= text/javascript src=../common/webgl-utils.js > </script> <script type= text/javascript src=../common/initshaders.js > </script> <script type= text/javascript src= triangle.js > </script> <canvas id= gl-canvas width= 512 height= 512 > </canvas> </html> (a) var gl; var points = new Float32Array([-1, -1,0, 1,1, -1 ]); window.onload = function init() var canvas = document.getelementbyid( gl-canvas ); ; gl = WebGLUtils.setupWebGL( canvas ); gl.viewport( 0, 0, canvas.width, canvas.height ); gl.clearcolor( 1.0, 1.0, 1.0, 1.0 ); var program = initshaders( gl, vertex-, fragment- ); gl.useprogram( program ); var bufferid = gl.createbuffer(); gl.bindbuffer( gl.array_buffer, bufferid ); gl.bufferdata( gl.array_buffer, points, gl.static_draw); var vposition = gl.getattriblocation( program, vposition ); gl.vertexattribpointer( vposition, 2, gl.float, false, 0, 0 ); gl.enableattribarray( vposition ); render(); function render() gl.clear( gl.color_buffer_bit ); gl.drawarrays( gl.triangles, 0, 3 ); (b) Figure 4. WebGL 1.0 version of the example in Figure 3: (a) an HTML file that includes the s and (b) a JavaScript file. IEEE Computer Graphics and Applications 109

The notion of a WebGL context can be confusing to students at first, but ultimately it performs similar functionality for WebGL applications, as do the various libraries and include files that are necessary with OpenGL. The file initshaders.js creates a program object containing the s and necessary information to link all the code together. It uses a number of WebGL functions to read, compile, and link the s with the application. These functions have little to do with the graphics, so they can be discussed later in the class. 5 We can now turn to the JavaScript file (see Figure 4b). Although it is possible to put everything (the s, the WebGL application code, and setup information) into a single HTML file, we have found it clearer to use the HTML file for setup and the JS file to generate the graphics. Because scope and inheritance are different in JavaScript than in languages such C++ and Python, a few lines of the code will appear odd at first. In particular, the objects document and canvas are defined outside the application. The discussion of such differences can be covered in one or two lectures. Working with JavaScript and WebGL Like HTML5, JavaScript is a Web language that is supported in all recent browsers. Because WebGL uses the HTML5 Canvas element to provide a drawing surface, WebGL can be integrated with other web resources. For example, we can use Cascading Style Sheets (CSS) and jquery to design the page and the interactive elements (buttons.menus, sliders). However, even without other libraries, it is simple to add basic interactive elements to an application. For example, we can add a button with the label mybutton to the page with the single line of code in the HTML file: <button id= button1 >mybutton</button> Then we can have its actions specified in the JS file: document.getelementbyid( button1 ).onclick = function () // put action code here ; Other standard interactive elements can be added just as easily. Not only is the code simple, it runs everywhere and avoids the complications we often encountered with OpenGL libraries, such as GLUT, freeglut, and GLFW. 5 Because WebGL runs in a browser environment, resources including the application can be accessed from remote sites by their URLs but run locally using the local hardware. Students love the ability to show their work on any available device, including their cell phones. From the instructor s perspective, grading becomes much easier no more trying to recompile OpenGL code and get it running on the instructor s system. All students have to do is put their assignments on a website. Recently, I taught a massive open online course (MOOC) with Coursera using WebGL. In that course, I was able to give assignments that required participants to write complete WebGL applications and use Coursera s peer-grading methodology. With thousands of participants in the course, there was no way I could have done this with OpenGL. Potential Issues with JavaScript Many instructors are leery about using WebGL because JavaScript has not yet been accepted as a standard programming language by many in the CS community. Concerns range from differences between JavaScript and languages such as Java and Python on scoping and object orientation to its reputation as a hacker s language. As valid as these concerns may be and I contend they are less of an issue as JavaScript evolves JavaScript is the language of the Web and being able to develop 3D applications that run over the web overrides these concerns. There are three key points about JavaScript that we need to keep in mind. First, it is supported by all browsers and is the language of WebGL. Trying to get around this fact by using transpilers that convert code in languages such as Java and Python to JavaScript can bring on a new set of problems. Better to use the language. Second, JavaScript is a large dynamic language undergoing constant development. There were many questionable aspects of the early versions of the language that have evoked much criticism from the programming community. However, there are multiple ways of going about tasks in JavaScript, and there is almost always a good way. Recent JavaScript versions are far more stable and have incorporated many of the best features of other languages. Lastly, many instructors, myself included, found it took time at first to adjust to JavaScript and to exploit its features rather than simply translating C or C++ code to JavaScript. Students have no problem with this, however. To them, JavaScript at worst is just another language, and they learn it quickly. JavaScript differs from the standard languages (Java, Python, C, and C++) that our students learn first and with which they are most adept. Basic concepts such as its object orientation, dynamic typing, and scoping are all different. 110 March/April 2017

JavaScript has only three atomic types: numbers, strings, and Booleans. Numbers are all 64- bit floats. In general, students are unlikely to experience problems with the lack of a separate integer type. Everything else is an object. Objects use prototype inheritance so that constructs that appear similar to those in other languages are in fact objects that inherit methods and attributes from their prototypes. This difference is key in WebGL for arrays. JS arrays are not the simple C-like arrays that we transfer to the GPU in OpenGL. JavaScript has typed arrays that are more familiar and efficient. In our simple example, we use one to hold the three vertices: var points = new Float32Array([-1, -1,0, 1,1, -1]); We later send them to the GPU from the vertex buffer object: gl.bufferdata( gl.array_buffer, points, gl. STATIC_DRAW ); This is simple, but it doesn t use the expressive power of JavaScript. Consider the following option that uses JS arrays. We start with an empty array: var points = [];, which is an object with both attributes and methods. In particular, we can use the push() method to add data to the array and the length attribute to keep track of its size. Thus, code like points.push([-1, 1]); is a much clearer way to add a vertex. Even better is when we define geometric objects that match the types in GLSL. The code then looks more like this: var points = [] points.push(vec2(-1, 1)); It differs in some subtle ways. For example, when we add the other two vertices, points.length is now three instead of six because the points array contains three vertices, each of which is an object. The more serious problem is that because a JS array is an object it cannot be sent to the GPU directly with gl.bufferdata. We have to extract the data from the array that we want to send to the GPU and put these values into a typed array. We give the students a function flatten() that does this, and we send the data to the GPU with code such as this: gl.bufferdata( gl.array_buffer, flatten(points), gl.static_draw ); var at = vec3(0.0, 0.0, 0.0); var up = vec3(0.0, 1.0, 0.0); var eye = vec3(1.0, 1.0, 1.0); var mvmatrix = lookat(eye, at, up); var pmatrix = ortho(left, right, bottom, top, near, far); gl.uniformmatrix4fv( modelview, false, flatten(mvmatrix) ); gl.uniformmatrix4fv( projection, false, flatten(pmatrix) ); Figure 5. Example MV.js functions. This code sets up a simple modelview and projection matrices and sends them to a. The constructors for vec3 and the functions lookat and ortho are in MV.js. Working with Matrices and Vectors Instructors using WebGL (or OpenGL 3 and 4) have to contend with two other issues. First, OpenGL is not object oriented. With some minor exceptions, neither is WebGL. For the seventh edition of Interactive Computer Graphics (bit.ly/ icg7th), we created a small JS library MV.js that contains functions to create and manipulate the matrix and vector types in GLSL as JS objects. It also includes the flatten function. Second, when the fixed-function pipeline was deprecated, we lost the majority of the functions in the API, including the ones for viewing (glortho, glperspective), transformation matrices (glrotation, gltranslate, glscale), and many attributes (glcolor, glnormal, gltexcoord). As specified in the original OpenGL standard, these functions are incompatible with -based graphics and thus WebGL. Rather than having students create their own functions that provide similar functionality, we added these functions to MV.js. For example, the code in Figure 5 sets up a simple model-view and projection matrices and sends them to a. Within MV.js, the types can be implemented with either JS arrays or typed arrays. For example, with JS arrays, a mat4 is implemented as a four-element JS array of numbers. With typed arrays, a mat4 is implemented as a 16-element array. The array used does not affect the MV.js API. MV.js and many code examples are available online at bit.ly/icg7th. Some of the examples use MV.js to build geometric objects such as cubes, quadrics, and meshes that possess their own methods for transformation and display. If such an object orientation is important to a course, then three.js is an important scene graph API that is usually implemented with WebGL. Scene Graphs and three.js Many courses in computer graphics focus on geometric modeling. Such courses are not concerned with low-level code such as is needed to set up buffers and s in OpenGL. Generally, we can look at the API needed for such a course as IEEE Computer Graphics and Applications 111

three.js app WebGL app three.js JS file WebGL Browser HTML file Figure 6. Flow of three.js and WebGL applications. Although the WebGL application requires four distinct software entities that require an understanding of the underlying architecture, the three.js application only communicates with the three.js API. being at a higher level than OpenGL. The goal of an application is to produce a scene. The scene elements include not only geometric objects but also the camera, materials, colors, and lights. Once all the scene s parts are specified in the application, it is rendered. Three.js is a 3D JS API that almost always uses WebGL for rendering. In three.js, a scene is initiated by var scene = new THREE.Scene();. A perspective camera is specified by var camera = new THREE.PerspectiveCamera(...);, with the usual perspective function parameters. A unit cube with green faces can be added to the empty scene: var geometry = new THREE.CubeGeometry(1,1,1); var material = new THREE.MeshBasicMaterial( color: 0x00ff00); var cube = new THREE.Mesh(geometry, material); scene.add(cube); Now we can render the scene with a render function that is similar to the one we used for our WebGL example. This partial example does not show the power of three.js. In particular, the three.js rendering code can make use of all the information in the entire scene to render with great efficiency using s, sorting, and frustum culling. In addition, the API is large enough to provide a range of objects, attributes, and methods to the application, all without forcing the application programmer to use any of the low-level details required in WebGL programming. Figure 6 shows the flow of a simple three. js application and a simple WebGL application. Although the WebGL application requires four distinct software entities that require an understanding of the underlying architecture, the three.js application only communicates with the three.js API. Consequently, three.js is well suited for a CAD-oriented course but is generally not the best choice for the main API in computer science and engineering introductory courses in computer graphics, which tend to emphasize the architecture and its implementation. Although many introductory computer graphics courses have yet to transition to WebGL, three.js, or some combination of the two, the arguments for making the switch are strong. In my teaching (MOOCs, Siggraph, and university) and in my interactions with instructors using my textbook, there have been uniformly positive reactions to using WebGL and three.js. The future path for graphics APIs reinforces these views. WebGL 2.0, the JS implementation of OpenGL ES 3.0, is now supported on most browsers, as is ES6, the latest version of JavaScript. Both add significant new capabilities. The successor to OpenGL, Vulcan (www.khronos.org), is proving to be popular with developers. Vulcan moves the detailed control of the GPU and CPU from the OpenGL drivers into the application program. From the perspective of an introductory course, however, Vulcan is too complex to be the API of choice. Many valuable perspectives and references can be found in print 6 and online (bit.ly/webglhelp). All the examples and libraries referred to in this article are available online at www.cs.unm.edu/~angel/ CGA. References 1. W. Newman and R. Sproull, Principles of Interactive Computer Graphics, McGraw-Hill, 1973. 2. J. Foley and A. van Dam, Fundamentals of Interactive Computer Graphics, Addison-Wesley, 1982. 3. E. Angel, Interactive Computer Graphics: A Top-Down Approach Using OpenGL, Addison-Wesley, 1997. 4. E. Angel and D. Shreiner, Interactive Computer Graphics: A Top-Down Approach with Shader-Based OpenGL, 6th ed., Addison-Wesley, 2006. 5. E. Angel and D. Shreiner, Interactive Computer Graphics: A Top-Down Approach with WebGL, 7th ed., Addison-Wesley, 2015. 6. P. Cozzi, ed., WebGL Insights, CRC Press, 2016. Ed Angel is a professor emeritus of computer science at the University of New Mexico and founding Director of the Art, Research, Technology and Science Laboratory (ARTS Lab). He was the first UNM Presidential Teaching Fellow. Contact him at angel@cs.unm.edu. Contact department editor Beatriz Sousa Santos at bss@ua.pt and department editor Ginger Alford at ginger.siggraph @gmail.com. 112 March/April 2017