Computer Graphics. Bing-Yu Chen National Taiwan University

Similar documents
Lecture 4 of 41. Lab 1a: OpenGL Basics

1 Transformations. Chapter 1. Transformations. Department of Computer Science and Engineering 1-1

CS 4204 Computer Graphics

Matrix-Rechnung I ' z =... Universität Frankfurt

The Viewing Pipeline adaptation of Paul Bunn & Kerryn Hugo s notes

Modeling Transform. Chapter 4 Geometric Transformations. Overview. Instancing. Specify transformation for objects 李同益

OpenGL Transformations

Fachhochschule Regensburg, Germany, February 15, 2017

Lecture 5: Viewing. CSE Computer Graphics (Fall 2010)

Getting Started. Overview (1): Getting Started (1): Getting Started (2): Getting Started (3): COSC 4431/5331 Computer Graphics.

Describe the Orthographic and Perspective projections. How do we combine together transform matrices?

CS D Transformation. Junqiao Zhao 赵君峤

Spring 2013, CS 112 Programming Assignment 2 Submission Due: April 26, 2013

Computer Viewing Computer Graphics I, Fall 2008

Geometry: Outline. Projections. Orthographic Perspective

Fundamental Types of Viewing

Computer Graphics. Chapter 10 Three-Dimensional Viewing

Graphics and Visualization

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

CS354 Computer Graphics Viewing and Modeling

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Viewing Transformation

CSC 470 Computer Graphics

Precept 2 Aleksey Boyko February 18, 2011

Overview. Viewing and perspectives. Planar Geometric Projections. Classical Viewing. Classical views Computer viewing Perspective normalization

Advanced Computer Graphics (CS & SE )

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

An Interactive Introduction to OpenGL Programming

7. 3D Viewing. Projection: why is projection necessary? CS Dept, Univ of Kentucky

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

COMS 4160: Problems on Transformations and OpenGL

3D Graphics Pipeline II Clipping. Instructor Stephen J. Guy

Computer Graphics Geometric Transformations

CIS 636 Interactive Computer Graphics CIS 736 Computer Graphics Spring 2011

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

Transformations (Rotations with Quaternions) October 24, 2005

CS 591B Lecture 9: The OpenGL Rendering Pipeline

CS380: Computer Graphics Viewing Transformation. Sung-Eui Yoon ( 윤성의 ) Course URL:

CITSTUDENTS.IN VIEWING. Computer Graphics and Visualization. Classical and computer viewing. Viewing with a computer. Positioning of the camera

Objectives. transformation. General Transformations. Affine Transformations. Notation. Pipeline Implementation. Introduce standard transformations

Transformations. Standard Transformations. David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel

1 (Practice 1) Introduction to OpenGL

Introduction to Computer Graphics with WebGL

Transformations. Overview. Standard Transformations. David Carr Fundamentals of Computer Graphics Spring 2004 Based on Slides by E.

3D Viewing. With acknowledge to: Ed Angel. Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Three-Dimensional Graphics III. Guoying Zhao 1 / 67

Today s class. Viewing transformation Menus Mandelbrot set and pixel drawing. Informationsteknologi

Order of Transformations

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

Computer Viewing. CITS3003 Graphics & Animation. E. Angel and D. Shreiner: Interactive Computer Graphics 6E Addison-Wesley

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

COMP Computer Graphics and Image Processing. 5: Viewing 1: The camera. In part 1 of our study of Viewing, we ll look at ˆʹ U ˆ ʹ F ˆʹ S

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

Reading. Hierarchical Modeling. Symbols and instances. Required: Angel, sections , 9.8. Optional:

OpenGL. Toolkits.

Wire-Frame 3D Graphics Accelerator IP Core. C Library Specification

Computer Graphics. Chapter 7 2D Geometric Transformations

3D Graphics for Game Programming (J. Han) Chapter II Vertex Processing

An Interactive Introduction to OpenGL and OpenGL ES Programming. Ed Angel Dave Shreiner

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

Computer Graphics. Bing-Yu Chen National Taiwan University

