Programming with OpenGL Complete Programs Objectives Build a complete first program

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

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.

CS452/552; EE465/505. Image Formation

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

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

Introduction to Computer Graphics with WebGL

Introduction to Computer Graphics with WebGL

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

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

Over the past 15 years, OpenGL has become

Introduction to Computer Graphics with WebGL

CS452/552; EE465/505. Review & Examples

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

Introduction to OpenGL/GLSL and WebGL GLSL

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

10/24/2017. WebGL. What is WebGL? What are the Important Things to Know about OpenGL-ES 2.0? What are the Important Things to Know about WebGL?

3/9/2018. WebGL. What is WebGL? What are the Important Things to Know about WebGL? What are the Important Things to Know about OpenGL-ES 2.0?

WebGL. Mike Bailey. Computer Graphics

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

CS452/552; EE465/505. Models & Viewing

BCA611 Video Oyunları için 3B Grafik

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

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

CS 432 Interactive Computer Graphics

2D graphics with WebGL

Introduction to Computer Graphics with WebGL

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

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

We assume that you are familiar with the following:

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

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

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

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 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

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

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

CS452/552; EE465/505. Transformations

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

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

Web-Based Visualization

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

Introduce the basic input devices Input, Interaction and Animation

Transformation, perspective projection, and LookAT in. Ming Ouhyoung, September 2018

CSE Real Time Rendering Week 3 & 4

6.S096 Lecture 9 Visualization

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

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

Mobile Application Programing: Android. OpenGL Operation

Rasterization-based pipeline

CS 432 Interactive Computer Graphics

Tutorial 04. Harshavardhan Kode. September 14, 2015

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

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

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

Geometry Shaders. And how to use them

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

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

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

Lecture 12: Mid-term test solution & project. CITS 3003 Graphics & Animation

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

Mobile Application Programing: Android. OpenGL Operation

OPENGL AND GLSL. Computer Graphics

Shaders. Slide credit to Prof. Zwicker

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

WebGL Game Development

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

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

Mobile Application Programming: Android. OpenGL Operation

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

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

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

Lecture 9(B): GPUs & GPGPU

Lecture 11 Shaders and WebGL. October 8, 2015

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

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

GLSL Overview: Creating a Program

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

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

WebGL Seminar: O3D. Alexander Lokhman Tampere University of Technology

Vertex Buffer Objects

Vertex Buffer Objects. Vertex Buffer Objects: The Big Idea

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

I Can t Believe It s Not

The Projection Matrix

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

Computer Graphics (CS 4731) & 2D Graphics Systems

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

GPU Programming EE Final Examination

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

COMP371 COMPUTER GRAPHICS

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

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

The Projection Matrix

Input and Interaction. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

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

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

Transcription:

Programming with OpenGL Complete Programs 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 1

Program Structure Most OpenGL programs have a similar structure that consists of the following functions main(): defines the callback functions opens one or more windows with the required properties enters event loop (last executable statement) init(): sets the state variables Viewing Attributes callbacks Display function Input and window functions 2

A Square Program 3

WebGL Five steps Describe page (HTML file) 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) Send data to GPU (JS file) Render data (JS file) 4

Square.html <!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, 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 vec4 Must set precision in fragment shader 6

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> 7

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 8

square.js 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" ); } // Four Vertices var vertices = [ vec2( -0.5, -0.5 ), vec2( -0.5, 0.5 ), vec2( 0.5, 0.5 ), vec2( 0.5, -0.5) ]; 9

square.js (cont) // Configure WebGL 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 our shader variables with our data buffer var vposition = gl.getattriblocation( program, "vposition" ); gl.vertexattribpointer( vposition, 2, gl.float, false, 0, 0 ); gl.enablevertexattribarray( vposition ); 10

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 11

square.js (cont) render(); }; function render() { gl.clear( gl.color_buffer_bit ); gl.drawarrays( gl.triangle_fan, 0, 4 ); } 1 2 0 3 12

Triangles, Fans or Strips gl.drawarrays( gl.triangles, 0, 6 ); // 0, 1, 2, 0, 2, 3 gl.drawarrays( gl.triangle_fan, 0, 4 ); // 0, 1, 2, 3 1 2 1 2 0 3 0 3 gl.drawarrays( gl.triangle_strip, 0, 4 ); // 0, 1, 3, 2 Angel Angel and Shreiner: and Shreiner: Interactive Interactive Computer Computer Graphics Graphics 7E Addison-Wesley 7E Addison-Wesley 2015 2015 13

OpenGL Primitives POINTS LINES LINE_STRIP 0 1 LINE_LOOP 2 3 4 TRIANGLES 0 1 2 3 TRIANGLE_STRIP TRIANGLE_FAN New point gives First point in all new triangle triangles 14