2015-12-15 57 views
-3

我的任務是使用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; 
} 
+1

您從用戶,並立即'return'獲取輸入。你不想那麼做嗎? –

+0

'return'表示現在離開功能。返回後的任何代碼都會被忽略。應該是一個編譯器警告或兩個有關。 – user4581301

+1

我投票結束這個問題作爲題外話,因爲它不是一個錯字,但是一個糟糕的'返回'對幫助未來的提問者沒有多大用處。 – user4581301

回答

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; 
} 
+0

非常感謝!你一直在幫助很大! – helphelp

+0

@helphelp不客氣:) –