resizing window while keeping the same width/height ratio

I feel like this question must have been asked a 1000 times but I can’t seem to find it in the forum. When I resize my window all my objects get distorted. I want them to keep the same width/height ratio as I resize the window. I’m using GLUT. My reshape function I got from the Red Book:


void reshape(int w, int h)
{
  glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-100.0, 100.0, 0.0, 200.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

Hi,

Take a look at http://www.opengl.org/archives/resources/faq/technical/transformations.htm
9.140 How do I keep my aspect ratio correct after a window resize?

ok, I figured out how to keep the aspect ration with gluPerspective but I can’t figure it out with glFrustum. Specifically I don’t know how to calculate the cx variable. The instructions for glFrustum say this:


float cx, halfWidth = windowWidth*0.5f;
float aspect = (float)windowWidth/(float)windowHeight;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* cx is the eye space center of the zNear plane in X */
glFrustum(cx-halfWidth*aspect, cx+halfWidth*aspect, bottom, top, zNear, zFar);

My left, right, bottom, top coordinates are, respectively, -1.0, 1.0, -1.0, 1.0. So I’m guessing the eye space center of the zNear plane in X would be 0.0. But that doesn’t work…