OpenGL/GLM Transformation

 // MVP matrix
 Projection = glm::perspective(g_fovy, g_aspect, g_zNear, g_zFar);
 View = glm::lookAt(g_eye, g_at, g_up);
 spMain.useProgram();
 spMain.setUniform(“gProjection”, Projection);
 spMain.setUniform(“gView”, View);

 // p’ = M3 * M2 * M1 * p (OpenGL uses Column-Major Order)
 glm::mat4 Tx = glm::translate(glm::mat4(1.0f), glm::vec3(3.0f, 0.0f, 0.0f)); // RHS x+ right
 glm::mat4 Rz = glm::rotate(glm::mat4(1.0f), 45.0f, glm::vec3(0.0f, 0.0f, 1.0f)); // RHS z+ (X->Y rotation)
 glm::mat4 S = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f, 0.7f, 1.0f)); // RHS

 World = glm::mat4(1.0f);
 spMain.setUniform(“gModel”, World);
 cube1->draw();

 
 // p’= R T p (red) => translate, and then rotate
 glm::mat4 RT = Rz * Tx; // Translate X, and then Rotate Z
 World = RT;
 spMain.setUniform(“gModel”, World);
 cube2->draw();

 
 // p’= T R p (green) => rotate, and then translate
 glm::mat4 TR = Tx * Rz; // Rotate Z, and then Translate X
 World = TR;
 spMain.setUniform(“gModel”, World);
 cube3->draw();

 
 // p’= T R S p (blue) => scale, and then rotate, and then translate
 glm::mat4 TRS = Tx * Rz * S; // Scale XY, and then Rotate Z, and then Translate X
 World = TRS;
 spMain.setUniform(“gModel”, World);
 cube4->draw();

사용자 삽입 이미지