以下openGL代码 APPARENTLY 将显示函数作为glutDisplayFunction的参数传递。实际上,只有函数指针可以作为参数传递给glutDisplayFunction。我想知道这个程序是如何成功运行的?
#include <GL/glut.h>
#include<GL/gl.h>// Header File For The GLUT Library
void init(void)
{
glClearColor (0.0,0.0,0.4,0.0);
glShadeModel(GL_FLAT);
}
void reshape (int w, int h)
{
glViewport(0,0, (GLsizei) w, (GLsizei)h); // indicates the shape of the available screen area into which the scene is mapped
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10,10,-10,10,-10,10);
}
void display (void)
{
int i,j;
while(1){
for (i=-10; i <=10; i++){
glClearColor (1.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_TRIANGLES);
glVertex3f(i,2,5);
glVertex3f(6,-i,-5);
glVertex3f(1,9,-1);
glEnd();
glFlush();}
}
}
int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop ();
return 0;
}
答案 0 :(得分:3)
将没有括号的函数的名称作为参数传递,会隐式传递指向该函数的指针。无法在C中按值传递函数。
答案 1 :(得分:3)
我们不需要使用&amp; operator创建一个函数的引用。我们可以直接将'display'传递给'glutDisplayFunc'。 Something like数组名称引用第一个元素的地址?
这个-glutDisplayFunc(&amp; display) - 也应该没问题