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(1.5, 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(1.5, 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(1.5, 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();



 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.matrix() );
 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.matrix() );
 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.matrix() );
 glColor3f(0.0, 0.0, 1.0);
 glutWireCube(1);
 glPopMatrix();


 glutSwapBuffers();
}




사용자 삽입 이미지

Leave a Reply

Your email address will not be published. Required fields are marked *