简体   繁体   中英

Why is display function called twice?

In the OpenGL code below, used for initialization and for the main function, why is the display function getting called twice? I can't see a call that would be executed other than glutDisplayFunc(display) ;

void init(void)
{
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
   glClearDepth(1.0f);                   // Set background depth to farthest
   glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
   glEnable(GL_POINT_SMOOTH);
   glDepthFunc(GL_LEQUAL);    // Set the type of depth-test
   glShadeModel(GL_SMOOTH);   // Enable smooth shading
   gluLookAt(0.0, 0.0, -5.0,  /* eye is at (0,0,5) */
             0.0, 0.0, 0.0,      /* center is at (0,0,0) */
             0.0, 1.0, 0.0);      /* up is in positive Y direction */

   glOrtho(-5,5,-5,5,12,15); 
  //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Nice perspective corrections

}

int
main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutCreateWindow("red 3D lighted cube");
  glutInitWindowSize(1280,800 );   // Set the window's initial width & height
  //glutInitWindowPosition(50, 50); // Position the window's initial top-left corner    
  //glutReshapeWindow(800,800);
  init();
  compute();
  glutDisplayFunc(display);

  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}

Your display() callback is called whenever GLUT decides it wants the application (that's you) to redraw the contents of the window.

Perhaps there is some events happening as the window opens that causes a need to make sure the window is redrawn.

You're not supposed to "care"; just make sure you redraw the content in the display() function and never mind how many times it gets called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM