简体   繁体   English

OpenGL C ++类型为void的参数与类型为void(*)()的参数不兼容

[英]OpenGL C++ Argument of type void is incompatible with parameter of type void(*)()

I am trying to draw several triangles in 3d and can't execute glutDisplayFunc() function. 我试图在3d中绘制几个三角形,但是无法执行glutDisplayFunc()函数。 Here is my code: 这是我的代码:

Array of triangles and size. 三角形和大小的数组。

int const trsize = (size-1)*(size-1)*2;
Triangle *trarr = new Triangle[trsize];

Code, where I try to call the function. 代码,我尝试在其中调用该函数。

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);        
glutInitWindowPosition(100,100);    
glutCreateWindow("Height Map");     
Initialize();                       
glutDisplayFunc(Draw1(trarr, trsize));              
glutMainLoop();

Draw1() function. Draw1()函数。

void Draw1(Triangle arr[], int size){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINES);
    for(int i=0; i<size; ++i){
        glVertex3d((GLdouble)arr[i].p1().x(), (GLdouble)arr[i].p1().y(), (GLdouble)arr[i].p1().z());
        glVertex3d((GLdouble)arr[i].p2().x(), (GLdouble)arr[i].p2().y(), (GLdouble)arr[i].p2().z());
        glVertex3d((GLdouble)arr[i].p3().x(), (GLdouble)arr[i].p3().y(), (GLdouble)arr[i].p3().z());
    }
    glEnd();
    glFlush();
}

I use VS2012 and IntelliSense writes: argument of type "void" is incompatible with parameter of type "void (*)()" 我使用VS2012,并且IntelliSense写道: argument of type "void" is incompatible with parameter of type "void (*)()"

I don't know why DisplayFunc needs another type. 我不知道为什么DisplayFunc需要另一种类型。 With Draw() function without arguments it works fine. 使用不带参数的Draw()函数,它可以正常工作。

glutDisplayFunc only allow function with that signature: void func(void). glutDisplayFunc仅允许具有该签名的函数:void func(void)。

so in you code, your drawing routine should be: 因此在您的代码中,绘图例程应为:

void Draw1(void){...} 无效Draw1(void){...}

and you should find another way (global variable for example) to pass parameters to it. 并且您应该找到另一种方式(例如,全局变量)将参数传递给它。 And your glut statement would be: 您的过剩声明将是:

glutDisplayFunc(Draw1); glutDisplayFunc(DRAW1);

those parameters should be globals that you assign to as you set the drawFunc: 这些参数应该是在设置drawFunc时分配给它们的全局变量:

Triangle arr[];
int size;

void Draw1(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINES);
    for(int i=0; i<size; ++i){
        glVertex3d((GLdouble)arr[i].p1().x(), (GLdouble)arr[i].p1().y(), (GLdouble)arr[i].p1().z());
        glVertex3d((GLdouble)arr[i].p2().x(), (GLdouble)arr[i].p2().y(), (GLdouble)arr[i].p2().z());
        glVertex3d((GLdouble)arr[i].p3().x(), (GLdouble)arr[i].p3().y(), (GLdouble)arr[i].p3().z());
    }
    glEnd();
    glFlush();
}


Initialize();         
arr = trarr; size = trsize;              
glutDisplayFunc(Draw1);              
glutMainLoop();

it's a shortcoming of the glut library that you have to use globals to pass data around the callbacks 这是glut库的一个缺点,您必须使用全局变量在回调周围传递数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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