Mouse Input. For Further Reading Learning WebGL, Lesson 11: 2.

Slides:



Advertisements
Similar presentations
TEXTURE MAPPING JEFF CHASTINE 1. TEXTURE MAPPING Applying an image (or a texture ) to geometry 2D images (rectangular) 3D images (volumetric – such as.
Advertisements

Modeling/Transformation Examples Modeling a colored cube Hierarchical modeling.
1 Lecture 12 Texture Mapping uploading of the texture to the video memory the application of the texture onto geometry.
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 Texture Maps Jeff Parker Oct Objectives Introduce Mapping Methods Texture Mapping Environment Mapping Bump Mapping Billboards Consider basic.
Texturing in OpenGL Lab #7. Creating Textures glEnable ( GL_TEXTURE_2D ); GLuint myTex; glGenTextures ( 1, (GLuint*) &myTex ); glBindTexture ( GL_TEXTURE_2D,
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 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.
WebGL: in-browser 3D graphics Nick Whitelegg Maritme and Technology Faculty Southampton Solent University.
CS 4363/6353 TEXTURE MAPPING PART II. WHAT WE KNOW We can open image files for reading We can load them into texture buffers We can link that texture.
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.
CG1 Labs Wei Li. Back Face Culling // enable back-face culling glEnable( GL_CULL_FACE ); // orientation of front-facing polygons glFrontFace( GL_CCW );
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.
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.
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.
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.
OpenGL-ES 3.0 And Beyond Boston Photo credit :Johnson Cameraface OpenGL Basics.
1 Graphics CSCI 343, Fall 2015 Lecture 21 Lighting and Shading III.
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.
Shading. For Further Reading Angel 7 th Ed: ­Chapter 6 2.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
 Learn how you can use the shader through OpenGL ES  Add texture on object and make the object have a different look!!
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.
MP3 Frequently Asked Questions (IN OFFICE HOURS).
Texture Mapping. For Further Reading Angel 7 th Ed: ­Chapter 7: 7.3 ~ 7.9 Beginning WebGL: ­Chapter 3 2.
Advanced Texture Mapping Bump Mapping & Environment Mapping (Reflection)
MP3.. Start at the very beginning. Almost. Either start from nothing yourself, or use the empty template for this MP. Run through the provided files are.
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.
An Introduction to WebGL Programming Ed Angel Dave Shreiner.
Real-Time Rendering Buffers in OpenGL 3.3
Tips for Environment Mapping
Texture Mapping We can improve the realism of graphics models by mapping a texture pattern (image) onto the modeled object surface. We refer to this technique.
Introduction to Computer Graphics with WebGL
Texture Mapping Part II
Introduction to Computer Graphics with WebGL
COMP 5441: ADVANCED COMPUTER GRAPHICS FALL 2017
Objectives Simple Shaders Programming shaders with GLSL
Introduction to Computer Graphics with WebGL
OpenGL Texture Mapping
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 2: Complete Programs
Introduction to Computer Graphics with WebGL
Chapter VI OpenGL ES and Shader
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
OpenGL Texture Mapping
Advanced Texture Mapping
Introduction to Computer Graphics with WebGL
Mouse Input.
Texture Mapping Ed Angel Professor Emeritus of Computer Science
Textures in WebGL.
Introduction to Computer Graphics with WebGL
Presentation transcript:

Mouse Input

For Further Reading Learning WebGL, Lesson 11: 2

Rotating Cube by Mouse Input

Summary Adding handlers for mouse events. –Note that the mouse click may start on canvas but finish outside of canvas. canvas.onmousedown = handleMouseDown; document.onmouseup = handleMouseUp; document.onmousemove = handleMouseMove; 4

Rotating Cube (JavaScript code)JavaScript code // handlers for mouse input (borrowed from "Learning WebGL" lesson 11) var mouseDown = false; var lastMouseX = null; var lastMouseY = null; var moonRotationMatrix = mat4(); function handleMouseDown(event) { mouseDown = true; lastMouseX = event.clientX; lastMouseY = event.clientY; } function handleMouseUp(event) { mouseDown = false; }

function handleMouseMove(event) { if (!mouseDown) { return; } var newX = event.clientX; var newY = event.clientY; var deltaX = newX - lastMouseX; var newRotationMatrix = rotate( deltaX/10, 0, 1, 0 ); var deltaY = newY - lastMouseY; newRotationMatrix = mult( rotate( deltaY/10, 1, 0, 0 ), newRotationMatrix); moonRotationMatrix = mult(newRotationMatrix, moonRotationMatrix); lastMouseX = newX lastMouseY = newY; }

window.onload = function init() { var canvas = document.getElementById( "gl-canvas" );... //event listeners for buttons document.getElementById( "xButton" ).onclick = rotateX; document.getElementById( "yButton" ).onclick = rotateY; document.getElementById( "zButton" ).onclick = rotateZ;... // event handlers for mouse input canvas.onmousedown = handleMouseDown; document.onmouseup = handleMouseUp; document.onmousemove = handleMouseMove; render(); };

Notice! The rotation is defined in object space. –It works well only if the viewing direction is along the Z axis. –Change the eye position in lookAt() and see what happens! 8

Lab Time! Change the eye position and see what happens. Use the mouse input to rotate the light position instead of the object. Hints: –Apply the mouse-controlled rotation to the light position, either at the JavaScript or at the GLSL shader. –If you choose the former, then you need a matrix- vector multiplication in JS. –If you choose the latter, then an additional mat4 is needed. 9

Textures in WebGL

For Further Reading Angel 7 th Ed: –Section 7.5: Texture Mapping in WebGL. Beginning WebGL: –Chapter 3 Learning WebGL, Lesson 5: 11

Textured Cube

Summary (1/2) OpenGL setup (house keeping) for textures: –gl.createTexture() –gl.bindTexture() –gl.pixelStorei() –gl.texImage2D() –gl.texParameter() 13

Summary (2/2) Creating image in JS. –Assuming the image file is at the same folder on the server. –Cross-Origin Resource Sharing (CORS) is discussed in P.60 of the “Beginning WebGL” eBook. Adding texture coordinates to vertex attributes. Texture lookup in GLSL shaders. 14

Cross-Origin Resource Sharing Cross-Origin Resource Sharing (CORS) prohibits loading textures from local files. To solve this, please invoke Chrome with --allow-file-access-from-files Or use Firefox instead! More detail in:

Textured Cube (HTML code)HTML code... attribute vec2 vTexCoord;... varying vec2 fTexCoord;... void main() {... fColor = (ambient + diffuse) * vColor; fTexCoord = vTexCoord; gl_Position = projectionMatrix *... }

... varying vec2 fTexCoord; uniform sampler2D texture; void main() { gl_FragColor = fColor * texture2D( texture, fTexCoord ); }

Textured Cube (JavaScript code)JavaScript code var pointsArray = []; var colorsArray = []; var normalsArray = []; var texCoordsArray = []; var texture; var texCoord = [ vec2(0, 0), vec2(0, 1), vec2(1, 1), vec2(1, 0) ];

function configureTexture( image ) { texture = gl.createTexture(); gl.bindTexture( gl.TEXTURE_2D, texture ); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image ); gl.generateMipmap( gl.TEXTURE_2D ); gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR ); gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST ); gl.uniform1i(gl.getUniformLocation(program, "texture"), 0); } function quad(a, b, c, d) {... pointsArray.push(vertices[a]); colorsArray.push(vertexColors[a]); normalsArray.push(normal); texCoordsArray.push(texCoord[0]);...

window.onload = function init() {... var tBuffer = gl.createBuffer(); gl.bindBuffer( gl.ARRAY_BUFFER, tBuffer ); gl.bufferData( gl.ARRAY_BUFFER, flatten(texCoordsArray), gl.STATIC_DRAW ); var vTexCoord = gl.getAttribLocation( program, "vTexCoord" ); gl.vertexAttribPointer( vTexCoord, 2, gl.FLOAT, false, 0, 0 ); gl.enableVertexAttribArray( vTexCoord ); // // Initialize a texture // var image = new Image(); image.onload = function() { configureTexture( image ); } image.src = "SA2011_black.gif";... }

Lab Time! Use MS Paint ( 小畫家 ) to draw something and save it to a GIF file. Use it as the texture for the above “Textured Cube” example. You may need to create a short cut to Chrome and add --allow-file- access-from-files to the target command.