2012-05-19 61 views
1

我希望我的程序在鼠標點擊我在屏幕上創建的按鈕時繪製多邊形。我應該在哪裏放置繪製多邊形命令?我明白我不能放入我的鼠標功能,因爲下次我的顯示回調運行時它的效果會丟失,並且它必須位於我的顯示功能中。但是,我可以在我的顯示功能中設置if條件嗎?如何在OpenGL中單擊鼠標時繪製多邊形?

回答

0

希望這可以幫助你喜歡它幫助我。將鼠標座標轉換爲3D對象可以使用的內容(請參見下面的代碼)並將其存儲在某處後,可以使用變換在存儲的座標中將對象繪製在循環中。我在我的OpenGL程序的init函數中初始化了我的形狀,顏色等,但只在鍵盤上選擇了一個數字然後在視口中的某處點擊時才繪製它。重複這些步驟將該對象移動/轉換爲新的座標。

void MouseButton(int button, int state, int x, int y) 
{ 
if (button == GLUT_LEFT_BUTTON) 
{ 
    leftmousebutton_down = (state == GLUT_DOWN) ? TRUE : FALSE;  
    if(leftmousebutton_down) 
    { 
     cout << "LEFT BUTTON DOWN" << endl; 
     //pTransInfo[0].vTranslate = Vector3(0.5f, 0.5f, 0.5f); // center of scene 
     GLint viewport[4]; //var to hold the viewport info 
     GLdouble modelview[16]; //var to hold the modelview info 
     GLdouble projection[16]; //var to hold the projection matrix info 
     GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates 
     GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates 

     glGetDoublev(GL_MODELVIEW_MATRIX, modelview); //get the modelview info 
     glGetDoublev(GL_PROJECTION_MATRIX, projection); //get the projection matrix info 
     glGetIntegerv(GL_VIEWPORT, viewport); //get the viewport info 

     winX = (float)x; 
     winY = (float)viewport[3] - (float)y; 
     winZ = 0; 

     //get the world coordinates from the screen coordinates 
     gluUnProject(winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ); 

     cout << "coordinates: worldX = " << worldX << " worldY = " << worldY << " worldZ = " << worldZ << endl; //THIS IS WHAT YOU WANT, STORE IT IN AN ARRAY OR OTHER STRUCTURE 
    } 
} 

}