如何从 main 到 void 获取信息?

How to get info from main to void?

我的任务是使用 GLUT 要求用户输入坐标并显示矩形。但是,我似乎无法获得从 "int main" 到 "void display" 的坐标。

到目前为止,这是我的代码:

#include<iostream>
#include<gl/glut.h>

using namespace std;
void display(float yaxis, float xaxis)
{
    glClearColor(1, 1, 1, 1);

    glClear (GL_COLOR_BUFFER_BIT);

    glBegin (GL_QUADS);

    glColor3f(0, 0, 0);

    glVertex2f(yaxis, -xaxis);
    glVertex2f(yaxis, xaxis);
    glVertex2f(-yaxis, xaxis);
    glVertex2f(-yaxis, -xaxis);
    glEnd();

    glFlush();

}
int main(int argc, char** argv)
{
    float xaxis;
    float yaxis;

    cout << "Please enter the co-ordinates for the x axis and press enter.";
    cin >> xaxis;
    cout << "You entered: " << xaxis
            << ".\n Please enter the co-ordinates for the y axis and press enter.";
    cin >> yaxis;
    cout << "You entered: " << yaxis << ".\n Here is your rectangle.";

    glutInit(&argc, argv);

    glutInitWindowSize(640, 500);

    glutInitWindowPosition(100, 10);

    glutCreateWindow("Triangle");

    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

glutDisplayFunc 函数具有以下声明:

void glutDisplayFunc(void (*func)(void));

因此您不能使用您实现的 display 功能。 这是一个 快速 示例,可以修复您的错误:

#include<iostream>
#include<gl/glut.h>

using namespace std;
static float yaxis;
static float xaxis;
void display()
{
    glClearColor(1, 1, 1, 1);

    glClear (GL_COLOR_BUFFER_BIT);

    glBegin (GL_QUADS);

    glColor3f(0, 0, 0);

    glVertex2f(yaxis, -xaxis);
    glVertex2f(yaxis, xaxis);
    glVertex2f(-yaxis, xaxis);
    glVertex2f(-yaxis, -xaxis);
    glEnd();

    glFlush();

}
int main(int argc, char** argv)
{
    cout << "Please enter the co-ordinates for the x axis and press enter.";
    cin >> xaxis;
    cout << "You entered: " << xaxis
            << ".\n Please enter the co-ordinates for the y axis and press enter.";
    cin >> yaxis;
    cout << "You entered: " << yaxis << ".\n Here is your rectangle.";

    glutInit(&argc, argv);

    glutInitWindowSize(640, 500);

    glutInitWindowPosition(100, 10);

    glutCreateWindow("Rectangle");

    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}