[英]How this openGL code is working, passing a function as an argument instead of a function pointer?
The following openGL code APPARENTLY passes display function as an argument to glutDisplayFunction. 下面的openGL代码显然通过显示功能作为参数传递给glutDisplayFunction。 In reality only a function pointer can be passed as argument to glutDisplayFunction.
实际上,只能将函数指针作为参数传递给glutDisplayFunction。 I wonder how this program is running successfully?
我不知道该程序如何成功运行?
#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;
}
Passing the name of a function without parentheses as an argument implicitly passes a pointer to the function. 传递不带括号的函数名称作为参数会隐式传递指向该函数的指针。 It is not possible to pass a function by value in C.
不能通过C中的值传递函数。
We need not use & operator to create a reference to a function.We can pass 'display' to 'glutDisplayFunc' directly. 我们不需要使用&运算符来创建对函数的引用,我们可以直接将'display'传递给'glutDisplayFunc'。 Something Like array name referring to address of first element?
像数组名称一样指向第一个元素的地址?
This -glutDisplayFunc(&display) - also should be fine 这个-glutDisplayFunc(&display)-也应该很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.