## Friday, May 31, 2013

### Modern OpenGL tutorial Rotation, Scalling and Translation Matrix using GML (OpenGL Mathematics)

In previous article perspective projection I have shown you how to do perspective projection. Now its time to do Scalling, Translation and Rotation which are very important task. In ancient version of OpenGL for these operation opengl provide funcition like glRotate, glScale, glTranslate but in modern opengl this function are not available so we are using GLM (OpenGL Mathematics) for doing such task. From this article I have change the source code with OOP. I have made two reusable classes Program and Shader.

All the code in this series of articles is available from github: https://github.com/smokindinesh/Modern-OpenGL-Series  You can download a zip of all the files from that page, or you can clone the repository if you are familiar with git.

GLM function for rotation, scalling and translation is given below.
```// Projection matrix : 45° Field of View, aspect ratio, display range : 1 unit <-> 200 units
projectionMatrix = glm::perspective(45.0f, (float)SCREEN_SIZE.x / (float)SCREEN_SIZE.y, 1.0f, 200.0f);
glUniformMatrix4fv(program->uniform("prespective"),1,GL_FALSE,glm::value_ptr(projectionMatrix));
// Camera matrix
viewMatrix       = glm::lookAt(
glm::vec3(0,0,1), // Camera is at (4,3,3), in World Space
glm::vec3(0,0,0), // and looks at the origin
glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
);

glUniformMatrix4fv(program->uniform("camera"),1,GL_FALSE,glm::value_ptr(viewMatrix));
// Model matrix : an identity matrix (model will be at the origin)
modelMatrix= glm::mat4(1.0f);// Changes for each model !
modelMatrix= glm::translate(modelMatrix,glm::vec3(0,0,-5));//translating to negative z-axis
modelMatrix= glm::scale(modelMatrix,glm::vec3(2,2,2));//scalling by 2x
modelMatrix= glm::rotate(modelMatrix, -rotationAngle, glm::vec3(0,0,1));//rotating in clockwise direction

glUniformMatrix4fv(program->uniform("model"),1,GL_FALSE,glm::value_ptr(modelMatrix));
```
For continuous rotaton, to update the value of  rotationAngle I have made the update function.
```    // update the scene based on the time elapsed since last update
void Update(float secondsElapsed) {
const GLfloat degreesPerSecond = 100.0f;
rotationAngle += secondsElapsed * degreesPerSecond;
while(rotationAngle > 360.0f) rotationAngle -= 360.0f;
}
```

Next Chapter: Playing with Color