Game Development Reference
In-Depth Information
private void Setup2DGraphics(double width, double height)
{
double halfWidth = width / 2;
double halfHeight = height / 2;
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Gl.glOrtho(-halfWidth, halfWidth, -halfHeight, halfHeight, -100, 100);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
}
This code has a lot of new OpenGL functions, but they're all straightforward.
OpenGL has a number of matrix modes. The value
GL_PROJECTION
changes
the OpenGL state. Once the state is changed, all OpenGL commands will affect
the projection matrix. This matrix can now be altered to set up an orthographic
projection matrix.
glLoadIdentity
clears the current projection information. The next com-
mand
glOrtho
, sets up an orthographic projection matrix. There are six argu-
ments for this function.
void glOrtho(GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble nearVal,
GLdouble farVal);
The first four arguments describe how big you want the view of the world to be.
Figure 6.4 shows the orthographic projection and how the six arguments affect it.
At the moment, the origin is right in the center of the screen. I've decided to keep
that. To make the origin the top-left corner, you could write the following.
Gl.glOrtho(0, width, -height, 0, -100, 100);
The final two values are the near and far planes. If the z position of a vertex is
greater than the far plane, the vertex isn't rendered. If it's lower than the near
plane, then it also isn't rendered. Generally, 2D graphics have the z position all
set to 0 so the near and far planes don't really matter. They're much more im-
portant for rendering 3D graphics.
The setup function can be called in the Form.cs constructor.
Search WWH ::
Custom Search