introduction to webglgraphics.ics.uci.edu/cs112/discussion_week1.pdf · popular ones: three.js,...

25
Introduction to WebGL Jia Chen Jan 12, 2018

Upload: others

Post on 23-Mar-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Introduction to WebGLJia Chen

Jan 12, 2018

Nitin Agarwal
Page 2: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

What is WebGL?WebGL is a JavaScript API that allows us to implement interactive 3D graphics, straight in the browser.

WebGL runs as a specific context for the HTML <canvas> element, which gives you access to hardware-accelerated (GPU) 3D rendering in JavaScript.

Run on many different devices, such as desktop computers, mobile phones and TVs.

Page 3: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

What can WebGL do?Real-time interactive 3D graphics in browser.

Interactive music videos, games, data visualization, art, 3D design environments, 3D modeling of space, plotting mathematical functions, physical simulations...

Demos:

http://helloracer.com/webgl/

http://arodic.github.io/p/jellyfish/

Page 4: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Rendering pipeline

Page 5: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Libraries?Provide common functions, ready-made models, shaders

Faster development

Robust

Popular ones: Three.js, PhiloGL, GLGE, J3D

In our class, we will directly work on WebGL for learning purpose.

Page 6: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

You need a text editorBrackets, Sublime Text….

Page 7: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Time to write some HTMLWe will keep everything in a single HTML file for example HelloTriangle

For larger programs, we will separate the HTML and JavaScript, like we are doing in our programming assignment

Using WebGL entails writing a bunch of startup code

Complexity comes from the flexibility of the API

will enable you to do really sophisticated stuff later on

Eventually we will use a helper library for the startup code

Example code: graphics.ics.uci.edu/CS112/discussion_week1_examples.zip

Page 8: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

The HTML

Page 9: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Adding JavaScript

Page 10: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Getting a WebGL Context

Page 11: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Creating Vertex Shader

Page 12: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Creating Fragment Shader

Page 13: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Compiling the Shaders

Page 14: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Creating Program Object and Linking Shaders

Page 15: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Setting up the Buffers

Page 16: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Drawing the Scene

Page 17: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Result

Page 18: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Can you?

Page 19: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Programming Assignment 0

Page 20: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

Geometric Primitives in WebGL

Page 21: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

gl.TRIANGLES

Page 22: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

gl.TRIANGLE_STRIP

https://en.wikipedia.org/wiki/Triangle_strip

Page 23: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

gl.TRIANGLE_FAN

Page 24: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

gl.LINES

Page 25: Introduction to WebGLgraphics.ics.uci.edu/CS112/discussion_week1.pdf · Popular ones: Three.js, PhiloGL, GLGE, J3D In our class, we will directly work on WebGL for learning purpose

gl.POINTS