openGL 2D鼠标点击位置

Wal*_*ter 2 c opengl 2d mouseevent

就像这个问题所述 - 我想将点击(2D)转换为与屏幕上的渲染相关的坐标。我将函数glutMouseFunc绑定到我的函数(如下),但无法理解传入的 x 和 y - 它们似乎与距左上角的像素距离有关。

我想知道如何将 x 和 y 转换为世界坐标:)

void mouse(int button, int state, int x, int y) {
  switch(button) {
  case GLUT_LEFT_BUTTON:
    printf(" LEFT ");
    if (state == GLUT_DOWN) {
        printf("DOWN\n");
        printf("(%d, %d)\n", x, y);
    }
    else 
      if (state == GLUT_UP) {
        printf("UP\n");
      }
    break;

  default:
    break;
  }
  fflush(stdout);                             // Force output to stdout
}
Run Code Online (Sandbox Code Playgroud)

Wal*_*ter 5

GLdouble ox=0.0,oy=0.0,oz=0.0;
void Mouse(int button,int state,int x,int y) {
  GLint viewport[4];
  GLdouble modelview[16],projection[16];
  GLfloat wx=x,wy,wz;

  if(state!=GLUT_DOWN)
    return;
  if(button==GLUT_RIGHT_BUTTON)
    exit(0);
  glGetIntegerv(GL_VIEWPORT,viewport);
  y=viewport[3]-y;
  wy=y;
  glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
  glGetDoublev(GL_PROJECTION_MATRIX,projection);
  glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
  gluUnProject(wx,wy,wz,modelview,projection,viewport,&ox,&oy,&oz);
  glutPostRedisplay();
}
Run Code Online (Sandbox Code Playgroud)

ox, oy, oz你的输出值在哪里

http://hamala.se/forums/viewtopic.php?t=20