我的任务是使用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;
}
答案 0 :(得分: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;
}