views:

73

answers:

3

I'm doing 2D drawing in a glut window, and I'm having trouble making the window resize properly.

My glutDisplayFunc and glutReshapeFunc are below. As it is, the drawing disappears when the window is resized. If I delete the glClear() from displayFunc(), new pixels don't "initialize" and there are clipping problems. How do I fix this?

glutDisplayFunc:

void displayFunc() {
  glDisable( GL_DEPTH_TEST );
  glClear( GL_COLOR_BUFFER_BIT );
  glPointSize ( 3.0 );
  glFlush();
}

glutReshapeFunc:

void windowReshapeFunc( GLint newWidth, GLint newHeight ) {
  glViewport( 0, 0, newWidth, newHeight );
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  gluOrtho2D( 0, GLdouble (newWidth), 0, GLdouble (newHeight) );

  windowWidth = newWidth;
  windowHeight = newHeight;
}
+1  A: 

I'd try adding a call to glutPostRedisplay() around the end of your reshape function.

Jerry Coffin
Thanks for the reply, but that doesn't seem to help. Can you explain why you would expect it to?
obh
It causes the display to be updated after the window is resized. Looking more carefully, however, the only actual drawing you have in your display func is clearing the screen, so I wouldn't expect anything else to happen. I'm guessing you have some drawing somewhere else, which really shouldn't be the case.
Jerry Coffin
Diaply IS updated every time reshape is called, that's not the point, obh please post your full code so we can see not guess.
Green Code
A: 

I guess your code does not draw everything on scene in display func, you see, if no events occcur ever you have to call display one time and in the first time it has your drawing. But your problem rises when there is an event which says the window is resized! try putting your drawing part in display function. Like so,

void displayFunc() {
  glDisable( GL_DEPTH_TEST );
  glClear( GL_COLOR_BUFFER_BIT );
  glPointSize ( 3.0 );
  glBegin(GL_POINTS);
  {
          //Blah blah blah some points here which you expect to draw
  }
  glEnd();
  glFlush();
}

Please post the full code if this was not helpful.

Green Code
A: 

You're not setting the matrix mode back to GL_MODELVIEW at the end of your reshape function.

void reshape(int width, int height) {
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, GLdouble (width), 0, GLdouble (height) );
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

It's hard to say if there's something else in your display function without seeing more code. I hope that helps.

Ryan