Comp4422. Computer Graphics. Lab 02: WebGL API Prof. George Baciu. PQ838 x7272.

Similar documents
Introduction to OpenGL/GLSL and WebGL GLSL

Programming with OpenGL Complete Programs Objectives Build a complete first program

BCA611 Video Oyunları için 3B Grafik

WebGL: Hands On. DevCon5 NYC Kenneth Russell Software Engineer, Google, Inc. Chair, WebGL Working Group

Rasterization-based pipeline

Web-Based Visualization

CS452/552; EE465/505. Image Formation

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

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

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

Mining the Rendering Power in Web Browsers. Jianxia Xue Jan. 28, 2014

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

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

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

I Can t Believe It s Not

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

OPENGL RENDERING PIPELINE

OpenGL shaders and programming models that provide object persistence

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

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

WebGL Game Development

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

2D graphics with WebGL

CS 432 Interactive Computer Graphics

Lecture 11 Shaders and WebGL. October 8, 2015

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

COMP371 COMPUTER GRAPHICS

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

CS452/552; EE465/505. Review & Examples

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

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

CPSC 436D Video Game Programming

WebGL Seminar: O3D. Alexander Lokhman Tampere University of Technology

Introduction to Computer Graphics with WebGL

Introduction to Computer Graphics with WebGL

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

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

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

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

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

Visual HTML5. Human Information Interaction for Knowledge Extraction, Interaction, Utilization, Decision making HI-I-KEIUD

CS452/552; EE465/505. Shadow Mapping in WebGL

Game Graphics & Real-time Rendering

COPYRIGHTED MATERIAL. 1Introducing WebGL THE BASICS OF WEBGL

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

CS475/CS675 - Computer Graphics. OpenGL Drawing

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

Ciril Bohak. - INTRODUCTION TO WEBGL

Water Simulation on WebGL and Three.js

Over the past 15 years, OpenGL has become

Get your port on! porting to Native Client as of Pepper 18. Colt "MainRoach" McAnlis

INTRODUCTION TO OPENGL PIPELINE

Converts geometric primitives into images Is split into several independent stages Those are usually executed concurrently

Qiufeng Zhu Advanced User Interface Spring 2017

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

Introductory Seminar

CS 432 Interactive Computer Graphics

DECLARATIVE AR IN THE WEB WITH XML3D & XFLOW. By Felix Klein

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

Lewis Weaver. Nell Waliczek. Software Engineering Lead. Program github.

Content. Building Geometry Appearance Lights Model Loaders

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

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

CMSC427 OpenGL and JOGL

We assume that you are familiar with the following:

Rendering Objects. Need to transform all geometry then

Introduction to Computer Graphics. April 6, 2017 Kenshi Takayama

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

Web Programming 1 Packet #5: Canvas and JavaScript

The Graphics Pipeline

Introduction to Computer Graphics with WebGL

JavaScript. Interpolated points

Practical Texturing (WebGL) CS559 Fall 2016 Lecture 20 November 7th 2016

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

Picking (In WebGL) Picking idea

The course also includes an overview of some of the most popular frameworks that you will most likely encounter in your real work environments.

Computação Gráfica. Computer Graphics Engenharia Informática (11569) 3º ano, 2º semestre. Chap. 4 Windows and Viewports

The Graphics Pipeline

Mobile Application Programing: Android. OpenGL Operation

COURSE OUTLINE MOC 20480: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

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

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

CS452/552; EE465/505. Models & Viewing

-1, -1, 0, 1, 1, //first corner: -> bottom left of the viewport -1, 1, 1, 1, 1, //top left of the viewport 1, 1, 0, 0, 0 //top right of the viewport

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

OPENGL AND GLSL. Computer Graphics

Web Site Development with HTML/JavaScrip

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

Web API for Vehicle Data RI

Fundamentals of Website Development

Computer Graphics Lecture 2

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

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

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

CS452/552; EE465/505. Transformations

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Introduction to Computer Graphics with WebGL

Understanding M3G 2.0 and its Effect on Producing Exceptional 3D Java-Based Graphics. Sean Ellis Consultant Graphics Engineer ARM, Maidenhead

CISC 3620 Lecture 7 Lighting and shading. Topics: Exam results Buffers Texture mapping intro Texture mapping basics WebGL texture mapping

Transcription:

Comp4422 Computer Graphics Lab 02: WebGL API www.comp.polyu.edu.hk/~csgeorge/comp4422 Prof. George Baciu csgeorge@comp.polyu.edu.hk PQ838 x7272 9/6/2018 COMP4422 Lab 02 George Baciu 2018 1

