c++ Glut 显示需要调用我的绘制函数,但我无法传递任何参数

c++ Glut display needs to call my draw function but I can't pass any parameters

本文关键字:参数 任何 函数 显示 Glut 调用 绘制 我的 c++      更新时间:2023-10-16

我正在尝试在 c++ 中使用 glut 根据我输入到 main 函数中的文件中的信息来创建绘图。

int main (int argc, char** argv)  //I think this is all relevant info in main
{   
glutDisplayFunc (display);
fstream file = fstream ("Tree.txt");
Tree myTree = Tree(file);
}
static    //this is the function I need to draw from but it can't take any parameters
void display(void)
{  
}
static void draw_frame(Tree::Node* t) //my draw function needs access to info from my file stream
{
    int x = t->xValue;  //for example I need to access these variables
    int y = t->yValue;
}

我尝试将 Tree* myTree 设置为全局变量,但我只是遇到了更多错误,它会完全停止读取我的文件。 如果有人有任何想法,我将不胜感激!

在您的情况下,您需要将 Tree 作为全局变量。我建议从构造函数中移动您的"树构建"代码,并从 main 函数初始化它。大致如下:

Tree GTree;
int main (int argc, char** argv)  //I think this is all relevant info in main
{   
    glutDisplayFunc (display);
    fstream file = fstream ("Tree.txt");
    GTree.Initialize(file);
}

然后,在display中,您可以使用GTree进行渲染。

请注意,如果您需要更新渲染,则需要调用 glutPostRedisplay 或将display作为 glutIdleFunc