호프가 도움이 되었기를 바랍니다. 마우스 좌표를 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
}
}
}