Changing From Perspective to Orthographic Matrix
Asked Answered
F

1

16

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, 
};
Floss answered 5/3, 2012 at 20:49 Comment(0)
T
16

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.

Tiana answered 5/3, 2012 at 21:10 Comment(2)
@Nico_Schertler Thanks, i am really stupid :\ That worked! if i could ask you just one more thing - i tried completely removing the model and view matrices and everything is still displayed properly! Do i need only Projection matrix when i am using orthographic? Sorry if the question is little bit confusing, i've just started with openGL few days ago. Thanks alot!Floss
The default untransformed coordinate system is top left (-1,1), bottom right (1,-1). z-coordinate depends on whether you use a RH or LH coordinate system. If this extent fits your needs, you can leave the projection matrix at identity and use only the view matrix to navigate the scene. Otherwise you can use the projection matrix to scale the scene etc.Tiana

© 2022 - 2024 — McMap. All rights reserved.