Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.

Slides:



Advertisements
Similar presentations
Animation and Input CSCI Day Six. Animation Basic Steps to Draw Something: var vertices = [ … ]; var BufferId = gl.CreateBuffer(); gl.bindBuffer.
Advertisements

 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.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
Open Graphics Library (OpenGL)
Michael Robertson Yuta Takayama. WebGL is OpenGL on a web browser. OpenGL is a low-level 3D graphics API Basically, WebGL is a 3D graphics API that generates.
CSE Real Time Rendering. TBT (Not So) Real Time Rendering.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
WebGL: Hands On Zhenyao Mo Software Engineer, Google, Inc. Chrome GPU Team.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
A Really (too) Short Introduction to OpenGL Peter Rautek.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
CS 418: Interactive Computer Graphics Introduction to WebGL: HelloTriangle.html Eric Shaffer.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
1 Graphics CSCI 343, Fall 2015 Lecture 2 Introduction to HTML, JavaScript and WebGL.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Building Models Ed Angel Professor Emeritus of Computer Science University of New Mexico Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley.
User Input and Animation. For Further Reading Angel 7 th Ed: –Chapter 3: Input and Animation. –Chapter 4: Rotation and other transformation. Beginning.
1 Graphics CSCI 343, Fall 2015 Lecture 6 Viewing, Animation, User Interface.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in WebGL.
1 Graphics CSCI 343, Fall 2015 Lecture 3 Introduction to WebGL.
Vertex Buffer Objects and Shader Attributes. For Further Reading Angel 7 th Ed: –Most parts of Chapter 2. Beginning WebGL: –Chapter 1: vertex Buffer Objects,
Programming with OpenGL Part 0: 3D API. For Further Reading Angel 7 th Ed: –2.2: JavaScript –2.3: OpenGL & WebGL –2.8: fragment & vertex shaders Beginning.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
Programming with OpenGL Part 5: More GLSL Isaac Gang University of Mary Hardin-Baylor E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 4: Color and Attributes Isaac Gang University.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
1 Introduction to Computer Graphics with WebGL ADOPTED from Ed Angel Chapter 2 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
The Basics: HTML5, Drawing, and Source Code Organization
Introduction to Computer Graphics with WebGL
Introduction to OpenGL
Introduction to Computer Graphics with WebGL
Objectives Simple Shaders Programming shaders with GLSL
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 2: Complete Programs
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 4: Color and Attributes
Introduction to Computer Graphics with WebGL
Transformation, perspective projection, and LookAT in WebGL vs.OpenGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 4: Color and Attributes
Introduction to Computer Graphics with WebGL
Introduction to OpenGL
Textures in WebGL.
Introduction to Computer Graphics with WebGL
Presentation transcript:

Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico

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 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Square Program Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

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) Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

square.html (see square.html)square.html attribute vec4 vPosition; void main() { gl_Position = vPosition; } precision mediump float; void main() { gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 ); } Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

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 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

square.html (cont) Oops... your browser doesn't support the HTML5 canvas element Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

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 (note: bugs in original version…) square.js : the application file Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

square.js (square.js)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) ]; Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

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 (-1 to +1) Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

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 ); // locate vPosition in the shaders var vPosition = gl.getAttribLocation(program, "vPosition"); // 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 ); // Now render gl.enableVertexAttribArray(vPosition); // activate vertex array for vPosition Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

square.js (cont) // gl.bindBuffer(gl.ARRAY_BUFFER, bufferId); // select buffer to be used // The previous line is not necessary because the correct buffer is // already selected gl.vertexAttribPointer(vPosition, 2, gl.FLOAT, false, 0, 0); // define data format render(); }; Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

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 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

square.js (cont) function render() { gl.clear( gl.COLOR_BUFFER_BIT ); gl.drawArrays( gl.TRIANGLE_FAN, 0, 4 ); } Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

15 WebGL Primitives GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_POINTS GL_LINES GL_LINE_LOOP GL_LINE_STRIP GL_TRIANGLES Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Triangles, Fans or Strips gl.drawArrays( gl.TRIANGLES, 0, 6 ); // 0, 1, 2, 0, 2, 3 //(six vertices required) gl.drawArrays( gl.TRIANGLE_STRIP, 0, 4 ); // 0, 1, 3, 2 gl.drawArrays( gl.TRIANGLE_FAN, 0, 4 ); // 0, 1, 2, Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Selecting colors… Interesting tool : ­ Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Typical program structure Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015 Setup 1.Locate variable(s) in shaders Using getAttribLocation() and getUniformLocation() 2.Create buffer(s) using createBuffer() 3.Load data into GPU select buffer(s) => bindBuffer() load data => bufferData() Rendering 1.Activate/desactivate vertex array(s) enableVertexAttribArray() disableVertexAttribArray() 2.Select the buffer(s) to be used => bindBuffer() And define data format => vertexAttribPointer() 3.Draw using drawArrays() or drawElements()