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

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

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

Programming with OpenGL Complete Programs Objectives Build a complete first program

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

CS452/552; EE465/505. Image Formation

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

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

Introduction to Computer Graphics with WebGL

Introduction to Computer Graphics with WebGL

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

Programming with OpenGL Part 1: Background

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

LECTURE 02 OPENGL API

OPEN GL BACKGROUND. Objectives. Early History of APIs. SGI and GL. PHIGS and X. Modified from Angel 6e book slides

CS452/552; EE465/505. Review & Examples

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

Introduction to OpenGL/GLSL and WebGL GLSL

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

Introduction to OpenGL Week 1

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

Over the past 15 years, OpenGL has become

Introduction to Computer Graphics with WebGL

CS 432 Interactive Computer Graphics

Graphics Programming

Programming with OpenGL Part 1: Background

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

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

Shaders. Slide credit to Prof. Zwicker

We assume that you are familiar with the following:

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

CS452/552; EE465/505. Models & Viewing

Rendering Objects. Need to transform all geometry then

2D graphics with WebGL

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

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

Computer Graphics. Bing-Yu Chen National Taiwan University

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

Rasterization-based pipeline

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

COMP371 COMPUTER GRAPHICS

CS 450: COMPUTER GRAPHICS REVIEW: STATE, ATTRIBUTES, AND OBJECTS SPRING 2015 DR. MICHAEL J. REALE

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

CS 432 Interactive Computer Graphics

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

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

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

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

An Overview GLUT GLSL GLEW

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

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

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

Introduce the basic input devices Input, Interaction and Animation

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

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

GLSL Overview: Creating a Program

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

Introduction to Shaders.

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

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

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

Introduction to Computer Graphics with WebGL

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

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

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

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

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

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

WebGL. Mike Bailey. Oregon State University. What is WebGL?

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

Graphics Pipeline & APIs

Basic Graphics Programming

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

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

Ciril Bohak. - INTRODUCTION TO WEBGL

Lecture 3. Programming with OpenGL GLUT + GLEW. CS 354 Computer Graphics Sunday, January 20, 13

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

BCA611 Video Oyunları için 3B Grafik

Lecture 13: OpenGL Shading Language (GLSL)

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

Programming using OpenGL: A first Introduction

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

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

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

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

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

Graphics Pipeline & APIs

OPENGL RENDERING PIPELINE

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

CS452/552; EE465/505. Transformations

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

CS 548: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Lectures OpenGL Introduction

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

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

Basic Graphics Programming

Intro to OpenGL III. Don Fussell Computer Science Department The University of Texas at Austin

Transcription:

Objectives Programming with WebGL Part 1: Background CS 432 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science Development of the OpenGL API OpenGL Architecture - OpenGL as a state machine - WebGL as a data flow machine Functions - Types - Formats Simple program 1 2 Retained vs. Immediate Mode Graphics Immediate - Geometry is drawn when CPU sends it to GPU - All data needs to be resent even if little changes - Once drawn, geometry on GPU is discarded - Requires major bandwidth between CPU and GPU - Minimizes memory requirements on GPU Retained - Geometry is sent to GPU and stored - It is displayed when directed by CPU - CPU may send transformations to move geometry - Minimizes data transfers, but GPU now needs enough memory to store geometry 3 Early History of APIs IFIPS (1973) formed two committees to come up with a standard graphics API - Graphical Kernel System (GKS) 2D but contained good workstation model - Core Both 2D and 3D - GKS adopted as IS0 and later ANSI standard (1980s) GKS not easily extended to 3D (GKS-3D) - Far behind hardware development 4 PHIGS and X Programmers Hierarchical Graphics System (PHIGS) - Arose from CAD community - Database model with retained graphics (structures) X Window System - DEC/MIT effort - Client-server architecture with graphics PEX combined the two - Not easy to use (all the defects of each) SGI and GL Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the graphics pipeline in hardware (1982) To access the system, application programmers used a library called GL With GL, it was relatively simple to program three dimensional interactive applications 5 6 1

