Introduction to Computer Graphics with WebGL

Similar documents
Introduction to Computer Graphics with WebGL

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

CS452/552; EE465/505. Image Formation

Lecture 1. Computer Graphics and Systems. Tuesday, January 15, 13

National Chiao Tung Univ, Taiwan By: I-Chen Lin, Assistant Professor

Programming with OpenGL Complete Programs Objectives Build a complete first program

Computer Graphics. Bing-Yu Chen National Taiwan University

Image Formation. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Introduction to Computer Graphics with WebGL

INTRODUCTION. Slides modified from Angel book 6e

Introduction to Computer Graphics. Overview. What is Computer Graphics?

MMGD0206 Computer Graphics. Chapter 1 Development of Computer Graphics : History

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

Overview CS Plans for this semester. References. CS 4600 Fall Prerequisites

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.

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

Image Formation. Introduction to Computer Graphics. Machiraju/Zhang/Möller/Klaffenböck

Image Formation. Introduction to Computer Graphics Torsten Möller. Machiraju/Zhang/Möller

3D graphics, raster and colors CS312 Fall 2010

Survey in Computer Graphics Computer Graphics and Visualization

Game Programming. Bing-Yu Chen National Taiwan University

Computer Graphics Lecture 2

Computer Graphics. Bing-Yu Chen National Taiwan University The University of Tokyo

Graphics Systems and Models

CS452/552; EE465/505. Review & Examples

Over the past 15 years, OpenGL has become

CSE4030 Introduction to Computer Graphics

Lecturer Athanasios Nikolaidis

Computer Graphics and Visualization. Graphics Systems and Models

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

Introduction to Computer Graphics with WebGL

CS452/552; EE465/505. Models & Viewing

Introduction to OpenGL/GLSL and WebGL GLSL

Introduction to Computer Graphics with WebGL

We assume that you are familiar with the following:

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

Shaders. Slide credit to Prof. Zwicker

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 - Chapter 1 Graphics Systems and Models

Models and Architectures

VTU QUESTION PAPER SOLUTION UNIT -1 INTRODUCTION

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

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

Rasterization-based pipeline

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

CHAPTER 1 Graphics Systems and Models 3

CS130 : Computer Graphics Lecture 2: Graphics Pipeline. Tamar Shinar Computer Science & Engineering UC Riverside

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

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

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

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

Interactive Computer Graphics A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL

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

Models and Architectures. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

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

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

Introduce the basic input devices Input, Interaction and Animation

CS452/552; EE465/505. Transformations

CS452/552; EE465/505. Finale!

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

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

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

CS 354R: Computer Game Technology

Rendering Objects. Need to transform all geometry then

Computer Graphics Fundamentals. Jon Macey

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

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

CS452/552; EE465/505. Color Display Issues

CS451Real-time Rendering Pipeline

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

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

GLOBAL EDITION. Interactive Computer Graphics. A Top-Down Approach with WebGL SEVENTH EDITION. Edward Angel Dave Shreiner

Illumination and Shading

Basic Graphics Programming

An Introduction to Computer Graphics

OPENGL RENDERING PIPELINE

CS 432 Interactive Computer Graphics

COMP Preliminaries Jan. 6, 2015

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

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

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

CS 543: Computer Graphics. Introduction

Basic Graphics Programming

Development of Computer Graphics

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

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

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Some Resources. What won t I learn? What will I learn? Topics

BCA611 Video Oyunları için 3B Grafik

Water Simulation on WebGL and Three.js

Computer Graphics (CS 543) Lecture 1 (Part 1): Introduction to Computer Graphics

CSC 8470 Computer Graphics. What is Computer Graphics?

Computer Graphics Introduction. Taku Komura

CS354 Computer Graphics Introduction. Qixing Huang Januray 17 8h 2017

Today. Rendering pipeline. Rendering pipeline. Object vs. Image order. Rendering engine Rendering engine (jtrt) Computergrafik. Rendering pipeline

Transcription:

Introduction to Computer Graphics with WebGL Rongkai Guo Assistant Professor at Computer Game Design program Kennesaw State University 1

Overview These lectures are for a senior/graduate elective for computer science and engineering majors(and others with good programming skills) The course is based on a modern approach using programmable shaders in the new textbook: Ed Angel and Dave Shreiner, Interactive Computer Graphics, A Top-down Approach with WebGL(Seventh Edition), Addison- Wesley These lectures cover Chapters 1-7 in detail and survey Chapters 8-12 2

Lecture 1 1.1: Introduction 1.2: Detailed Outline and Examples 1.3: Example Code in JS 1.4: What is Computer Graphics? 1.5: Image Formation Reading: Chapter 1 Exercises: Run some examples on your browser 3

1.1 Course Overview Required Background References 4

Contact Information rguo@kennesaw.edu http://ksuweb.kennesaw.edu/~rguo/ http://ksuweb.kennesaw.edu/~rguo/2017_summer/cs4722-7327/ 5

Objectives Broad introduction to Computer Graphics Software Hardware Applications Top-down approach Shader-Based WebGL Integrates with HTML5 Code runs in latest browsers 6

