Search code examples
pythonopenglpyopengl

PyopenGL - how to move camera frustrum to change perspective in normalized device coordinates?


I want to render some stuff from a bird perspective with Pyopengl.

The "bird" camera should be able to move in X, Y a rotate around Z vector.

It works, but I would like to place the "bird" a the bottom of the screen, because the top of the screen should be in direction of movement.

However, this option is not pleasant. What I would really prefer, is to still look down (Z direction), but with some kind of offset.

In other words, it should look like I crop the lower part of screen and use only the upper part.

The problem is illustrated in the following figure:

enter image description here

(the original figure was taken from http://www.songho.ca/opengl/gl_projectionmatrix.html)

How can I achieve that?


Solution

  • Finally I find the solution. The magic trick is the following command:

    glViewport (offset_X, offset_Y, screen_X, screen_Y)
    

    where:

    • offset_X - offset of the camera in X axis
    • offset_Y - offset of the camera in Y axis
    • screen_X - screen resolution you use in X axis
    • screen_Y - screen resolution you use in Y axis

    For case described in question (if the camera should be placed 25% of the screen height from the bottom of the screen):

    gluPerspective(45, (screen_X / screen_Y), 0.1, 2000.0)
    glViewport (0, -int(screen_Y*0.75), screen_X, int(screen_Y*1.75) )