Image Rendering. Rendering can be divided into three sub-problems. Image Formation The hidden surface problem visibility determination steps

CSE 167: Lecture #4: Vertex Transformation. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

蔡侑庭 (Yu-Ting Tsai) National Chiao Tung University, Taiwan. Prof. Wen-Chieh Lin s CG Slides OpenGL 2.1 Specification

part 3 Martin Samuelčík Room I4

CSE 167: Introduction to Computer Graphics Lecture #5: Projection. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2017

Realtime 3D Computer Graphics & Virtual Reality. Viewing

Viewing and Modeling

Viewing and Projection

CS 4204 Computer Graphics

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

12. Selection. - The objects selected are specified by hit records in the selection buffer. E.R. Bachmann & P.L. McDowell MV 4202 Page 1 of 13

CS 130 Final. Fall 2015

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

Transformations. CSCI 420 Computer Graphics Lecture 4

3.1 Viewing and Projection

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

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

Lecture 4: Transformations and Matrices. CSE Computer Graphics (Fall 2010)

Overview of Projections: From a 3D world to a 2D screen.

Viewing and Projection

3D Viewing Episode 2

CSC 470 Computer Graphics. Three Dimensional Viewing

The Graphics Pipeline and OpenGL I: Transformations!

CS452/552; EE465/505. Transformations

Today. Rendering pipeline. Rendering pipeline. Object vs. Image order. Rendering engine Rendering engine (jtrt) Computergrafik. Rendering pipeline

Transformation Pipeline

Computer Graphics. Chapter 5 Geometric Transformations. Somsak Walairacht, Computer Engineering, KMITL

CSC 470 Computer Graphics

Overview. By end of the week:

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

Prof. Feng Liu. Fall /19/2016

IMAGE-BASED RENDERING AND ANIMATION

Chapter 3 - Basic Mathematics for 3D Computer Graphics

CSE 690: GPGPU. Lecture 2: Understanding the Fabric - Intro to Graphics. Klaus Mueller Stony Brook University Computer Science Department

CHAPTER 1 Graphics Systems and Models 3

Transforms 1 Christian Miller CS Fall 2011

COMP3421. Introduction to 3D Graphics

Transforms 3: Projection Christian Miller CS Fall 2011

GEOMETRIC TRANSFORMATIONS AND VIEWING

Transcription:

Computer Graphics Bing-Yu Chen National Taiwan University

Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing with OpenGL Transformations Animation and Depth Buffering Lighting Evaluation and NURBS Texture Mapping Advanced OpenGL Topics Imaging modified from Dave Shreiner, Ed Angel, and Vicki Shreiner. An Interactive Introduction to OpenGL Programming. ACM SIGGRAPH 2001 Conference Course Notes #54. & ACM SIGGRAPH 2004 Conference Course Notes #29.

Transformations in OpenGL Modeling Viewing orient camera projection Animation Map to screen

Camera Analogy 3D is just like taking a photograph (lots of photographs!) camera viewing volume tripod model

Camera Analogy & Transformations Projection transformations adjust the lens of the camera Viewing transformations tripod define position and orientation of the viewing volume in the world Modeling transformations moving the model Viewport transformations enlarge or reduce the physical photograph

Coordinate Systems & Transformations Steps in Forming an Image specify geometry (world coordinates) specify camera (camera coordinates) project (window coordinates) map to viewport (screen coordinates) Each step uses transformations Every transformation is equivalent to a change in coordinate systems (frames)

Affine Transformations Want transformations which preserve geometry lines, polygons, quadrics Affine = line preserving Rotation, translation, scaling Projection Concatenation (composition)

Homogeneous Coordinates each vertex is a column vector x y v = z w w is usually 1.0 all operations are matrix multiplications directions (directed line segments) can be represented with w = 0.0

3D Transformations A vertex is transformed by 4 x 4 matrices all affine operations are matrix multiplications all matrices are stored column-major in OpenGL matrices are always post-multiplied product of matrix and vector is M = m m m m 0 1 2 3 m m m m 4 5 6 7 m m m m 8 9 10 11 m m m m Mv 12 13 14 15

