When you see painter's algorithm (last object drawn appears in front), it means a problem with your depth buffer. In your case, you need to enable depth testing and writing and in your initialization (it's off by default):
glEnable(GL_DEPTH_TEST);
You'll also want to clear the depth buffer at the beginning of each frame (clearing the color buffer is usually a good idea as well):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Have a look at what the OpenGL wiki has to say about depth buffering.