Search code examples
openglperspectiveorthographic

How do I find the projection matrix that is equivalent to an orthographic matrix but gives perspective when not in xy-plane?


I am messing around in a program that performs all rendering using a matrix set by gluOrtho2D(). What I would like to do is to rotate a specific texture around the y-axis (0,1,0) so that it looks like the original matrix was provided by gluPerspective() instead. I realize that this is not possible with an orthographic projection matrix and that I will have to set up a new perspective projection matrix. I want the textures I draw to look exactly the same as when drawn with the orthographic matrix, except sometimes when I have rotated them around the y-axis.

So to summarize:
How do I find a perspective projection matrix that is equivalent to a given orthographic matrix when shapes are drawn in the xy-plane, but gives me perspective when not in the xy-plane?


Solution

  • Basically what I understand is: You want to have a perspective matrix that yields the same image as the orthographic projection, when the object lies in the XY-Plane.

    So lets begin: You want to draw your plane in the XY-plane and you had gluOrtho2D(-w, w, -h, h); (asymmetric orthographic projection is a bit harder, if you need it, then say so). What you need to do now is:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(2 * atan(h / 2)  * 180.0 / PI, w / h, 1, 3);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0, 0, -2);
    

    What I have done here is matching the center of the projective view frustum on the section of the XY-plane that was visible in the orthographic projection.

    Take the graphs of the projection frustums and some math and you can do this yourself :)


    Just as a reminder I will leave my answer to the old question here.

    Either you have ortho or perspective projection. You have to decide for one. You will not gain perspective by rotating on any axis.

    Why would you have to change a whole program just to change the projection mode?

    One possible solution: I assume the old GL with different matrix modes and so on:

    glMatrixMode(GL_PROJECTION); //switch to projection matrix mode
    glPushMatrix(); //push the old matrix
    glLoadIdentity();
    //apply the perspective projection
    //do your rendering
    glPopMatrix(); //pop the old matrix
    

    With the newer GL scheme you will have to do most of this on your own but it should be easy though.