420 likes | 653 Views
Transformation of objects 2D transformations 3D transformations Matrix representation OpenGL functions. Rendering Pipeline. Rendering Pipeline. Basic 2D Transformations. Basic 2D Transformations. Matrix representation. Combination of 2D transformations. 2x2 matrices. 2x2 matrices.
E N D
Transformation of objects 2D transformations 3D transformations Matrix representation OpenGL functions
2x2 matrices • Only rotation, scaling, and shear can be represented in 2x2 matrix form. • These are linear transformations.
Rotation about an arbitrary point Alternative Form for a Rotation About a Point. From Example 5.2.5, we have Qx= cos() Px - sin() Py + dx = cos() Px - sin() Py - cos() Vx + sin() Vy + Vx = cos()( Px – Vx) - sin() (Py – Vy) + Vx Qy is same as Qx.
P=(4,7) 45 degrees about V(5,4)
OpenGL Matrix Stacks OpenGL works with stacks of 4x4 matrices: glMatrixMode(enum mode); mode: GL_MODELVIEWGL_PROJECTIONGL_TEXTURE Specifies whether the modelview, projection or texture matrix will be modified.
OpenGL Matrix Stacks glLoadIdentity(void); Clears the currently modifiable matrix for future transformation instructions. Typically we call this instruction before specifying modeling, viewing or projection transformations.
OpenGL Matrix Stacks Manipulating stacks: the OpenGL calls to push, pop, or multiply top of stack glLoadMatrix {fd}(const TYPE *m); sets 16 value of the current matrix glMultMatrix {fd}(const TYPE *m)); multiply by m glPushMatrix(void); the topmost matrix is copied glPopMatrix(void); the topmost matrix is destroyed All vertices of the object are multiplied by the matrix.
OpenGL Matrix Stacks Manipulating stacks: the OpenGL calls to push, pop, or multiply top of stack glLoadMatrix(const type *m); glMultMatrix(const type *m); m1 m5 m9 m13 m2 m6 m10 m14 m3 m7 m11 m15 m4 m8 m12 m16
OpenGL Transformations glTranslate(TYPE x, TYPE y, TYPE z); Multiplies the current matrix by a translation matrix. glRotate(TYPE angle, TYPE x, TYPE y, TYPE z); Multiplies the current matrix by a rotation matrix. glScale(TYPE x, TYPE y, TYPE z); Multiplies the current matrix by a scaling matrix.
//additional methods for Canvas class //components of 2D transformation //<<<<<<<<<<<<<<< initCT >>>>>>>>>>>>>>>>> void Canvas:: initCT(void) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // set CT to the identity matrix } //<<<<<<<<<<<<<< scale2D >>>>>>>>>>>>>>>>>>>> void Canvas:: scale2D(double sx, double sy) { glMatrixMode(GL_MODELVIEW); glScaled(sx, sy, 1.0); // set CT to CT * (2D scaling) } //<<<<<<<<<<<<<<< translate2D >>>>>>>>>>>>>>>>> void Canvas:: translate2D(double dx, double dy) { glMatrixMode(GL_MODELVIEW); glTranslated(dx, dy, 1.0); // set CT to CT * (2D translation) } //<<<<<<<<<<<<<<<< rotate2D >>>>>>>>>>>>>>>>>>>> void Canvas:: rotate2D(double angle) { glMatrixMode(GL_MODELVIEW); glRotated(angle, 0.0, 0.0, 1.0); // set CT to CT * (2D rotation) }
void Canvas:: pushCT(void) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); // push a copy of the top matrix } void Canvas:: popCT(void) { glMatrixMode(GL_MODELVIEW); glPopMatrix(); // pop the top matrix from the stack }