Specifying Transformations Programmer has two styles of specifying transformations specify matrices (glloadmatrix, glmultmatrix) specify operation (glrotate, glortho) Programmer does not have to remember the exact matrices check appendix of Red Book (Programming Guide)

Programming Transformations Prior to rendering, view, locate, and orient: eye/camera position 3D geometry Manage the matrices including matrix stack Combine (composite) transformations

Transformation Pipeline CPU CPU Poly. Poly. DL DL Pixel Pixel Per Per Vertex Vertex Texture Texture Raster Raster Frag Frag FB FB v e r t e x object eye clip normalized device Modelview Matrix Projection Matrix Perspective Division Viewport Transform window Modelview Modelview Projection other calculations here material color shade model (flat) polygon rendering mode polygon culling clipping

OpenGL Matrices In OpenGL matrices are part of the state Three types Model-View (GL_MODEL_VIEW) Projection (GL_PROJECTION) Texture (GL_TEXTURE) (ignore for now) Single set of functions for manipulation Select which to manipulated by glmatrixmode(gl_model_view); glmatrixmode(gl_projection);

Current Transformation Matrix (CTM) Conceptually there is a 4 x 4 homogeneous coordinate matrix, the current transformation matrix (CTM) that is part of the state and is applied to all vertices that pass down the pipeline The CTM is defined in the user program and loaded into a transformation unit vertices p C CTM p =Cp vertices

CTM operations The CTM can be altered either by loading a new CTM or by postmutiplication Load an identity matrix: C I Load an arbitrary matrix: C M Load a translation matrix: C T Load a rotation matrix: C R Load a scaling matrix: C S Postmultiply by an arbitrary matrix: C CM Postmultiply by a translation matrix: C CT Postmultiply by a rotation matrix: C CR Postmultiply by a scaling matrix: C CS

Rotation about a Fixed Point Start with identity matrix: C I Move fixed point to origin: C CT -1 Rotate: C CR Move fixed point back: C CT Result: C = T -1 RT Each operation corresponds to one function call in the program. Note that the last operation specified is the first executed in the program.

CTM in OpenGL OpenGL has a model-view and a projection matrix in the pipeline which are concatenated together to form the CTM Can manipulate each by first setting the matrix mode

Matrix Operations Specify Current Matrix Stack glmatrixmode( GL_MODELVIEW or GL_PROJECTION ) Other Matrix or Stack Operations glloadidentity() glpushmatrix() glpopmatrix() Viewport usually same as window size viewport aspect ratio should be same as projection transformation or resulting image may be distorted glviewport( x, y, width, height )

Projection Transformation Shape of viewing frustum Perspective projection gluperspective( fovy, aspect, znear, zfar ) glfrustum( left, right, bottom, top, znear, zfar ) Orthographic parallel projection glortho( left, right, bottom, top, znear, zfar ) gluortho2d( left, right, bottom, top ) calls glortho with z values near zero

Applying Projection Transformations Typical use (orthographic projection) glmatrixmode( GL_PROJECTION ); glloadidentity(); glortho( left, right, bottom, top, znear, zfar );

Viewing Transformations Position the camera/eye in the scene place the tripod down; aim camera tripod To fly through a scene change viewing transformation and redraw scene glulookat( eyex, eyey, eyez, aimx, aimy, aimz, upx, upy, upz ) up vector determines unique orientation careful of degenerate positions

Projection Tutorial

Modeling Transformations Move object gltranslate{fd}( x, y, z ) Rotate object around arbitrary axis glrotate{fd}( angle, x, y, z ) angle is in degrees ( x y z) Dilate (stretch or shrink) or mirror object glscale{fd}( x, y, z )

Example Rotation about z axis by 30 degrees with a fixed point of (1.0, 2.0, 3.0) glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(1.0, 2.0, 3.0); glrotatef(30.0, 0.0, 0.0,.10); gltranslatef(-1.0, -2.0, -3.0); Remember that last matrix specified in the program is the first applied

Transformation Tutorial

