1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.

Slides:



Advertisements
Similar presentations
Computer Graphics - Transformation -
Advertisements

Figures based on regular polygons (lab3)
Transformations Ed Angel Professor Emeritus of Computer Science
Objectives Learn to build arbitrary transformation matrices from simple transformations Learn to build arbitrary transformation matrices from simple transformations.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 OpenGL Transformations Ed Angel Professor of Computer Science, Electrical and Computer.
Chapter 3: Geometric Objects and Transformations Part 2
CS 4731: Computer Graphics Lecture 8: 3D Affine transforms Emmanuel Agu.
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.
Computer Graphics Bing-Yu Chen National Taiwan University.
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.
Graphics Graphics Korea University kucg.korea.ac.kr Transformations 고려대학교 컴퓨터 그래픽스 연구실.
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 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.
CSE Real Time Rendering Week 5. Slides(Some) Courtesy – E. Angel and D. Shreiner.
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
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.
Computer Graphics I, Fall 2010 OpenGL Transformations.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 OpenGL Transformations.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Transformations Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
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.
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 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 OpenGL Transformations. 2 Objectives Learn how to carry out transformations in OpenGL ­Rotation ­Translation ­Scaling Introduce OpenGL matrix modes.
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 Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Transformations Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
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.
Introduction to Computer Graphics with WebGL
OpenGL Transformations
OpenGL Transformations
Introduction to Computer Graphics with WebGL
OpenGL Transformations
Computer Graphics OpenGL Transformations
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Virtual Trackball and Quaterion Rotation
OpenGL Transformations
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
OpenGL Transformations
OpenGL Transformations
Computer Graphics Transformations
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Presentation transcript:

1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science Laboratory University of New Mexico Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

2 Applying Transformations Ed Angel Professor Emeritus of Computer Science University of New Mexico Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

3 Using Transformations Example: Begin with a cube rotating Use mouse or button listener to change direction of rotation Start with a program that draws a cube in a standard way ­Centered at origin ­Sides aligned with axes ­Will discuss modeling in next lecture Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Where do we apply transformation? Same issue as with rotating square ­in application to vertices ­in vertex shader: send MV matrix ­in vertex shader: send angles Choice between second and third unclear Do we do trigonometry once in CPU or for every vertex in shader ­GPUs have trig functions hardwired in silicon 4 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Rotation Event Listeners 5 document.getElementById( "xButton" ).onclick = function () { axis = xAxis; }; document.getElementById( "yButton" ).onclick = function () { axis = yAxis; }; document.getElementById( "zButton" ).onclick = function () { axis = zAxis; }; function render(){ gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); theta[axis] += 2.0; gl.uniform3fv(thetaLoc, theta); gl.drawArrays( gl.TRIANGLES, 0, NumVertices ); requestAnimFrame( render ); // display as soon as possible (loop) } Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Rotation Shader (see cube.html + cube.js)cube.htmlcube.js 6 attribute vec4 vPosition; attribute vec4 vColor; varying vec4 fColor; uniform vec3 theta; void main() { vec3 angles = radians( theta ); // in GLSL // in Javascript -> Math.radians() vec3 c = cos( angles ); // in Javascript -> Math.cos() vec3 s = sin( angles ); // in Javascript -> Math.sin() vec4 tmp_position; // Remember: these matrices are column-major // (columns are stored one after another) mat4 rx = mat4( 1.0, 0.0, 0.0, 0.0, 0.0, c.x, s.x, 0.0, 0.0, -s.x, c.x, 0.0, 0.0, 0.0, 0.0, 1.0 ); Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Rotation Shader (cont) 7 mat4 ry = mat4( c.y, 0.0, -s.y, 0.0, 0.0, 1.0, 0.0, 0.0, s.y, 0.0, c.y, 0.0, 0.0, 0.0, 0.0, 1.0 ); mat4 rz = mat4( c.z, s.z, 0.0, 0.0, -s.z, c.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ); tmp_position = rz * ry * rx * vPosition; Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Rotation Shader (cont) 8 // Now, we need to invert the Z coordinates since Web // browsers map the clip coordinates to the NDC (normalized // device coordinates) expecting the Z axis to be inverted // (as do perspective projection matrices). tmp_position.z = -tmp_position.z; fColor = vColor; gl_Position = tmp_position; } Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

9 (Smooth Rotation) From a practical standpoint, we are often want to use transformations to move and reorient an object smoothly ­Problem: find a sequence of model-view matrices M 0,M 1,…..,M n so that when they are applied successively to one or more objects we see a smooth transition For orientating an object, we can use the fact that every rotation corresponds to part of a great circle on a sphere ­Find the axis of rotation and angle ­Virtual trackball (see text) Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

10 (Incremental Rotation) Consider the two approaches ­For a sequence of rotation matrices R 0,R 1,…..,R n, find the Euler angles for each and use R i = R iz R iy R ix Not very efficient ­Use the final positions to determine the axis and angle of rotation, then increment only the angle Quaternions can be more efficient than either Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

11 Quaternions Extension of imaginary numbers from two to three dimensions Requires one real and three imaginary components i, j, k Quaternions can express rotations on sphere smoothly and efficiently. Process: ­Model-view matrix  quaternion ­Carry out operations with quaternions ­Quaternion  Model-view matrix q=q 0 +q 1 i+q 2 j+q 3 k Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

12 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Quaternions Graphical examples to be shown in class : ­CameraEuler ­CameraQuat Reference document ­A practical example (pdf file)pdf file

13 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Quaternions Rotation about the z axis (extracted from the manual p. 229)

14 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Quaternions Generalizing for an arbitrary rotation about vector v: x How to derive the matrix…

15 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Quaternions

16 Rotation controlled by mouse mouvements Basic Principle: Click button -> get mouse coordinates in window Drag mouse ­Horizontal displacement (in pixels) corresponds to a rotation around the vertical axis. ­Vertical displacement (in pixels) corresponds to a rotation around the horizontal axis. Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

17 Rotation controlled by mouse mouvements See simple-rotator.jssimple-rotator.js In the init() function: rotator = new SimpleRotator(canvas, render); rotator.setView([0, 0, 1], [0, 1, 0], 40); In the render() function: flattenedmodelview = rotator.getViewMatrix(); modelview = unflatten(flattenedmodelview); Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

18 (Interfaces) One of the major problems in interactive computer graphics is how to use a two- dimensional device such as a mouse to interface with three dimensional objects Example: how to form an instance matrix? Some alternatives ­Virtual trackball ­3D input devices such as the spaceball ­Use areas of the screen Distance from center controls angle, position, scale depending on mouse button depressed Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015