OpenGL The success of GL lead to OpenGL (1992), a platform-independent API that was - Easy to use - Close enough to the hardware to get excellent performance - Focused on rendering - Omitted windowing and input to avoid window system dependencies - An immediate mode system, that later added retained mode functionality OpenGL Evolution Originally controlled by an Architectural Review Board (ARB) - Members included SGI, Microsoft, Nvidia, HP, 3DLabs, IBM,. - Now Kronos Group - Was relatively stable (through version 2.5) Backward compatible Evolution reflected new hardware capabilities 3D texture mapping and texture objects Vertex and fragment programs - Allows platform specific features through extensions 7 8 Modern OpenGL OpenGL 3.1 (2009) Performance is achieved by using GPU rather than CPU Control GPU through programs called shaders Application s job is to send data to GPU GPU does all rendering Totally shader-based - No default shaders - Each application must provide both a vertex and a fragment shader No immediate mode Few state variables Most 2.5 functions deprecated - deprecate in CS - To mark (a component of a software standard) as obsolete to warn agains t its us e in the future, s o that it may be phas ed out. Backward compatibility not required 9 10 Other Versions OpenGL ES - Embedded systems - Version 1.0 simplified OpenGL 2.1 - Version 2.0 simplified OpenGL 3.1 Shader based - Version 3.0 simplified OpenGL 4.3 WebGL - Javascript implementation of ES 2.0 - Supported on newer browsers OpenGL 4.1 è 4.5 - Added geometry & compute shaders and tessellator What About Other Low-Level Graphics Libraries? Direct3D - Part of DirectX, Windows-only Mantle - Developed by AMD Metal - Developed by Apple Vulkan - next-gen OpenGL - Derived from Mantle 11 12 2

OpenGL Architecture A Simple Program (?) Generate a square on a solid background 13 15 It used to be easy What happened #include <GL/glut.h> void mydisplay(){ glclear(gl_color_buffer_bit); glbegin(gl_quad; glvertex2f(-0.5, -0.5); glvertex2f(-0,5, 0,5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); glend() int main(int argc, char** argv){ glutcreatewindow("simple"); glutdisplayfunc(mydisplay); glutmainloop(); Most OpenGL functions deprecated Makes heavy use of state variable default values that no longer exist - Viewing - Colors - Window parameters Next version will make the defaults more explicit However, processing loop is the same 16 17 Execution in Browser Event Loop HTML JS files Browser JS Engine Application CPU/GPU program Framebuffer URL Web Page Canvas Web Server Remember that the sample program specifies a render function which is a event listener orcallback function - Every program should have a render callback - For a static application we need only execute the render function once - In a dynamic application, the render function can call itself recursively but each redrawing of the display must be triggered by an event 18 19 3

Lack of Object Orientation All versions of OpenGL are not object oriented so that there are multiple functions for a given logical function Example: sending values to shaders -gluniform3f -gluniform2i -gluniform3dv Underlying storage mode is the same WebGL function format function name dimension gl.uniform3f(x,y,z) belongs to WebGL canvas x,y,z are variables gl.uniform3fv(p) p is an array 21 22 WebGL constants Most constants are defined in the canvas object - In desktop OpenGL, they were in #include files such as gl.h Examples -desktop OpenGL glenable(gl_depth_test); -WebGL gl.enable(gl.depth_test) -gl.clear(gl.color_buffer_bit) WebGL and GLSL WebGL requires shaders and is based less on a state machine model than a data flow model Most state variables, attributes and related pre-3.1 OpenGL functions have been deprecated Action happens in shaders Job of application is to get data to GPU 23 24 GLSL OpenGL Shading Language C-like with - Matrix and vector types (2, 3, 4 dimensional) - Overloaded operators - C++ like constructors Similar to Nvidia s Cg and Microsoft HLSL Code sent to shaders as source code WebGL functions compile, link and get information to shaders Programming with OpenGL Part 2: Complete Programs 25 26 4

