OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360/4360.

Slides:



Advertisements
Similar presentations
Computer Graphics - Transformation -
Advertisements

Figures based on regular polygons (lab3)
Based on slides created by Edward Angel
Modeling Objects by Polygonal Approximations
OpenGL and Projections
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
OpenGL (II). How to Draw a 3-D object on Screen?
2D Transformations. World Coordinates Translate Rotate Scale Viewport Transforms Hierarchical Model Transforms Putting it all together.
Transformations of Objects CVG lab. Introduction  Affine transformations : Affine transformations are a fundamental cornerstone of computer graphics.
Fundamentals of Computer Graphics Part 4
OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360.
Graphics Graphics Korea University kucg.korea.ac.kr Transformations 고려대학교 컴퓨터 그래픽스 연구실.
Geometric transformations The Pipeline
PPT&Programs&Labcourse 1.
Computer Graphics Bing-Yu Chen National Taiwan University.
16/5/ :47 UML Computer Graphics Conceptual Model Application Model Application Program Graphics System Output Devices Input Devices API Function.
Modeling with OpenGL Practice with OpenGL transformations.
Computer Graphics I, Fall 2010 OpenGL Transformations.
2 COEN Computer Graphics I Evening’s Goals n Discuss viewing and modeling transformations n Describe matrix stacks and their uses n Show basic geometric.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 OpenGL Transformations.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
Affine Transformation. Affine Transformations In this lecture, we will continue with the discussion of the remaining affine transformations and composite.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.
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
University of New Mexico
Chap 3 Viewing and Transformation
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 73 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 73 Computer Graphics Three-Dimensional Graphics II.
1 OpenGL Transformations. 2 Objectives Learn how to carry out transformations in OpenGL ­Rotation ­Translation ­Scaling Introduce OpenGL matrix modes.
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
Homogeneous Coordinates and Matrix Representations Cartesian coordinate (x, y, z) Homogeneous coordinate (x h, y h, z h, h) Usually h = 1. But there are.
1 Geometric Transformations-II Modelling Transforms By Dr.Ureerat Suksawatchon.
Geometric Transformations. Transformations Linear transformations Rigid transformations Affine transformations Projective transformations T Global reference.
1 Geometric Transformations Modelling Transforms By Dr.Ureerat Suksawatchon.
Geometric Transformations Ceng 477 Introduction to Computer Graphics Computer Engineering METU.
Implement of transformation,projection, viewing Hanyang University Jungsik Park.
OpenGL LAB III.
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Viewing.
OpenGL Transformations
OpenGL Transformations
Computer Viewing.
Camera Position (5.6) we specify the position and orientation of the camera to determine what will be seen. use gluLookAt (eye x, y, z, at x, y, z, up.
2D Imaging and Transformation
Isaac Gang University of Mary Hardin-Baylor
OpenGL Transformations
Computer Graphics OpenGL Transformations
Reference1. [OpenGL course slides by Rasmus Stenholt]
Introduction to Computer Graphics with WebGL
Advanced Graphics Algorithms Ying Zhu Georgia State University
CSC461: Lecture 19 Computer Viewing
OpenGL Transformations
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Unit-5 Geometric Objects and Transformations-II
Computer Graphics (Spring 2003)
Geometric Transformations
Lecture 6 and 7 Transformations
Transformations 고려대학교 컴퓨터 그래픽스 연구실 kucg.korea.ac.kr.
Transformations in OpenGL
Geometric Objects and Transformations (II)
Advanced Graphics Algorithms Ying Zhu Georgia State University
OpenGL Transformations
OpenGL Transformations
Computer Graphics Transformations
Geometry Objects and Transformation
Chapter 4: Geometry.
Geometry Objects and Transformation
Presentation transcript:

OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360/4360

Overview Saw transformation “in theory” last time, now OpenGL focus –OpenGL transformations, matrices and coordinate systems Transformations in OpenGL –Rotation –Translation –Scaling –… and others Look at OpenGL matrix modes –Model-view –Projection

OpenGL Transformations, 1/2 Series of “viewing transformations” –transforms a point (its coordinates) from world space to eye space … to window coord –Set these transformation matrices as part of OpenGL programming –Each transformation operates on different spaces and coordinates Model – View – Projection – Perspective Divide - Viewport From “red book”:

OpenGL Transformations, 2/2 Recall - viewing process has 2 parts: –Use model-view matrix to switch vertex reps. from object frame in which objects defined to their representation in eye/camera frame – eye at origin Allows use of canonical viewing procedures –Type of projection (parallel or perspective) and part of world to image (clipping or view volume) Specifications allow formation of a projection matrix concatenated with model-view matrix

Recall, Coordinate Systems in Viewing Coordinate Systems in the Graphics Pipeline –OCS – object coordinate system –WCS – world coordinate system –VCS – viewing coordinate system –CCS – clipping coordinate system –NDCS - normalized device coordinate system –DCS – device coordinate system And images are formed on the image plane

Transformations and Coordinate Systems Coordinate Systems in the Graphics Pipeline –OCS – object coordinate system –WCS – world coordinate system –VCS – viewing coordinate system –CCS – clipping coordinate system –NDCS - normalized device coordinate system –DCS – device coordinate system Series of “viewing transformations” –transforms a point (its coordinates) from world space to eye space –Set these transformation matrices as part of OpenGL programming –Each transformation operates on different spaces and coordinates Model – View – Projection – Perspective Divide - Viewport

OpenGL Matrix Programming Arch. In OpenGL matrices are part of the state Multiple types of matrices –Model-View ( GL_MODELVIEW ) –Projection ( GL_PROJECTION ) –Texture ( GL_TEXTURE ) (ignore for now) –Color( GL_COLOR ) (ignore for now) Select which to manipulated by – glMatrixMode(GL_MODELVIEW); – glMatrixMode(GL_PROJECTION); Stack Current Stack Current Load Matrix Vertices 3D Model Vertices 3D 2D ModelviewProjection Matrix Mode

Current Transformation Matrix (CTM) So, far have thought only in OpenGL terminology –… which is fine –More general “computer graphics” term for the concatenation of the modelview and projection matrix is “current transformation matrix” Angel describes the current transformation matrix (CTM) as 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” CTMvertices p p’=Cp C

CTM and OpenGL Matrix Operations Summary Again, same as for OpenGL matrices CTM and OpenGL matrices can be altered either by loading a new matrix 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  C R Postmultiply by a scaling matrix: C  C S

Example – Object Rotation, 1/1 (recall) Rotation of object (at -4.0, -5.0, -6.0) by 45 degrees Recall, rotation “rotates” everything in coordinate system –Here, want to just rotate object –So, move to origin, rotate, move back Get things set up –(save, if need be) –glMatrixMode(GL_MODELVIEW) –glLoadIdentity(); Move the fixed point to the origin: –glTranslatef(4.0, 5.0, 6.0); Rotate about the origin: –glRotatef(45.0, 1.0, 2.0, 3.0); Move the fixed point back again: –glTranslatef(-4.0, -5.0, -6.0);

Example - Postmultiplication, 2/2 However, recall that OpenGl operates by postmultiplication –The expression (A(B(C))) is evaluated in the order –B * C, then A times that product So, order in program is reversed (gotcha) The “conceptual” sequence: –A - Move the fixed point to the origin: glTranslatef(4.0, 5.0, 6.0); –B - Rotate about the origin: glRotatef(45.0, 1.0, 2.0, 3.0); –C - Move the fixed point back again: glTranslatef(-4.0, -5.0, -6.0); Requires the code: –C - Move the fixed point back again: glTranslatef(-4.0, -5.0, -6.0); –B - Rotate about the origin: glRotatef(45.0, 1.0, 2.0, 3.0); –A - Move the fixed point to the origin: glTranslatef(4.0, 5.0, 6.0);

Matrix Stacks In many situations want to save transformation matrices for use later –Traversing hierarchical data structures (Chapter 10) –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() Nothing fancy here … –Just convenient place to save –All matrices have associated stack Stack Current Stack Current Load Matrix Vertices 3D Model Vertices 3D 2D ModelviewProjection Matrix Mode

Matrix Queries and Arbitrary Matrices Recall, OpenGL query functions for determining state – glGetIntegerv – glGetFloatv – glGetBooleanv – glGetDoublev – glIsEnabled – Etc. For matrix query (returning entire matrix), use – double m[16]; – glGetFloatv(GL_MODELVIEW, m); 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

Using Transformations Angel Example Angel example: –use idle function to rotate a cube –mouse function to change axis around which rotation occurs rotation Start with a program that draws a cube ( colorcube.c ) in a standard way –Centered at origin –Sides aligned with axes void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); glutMainLoop(); }

