WebGL: a New Platform for 3D Games Advanced Games Programming by Jarek Francik 2011.

Slides:



Advertisements
Similar presentations
CSPC 352: Computer Graphics Introduction to WebGL.
Advertisements

Animation and Input CSCI Day Six. Animation Basic Steps to Draw Something: var vertices = [ … ]; var BufferId = gl.CreateBuffer(); gl.bindBuffer.
An introduction to WebGL Viktor Kovács. Content A little 3D modeling. What is WebGL? Introducing Three.js. Visualizing GDL objects.
WebGL Seminar TUT Technical Overview Arto Salminen, Matti Anttonen
WebGL - use JavaScript for 3D visualization By Christo Tsvetanov.
 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.
As well as colors, normals, and other vertex data PUSHIN’ GEO TO THE GPU JEFF CHASTINE 1.
2D graphics 3D graphics Segoe UI Fonts, text analysis, layout Image & video decoding GPGPU Too.
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.
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.
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.
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.
WebGL: in-browser 3D graphics Nick Whitelegg Maritme and Technology Faculty Southampton Solent University.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
Exam Review Questions. Problem: A cube has vertices with world coordinates: (1, 0, 0) (2, 0, 0) (1, 1, 0) (2, 1, 0) (1, 0, 1) (2, 0, 1) (1, 1, 1) (2,
GRAPHICS PIPELINE & SHADERS SET09115 Intro to Graphics Programming.
80 줄로 웹지엘 입문하기. canvas javascript WebGL Library GLSL 개발환경.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
User Input and Animation. For Further Reading Angel 7 th Ed: –Chapter 3: Input and Animation. –Chapter 4: Rotation and other transformation. Beginning.
OpenGL-ES 3.0 And Beyond Boston Photo credit :Johnson Cameraface OpenGL Basics.
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in 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.
CS 480/680 Computer Graphics Programming with Open GL Part 5: Putting it all together Dr. Frederick C Harris, Jr. Fall 2011.
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 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
3D Objects in WebGL Loading 3D model files. Announcement Midterm exam on 12/2 –No Internet connection. –Example code and exam questions will be on USB.
Mouse Input. For Further Reading Learning WebGL, Lesson 11: 2.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
MP3 Frequently Asked Questions (IN OFFICE HOURS).
1 Introduction to Computer Graphics with WebGL ADOPTED from Ed Angel Chapter 2 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015.
Martin Kruliš by Martin Kruliš (v1.1)1.
Pushin’ Geo to the GPU As well as colors, normals, and other vertex data Made by Jeff Chastine, Modified by Chao Mei Jeff Chastine.
Interactive Computer Graphics CS 418 – Fall 2017
Interactive Computer Graphics CS 418 – Fall 2017 Indexed Meshes Transformations Background Imagery:
Introduction to Computer Graphics with WebGL
Graphics CSCI 343, Fall 2017 Lecture 13 More on WebGL
The Basics: HTML5, Drawing, and Source Code Organization
Graphics hardware and software
Tips for Environment Mapping
Introduction to Computer Graphics with WebGL
COMP 5441: ADVANCED COMPUTER GRAPHICS FALL 2017
Objectives Simple Shaders Programming shaders with GLSL
Mitch Williams 3D-Online
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
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
Programming with OpenGL Part 5: More GLSL
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 5: More GLSL
Textures in WebGL.
Introduction to Computer Graphics with WebGL
<!DOCTYPE html> <body>
Engine and functionalities
Presentation transcript:

WebGL: a New Platform for 3D Games Advanced Games Programming by Jarek Francik 2011

HTML5, Canvas & WebGL Google Chromeok Mozilla Firefoxok, but not today Safaridev version only Operadev version only Internet ExplorerNO!!!

HTML5 WebGL Tutorial WebGL Tutorial If nothing visible, try to run with the latest version of Chrome. If it still doesn't help, try to upload to any web server and run through http.

Fragment Shader #ifdef GL_ES precision highp float; #endif varying vec4 vColor; void main(void) { gl_FragColor = vColor; }

Vertex Shader attribute vec3 aVertexPosition; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; varying vec4 vColor; void main(void) { gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); vColor = vec4(1.0, 1.0, 1.0, 1.0); }

