post redisplay works but in the right place?

My program works however I feel like the glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutPostRedisplay(); functions are maybe not where they should be. When the user presses ‘w’ some variables change, and a redraw is necessary because the variables change the scene. Is the post redisplay fine where it is.

void keyboard(unsigned char key, int x, int y)
	switch ( key )
		case 27:
			exit( 0 );
		case 'x':
		case 'z':
		// Do nothing...

The glutPostRedisplay() call is fine where it is. The glClear() call should be in the display function.

More generally, all OpenGL calls should be in the initialisation code or in the display function. Calling glViewport() from the resize function is common, and it works for simple cases, but it’s better to just store the window dimensions and set the viewport (and projection matrix, if applicable) at the beginning of the display function. I can’t think of any cases where OpenGL calls should be made elsewhere (except perhaps for asynchronous initialisation, but at that point you should probably be using something other than GLUT).

glViewport in the resize function is a micro-optimization that you sometimes see in tutorials. There’s no reason to not just call it every frame - you won’t even notice a performance difference.

The same applies to your perspective projection matrix; that can be calculated every frame as well.

So basically the display function number of calls is the frame rate when measured over a second? Many of the functions call other functions when an event occurs e.g. keyup, key down. But what part of a glut program for example just is on all the time? So if I needed to perform a task every frame where would in the program would I code that task?

Well, each frame is rendered by the display function, so anything in that function is, by definition, executed every frame.

But note that the frame rate won’t necessarily be constant. If you need to perform a task at a constant rate (e.g. simulation updates), you should check the time elapsed since the last update (ideally, you should predict the time at which the next buffer swap will occur, but that’s easier said than done).