WebGL Prerequisites HTML DOM (Document Object Management) JavaScript programming JavaScript-WebGL bindings: Three.js glmatrix.js webgl-utils.js CSS: Cascading Style Sheets 9/6/2018 COMP4422 Lab 02 George Baciu 2018 2

Use Google Chrome 9/6/2018 COMP4422 Lab 02 George Baciu 2018 3

Setting up the Scene Obtain WebGL context Create primitive types Create buffer objects (VBOs) Create attributes Two-dimensional static rendering Create a program and shaders Set up the view matrices Add animation and movement 9/6/2018 COMP4422 Lab 02 George Baciu 2018 4

Html Canvas HTML5 <!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> 9/6/2018 COMP4422 Lab 02 George Baciu 2018 5

If HTML5 is NOT supported Your browser does not support the HTML5 canvas element. 9/6/2018 COMP4422 Lab 02 George Baciu 2018 6

If HTML5 is NOT supported Then, you get: Your browser does not support the HTML5 canvas element. 9/6/2018 COMP4422 Lab 02 George Baciu 2018 7

Otherwise, you get 9/6/2018 COMP4422 Lab 02 George Baciu 2018 8

Exercise Change the canvas size to 1024 x 768 1600 x 1200 9/6/2018 COMP4422 Lab 02 George Baciu 2018 9

Setting up WebGL Define a JavaScript setup function that is called once when the window DOM is loaded. 9/6/2018 COMP4422 Lab 02 George Baciu 2018 10

<!doctype html> <html> <head> </html> </head> <body> </body> <title>a blank canvas</title> <style> body canvas </style> <script> { background-color: grey; } { background-color: white; } window.onload = setupwebgl; var gl = null; function setupwebgl() { var canvas = document.getelementbyid( my-canvas ); try { gl = canvas.getcontext( experimental-webgl ); } } </script> <canvas id="my-canvas" width="400" height="300"> Your browser does not support the HTML5 canvas element. </canvas> 9/6/2018 COMP4422 Lab 02 George Baciu 2018 14

WebGL Components Drawing buffers Primitive types 9/6/2018 COMP4422 Lab 02 George Baciu 2018 12

Drawing Buffers A color buffer A depth buffer A stencil buffer 9/6/2018 COMP4422 Lab 02 George Baciu 2018 13

What is a buffer? A block of memory for temporarily storing data 9/6/2018 COMP4422 Lab 02 George Baciu 2018 14

The Color Buffer The color buffer stores values (usually 8 bits) for each of the three color components: Red Green Blue 9/6/2018 COMP4422 Lab 02 George Baciu 2018 15

Primitive Types Points Lines Triangles 9/6/2018 COMP4422 Lab 02 George Baciu 2018 16

Seven ways to render POINTS LINES LINE_STRIP LINE_LOOP 9/6/2018 COMP4422 Lab 02 George Baciu 2018 17

Seven ways to render TRIANGLES TRIANGLE_STRIP TRIANGLE_FAN 9/6/2018 COMP4422 Lab 02 George Baciu 2018 18

Obtain Context For setting background color For drawing points/triangles... 9/6/2018 COMP4422 Lab 02 George Baciu 2018 19

Obtain Context <!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> JavaScript Code <script> // Getting Context </script> </html> 9/6/2018 COMP4422 Lab 02 George Baciu 2018 20

