In OpenGL the viewing volume determines what you can see on the screen. An OpenGL viewing volume can have one of two projections: perspective and orthographic. 3D games normally use perspective projection. With perspective projection objects appear larger the closer they get to the camera. For a 2D game you want to use orthographic projection, where objects stay the same size no matter how far away they are from the camera.
To create a viewing volume with an orthographic projection, call the function gluOrtho2D(). This function is part of the OpenGL Utility Library (GLU). GLU is part of Apple’s OpenGL framework, but you must include the header file glu.h to use GLU functions. The gluOrtho2D() function takes four arguments: left, right, bottom, and top.
How large a viewing volume should you supply to gluOrtho2D()? You can make the viewing volume as large as you want, but there are two common ways to create an orthographic viewing volume. First, you can decide how many tiles you want to appear on the screen and supply that to gluOrtho2D(). Specifying the viewing volume in terms of tiles will make your life easier for a tile-based game. You can use tiles as the basic unit of measurement in the game world, which makes things like keeping the player from walking through walls easier to code. The following call displays 40 tiles horizontally and 30 tiles vertically in the viewing volume:
gluOrtho2D(0.0, 40.0, 0.0, 30.0);
What is cool about orthographic projections for 2D games is the screen size is the same no matter what the player’s screen resolution is. In the previous example, 40 tiles will fit across the screen on a 12-inch laptop display and on a 30-inch widescreen monitor.
The second common way to build a viewing volume is to decide how many “pixels” you want on the screen. The following call displays a 640 by 480 view volume:
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
The reason why I put the term pixels in quotation marks in the last paragraph is that the viewing volume isn’t specified in pixels. The monitor’s screen resolution determines the number of pixels on the screen. If the player’s screen resolution is 1280 by 1024 pixels, each horizontal unit in a 640 by 480 view volume corresponds to two pixels (640 units and 1280 pixels). A 640 by 480 view volume lets your game treat the screen as if it has a 640 by 480 screen resolution.
Next (Creating the Texture Map)