为什么显示功能被调用两次?

Moh*_*ain 3 c opengl graphics

在下面的OpenGL代码中,用于初始化和主函数,为什么显示函数被调用两次?我看不到除了之外会执行的调用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. */
}
Run Code Online (Sandbox Code Playgroud)

unw*_*ind 6

display()只要GLUT决定它想要应用程序(那是你)重绘窗口的内容,就会调用你的回调.

也许在窗口打开时会发生一些事件,导致需要确保重绘窗口.

你不应该"关心"; 只需确保重新绘制display()函数中的内容,不要在意它被调用的次数.

  • @MohitJain你不应该在`display()`中计算任何东西.期望随机调用此函数.不要仅仅在某些情况下依赖它. (5认同)