Changing From Perspective to Orthographic Matrix

IanDess picture IanDess · Mar 5, 2012 · Viewed 17.1k times · Source

I have the scene with one simple triangle. And i am using perspective projection. I have my MVP matrix set up (with the help of GLM) like this:

glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
glm::mat4 View       = glm::lookAt(
    glm::vec3(0,0,5), // Camera is at (0,0,5), 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)
);  
glm::mat4 Model      = glm::mat4(1.0f);
glm::mat4 MVP        = Projection * View * Model;

And it all works ok, i can change the values of the camera and the triangle is still displayed properly.

But i want to use orthographic projection. And when i change the projection matrix to orthographic, it works unpredictable, i can't display the triangle, or i just see one small part of it in the corner of the screen. To use the orthographic projection, i do this:

glm::mat4 Projection = glm::ortho( 0.0f, 800.0f, 600.0f, 0.0f,-5.0f, 5.0f);

while i don't change anything in View and Model matrices. And i just doesn't work properly.

I just need a push in the right direction, am i doing something wrong? What am i missing, what should i do to properly set up orthographic projection?

PS i don't know if it's needed, but these are the coordinates of the triangle:

static const GLfloat g_triangle[] = {
    -1.0f, 0.0f, 0.0f, 
    1.0f, 0.0f, 0.0f,
    0.0f, 2.0f, 0.0f, 
};

Answer

Nico Schertler picture Nico Schertler · Mar 5, 2012

Your triangle is about 1 unit large. If you use an orthographic projection matrix that is 800/600 units wide, it is natural that you triangle appears very small. Just decrease the bounds of the orthographic matrix and make sure that the triangle is inside this area (e.g. the first vertex is outside of the view, because its x-coordinate is less than 0).

Furthermore, make sure that your triangle is not erased by backface culling or z-clipping. Btw.. negative values for zNear are a bit unusual but should work for orthographic projections.