2012-05-19 5 views
1

화면에서 만든 버튼을 마우스로 클릭하면 프로그램에서 다각형을 그려야합니다. Draw polygon 명령은 어디에 두어야합니까? 다음 번에 디스플레이 콜백이 실행될 때 그 효과가 사라지고 내 디스플레이 기능에 있어야하므로 마우스 기능을 사용할 수 없다는 것을 알고 있습니다. 하지만 디스플레이 기능에서 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 
    } 
} 

}