胶合鼠标坐标 [英] Glut Mouse Coordinates

查看:71
本文介绍了胶合鼠标坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

操作:

我试图通过做类似这样的事情来使用guUnPorject

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

推荐答案

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

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.

因此,您实际上可以获得的是从相机"到场景的光线.为此,您需要执行从屏幕空间到世界空间的反向投影.为此,采用投影矩阵P和视图矩阵V(由gluLookAt或类似方法生成),形成乘积P * V并将其求逆,即(P * V)^-1 = V ^ -1 * P ^- 1.注意,反转不是换位.通过高斯-乔丹消除法对矩阵求逆,最好进行一些旋转.任何有关线性代数的数学教科书都对此进行了解释.

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.

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

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.

整个反投影过程已打包到gluUnProject中.但是,我建议不要以其GLU形式使用它,而是查看源代码以学习它,或者使用GLM提供的现代替代方法.

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.

这篇关于胶合鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