당신은 gluUnProject
사용해야
첫째, 가까운면에 "unprojection을"계산 :
GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
GLdouble x, y, z;
gluUnProject(sx, viewport[1] + viewport[3] - sy, 0, modelMatrix, projMatrix, viewport, &x, &y, &z);
후까지면 :
// replace the above gluUnProject call with
gluUnProject(sx, viewport[1] + viewport[3] - sy, 1, modelMatrix, projMatrix, viewport, &x, &y, &z);
이제했습니다 세계 좌표계에서 가능한 모든 점을 찾아내는 선이 있습니다. 은을 클릭합니다. 자 이제 z 좌표를 입력했다고 가정 해 보겠습니다.
GLfloat nearv[3], farv[3]; // already computed as above
if(nearv[2] == farv[2]) // this means we have no solutions
return;
GLfloat t = (nearv[2] - z)/(nearv[2] - farv[2]);
// so here are the desired (x, y) coordinates
GLfloat x = nearv[0] + (farv[0] - nearv[0]) * t,
y = nearv[1] + (farv[1] - nearv[1]) * t;
멍청한 탓에 죄송합니다. gluPerspective를 사용하여 투영 행렬을 설정할 때 gluUnproject 호출의 세 번째 매개 변수가 항상 동일 (0 및 1)합니까? 아니면 zNear 및 zFar 값에 의존합니까? – rgngl