OpenGL:将鼠标点击到几何体上

时间:2022-09-10 18:35:53

I have this view set:

我有这个视图集:

glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective

and I get a screen position (sx, sy) from a mouse click.

然后我从鼠标点击中获得一个屏幕位置(sx,sy)。

Given a value of z, how can I calculate x and y in 3d-space from sx and sy?

给定z的值,如何从sx和sy计算3d空间中的x和y?

4 个解决方案

#1


11  

You should use gluUnProject:

你应该使用gluUnProject:

First, compute the "unprojection" to the near plane:

首先,计算*面的“不投影”:

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

and then to the far plane:

然后到远处的飞机:

// replace the above gluUnProject call with
gluUnProject(sx, viewport[1] + viewport[3] - sy, 1, modelMatrix, projMatrix, viewport, &x, &y, &z);

Now you've got a line in world coordinates that traces out all possible points you could have been clicking on. So now you just need to interpolate: suppose you're given the z-coordinate:

现在,您已经在世界坐标中找到了一条线,可以找出您可能点击的所有可能点。所以现在你只需要插值:假设你给了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;

#2


3  

This is best answered by the most authoritative source, OpenGL's web site.

这是最权威的来源,OpenGL的网站最好的答案。

#3


2  

This is actually dependent on the projection matrix, not the model-view matrix. http://www.toymaker.info/Games/html/picking.html should help you out -- it's D3D-centric but the theory is the same.

这实际上取决于投影矩阵,而不是模型视图矩阵。 http://www.toymaker.info/Games/html/picking.html应该帮助你 - 它以D3D为中心,但理论是一样的。

If you're looking to do mouse-based picking, though, I suggest using the selection rendering mode.

但是,如果您正在寻找基于鼠标的拾取,我建议使用选择渲染模式。

Edit: Mind you, the model-view matrix does come into play, but since yours is identity it's a nonissue.

编辑:请注意,模型 - 视图矩阵确实发挥作用,但由于你的身份,它是一个非问题。

#4


0  

libqglviewer has a good selection framework if that's what you're looking for

如果你正在寻找的话,libqglviewer有一个很好的选择框架

#1


11  

You should use gluUnProject:

你应该使用gluUnProject:

First, compute the "unprojection" to the near plane:

首先,计算*面的“不投影”:

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

and then to the far plane:

然后到远处的飞机:

// replace the above gluUnProject call with
gluUnProject(sx, viewport[1] + viewport[3] - sy, 1, modelMatrix, projMatrix, viewport, &x, &y, &z);

Now you've got a line in world coordinates that traces out all possible points you could have been clicking on. So now you just need to interpolate: suppose you're given the z-coordinate:

现在,您已经在世界坐标中找到了一条线,可以找出您可能点击的所有可能点。所以现在你只需要插值:假设你给了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;

#2


3  

This is best answered by the most authoritative source, OpenGL's web site.

这是最权威的来源,OpenGL的网站最好的答案。

#3


2  

This is actually dependent on the projection matrix, not the model-view matrix. http://www.toymaker.info/Games/html/picking.html should help you out -- it's D3D-centric but the theory is the same.

这实际上取决于投影矩阵,而不是模型视图矩阵。 http://www.toymaker.info/Games/html/picking.html应该帮助你 - 它以D3D为中心,但理论是一样的。

If you're looking to do mouse-based picking, though, I suggest using the selection rendering mode.

但是,如果您正在寻找基于鼠标的拾取,我建议使用选择渲染模式。

Edit: Mind you, the model-view matrix does come into play, but since yours is identity it's a nonissue.

编辑:请注意,模型 - 视图矩阵确实发挥作用,但由于你的身份,它是一个非问题。

#4


0  

libqglviewer has a good selection framework if that's what you're looking for

如果你正在寻找的话,libqglviewer有一个很好的选择框架