Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing From Perspective to Orthographic Matrix

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, 
};
like image 488
IanDess Avatar asked Mar 05 '12 20:03

IanDess


People also ask

What is the transformation matrix for orthographic projection?

The Orthographic Projection Matrix Translate the volume defined by the createOrthographic parameters so that it is centered at the origin, then. Scale the volume to be a 2 unit wide cube using an appropriate scale factor for each axis, and then. Flip the z axis to match the clipping space's coordinate system.

Under what conditions a perspective projection becomes orthographic projection?

When projectors are perpendicular to view plane then is called orthographic projection.

What is perspective transformation matrix?

The homography or perspective transformation is defined by a matrix that defines a mapping of pixel coordinates: (6.4) Here are pixel coordinates in the output image, and are uniform pixel coordinates in the input image. The normal input pixel coordinates are given by. (6.5)

What is the difference between a perspective and orthographic drawings?

In the perspective view (the default), objects which are far away are smaller than those nearby. In the orthographic view, all objects appear at the same scale. Since some prefer one over the other, both options are available.


1 Answers

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.

like image 79
Nico Schertler Avatar answered Sep 21 '22 13:09

Nico Schertler