main function (1)

main function (2) function main() { // Initialize WebGL context var canvas = document.getElementById("idCanvas"); var gl = canvas.getContext("experimental-webgl"); gl.viewport(0, 0, canvas.width, canvas.height); gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.enable(gl.DEPTH_TEST); //.... // see following slides

main function (3) // Initialize Shader Program var txtShaderFrag = document.getElementById("shader-fs").innerText; var txtShaderVert = document.getElementById("shader-vs").innerText; var shaderProgram = gl.createProgram(); var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fragmentShader, txtShaderFrag); gl.compileShader(fragmentShader); gl.attachShader(shaderProgram, fragmentShader); var vertexShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vertexShader, txtShaderVert); gl.compileShader(vertexShader); gl.attachShader(shaderProgram, vertexShader); gl.linkProgram(shaderProgram); gl.useProgram(shaderProgram); gl.enableVertexAttribArray(gl.getAttribLocation(shaderProgram, "aVertexPosition")); gl.enableVertexAttribArray(gl.getAttribLocation(shaderProgram, "aVertexColor")); gl.shaderProgram = shaderProgram;

main function (4) // Init Objects initObjects(gl); // Render buffers render(gl); }

main function function main() { // Initialize WebGL context var canvas = document.getElementById("idCanvas"); var gl = canvas.getContext("experimental-webgl"); gl.viewport(0, 0, canvas.width, canvas.height); gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.enable(gl.DEPTH_TEST); // Initialize Shader Program var txtShaderFrag = document.getElementById("shader-fs").innerText; var txtShaderVert = document.getElementById("shader-vs").innerText; var shaderProgram = gl.createProgram(); var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fragmentShader, txtShaderFrag); gl.compileShader(fragmentShader); gl.attachShader(shaderProgram, fragmentShader); var vertexShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vertexShader, txtShaderVert); gl.compileShader(vertexShader); gl.attachShader(shaderProgram, vertexShader); gl.linkProgram(shaderProgram); gl.useProgram(shaderProgram); gl.enableVertexAttribArray(gl.getAttribLocation(shaderProgram, "aVertexPosition")); gl.enableVertexAttribArray(gl.getAttribLocation(shaderProgram, "aVertexColor")); gl.shaderProgram = shaderProgram; // Init Objects initObjects(gl); // Render buffers render(gl); }

initObjects function initObjects(gl) { var bufVertexPositions = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, bufVertexPositions); var vertices = [ 0.0, 1.0, 0.0, // triangle -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0, // square -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, -1.0, 0.0, ]; gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); gl.bufVertexPositions = bufVertexPositions; }

render (1) function render(gl) { // clear canvas gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); // bind and set the vertex buffer gl.bindBuffer(gl.ARRAY_BUFFER, gl.bufVertexPositions); loc = gl.getAttribLocation(gl.shaderProgram, "aVertexPosition"); gl.vertexAttribPointer(loc, 3, gl.FLOAT, false, 0, 0);

render (2) // set-up the view matrix var pMatrix = mat4.create(); mat4.perspective(45, gl.canvas.width / gl.canvas.height, 0.1, 100.0, pMatrix); mat4.translate(pMatrix, [0, 0.0, -7.0]); loc = gl.getUniformLocation(gl.shaderProgram, "uPMatrix"); gl.uniformMatrix4fv(loc, false, pMatrix); // set up the world matrix var mvMatrix = mat4.create(); mat4.identity(mvMatrix); mat4.translate(mvMatrix, [-1.5, 0.0, 0.0]); loc = gl.getUniformLocation(gl.shaderProgram, "uMVMatrix"); gl.uniformMatrix4fv(loc, false, mvMatrix);

render (3) // draw the triangle (3 vertices) gl.drawArrays(gl.TRIANGLES, 0, 3); // modify the world matrix mat4.translate(mvMatrix, [3.0, 0.0, 0.0]); loc = gl.getUniformLocation(gl.shaderProgram, "uMVMatrix"); gl.uniformMatrix4fv(loc, false, mvMatrix); // draw the square (4 vertices, starting from no 3) gl.drawArrays(gl.TRIANGLE_STRIP, 3, 4); }

render function render(gl) { // clear canvas gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); // bind and set the vertex buffer gl.bindBuffer(gl.ARRAY_BUFFER, gl.bufVertexPositions); loc = gl.getAttribLocation(gl.shaderProgram, "aVertexPosition"); gl.vertexAttribPointer(loc, 3, gl.FLOAT, false, 0, 0); // set-up the view matrix var pMatrix = mat4.create(); mat4.perspective(45, gl.canvas.width / gl.canvas.height, 0.1, 100.0, pMatrix); mat4.translate(pMatrix, [0, 0.0, -7.0]); loc = gl.getUniformLocation(gl.shaderProgram, "uPMatrix"); gl.uniformMatrix4fv(loc, false, pMatrix); // set up the world matrix var mvMatrix = mat4.create(); mat4.identity(mvMatrix); mat4.translate(mvMatrix, [-1.5, 0.0, 0.0]); loc = gl.getUniformLocation(gl.shaderProgram, "uMVMatrix"); gl.uniformMatrix4fv(loc, false, mvMatrix); // draw the triangle (3 vertices) gl.drawArrays(gl.TRIANGLES, 0, 3); // modify the world matrix mat4.translate(mvMatrix, [3.0, 0.0, 0.0]); loc = gl.getUniformLocation(gl.shaderProgram, "uMVMatrix"); gl.uniformMatrix4fv(loc, false, mvMatrix); // draw the square (4 vertices, starting from no 3) gl.drawArrays(gl.TRIANGLE_STRIP, 3, 4); }

animate (1) 1.Add (before render): var angle = 0; 2.Add (in world matrix configuration): mat4.rotateY(mvMatrix, angle); 3.Replace render() call in main() with: // Start animation loop animate(gl);

animate (2) function animate(gl) { window.webkitRequestAnimationFrame(function() { animate(gl); }); render(gl); angle += 0.025; }

Next Stages 1.(Animated Triangle and Square) 2.Added Colours 3.Added 3D: Pyramid and Cube 4.Added Texture 5.Added Lighting (not covered today)

Three.js: HTML WebGL/Three.js Tutorial

Three.js: javascript (1) var camera, scene, renderer, sphere; function main() { // create a renderer, camera, and scene renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000); camera.position.z = 600; scene = new THREE.Scene();

Three.js: javascript (2) var texture = THREE.ImageUtils.loadTexture( "land_ocean_ice_cloud_2048.jpg" ); sphere = new THREE.Mesh(new THREE.SphereGeometry(200, 100, 100), new THREE.MeshBasicMaterial( { map:texture } )); scene.add(sphere); // add lighting var light = new THREE.DirectionalLight(0xFFFFFF); light.position.x = 150; light.position.y = 250; light.position.z = 150; scene.add(light); // attach the scene to the DOM document.getElementById("container").appendChild(renderer.domElement); animate(); }

Three.js: javascript function animate() { window.webkitRequestAnimationFrame(animate); renderer.render(scene, camera); }

Three.js: javascript var camera, scene, renderer, sphere; function main() { // create a renderer, camera, and scene renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000); camera.position.z = 600; scene = new THREE.Scene(); var texture = THREE.ImageUtils.loadTexture( "land_ocean_ice_cloud_2048.jpg" ); sphere = new THREE.Mesh(new THREE.SphereGeometry(200, 100, 100), new THREE.MeshBasicMaterial( { map:texture } )); scene.add(sphere); // add lighting var light = new THREE.DirectionalLight(0xFFFFFF); light.position.x = 150; light.position.y = 250; light.position.z = 150; scene.add(light); // attach the scene to the DOM document.getElementById("container").appendChild(renderer.domElement); animate(); } function animate() { window.webkitRequestAnimationFrame(animate); renderer.render(scene, camera); }