By the end of this tutorial you will have learned how to create and use orthographic projections in the LWJGL.
What is an “orthographic projection”?
An orthographic projection is way of mapping primitives to a canvas. Unlike a perspective projection, an orthographic projection does not give you the illusion of depth. Objects that are far away from the camera will not appear smaller than objects that are close to the camera. It is mainly used to form a rectangle that defines a 2D “camera”. A 2D camera could also be described as a rectangular mask.
How do I create an orthographic projection?
You have actually already created and used an orthographic projection using the glOrtho function. This method multiples the current projection matrix, a fancy name for a “way of mapping primitives to a canvas”, by an orthographic projection that it generates with the given parameters. You set the projection matrix to the orthographic projection by first calling glLoadIdentity and then calling glOrtho.
How should I use glOrtho?
With glOrtho you can construct the previously discussed rectangle by specifying its coordinates. The first and third parameter, left and bottom, specify the coordinates of the bottom-left corner of the camera and the second and fourth parameter, right and top, specify the coordinates of the top-right corner of the camera. The last two parameters are used for 3D projections, but that is beyond the scope of this tutorial.
Examples / Exercises
1. Let’s say you want (0, 0) to be in the top-left corner of the camera and you want (640, 480) to be in the bottom-right corner of the camera. This means that (0, 480) will be in the bottom-left corner and (640, 0) will be in the top-right corner. This is the code you would write before the game loop:
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, 640, 480, 0, 1, -1); glMatrixMode(GL_MODELVIEW);
2. Now let’s say you want (0, 0) to be in the middle of the camera and you want (1, -1) to be in the bottom-right corner. This means that (-1, -1) will be in the bottom-left corner and (1, 1) will be in the top-right one. This is the code you would write before the game loop:
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1, 1, -1, 1, -1, 1); glMatrixMode(GL_MODELVIEW);
This is, by the way, the default state of the projection matrix (what you get by calling glLoadIdentity).
Questions and remarks
If you didn’t understand something or have questions, please tell me what in the comments. Also, please tell me what you thought about this tutorial.