Idle and Mouse callbacks void mouse(int btn, int state, int x, int y) // change direction of rotation, axis is global { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; } void spinCube() /* idle function, so continually called */ /* for rotation, change only array theta[axis] */ { theta[axis] += 2.0; // globals if( theta[axis] > ) theta[axis] -= 360.0; glutPostRedisplay(); // cause redraw } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // rotate for each of x, y, z, // but only theta[axis] changes … obscure glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); colorcube(); glutSwapBuffers(); } void colorcube() // draw the cube

Model-view and Projection Matrices In OpenGL the model-view matrix is used to –Position the camera Can be done by rotations and translations but is often easier to use gluLookAt –Build models of objects The projection matrix is used to define the view volume and to “select a camera lens” (set frustum or gluperspective) Although both are manipulated by the same functions, have to be careful because incremental changes are always made by postmultiplication –For example, rotating model-view and projection matrices by the same matrix are not equivalent operations –Postmultiplication of the model-view matrix is equivalent to premultiplication of the projection matrix

Some OpenGL Matrix Operations Again, matrices are 1-dim arrays type GLfloat in column major order glLoadIdentity(); –Loads an identity matrix onto the top of the current stack glLoadMatrixf(pointer_to_matrix); –Loads arbitrary matrix onto top of the current stack glMultMatrixf(pointer_to_matrix); –Postmultiplies current matrix by arbitrary matrix glTranslatef(dx, dy, dz); –Multiplies current matrix with translation matrix. dx, dy, and dz are translations along x,y, and z axes. glRotatef(angle, x, y, z); –Multiplies current matrix with rotation about the line from the origin through the point (x, y, z) by angle. glScalef(sx, sy, sz); –Multiplies current matrix with scaling matrix. sx, sy, sz are the scale factors along the x, y, and z axes.

End.