Objectives Build a complete first program - Introduce shaders - Introduce a standard program structure Simple viewing - Two-dimensional viewing as a special case of three-dimensional viewing Initialization steps and program structure Coding in WebGL Example: Draw a square - Each application consists of (at least) two files - HTML file and a JavaScript file HTML - describes page - includes utilities - includes shaders JavaScript - contains the graphics 27 28 Coding in WebGL Square Program Can run WebGL on any recent browser - Chrome - Firefox - Safari - IE Code written in JavaScript JS runs within browser - Use local resources 29 WebGL square.html Five steps - Describe page (HTML file) <!DOCTYPE html> <html> <head> <script id="vertex-shader" type="x-shader/x-vertex"> request WebGL Canvas read in necessary files - Define shaders (HTML file) could be done with a separate file (browser dependent) - Compute or specify data (JS file) attribute vec4 vposition; void main() { gl_position = vposition; </script> <script id="fragment-shader" type="x-shader/x-fragment"> - Send data to GPU (JS file) precision mediump float; Application - Render data (JS file) void main() { gl_fragcolor = vec4( 1.0, 1.0, 1.0, 1.0 ); </script> 5

Shaders We assign names to the shaders that we can use in the JS file These are trivial pass-through (do nothing) shaders that which set the two required built-in variables - gl_position - gl_fragcolor Note both shaders are full programs Note vector type vec2 Must set precision in fragment shader square.html (cont) <script type="text/javascript" src="../common/webgl-utils.js"></script> <script type="text/javascript" src="../common/initshaders.js"></script> <script type="text/javascript" src="../common/mv.js"></script> <script type="text/javascript" src="square.js"></script> </head> <body> <canvas id="gl-canvas" width="512" height="512"> Oops... your browser doesn't support the HTML5 canvas element </canvas> </body> </html> Files../Common/webgl-utils.js: Standard utilities for setting up WebGL context in Common directory on website../common/initshaders.js: contains JS and WebGL code for reading, compiling and linking the shaders../common/mv.js: our matrix-vector package square.js: the application file var gl; var points; square.js window.onload = function init(){ var canvas = document.getelementbyid( "gl-canvas" ); gl = WebGLUtils.setupWebGL( canvas ); if (!gl ) { alert( "WebGL isn't available" ); // Four Vertices var vertices = [ vec2( -0.5, -0.5 ), vec2( -0.5, 0.5 ), vec2( 0.5, 0.5 ), vec2( 0.5, -0.5) ]; Notes onload: determines where to start execution when all code is loaded canvas gets WebGL context from HTML file vertices use vec2 type in MV.js JS array is not the same as a C or Java array - object with methods - vertices.length // 4 Values in clip coordinates // Configure WebGL square.js (cont) gl.viewport( 0, 0, canvas.width, canvas.height ); gl.clearcolor( 0.0, 0.0, 0.0, 1.0 ); // Load shaders and initialize attribute buffers var program = initshaders( gl, "vertex-shader", "fragment-shader" ); gl.useprogram( program ); // Load the data into the GPU var bufferid = gl.createbuffer(); gl.bindbuffer( gl.array_buffer, bufferid ); gl.bufferdata( gl.array_buffer, flatten(vertices), gl.static_draw ); // Associate out shader variables with our data buffer var vposition = gl.getattriblocation( program, "vposition" ); gl.vertexattribpointer( vposition, 2, gl.float, false, 0, 0 ); gl.enablevertexattribarray( vposition ); 6

Notes initshaders used to load, compile and link shaders to form a program object Load data onto GPU by creating a vertex buffer object on the GPU - Note use of flatten() to convert JS array to an array of float32 s Finally we must connect variable in program with variable in shader - need name, type, location in buffer ; render(); square.js (cont) function render() { gl.clear( gl.color_buffer_bit ); gl.drawarrays( gl.triangle_fan, 0, 4 ); 0 1 2 3 Triangles, Fans or Strips JavaScript Notes gl.drawarrays( gl.triangle_fan, 0, 4 ); // 0, 1, 2, 3 gl.drawarrays( gl.triangles, 0, 6 ); // 0, 1, 2, 0, 2, 3 1 2 1 2 0 3 0 3 gl.drawarrays( gl.triangle_strip, 0, 4 ); // 0, 1, 3, 2 JavaScript (JS) is the language of the Web - All browsers will execute JS code - JavaScript is an interpreted object-oriented language References - Flanagan, JavaScript: The Definitive Guide, O Reilly - Crockford, JavaScript, The Good Parts, O Reilly - Many Web tutorials 42 JS Notes Is JS slow? - JS engines in browsers are getting much faster - Not a key issues for graphics since once we get the data to the GPU it doesn t matter how we got the data there JS is a (too) big language - We don t need to use it all - Choose parts we want to use - Don t try to make your code look like C or Java Very few native types: - numbers - strings JS Notes - booleans Only one numerical type: 32 bit float - var x = 1; - var x = 1.0; // same - potential issue in loops - two operators for equality == and === 43 Dynamic typing 44 7

Scoping Different from other languages Function scope variables are hoisted within a function - can use a variable before it is declared Note functions are first class objects in JS JS arrays are objects - inherit methods - var a = [1, 2, 3]; JS Arrays is not the same as in C++ or Java - a.length // 3 - a.push(4); // length now 4 - a.pop(); // 4 - avoids use of many loops and indexing - Problem for WebGL which expects C-style arrays 45 46 Typed Arrays A Minimalist Approach JS has typed arrays that are like C arrays var a = new Float32Array(3) var b = new Uint8Array(3) Generally, we prefer to work with standard JS arrays and convert to typed arrays only when we need to send data to the GPU with the flatten function in MV.js 47 We will use only core JS and HTML - no extras or variants No additional packages - CSS - JQuery Focus on graphics - examples may lack beauty 48 Buffer Object Why use Buffer Objects? Buffers objects allow us to transfer large amounts of data to the GPU Need to create, bind (make current) and identify/specify data var buffer_id; buffer_id = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, buffer_id); gl.bufferdata(gl.array_buffer, data, gl.static_draw); Only Advantages The memory manager in the buffer object will put the data into the best memory locations based on user's hints Memory manager can optimize the buffers by balancing between 3 kinds of memory: - system, GPU and video memory Data in current buffer is sent to GPU 49 8

gl.createbuffer() gl.bindbuffer() gl.createbuffer() - creates a buffer object and returns the buffer object WebGLBuffer gl.createbuffer() Returns a WebGLBuffer for storing data such as vertices or colors. - Once the buffer object has been created, we need to bind it to a target. - Also makes the buffer current void gl.bindbuffer(glenum target, WebGLBuffer buffer) - Target can be gl.array_buffer: Any vertex attribute, such as vertex coordinates, texture coordinates, normals and color component arrays gl.element_arra Y_B UFFE R: Index array which is used for gldraw[range]elements() - Once first called, the buffer is initialized with a zerosized memory buffer and sets the initial states gl.bufferdata() Usage Flags - You can initialize and copy the data into the buffer object with gl.bufferdata(). void gl.bufferdata(glenum target, GLsizeiptr size, GLenum usage) void gl.bufferdata(glenum target, ArrayBuffer data, GLenum usage) target is either GL_ARRAY_BUFFE R or GL_ELEMENT_ARRAY_BUFFER. size is the number of bytes of data to transfer. Data is the array holding the data to be copied. "usage" flag is a performance hint to provide how the buffer object is going to be used: static, dynamic or stream, and read, copy or draw. - gl.static_draw Contents of the buffer are likely to be used often and not change often. - gl.dynamic_draw Contents of the buffer are likely to be used often and change often. - gl.stream_draw Contents of the buffer are likely to not be used often. -All contents are written to the buffer, but not read. gl.buffersubdata() gl.deletebuffer() void gl.buffersubdata(glenum target, GLintptr offset, ArrayBuffer data) - Like gl.bufferdata(), used to copy data into BO - It only replaces a range of data into the existing buffer, starting from the given offset. - The total size of the buffer must be set by gl.bufferdata() before using gl.buffersubdata(). void gl.deletebuffers(w ebglbuffer buffer) - You can delete a BO with gl.deletebuffer(), if it is no longer needed. After a buffer object is deleted, its contents will be lost. 9

Program Execution WebGL runs within the browser - complex interaction among the operating system, the window system, the browser and your code (HTML and JS) Simple model - Start with HTML file - files read in asynchronously - start with onload function event driven input Coordinate Systems The units in points are determined by the application and are called object, world, model or problem coordinates Viewing specifications usually are also in object coordinates Eventually pixels will be produced in window coordinates WebGL also uses some internal representations that usually are not visible to the application but are important in the shaders Most important is clip coordinates Coordinate Systems and Shaders Vertex shader must output in clip coordinates Input to fragment shader from rasterizer is in window coordinates Application can provide vertex data in any coordinate system but shader must eventually produce gl_position in clip coordinates Simple example uses clip coordinates WebGL Camera WebGL places a camera at the origin in camera space pointing in the negative z direction The viewing/clipping volume is a box centered at the origin with sides of length 2 (-1,-1,-1) è (1,1,1) 63 Orthographic Viewing Viewports In the default orthographic (parallel) view, all points in the view volume are projected along the z axis onto the plane z =0. z=0 z=0 Do not have to use the entire canvas for the image: gl.viewport(x,y,w,h) Values in pixels (window coordinates) w and h should and x and y are recommended to be non-negative 64 65 10

Transformations and Viewing In WebGL, projection is carried out by a projection matrix (transformation) Transformation functions are also used for changes in coordinate systems Pre 3.0 OpenGL had a set of transformation functions which have been deprecated Three choices - Application code - GLSL functions - MV.js First Programming Assignment Get example code from HW1 web page Get test code running Make minor modifications to it Change color in square.html 66 67 Modify square.js First Programming Assignment -Draw a red pentagon, rather than a white square - Change viewport Add vertex/vertices to define another triangle Modify gl.drawarrays() 68 11