Prerequisites Good programming skills in C (or C++) Basic Data Structures Linked lists Arrays Geometry Simple Linear Algebra 7

Requirements 3 Assigned Projects Simple Interactive 3D Term Project You pick See http://ksuweb.kennesaw.edu/~rguo/2017_summer/cs472 2-7327/ for assignments, projects and other info 8

Why is this course different? Shader-based Most computer graphics use OpenGL but still use fixedfunction pipeline does not require shaders Does not make use of the full capabilities of the graphics processing unit (GPU) Web With HTML5, WebGL runs in the latest browsers makes use of local hardware no system dependencies 9

References Interactive Computer Graphics (7 th Edition) The OpenGL Programmer s Guide (the Redbook) 8 th Edition OpenGL ES 2.0 Programming Guide WebGL Programming Guide WebGL Beginner s Guide WebGL: Up and Running JavaScript: The Definitive Guide 10

Web Resources www.opengl.org get.webgl.org www.kronos.org/webgl www.chromeexperiments.com/webgl learningwebgl.com 11

1.2 Course Outline 12

Outline: Part 1 Introduction Text: Chapter 1 Lecture 1 What is Computer Graphics? Applications Areas History Image formation Basic Architecture 13

Outline: Part 2 Basic WebGL Graphics Text: Chapter 2 Lectures 2-4 Architecture JavaScript Web execution Simple programs in two and three dimensions Basic shaders and GLSL 14

Outline: Part 3 Interaction Text: Chapter 3 Lecture 4 Client-Server Model Event-driven programs Event Listeners Menus, Buttons, Sliders Position input 15

Outline: Part 4 Three-Dimensional Graphics Text: Chapters 4-6 Lecture 5-8 Geometry Transformations Homogeneous Coordinates Viewing Lighting and Shading 16

Outline: Part 5 Discrete Methods Text: Chapter 7 Lectures 9-11 Buffers Pixel Maps Texture Mapping Compositing and Transparency Off-Screen Rendering 17

Outline: Part 6 Hierarchy and Procedural Methods Text: Chapters 9-10 Lectures 11-12 Tree Structured Models Traversal Methods Scene Graphs Particle Systems Agent Based Models 18

Outline: Part 7 Advanced Rendering Text: Chapter 8 Leture 15 19

1.3 Example: Draw a triangle 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 20

Coding in WebGL Can run WebGL on any recent browser Chrome Firefox Safari IE Code written in JavaScript JS runs within browser Use local resources 21

Example: triangle.html 22

Example Code <!DOCTYPE html> <html> <head> <script id="vertex-shader" type="x-shader/x-vertex"> attribute vec4 vposition; void main(){ gl_position = vposition; } </script> <script id="fragment-shader" type="x-shader/x-fragment"> precision mediump float; void main(){ gl_fragcolor = vec4( 1.0, 0.0, 0.0, 1.0 ); } </script> 23

