OpenGL Transformation

// OpenGL 행렬는 column-order이고, 벡터와 행렬의 곱은 Post-multiply 방식 사용

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 1.0);
glutWireCube(1);

 

// p’= R T p (red) => translate, and then rotate
glPushMatrix();
glRotatef(45.0, 0.0, 0.0, 1.0);
glTranslatef(3.0, 0.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();

// p’= T R p (green) => rotate, and then translate
glPushMatrix();
glTranslatef(3.0, 0.0, 0.0);
glRotatef(45.0, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glutWireCube(1);
glPopMatrix();

// p’= T R S p (blue) => scale, and then rotate, and then translate

glPushMatrix();
glTranslatef(3.0, 0.0, 0.0);
glRotatef(45.0, 0.0, 0.0, 1.0);
glScalef(0.5, 0.7, 1.0);

glColor3f(0.0, 0.0, 1.0);
glutWireCube(1);

glPopMatrix();

////////////////////////////////////////////
// USING MATRIX
///////////////////////////////////////////
matrix4x4 M1, M2, M3, R, T, S;
R.rotate(45.0, ‘z’);
T.translate(vector3(1.5, 0.0, 0.0));
S.scale(vector3(0.5, 0.7, 1.0));

// p’= R T p (red) => translate, and then rotate
M1 = R * T;
glPushMatrix();
glMultMatrixf( M1 );
glColor3f(1.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();

// p’= T R p (green) => rotate, and then translate
M2 = T * R;
glPushMatrix();
glMultMatrixf( M2 );
glColor3f(0.0, 1.0, 0.0);
glutWireCube(1);
glPopMatrix();

// p’= T R S p (blue) => scale, and then rotate, and then translate
M3 = T * R * S;
glPushMatrix();
glMultMatrixf( M3 );
glColor3f(0.0, 0.0, 1.0);
glutWireCube(1);
glPopMatrix();


////////////////////////////////////////////
// USING glMultMatrixf
///////////////////////////////////////////

// p’= R T p (red) => translate, and then rotate
glPushMatrix();
glMultMatrixf( R );
glMultMatrixf( T );
glColor3f(1.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();


// p’= T R p (green) => rotate, and then translate
glPushMatrix();
glMultMatrixf( T );
glMultMatrixf( R );
glColor3f(0.0, 1.0, 0.0);
glutWireCube(1);
glPopMatrix();

// p’= T R S p (blue) => scale, and then rotate, and then translate
glPushMatrix();
glMultMatrixf( T );
glMultMatrixf( R );
glMultMatrixf( S );
glColor3f(0.0, 0.0, 1.0);
glutWireCube(1);
glPopMatrix();

  glutSwapBuffers();
}