Game Development Reference
In-Depth Information
Gl.glVertex3d( 100, 100, 0); // top right
Gl.glVertex3d( 100, -100, 0); // bottom right
Gl.glVertex3d(-100, -100, 0); // bottom left
These two triangles make a full square. Not all game sprites will be perfect
squares. Therefore, it's important to be able to specify the width and height. The
current quad is 200 by 200. OpenGL doesn't have explicit dimensions; the 200
can be whatever unit you likeā€”feet, meters, etc. The screen has been set up so
that 200 OpenGL units map up to 200 pixels, but that isn't always guaranteed.
Currently the width, height, and position of the sprite are all hard coded. These
magic numbers need to be replaced with variables.
double height = 200;
double width = 200;
double halfHeight = height / 2;
double halfWidth = width / 2;
Gl.glBegin(Gl.GL_TRIANGLES);
{
Gl.glVertex3d(-halfWidth, halfHeight, 0); // top left
Gl.glVertex3d( halfWidth, halfHeight, 0); // top right
Gl.glVertex3d(-halfWidth, -halfHeight, 0); // bottom left
Gl.glVertex3d( halfWidth, halfHeight, 0); // top right
Gl.glVertex3d( halfWidth, -halfHeight, 0); // bottom right
Gl.glVertex3d(-halfWidth, -halfHeight, 0); // bottom left
}
The values are exactly the same as before, but now the height and width are
stored as variables following the DRY principle. The height and width can now
be altered easily to form any number of interestingly shaped rectangles.
Positioning the Sprite
The sprite's size is now easy to modify, but there's no way to modify the position.
Changing the position is what will be added next.
double x = 0;
double y = 0;
double z = 0;
 
Search WWH ::




Custom Search