# 24. OpenGL Camera Part 3

In this OpenGL tutorial, we are going to look at creating a third person camera. I searched long and hard on the internet for a way to do this using my old calculations or something at least similar, but I had no luck, so here is what I hope you will find to be a simple way around creating this.

First off, we are going to start off with the code from the second camera tutorial. I am going to start this tutorial, by adding only one new variable for our program, this is going to be the distance that the camera will be from our character/target object:

Next off, I am going to remove the camera() function. For our third person camera to work, we need to work our camera in two different parts, first is the part that centres the character, and the second part is the part that draws the scenery, so one camera function is not sufficient as our camera code goes around our other code.

The first thing we need to do after our glLoadIdentity call, is to translate our camera behind our character/target by our cRadius value like so:

Running this, you will see a red square in front of the view. Although now we need to add some rotation ^_^ The next line here is going to rotate our view for *both* our character and our scenery, upon creating this tutorial, it is this little bit that kept messing me up, I had seperate glLoadIdentity calls for our character camera, and our scene camera, but I realised I had to combine the two. This is going to rotate the view on the x axis so you can look down and up around the character (including seeing the top and bottom of the character):

glRotatef(xrot,1.0,0.0,0.0);

The next section of code in this tutorial is just to draw our character, we do this like so:

glColor3f(1.0f, 0.0f, 0.0f);
glutSolidCube(2); //Our character to follow

Now we need to rotate the camera on our y axis to look around the world. In this tutorial, the character/target rotates with the view so you will only ever see the back of it unless you look up and over the character:

glRotatef(yrot,0.0,1.0,0.0); //rotate our camera on the y-axis (left and right)

After this, we need to move our scene like in our previous tutorials, only this time, we are removing the ypos value, this will move the scenery as if you were using a first person camera, but because of the previous rotations and translations, our character/target will always be in the centre of the view:

glTranslated(-xpos,0.0f,-zpos); //translate the screen to the position of our camera

Then finally we just draw all our cubes ^_^

glColor3f(1.0f, 1.0f, 1.0f);
cube(); //call the cube drawing function

This camera not only supports basic movement, it also keeps in tact our strafing movements ^_^ I am still yet to add jumping, running and a bobbing effect (using cycloids).

If you have any questions, email me at swiftless@gmail.com