Sunday, May 19, 2013

Modern OpenGL tutorial Perspective Projection using GLM (OpenGL Mathematics)

In my previous article you may notice that in vertex data  for drawing a triangle, I have fixed the co-ordinate of z-axis to 1. If you change the value of z-axis greater than 1 or less than -1 then Image will not render on the screen because we haven't define the perspective projection matrix. In this project we are using GLM ,  in previous article there is no especial use of it so I haven't talk about it before but here we use GLM function for perspective projection .GLM is header only library made by heavy use of C++ templates due to which performance can be reduce but make programming easier. GLM can be use with GLSL easily so we are using GLM.

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

I will explain the only GLM part other things are similar to previous article . At last you can download the complete source code.
GLM variables are:
glm::vec2 SCREEN_SIZE(800, 600);//screen size
glm::mat4 projectionMatrix; // Store the projection matrix
glm::mat4 viewMatrix; // Store the view matrix
glm::mat4 modelMatrix; // Store the model matrix
Now defining the projectionMatrix , viewMatrix and modelMatrisx:
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
    projectionMatrix = glm::perspective(45.0f, (float)SCREEN_SIZE.x / (float)SCREEN_SIZE.y, 1.0f, 200.0f);
    // Camera matrix
    viewMatrix       = glm::lookAt(
    glm::vec3(0,0,3), // 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)
    // Model matrix : an identity matrix (model will be at the origin)
    modelMatrix      = glm::mat4(1.0f);  // Changes for each model !
    // Our ModelViewProjection : multiplication of our 3 matrices

    // Remember, matrix multiplication is the other way around
    glm::mat4 MVP        = projectionMatrix * viewMatrix * modelMatrix;

    // Get a handle for our "MVP" uniform.
    // Only at initialisation time.
    GLuint MatrixID = glGetUniformLocation(programId, "MVP");

    // Send our transformation to the currently bound shader,
    // in the "MVP" uniform
    // For each model you render, since the MVP will be different (at least the M part)
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
Now our vertex shader:

#version 150 core

in vec3 vPosition;
uniform mat4 MVP;

void main()
gl_Position = MVP*vec4(vPosition,1.0);
Now I will explain the code a little. In function glm::perspective first argument is angle made by viewer eye and screen. Try to change its value form 45 to other and see what difference you observe. Second argument is aspect ratio of your screen. Third and forth value is near and far points it means all vertex point between them are render other vertex point are not render. So your vertex point should not be too small than near value i.e 1 and too big than far value i.e 200.
The function glm:lookAt is used to setup camera matrix. First argument is camera vector, second eye vector and third one is up vector ( the direction of your head )

Next Chapter: Rotation,Translation and Scalling

No comments:

Post a Comment