computer graphics course 2006
play

Computer Graphics Course 2006 OpenGL - Modeling and View ing - PowerPoint PPT Presentation

Computer Graphics Course 2006 OpenGL - Modeling and View ing Transformations OpenGL - Modeling View ing Transformations OpenGLs transformations operate on the vertices and are divided as follows:


  1. Computer Graphics Course 2006 OpenGL - Modeling and View ing Transformations

  2. OpenGL - Modeling View ing Transformations � OpenGL’s transformations operate on the vertices and are divided as follows: ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ X ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎛ ⎞ x ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ Viewport Projective Projection Modelview Y = • ° • • ⎜ ⎟ y ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ Transform Division Matrix Matrix Z ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎝ ⎠ z ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ − ⎠ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ ⎝ ation W � Notice that homogeneous coordinates are used.

  3. Modelview Matrix � Is a 4 by 4 matrix. � Is the first matrix that multiplies the vertices. � Its main purpose is: � modeling trans’ - position and orienting the models. � viewing trans’ - positioning and aiming the camera.

  4. Modelview Matrix � Modeling transformations: � Are used to move objects in the space without redefining their vertices values (useful for animations and convenient for defining objects around the origin).

  5. Modelview Matrix � Viewing transformations: � (we shall see next that OpenGL’s camera is positioned in the origin(0,0,0) facing the minus Z - axis (with the up vector (0,1,0)). z- x- x y z � In order to position a virtual camera in an arbitrary position and orientation, we need to “coincide” our virtual camera with OpenGL’s.

  6. Modelview Matrix � This is done by moving the world such that the virtual camera will be located at the origin. And rotate it such that it will face the minus Z axis (and have up vector(0,1,0)). Exactly as OpenGL’s camera.

  7. General OpenGL Matrices � glMatrixMode (GLenum mode) - specifies which matrix is being modified: � GL_MODELVIEW - the Modelview matrix � GL_PERSPECTIVE - the Perspective matrix � GL_TEXTURE - the Texture mapping matrix � glLoadI dentity () - Sets the identity matrix to the current matrix modified.

  8. General OpenGL Matrices � glLoadMatrix { fd} (const TYPE * m) - sets the current modified matrix to be the matrix defined in the input array m[i][j]. � glMultMatrix { fd} (const TYPE * m) - multiplies the current modified matrix with the matrix defined in the input array m[i][j]. (Right side multiplication). � m is defined as float/double m[4][4];

  9. Modeling Transformations � glTranslate { fd} (TYPE x, TYPE y, TYPE z) - Multiplies the current matrix by a matrix that moves (translates) vertices by x, y and z values. � How will such a matrix look like? + ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ 1 0 0 1 0 0 X X x x x ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ + ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ 0 1 0 0 1 0 y y Y Y y • = since : ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ + 0 0 1 0 0 1 z z Z Z z ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠ 0 0 0 1 0 0 0 1 1 1 � Question: In the above equations we took W= 1 what would happen if W was any non-zero (Real) number?

  10. Modeling Transformations � glRotate { fd} (TYPE angle, TYPE x, TYPE, y, TYPE z) - Multiplies the current matrix by a matrix that rotates the vertices by angle degrees around the axis (x, y, z). � Note that the vertices that lies further from the rotation axis will be affected more dramatically. � glScale { fd} (TYPE x, TYPE, y, TYPE z) - Multiplies the current matrix by a matrix that stretches/shrinks/reflects the vertices along the axes by the multiplication of the corresponding x, y and z. The matrix is defined as: ⎛ ⎞ x 0 0 0 � Question: what values for x,y and z will ⎜ ⎟ ⎜ ⎟ 0 y 0 0 stretch, shrink and reflect the object? ⎜ ⎟ 0 0 0 z ⎜ ⎟ ⎜ ⎟ ⎝ ⎠ 0 0 0 1

  11. Modeling Transformations � OpenGL’s matrix multiplication is right side: � glMatrixMode(GL_MODELVIEW) ; � glLoadIdentity() ; � glTranslatef(1.0, -1.0, 0.0) ; � glRotatef(45, 0.0, 1.0, 0.0) ; � glScalef(0.5, -1.0, 1.0) ; � Will result with: T(R(S(x))) = y. � The rule is: Last command operates first, First command operates last.

  12. Modeling Transformations � Does It matter? � Yep: Rotate Translate Rotate Translate � Remember Matrix multiplication do not generally AB ≠ BA commute ( ). � Question: Do the rotation matrices form a commutative (Abelian) group (what about the translation matrices).

  13. General OpenGL Matrices � glGetFloatv (GLenum pname,const TYPE * m) glGetDoublev (GLenum pname,const TYPE * m) - fills the array m with the state variable indicated by pname: � GL_MODELVIEW_MATRIX - the current modelview matrix � GL_PROJECTION_MATRIX - the current projection matrix � See more on OpenGL Programming Guide 1.1 pages 537+ � All the modelview operations multiply the current matrix from its right: M = > M * R/T/S. This has a geometrical meaning about the order of the transformations. � But what if we need to multiply a matrix from the left?

  14. General OpenGL Matrices � Multiplying the current matrix from its left: � double M[16] ; � glGetDoublev( GL_MODELVIEW_MATRI � M= C X, M) ; � glMatrixMode(GL_MODELVIEW) ; � C= Id The first C � glLoadIdentity() ; � C= Id* A= A � glRotate/glTranslate/glLoadMatrix(A) � glMultMatrix(M) ; � C= C* M= A* M= A* C

  15. View ing Transformations � As stated before, in order to change the position and orientation of the viewpoint, we can move and rotate the “world” (all the objects-vertices) instead of moving the camera. Y+ � Let us assume that the camera is: Z- � Positioned in the origin (0,0,0). � Looking at the -Z axis direction. X- X+ � Its up vector is (0,1,0) Z+ Y-

  16. View ing Transformations � Now, we can use glTranslate and glRotate to move the “world” and thus achieve the impression of a change of viewpoint. � For example lets say that we have some objects located at around the origin and we wish to move the camera away from the origin on the Z axis - that is we want to move the camera to Z- Z- the positive Z direction. Camera move − ( 0 , 0 , ) z Z+ Z+

  17. View ing Transformations � Considering the following equivalence, in terms of what the camera sees: Z- Z- Z- Objects Equivalent move ( 0 , 0 , ) z Z+ Z+ Z+ � This is a result of the fact that any rigid transformation on the objects and on the camera will not change what the camera views. � So all we have to do is glTranslatef(0,0,-z)! � Question: where in the code should we put this line?

  18. View ing Transformations � This applies also to the case of rotations: � In this case glRotate will be used. � Using glScale we can get camera zoom.

  19. View ing Transformations � gluLookAt (GLdouble eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz) - Is a GLU utility that: � Defines a viewing matrix of the desired viewpoint. � Multiplies it to the right of the current matrix (in our case the Modelview matrix).

  20. View ing Transformations � (eyex,eyey,eyez) - is the point where the desired camera is located. � (centerx,centery,centerz) - is a point in space to which the camera aims (will appear in the center of the projected image of the camera). � (upx,upy,upz) - is the camera up vector. This vector actually defines which direction in the world will be the up direction in the projected image of the camera: ( , , ) centerx centery centerz Image up direction ( , , ) upx upy upz Image center ( eyex , eyey , eyez )

  21. Projective Transformations � The projection matrix is responsible for the projective transformation: � glMatrixMode(GL_PROJECTION) ; � The camera model we will use in OpenGL are: � Pinhole perspective projection � Orthographic projection

  22. Projective Transformations � Pinhole Perspective Projection is when we − ℜ ℜ n 1 n project points in to along lines intersecting in a point (in the case of n= 2): Principal axis Center of projection Projection plane

  23. Projective Transformations � glFrustum (Gldouble left, right, bottom, top, near, far) - Create a matrix for a perspective view and multiplies the current matrix (near and far are non-zero positive).

  24. Projective Transformations � gluPerspective (GLdouble fovy, aspect, near, far) - Creates a matrix for symmetric perspecive view. � fovy is the angle of field of view in the X-Z plane [0…180]. � aspect is the aspect ratio of the frustum (its width/height television/computer screen is 4:3, Theatre wide screen is 16:9). � near and far are the distances from the clipping planes.

  25. Projective Transformations � glOrtho (GLdouble left, right, bottom, top, near, far) - Creates a matrix for an orthographic parallel viewing projection and multiplies the current matrix (In this case near and far can be also negative). direction of projection Projection plane

  26. Projective Transformations � gluOrtho2D (GLdouble left, right, bottom, top) - Creates a matrix for an orthographic parallel viewing projection and multiplies the current matrix. Where are near = -1.0 and far is 1.0. This is a simple case for 2D drawing using OpenGL. When glVertex2(x,y) is given a point with coordinate (x,y,0) is actually defined and thus it is not clipped when using gluOrtho2D.

Recommend


More recommend