Obtain Context <script> window.onload = setupwebgl; var gl = null; function setupwebgl() { var canvas = document.getelementbyid("my-canvas"); gl = canvas.getcontext("experimental-webgl"); if(gl) { //set the clear color to red gl.clearcolor(1.0, 0.0, 0.0, 1.0); gl.clear(gl.color_buffer_bit); }else{ alert( "Error: Your browser does not appear to support WebGL."); } } </script> Enable Color Writing 9/6/2018 COMP4422 Lab 02 George Baciu 2018 21

You get 9/6/2018 COMP4422 Lab 02 George Baciu 2018 22

Exercise Change the canvas background color Black color Green color Blue color Alpha value 9/6/2018 COMP4422 Lab 02 George Baciu 2018 23

Drawing Triangle Vertex Buffer Shader Primitive Type 9/6/2018 COMP4422 Lab 02 George Baciu 2018 24

Drawing Triangle Vertex Buffer var trianglevertices = [ //top triangle -0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, //bottom triangle -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 0.5, -0.5, 0.0 ]; 9/6/2018 COMP4422 Lab 02 George Baciu 2018 25

Drawing Triangle Shader Vertex Shader Fragment Shader 9/6/2018 COMP4422 Lab 02 George Baciu 2018 26

Drawing Triangle Shader Script (Vertex) <script id="shader-vs" type="x-shader/x-vertex"> attribute vec3 avertexposition; void main(void) { gl_position = vec4(avertexposition, 1.0); } </script> 9/6/2018 COMP4422 Lab 02 George Baciu 2018 27

Drawing Triangle Shader Script (Fragment) <script id="shader-fs" type="x-shader/x-fragment"> void main(void) { gl_fragcolor = vec4(1.0, 1.0, 1.0, 1.0); } </script> 9/6/2018 COMP4422 Lab 02 George Baciu 2018 28

Drawing Triangle Primitive Type 9/6/2018 COMP4422 Lab 02 George Baciu 2018 29

Drawing Triangle Primitive Type gl.drawarrays(gl.triangles, 0, 6); 9/6/2018 COMP4422 Lab 02 George Baciu 2018 30

Drawing Triangle Code Structure initwebgl setupwebgl initbuffers initshaders drawscene 9/6/2018 COMP4422 Lab 02 George Baciu 2018 31

You get 9/6/2018 COMP4422 Lab 02 George Baciu 2018 32

Exercise Draw four triangles Change the triangle color 9/6/2018 COMP4422 Lab 02 George Baciu 2018 33

Adding Color Setup ColorBuffer var triangleverticecolors = [ //top left triangle 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, //bottom right triangle 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0 ]; 9/6/2018 COMP4422 Lab 02 George Baciu 2018 34

Adding Color Change Shader Script (Vertex) <script id="shader-vs" type="x-shader/x-vertex"> attribute vec3 avertexposition; attribute vec3 avertexcolor; varying highp vec4 vcolor; void main(void) { gl_position = vec4(avertexposition, 1.0); vcolor = vec4(avertexcolor, 1.0); } </script> 9/6/2018 COMP4422 Lab 02 George Baciu 2018 35

Adding Color Change Shader Script (Fragment) <script id="shader-fs" type="x-shader/x-fragment"> varying highp vec4 vcolor; void main(void) { gl_fragcolor = vcolor; } </script> 9/6/2018 COMP4422 Lab 02 George Baciu 2018 36

Adding Color drawscene vertexcolorattribute = gl.getattriblocation(glprogram, "avertexcolor"); gl.enablevertexattribarray(vertexcolorattribute); gl.bindbuffer(gl.array_buffer, trianglescolorbuffer); gl.vertexattribpointer(vertexcolorattribute, 3, gl.float, false, 0, 0); numberofcomponents type normalizeflag stride offset 9/6/2018 COMP4422 Lab 02 George Baciu 2018 37

You get 9/6/2018 COMP4422 Lab 02 George Baciu 2018 38

Exercise Change the triangle color Set constant color in Vertex Shader Script 9/6/2018 COMP4422 Lab 02 George Baciu 2018 39

Animation updatebuffers var x_translation = Math.sin(angle)/2.0; var trianglevertices = [ //left triangle -0.5 + x_translation, 0.5, 0.0, 0.0 + x_translation, 0.0, 0.0, -0.5 + x_translation, -0.5, 0.0, //right triangle 0.5 + x_translation, 0.5, 0.0, 0.0 + x_translation, 0.0, 0.0, 0.5 + x_translation, -0.5, 0.0 ]; 9/6/2018 COMP4422 Lab 02 George Baciu 2018 40

Animation requestanimationframe window.requestanimframe = (function(){ return window.requestanimationframe window.webkitrequestanimationframe window.mozrequestanimationframe window.orequestanimationframe window.msrequestanimationframe function( callback ){ //define the interval time window.settimeout(callback, 1000 / 60); }; })(); 9/6/2018 COMP4422 Lab 02 George Baciu 2018 41

Animation animloop setupwebgl(); updatebuffers(); drawscene(); requestanimframe(animloop, canvas); 9/6/2018 COMP4422 Lab 02 George Baciu 2018 42

You get 9/6/2018 COMP4422 Lab 02 George Baciu 2018 43

Exercise Create color animation 9/6/2018 COMP4422 Lab 02 George Baciu 2018 44

Download: http://www.comp.polyu.edu.hk/~csgeorge/comp4422/lab/ 9/6/2018 COMP4422 Lab 02 George Baciu 2018 45

Try things out 9/6/2018 COMP4422 Lab 02 George Baciu 2018 46