Arbitrary Matrices Can load and multiply by matrices defined in the application program glloadmatrixf(m) glmultmatrixf(m) The matrix m is a one dimension array of 16 elements which are the components of the desired 4 x 4 matrix stored by columns In glmultmatrixf, m multiplies the existing matrix on the right

Matrix Stacks In many situations we want to save transformation matrices for use later Traversing hierarchical data structures Avoiding state changes when executing display lists OpenGL maintains stacks for each type of matrix Access present type (as set by glmatrixmode) by glpushmatrix() glpopmatrix()

Reading Back Matrices Can also access matrices (and other parts of the state) by enquiry (query) functions glgetintegerv glgetfloatv glgetbooleanv glgetdoublev glisenabled For matrices, we use as double m[16]; glgetfloatv(gl_modelview, m);

Connection: Viewing and Modeling Moving camera is equivalent to moving every object in the world towards a stationary camera Viewing transformations are equivalent to several modeling transformations glulookat() has its own command can make your own polar view or pilot view

Projection is left handed Projection transformations (gluperspective, glortho) are left handed think of znear and zfar as distance from view point Everything else is right handed, including the vertexes to be rendered y z+ y left handed x right handed z+ x

Common Transformation Usage 3 examples of resize() routine restate projection & viewing transformations Usually called when window resized Registered as callback for glutreshapefunc()

resize(): Perspective & LookAt void resize( int w, int h ) { glviewport( 0, 0, (GLsizei) w, (GLsizei) h ); glmatrixmode( GL_PROJECTION ); glloadidentity(); gluperspective( 65.0, (GLdouble) w / h, 1.0, 100.0 ) glmatrixmode( GL_MODELVIEW ); glloadidentity(); glulookat( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); }

resize(): Perspective & Translate Same effect as previous LookAt void resize( int w, int h ) { glviewport( 0, 0, (GLsizei) w, (GLsizei) h ); glmatrixmode( GL_PROJECTION ); glloadidentity(); gluperspective( 65.0, (GLdouble) w/h, 1.0, 100.0 ); glmatrixmode( GL_MODELVIEW ); glloadidentity(); gltranslatef( 0.0, 0.0, -5.0 ); }

resize(): Ortho (part 1) void resize( int width, int height ) { GLdouble aspect = (GLdouble) width / height; GLdouble left = -2.5, right = 2.5; GLdouble bottom = -2.5, top = 2.5; glviewport( 0, 0, (GLsizei) w, (GLsizei) h ); glmatrixmode( GL_PROJECTION ); glloadidentity(); continued

resize(): Ortho (part 2) } if ( aspect < 1.0 ) { left /= aspect; right /= aspect; } else { bottom *= aspect; top *= aspect; } glortho( left, right, bottom, top, near, far ); glmatrixmode( GL_MODELVIEW ); glloadidentity();

Compositing Modeling Transformations Problem 1: hierarchical objects one position depends upon a previous position robot arm or hand; sub-assemblies Solution 1: moving local coordinate system modeling transformations move coordinate system post-multiply column-major matrices OpenGL post-multiplies matrices

Compositing Modeling Transformations Problem 2: objects move relative to absolute world origin my object rotates around the wrong origin make it spin around its center or something else Solution 2: fixed coordinate system modeling transformations move objects around fixed coordinate system pre-multiply column-major matrices OpenGL post-multiplies matrices must reverse order of operations to achieve desired effect

Additional Clipping Planes At least 6 more clipping planes available Good for cross-sections Modelview matrix moves clipping plane Ax + By + Cz + D < 0 clipped glenable( GL_CLIP_PLANEi ) glclipplane( GL_CLIP_PLANEi, GLdouble* coeff )

Reversing Coordinate Projection Screen space back to world space glgetintegerv( GL_VIEWPORT, GLint viewport[4] ) glgetdoublev( GL_MODELVIEW_MATRIX, GLdouble mvmatrix[16] ) glgetdoublev( GL_PROJECTION_MATRIX, GLdouble projmatrix[16] ) gluunproject( GLdouble winx, winy, winz, mvmatrix[16], projmatrix[16], GLint viewport[4], GLdouble *objx, *objy, *objz ) gluproject goes from world to screen space

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

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

