# glViewport like camera

I'm using glViewport as a camera of sorts, but I'm not quite sure how it works. For example when I change do:

glOrtho(0, 800, 600, 0, -1, 1);
glViewport(0, 0, 400, 200);


It makes the viewing area larger rather than just viewing a 400x200 area...

glViewport sets the viewport of your application. The viewport should be set to the size of your screen, window, canvas (for webgl) or texture you are rendering to.

glOrtho sets the selected matrix parallel projection matrix, which can be used as a camera. Calling it like in the following sample lets you control the position with the xand y variables while allowing you to change the size with the widthand height variables.

float left = x - width / 2;
float right = x + width / 2;
float bottom = y + height / 2;
float top = y - width / 2;
glOrtho(left, right, bottom, top, -1, 1);


glViewport is a completely different transformation than you think it is. What glViewport actually does is re-map the scene after projection.

In OpenGL, the scene always projects to a special coordinate space where the viewing volume is defined by a cube: [-1,1] (following division by W). The location (-1,-1,-1) is the bottom-left of your viewport at the near clip plane. The location (1,1,1) is the top-right of your viewport at the far clip plane. Your projection matrix produces coordinates in a special coordinate space called clip-space, and the clip-space coordinates are divided by W to produce the NDC coordinates described above.

Thus, the only thing that glViewport actually does is define where within your window that your projected image is displayed. It will effectively stretch the scene, and I do not think that is what you want. To display a 400x200 area, you need to alter your projection matrix as well so that you do not simply compress your projected scene into a smaller area.

glOrtho    (0, 400, 200, 0,    -1, 1);