lecture 5b - universiti teknologi malaysia · transformation in opengl void glrotated(gldouble ang,...
TRANSCRIPT
Lecture 5b
Transformation
RefresherTransformation matrices [4 x 4]:
the fourth coordinate is homogenous coordinate.
Rotation Transformation:
Axis of rotation must through origin (0,0,0). If not, translation is applied before and after.
Composite Transformation:
must be in sequence.
Principle of Transformation in OpenGLPushing the transformation to the stack and after
application, discard the transformation
glPushMatrix(), which copies the current matrix and adds the copy to the top of the stack
glPopMatrix(), which discards the top matrix on the stack,
Procedure in OPenGL
glPushMatrix( );
… intended transformation;
… apply the transformation to geometrical properties.
glPopMatrix();
Transformation in OpenGL
void glRotated(GLdouble ang, GLdouble x, GLdouble y, GLdouble z)
void glRotatef( GLfloat ang, GLfloat x, GLfloat y, GLfloat z )
computes a matrix that performs a counterclockwise rotation of angle degrees about the vector from the origin through the point (x, y, z).
*Take note: x,y,z is not axis.
Transformation in OpenGL
void glTranslated( GLdouble x, GLdouble y, GLdouble z )
void glTranslatef( GLfloat x, GLfloat y, GLfloat z )
x, y, z Specify the x, y, and z coordinates of a translation vector
Rotation at axis y at point (x1, y1, z1)
Mat1= translation (-x1, -y1, -z1);
Mat2= rotation (ang, 0.0, 1.0, 0.0);
Mat3= translation (x1, y1, z1);
Transformation in OpenGL
void glScaled( GLdouble x, GLdouble y, GLdouble z )
void glScalef( GLfloat x, GLfloat y, GLfloat z )
x, y, z Specify scale factors along the x, y, and z axes, respectively.
glScalef (1.0, 1.0 3.0);
glutWireCube (1.0);
Composite Matrices in OpenGL
In OpenGL, the order of the transformation appears in reverse order
first will appear last
Intended transformation :[M] = [M1][M2][M3]… [Mi]
In OpenGL, [M] = [Mi]…[M3][M2][M1]
i.e: rotation at different point will
glTranslatef(x1, y1, z1); MAT3
glRotatef(ang, x, y, z); MAT2
glTranslatef(-x1, -y1, -z1); MAT1
glPushMatrix();
glTranslatef(4.0, 0.0, 0.0);
glRotatef (45, 0.0, 0.0, 1.0);
glTranslatef(-4.0, 0.0, 0.0);
glBegin(GL_POLYGON);
…
glEnd();
glPopMatrix();
Composite matrix in reverse order
Start modeling
User Defined Matrices
◊ The constructed matrices must be 4 x 4 matrix in single array form a[16]
]15[a]11[a]7[a]3[a
]14[a]10[a]6[a]2[a
]13[a]9[a]5[a]1[a
]12[a]8[a]4[a]0[a
M
Glfloat m[16];
for (int i = 0; i < 16; i++)
m[i] = 0.0;
Loading and multiplying self defined matricesLoading:
void glLoadMatrixd( const GLdouble *m )
void glLoadMatrixf( const GLfloat *m )
Multiplying
void glMultMatrixd( const GLdouble *m )
void glMultMatrixf( const GLfloat *m )
i.e:glPushMatrix();
glTranslatef(x1, y1,z1);
glMulMatrix(m);
glTranslatef(-x1, -y1, -z1);
glPopMatrix();
Rotating square by theta
glBegin( GL_LINE_LOOP );
for (int i = 0; i < 360 ; i= i+90 )
{
v1x = 0.8*cos ( (i+theta)* pi/180);
v1y = 0.8*sin ( (i+theta)* pi/180);
glVertex2f( v1x, v1y);
}
glEnd();
glPushMatrix();glRotatef (theta, 0.0, 0.0, 1.0);
glBegin( GL_LINE_LOOP );
for (int i = 0; i < 360 ; i= i+90 ){
v1x = 0.8*cos ( i* pi/180);v1y = 0.8*sin ( i* pi/180);
glVertex2f( v1x, v1y);
}glEnd();
glPopMatrix();
Previous Method Transformation Method
Rotate by theta at Z axis
Advantage: Transformation method does not interfere with the modeling formula.
Source Code: transformation.cpp
xx1= 0.5* sin (deg_to_rad * theta)+4.0; yy1 = 0.5* cos(deg_to_rad * theta);
xx2= 0.5* sin (deg_to_rad * (theta+90))+4.0; yy2 = 0.5* cos(deg_to_rad * (theta+90));
xx3= 0.5* sin (deg_to_rad * (theta+180))+4.0; yy3 = 0.5* cos(deg_to_rad * (theta+180));
xx4= 0.5* sin (deg_to_rad * (theta+270))+4.0; yy4 = 0.5* cos(deg_to_rad * (theta+270));
glBegin(GL_POLYGON);
glVertex2f(xx1, yy1);
glVertex2f(xx2, yy2);
glVertex2f(xx3, yy3);
glVertex2f(xx4, yy4);
glEnd()
(4,0) (r*sin(0)+4 , r*cos (0))
(r*sin(90)+4 , r*cos (90))
(r*sin(180)+4 , r*cos (180))
(r*sin(270)+4 , r*cos (270))
*Rotation at (4,0) using variablesOrbital rotation using transformation
Procedure to rotate the square (orbital rotation)
1. Rotate the square at (4,0) with increment of 10
(theta)
Translate (-4, 0 , 0)
Rotate (theta, Z axis)
Translate ( 4,0,0)
2. When square rotates full 3600, the square will be rotated by 10 at (0,0)
Rotate (theta2, Z axis) OpenGLglRotatef (theta2, 0.0, 0.0, 1.0);glTranslatef (4.0, 0.0, 0.0);glRotatef (theta, 0.0, 0.0, 1.0); glTranslatef (-4.0, 0.0, 0.0);
Source Code: transformation.cppglPushMatrix();
glRotatef (theta2, 0.0, 0.0, 1.0);
glTranslatef (4.0, 0.0, 0.0);
glRotatef (theta, 0.0, 0.0, 1.0);
glTranslatef (-4.0, 0.0, 0.0);
glBegin(GL_POLYGON);
xx1= 0.5* sin (deg_to_rad * 0)+4; yy1 = 0.5* cos(deg_to_rad * 0);
xx2= 0.5* sin (deg_to_rad * (90))+4; yy2 = 0.5* cos(deg_to_rad * (90));
xx3= 0.5* sin (deg_to_rad * (180))+4; yy3 = 0.5* cos(deg_to_rad * (180));
xx4= 0.5* sin (deg_to_rad * (270))+4; yy4 = 0.5* cos(deg_to_rad * (270));
glVertex2f(xx1, yy1);
glVertex2f(xx2, yy2);
glVertex2f(xx3, yy3);
glVertex2f(xx4, yy4);
glEnd();
glPopMatrix();
Modeling and animation of 2D robot arm)
1. Model the robot arm with all the arm horizontallyTransformation required: translation
2. Rotate all the arm using the arm1 rotation angleTransformation required: rotation
3. Rotate the arm2 using the rotation angle arm2.Transformation required: translation before after, rotation
Initial modelingglBegin( GL_LINES );
glVertex2f( 0, 0);
glVertex2f( 5, 0);
glEnd();
glPushMatrix();
glTranslatef(5,0,0);
glBegin( GL_LINES );
glColor3f(0.0,1.0,0.0);
glVertex2f( 0, 0);
glVertex2f( 2, 0);
glEnd();
glPopMatrix();
L1: (0,0) to (5,0)
L2: (0,0) to (2,0)Translate the line by 5,0,0
glPushMatrix();
glRotatef(45,0,0, 1.0);
glBegin( GL_LINES );
glVertex2f( 0, 0);
glVertex2f( 5, 0);
glEnd();
glPushMatrix();
glTranslatef(5,0,0);
glBegin( GL_LINES );
glColor3f(0.0,1.0,0.0);
glVertex2f( 0, 0);
glVertex2f( 2, 0);
glEnd();
glPopMatrix();
glPopMatrix();
Rotation by 45o
Rotate L1 and L2 by 45o (Z axis)
glPushMatrix();glRotatef(45,0,0, 1.0);
glBegin( GL_LINES );glVertex2f( 0, 0);glVertex2f( 5, 0);glEnd();
glPushMatrix();glTranslatef(5,0,0);glRotatef(-15,0,0, 1.0);glTranslatef(-5,0,0);
glPushMatrix();glTranslatef(5,0,0);glBegin( GL_LINES );
glColor3f(0.0,1.0,0.0);glVertex2f( 0, 0);glVertex2f( 2, 0);
glEnd();glPopMatrix();
glPopMatrix();glPopMatrix();
Rotate L2 by -15o
Step1. Translate by -5,0,02. Rotate by -15 at Z axis3. Translate by 5,0,0