c - openGL 2D鼠标点击位置

标签 c opengl 2d mouseevent

this问题状态 - 我想将点击(2D)转换为与屏幕上的渲染相关的坐标。我将函数 glutMouseFunc 绑定(bind)到我的函数(如下),但无法理解传入的 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
}

最佳答案

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();
}

ox, oy, oz 是你的输出值

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

关于c - openGL 2D鼠标点击位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9123648/

相关文章:

ssl - openssl 提供 Kubernetes 入口 Controller 假证书

2d - 将高度图映射到基于网格的轮廓格式

c - c中的静态矩阵乘法到动态矩阵

c++ - 如何生成循环矩阵?

检查空指针在 C 中不起作用(给出段错误)

c - 从静态 const 结构返回 const char **

java - LibGDX 动态构建(快速)模型

c - 写入视频内存(0xB8000)和 volatile 指针

c++ - "hello triangle"没有显示。是否有某些功能未正确使用?

python - 如何将 "array of integer pointers"从 python 传递给 c 库中的函数