Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么显示函数要调用两次?_C_Opengl_Graphics - Fatal编程技术网

C 为什么显示函数要调用两次?

C 为什么显示函数要调用两次?,c,opengl,graphics,C,Opengl,Graphics,在下面的OpenGL代码中,用于初始化和主函数,为什么显示函数会被调用两次?除了glutDisplayFunc(display),我看不到将执行的调用 每当GLUT决定让应用程序(即您)重画窗口内容时,就会调用您的display()回调 可能在窗口打开时发生了一些事件,导致需要确保重新绘制窗口 你不应该“关心”;只要确保在display()函数中重新绘制内容,不管调用多少次。如果我在display中计算某个值,该怎么办?它将被计算两次,然后可能产生错误的结果。@MohitJain您不应该在dis

在下面的OpenGL代码中,用于初始化和主函数,为什么显示函数会被调用两次?除了
glutDisplayFunc(display)
,我看不到将执行的调用

每当GLUT决定让应用程序(即您)重画窗口内容时,就会调用您的
display()
回调

可能在窗口打开时发生了一些事件,导致需要确保重新绘制窗口


你不应该“关心”;只要确保在
display()
函数中重新绘制内容,不管调用多少次。

如果我在display中计算某个值,该怎么办?它将被计算两次,然后可能产生错误的结果。@MohitJain您不应该在
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. */
}