Search code examples
opengltransformationperspectivevanishing-point

OpenGL: perspective view centered not in the middle of the view?


I have a main scene centered on the center of the viewport
in addition to that I want another small object to be displayed on the corner of the viewport. The trouble is that when I draw the small object, it is transformed by the main projection transformation and appears slanted. I want the small object to have its own vanishing point centered in its center.
Is this possible which just transformations?


Solution

  • You want your main scene to be projected in one way and your corner object to be projected in another way. This directly leads you to the solution:

    void render() {
        glMatrixMode(GL_PROJECTION);
        setUpMainProjection();
        glMatrixMode(GL_MODELVIEW);
        drawMainObject();
    
        glMatrixMode(GL_PROJECTION);
        setUpCornerProjection();
        glMatrixMode(GL_MODELVIEW);
        drawCornerObject();
    }
    

    Perhaps you're wondering how to implement setUpCornerProjection. It would look something like this:

    // let's say r is a rect, which, in eye space, contains the corner object and is
    // centered on it
    glFrustum(r.left, r.right, r.bottom, r.top, nearVal, farVal);
    // let's say p is the rect in screen-space where you want to
    // place the corner object
    glViewport(p.x, p.y, p.width, p.height);
    

    And then in setUpMainProjection() you'd need to also call glFrustum and glViewport.