Pernicious bug

Hi folks,
I’ve got a bug that is rather odd and I’ve spent a day trying to fix it.

My GL screen consist of 2 triangles that display a texture that fills the window.
Should be simple, right?

The problem is that whenever the window is resized, everything falls to pieces.

In pseudo C code, my approach is something like this:


static bool window_resized=false;
static int newwidth=0, newheight=0;
resize_window(int w, int h)
{
  window_resized=true;
  newwidth=w; newheight=h;
  glViewport (0,0,w,h);
}
draw_scene(){
  if (window_resized) {
   resize_texture(newwidth, newheight);
   window_resized=false;
  }
  glViewport (0,0,width, height);
  glMatrixMode(perspective..)
  glLoadIdentity()
  glOrtho(-width/2.,width/2.,height/2.,-height/2.,-1,1); // anchor at center of window
  glBindTexture(...)
  glTexImage2D(...)
  glMatrixMode(model..)
  glLoadIdentity()
  glBegin();
  ...draw texture on a quad...
  glEnd()
  glFlush()
  glutSwapBuffers()
}
void do_idle()
{
   glutPostRedisplay();
}

The bug is that whenever I resize, everything is out of whack until the idle routine can force a redisplay. I’d prefer to have no idle routine at all, and I found that removing it results in the entire texture being moved downward (on Linux and on Mac), or (on the Mac) resized oddly etc.
Anyone know what could cause this?

So why does your draw routine use newwidth/height for resizing the texture, but width/height for setting the active viewport for rendering and setting up the projection?

I would think you should use the latest for both.

That’s a typo, I was writing it out from memory.
width/height are updated to newwidth & newheight.