2D Game Development with OpenGL – Drawing a basic tank
This post will cover the very basics of how we will draw a tank, made up of 2 squares, one for the tank body and one for the tank turret. We will be drawing the tank the same way we drew the big white square, but by re-sizing and changing its colour!
Pretty simple.
There is only a small change to the original code. The addition of a new square for the turret and a change in colour for both squares along with a change in size so that they look like a tank and a turret.
In Player.cpp the draw method will change to look like this:
/**
<Insert obvious comment here>
*/
void Player::draw()
{
glPushMatrix();
glTranslatef(x, y, 0);
// Draw the tank bottom
glColor3f(0.0f, 0.2f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.05f, 0.0f, 0.0f);
glVertex3f(0.05f, 0.1f, 0.0f);
glVertex3f(0.0f, 0.1f, 0.0f);
glEnd();
// Draw turret
glColor3f(0.0f, 0.15f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(0.02f, -0.04f, 0.0f);
glVertex3f(0.03f, -0.04f, 0.0f);
glVertex3f(0.03f, 0.05f, 0.0f);
glVertex3f(0.02f, 0.05f, 0.0f);
glEnd();
glPopMatrix();
}
The first new function added is glPushMatrix. This is so anything we change will only apply to the tank as we call glPopMatrix to pop the stored matrix from the stack once we are finished with our tank drawing code. Also introduced is glTranslatef which will be explained if you click the link ;D. glColor3f sets the colour which will be used within the next glBegin and glEnd calls. glVertex3f defines the 4 points of the rectangles which will represent our tank body and turret.
If you save and run the code as it is, you should get something a little like this:
See, it kinda looks like a tank facing south, kinda.. But that’s as graphically pleasing as it really needs to be for now!
The next post will demonstrate how to handle input and move the tank

