过剩的鼠标坐标

发布于 2024-12-19 07:27:09 字数 592 浏览 0 评论 0原文

我正在尝试获取一个点的笛卡尔坐标,因为我正在使用一个简单的函数来传递窗口坐标,例如(100,200)。我想将其转换为笛卡尔坐标,问题是窗口大小是可变的,所以我真的不知道如何实现它。

编辑:

Op:

我尝试通过执行类似的操作来使用 guUnPorject

GLdouble modelMatrix[16];
glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
GLdouble projMatrix[16];
glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);


double position[3];
 gluUnProject(
  x,
   y,
  1,
 modelMatrix,
 projMatrix,
 viewport,
 &position[0], //-> pointer to your own position (optional)
 &position[1], // id
 &position[2] // 
 );

 cout<<position[0];

但是我得到的位置似乎完全随机

I am trying to obtain the Cartesian coordinates of a point for that I am using a simple function the devolves a window coordinates, something like (100,200). I want to convert this to Cartesian coordinates, the problems is that the window size is variable so I really don't know how to implement this.

Edit:

Op:

I tried to use the guUnPorject by doing something like this

GLdouble modelMatrix[16];
glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
GLdouble projMatrix[16];
glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);


double position[3];
 gluUnProject(
  x,
   y,
  1,
 modelMatrix,
 projMatrix,
 viewport,
 &position[0], //-> pointer to your own position (optional)
 &position[1], // id
 &position[2] // 
 );

 cout<<position[0];

However the position I got seems completely random

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2024-12-26 07:27:09

鼠标坐标已经是笛卡尔坐标,即窗口空间的坐标。我认为您正在寻找的是 OpenGL 场景的世界空间的转换。窗口中的 2D 鼠标坐标缺少一些信息:深度。

所以你能得到的实际上是从“相机”进入场景的光线。为此,您需要执行从屏幕空间到世界空间的反投影。为此,您采用投影矩阵 P 和视图矩阵 V(由 gluLookAt 或类似工具生成的),形成乘积 P*V 并将其反转,即 (P*V)^-1 = V^-1 * P^- 1.请注意,倒置不是转置。矩阵求逆是通过高斯约尔当消元法完成的,最好进行一些旋转。任何关于线性代数的数学教科书都会对此进行解释。

然后你需要两个向量来形成一条射线。为此,您采用两个具有相同 XY 坐标但深度不同(例如 0 和 1)的屏幕空间位置并进行反投影:

(P*V)^-1 * (x, y, {0, 1})

结果向量的差异为您提供了射线方向。

整个反投影过程已包含在 gluUnProject 中。不过,我建议不要以 GLU 形式使用它,而是查看源代码以从中学习,或者使用 GLM 提供的现代替代品。

Mouse coordinates are already in cartesian coordinates, namely the coordinates of window space. I think what you're looking for is a transformation into the world space of your OpenGL scene. The 2D mouse coordinates in the window lack some information thougn: The depth.

So what you can otain is actually a ray from the "camera" into the scene. For this you need to perform the back projection from screen space to world space. For this you take the projection matrix, P and the view matrix V (what's generated by gluLookAt or similar), form the product P*V and invert it i.e. (P*V)^-1 = V^-1 * P^-1. Take notice that inversion is not transposition. Inverting a matrix is done by Gauss-Jordan elimination, preferrably with some pivoting. Any mathtextbook on linear algebra explains it.

Then you need two vectors to form a ray. For this you take two screen space positions with the same XY coordinates but differing depth (say, 0 and 1) and do the backprojection:

(P*V)^-1 * (x, y, {0, 1})

The difference of the resulting vectors gives you a ray direction.

The whole backprojection process has been wrapped up into gluUnProject. However I recommend not using it in its GLU form, but either look at the source code to learn from it, or use a modern day substitute like it's offered by GLM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文