HTML File (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="triangle.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> 24

JS File var gl; var points; window.onload = function init(){ var canvas = document.getelementbyid( "gl-canvas" ); gl = WebGLUtils.setupWebGL( canvas ); if (!gl ) { alert( "WebGL isn't available" ); } // Three Vertices 25 var vertices = [ vec2( -1, -1 ), vec2( 0, 1 ), vec2( 1, -1 ) ];

JS File (cont) // Configure WebGL // gl.viewport( 0, 0, canvas.width, canvas.height ); gl.clearcolor( 1.0, 1.0, 1.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 26 var bufferid = gl.createbuffer(); gl.bindbuffer( gl.array_buffer, bufferid ); gl.bufferdata( gl.array_buffer, Angel and Shreiner: Interactive flatten(vertices), Computer Graphics 7E gl.static_draw );

JS File (cont) // 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 ); render(); function render() { gl.clear( gl.color_buffer_bit ); gl.drawarrays( gl.triangles, 0, 3 ); } 27

Exercise Download the Code Examples from the course website Load the triangle.html and triangle.js to your computer and run them from there Edit the two files to change the color and display more than one triangle 28

JavaScript Notes 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 29

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 30

JS Notes Very few native types: numbers strings 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 === Dynamic typing 31

JS Arrays JS arrays are objects inherit methods var a = [1, 2, 3]; 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 33

Typed Arrays 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 34

A Minimalist Approach We will use only core JS and HTML no extras or variants No additional packages CSS JQuery Focus on graphics examples may lack beauty You are welcome to use other variants as long as I can run them from your URL 35

What is Computer Graphics? 36

Computer Graphics Computer graphics deals with all aspects of creating images with a computer Hardware Software Applications 37

Example Where did this image come from? What hardware/software did we use to produce it? 38

Preliminary Answer Application: The object is an artist s rendition of the sun for an animation to be shown in a domed environment (planetarium) Software: Maya for modeling and rendering but Maya is built on top of OpenGL Hardware: PC with graphics card for modeling and rendering 39

Basic Graphics System Input devices Output device Image formed in frame buffer 40

Computer Graphics: 1950-1960 Computer graphics goes back to the earliest days of computing Strip charts Pen plotters Simple displays using A/D converters to go from computer to calligraphic CRT Cost of refresh for CRT too high Computers slow, expensive, unreliable 41

Cathode Ray Tube (CRT) Can be used either as a line-drawing device (calligraphic) or to display contents of frame buffer (raster mode) 42

Shadow Mask CRT 43

Computer Graphics: 1960-1970 Wireframe graphics Draw only lines Sketchpad Display Processors Storage tube wireframe representation of sun object 44

Sketchpad Ivan Sutherland s PhD thesis at MIT Recognized the potential of man-machine interaction Loop Display something User moves light pen Computer generates new display Sutherland also created many of the now common algorithms for computer graphics 45

Display Processor Rather than have the host computer try to refresh display use a special purpose computer called a display processor (DPU) Graphics stored in display list (display file) on display processor Host compiles display list and sends to DPU 46

Computer Graphics: 1970-1980 Raster Graphics Beginning of graphics standards IFIPS GKS: European effort Becomes ISO 2D standard Core: North American effort 3D but fails to become ISO standard Workstations and PCs 47

Raster Graphics Image produced as an array (the raster) of picture elements (pixels) in the frame buffer 48

Raster Graphics Allows us to go from lines and wire frame images to filled polygons 49

PCs and Workstations Although we no longer make the distinction between workstations and PCs, historically they evolved from different roots Early workstations characterized by Networked connection: client-server model High-level of interactivity Early PCs included frame buffer as part of user memory Easy to change contents and create images 50

Computer Graphics: 1980-1990 Realism comes to computer graphics smooth shading environment mapping bump mapping 51

Computer Graphics: 1980-1990 Special purpose hardware Silicon Graphics geometry engine VLSI implementation of graphics pipeline Industry-based standards PHIGS RenderMan Networked graphics: X Window System Human-Computer Interface (HCI) 52

Computer Graphics: 1990-2000 OpenGL API Completely computer-generated feature-length movies (Toy Story) are successful New hardware capabilities Texture mapping Blending Accumulation, stencil buffers 53

Computer Graphics: 2000-2010 Photorealism Graphics cards for PCs dominate market Nvidia, ATI Game boxes and game players determine direction of market Computer graphics routine in movie industry: Maya, Lightwave Programmable pipelines New display technologies 54

Generic Flat Panel Display 55

Computer Graphics 2011- Graphics is now ubiquitous Cell phones Embedded OpenGL ES and WebGL Alternate and Enhanced Reality 3D Movies and TV 56

Image Formation 57

Objectives Fundamental imaging notions Physical basis for image formation Light Color Perception Synthetic camera model Other models 58

Image Formation In computer graphics, we form images which are generally two dimensional using a process analogous to how images are formed by physical imaging systems Cameras Microscopes Telescopes Human visual system 59

Elements of Image Formation Objects Viewer Light source(s) Attributes that govern how light interacts with the materials in the scene Note the independence of the objects, the viewer, and the light source(s) 60

Light Light is the part of the electromagnetic spectrum that causes a reaction in our visual systems Generally these are wavelengths in the range of about 350-750 nm (nanometers) Long wavelengths appear as reds and short wavelengths as blues 61

Ray Tracing and Geometric Optics One way to form an image is to follow rays of light from a point source finding which rays enter the lens of the camera. However, each ray of light may have multiple interactions with objects before being absorbed or going to infinity. 62

Luminance and Color Images Luminance Image Monochromatic Values are gray levels Analogous to working with black and white film or television Color Image Has perceptional attributes of hue, saturation, and lightness Do we have to match every frequency in visible spectrum? No! 63

Three-Color Theory Human visual system has two types of sensors Rods: monochromatic, night vision Cones Color sensitive Three types of cones Only three values (the tristimulus values) are sent to the brain Need only match these three values Need only three primary colors 64

Shadow Mask CRT 65

Additive and Subtractive Color Additive color Form a color by adding amounts of three primaries CRTs, projection systems, positive film Primaries are Red (R), Green (G), Blue (B) Subtractive color Form a color by filtering white light with cyan (C), Magenta (M), and Yellow (Y) filters Light-material interactions Printing Negative film 66

Pinhole Camera Use trigonometry to find projection of point at (x,y,z) x p = -x/z/d y p = -y/z/d z p = d These are equations of simple perspective 67

Synthetic Camera Model projector p image plane projection of p center of projection 68

Advantages Separation of objects, viewer, light sources Two-dimensional graphics is a special case of threedimensional graphics Leads to simple software API Specify objects, lights, camera, attributes Let implementation determine image Leads to fast hardware implementation 69

Global vs Local Lighting Cannot compute color or shade of each object independently Some objects are blocked from light Light can reflect from object to object Some objects might be translucent 70

Why not ray tracing? Ray tracing seems more physically based so why don t we use it to design a graphics system? Possible and is actually simple for simple objects such as polygons and quadrics with simple point sources In principle, can produce global lighting effects such as shadows and multiple reflections but ray tracing is slow and not well-suited for interactive applications Ray tracing with GPUs is close to real time 71