Quaternions Extension of imaginary numbers from 2 to 3 dimensions Requires one real and three imaginary components i, j, k q=q0+q1i+q2j+q3k = [w, v]; w=q0, v=(q1,q2,q3) where i 2 =j 2 =k 2 =ijk=-1 w is called scalar and v is called vector Quaternions can express rotations on sphere smoothly and efficiently. Process: Model-view matrix Quaternion Carry out operations with Quaternions Quaternion Model-view matrix

Basic Operations Using Quaternions Addition q + q = [w + w, v + v ] Multiplication q q = [w w -v v, v x v + w v + w v] Conjugate q* = [w, -v] Length q =(w 2 + v 2 ) 1/2 Norm N(q) = q 2 = w 2 + v 2 =w 2 + x 2 + y 2 + z 2 Inverse q -1 = q* / q 2 = q* / N(q) Unit Quaternion q is a unit quaternion if q = 1 and then q -1 = q* Identity [1, (0, 0, 0)] (when involving multiplication) [0, (0, 0, 0)] (when involving addition)

Angle and Axis & Eular Angles Angle and Axis q = [cos(θ/2), sin(θ/2) v] Eular Angles q = q yaw q pitch q roll q roll = [cos (y/2), (sin(y/2), 0, 0)] q pitch = [cos (q/2), (0, sin(q/2), 0)] q yaw = [cos(f /2), (0, 0, sin(f /2)]

Matrix-to-Quaternion Conversion MatToQuat (float m[4][4], QUAT * quat) { float tr, s, q[4]; int i, j, k; int nxt[3] = {1, 2, 0}; tr = m[0][0] + m[1][1] + m[2][2]; if (tr > 0.0) { s = sqrt (tr + 1.0); quat->w = s / 2.0; s = 0.5 / s; quat->x = (m[1][2] - m[2][1]) * s; quat->y = (m[2][0] - m[0][2]) * s; quat->z = (m[0][1] - m[1][0]) * s; } else { i = 0; if (m[1][1] > m[0][0]) i = 1; if (m[2][2] > m[i][i]) i = 2; j = nxt[i]; k = nxt[j]; s = sqrt ((m[i][i] - (m[j][j] + m[k][k])) + 1.0); q[i] = s * 0.5; if (s!= 0.0) s = 0.5 / s; q[3] = (m[j][k] - m[k][j]) * s; q[j] = (m[i][j] + m[j][i]) * s; q[k] = (m[i][k] + m[k][i]) * s; quat->x = q[0]; quat->y = q[1]; quat->z = q[2]; quat->w = q[3]; } }

Quaternion-to-Matrix Conversion QuatToMatrix (QUAT * quat, float m[4][4]) { float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2; x2 = quat->x + quat->x; y2 = quat->y + quat->y; z2 = quat->z + quat->z; xx = quat->x * x2; xy = quat->x * y2; xz = quat->x * z2; yy = quat->y * y2; yz = quat->y * z2; zz = quat->z * z2; wx = quat->w * x2; wy = quat->w * y2; wz = quat->w * z2; m[0][0] = 1.0 - (yy + zz); m[1][0] = xy - wz; m[2][0] = xz + wy; m[3][0] = 0.0; m[0][1] = xy + wz; m[1][1] = 1.0 - (xx + zz); m[2][1] = yz - wx; m[3][1] = 0.0; m[0][2] = xz - wy; m[1][2] = yz + wx; m[2][2] = 1.0 - (xx + yy); m[3][2] = 0.0; m[0][3] = 0; m[1][3] = 0; m[2][3] = 0; m[3][3] = 1; }

SLERP-Spherical Linear interpolation Interpolate between two quaternion rotations along the shortest arc. SLERP(p,q,t)= p sin((1-t) θ)+q sin(t θ) sin(θ) where cos(θ)=w p w q +v p v q =w p w q +x p x q +y p y q +z p z q If two orientations are too close, use linear interpolation to avoid any divisions by zero.