使用glut函数将窗口坐标转换为3D世界坐标glutMouseFunc() [英] convert window coordinates to 3D world coordinates with glut function glutMouseFunc()

查看:822
本文介绍了使用glut函数将窗口坐标转换为3D世界坐标glutMouseFunc()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得鼠标的3D坐标点击C ++ / OpengGL与glut函数glutMouseFunc()。所以我创建了一个这样的函数:

  void mouse(int button,int state,int x,int y){
if(button == GLUT_LEFT_BUTTON&& state == GLUT_DOWN)
{
mouse_x = x;
mouse_y = y;
}
}

函数获取鼠标,我用它与glut函数glutMouseFunc()像这样:

  glutMouseFunc 

我的问题是如何修改鼠标函数给出的坐标, 3D世界。我的具体目的如下:能够看到我是否点击了世界上绘制的3D形状。



将3D对象的坐标映射到2D窗口坐标,然后将其与鼠标点击的坐标进行比较?

解决方案

不对应于3d空间中的点,而是对应于射线。



在任何情况下,您都可以使用 gluUnProject



如果你知道光标下的场景深度,那么你可以获得一个点击的3d位置 - code> winZ 参数。



如果您不知道深度,请在 code>参数,得到光线的开始,和1.0得到结束。你必须计算自己,如果这光线命中什么。


I am trying to get the 3D coordinates of a mouse click C++/OpengGL with the glut function glutMouseFunc(). So I created a function like this:

void mouse(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
    mouse_x=x;
    mouse_y=y;
    }
}

The function gets the window coordinates of the click of the mouse and i use it with the glut function glutMouseFunc() like this:

  glutMouseFunc(mouse);

My question is how would I modify the coordinates given by the mouse function so I could use them in a 3D world. My exact purpose would be the following: to be able to see if I have clicked on a 3D shape drawn in the world.

[EDIT] Would it be easier to transform the coordinates of the 3D object to 2D window coordinates and then compare it to the coordinates of the mouse click?

解决方案

Mouse click does not correspond to a point in 3d space, but to a ray.

In any case, you use gluUnProject.

If you know scene "depth" under cursor, then you can get 3d position of a click - by passing depth via winZ parameter.

If you don't know depth, pass 0.0 in winZ parameter, to get start of the ray, and 1.0 to get the "end". You'll have to calculate yourself if this ray hits anything.

这篇关于使用glut函数将窗口坐标转换为3D世界坐标glutMouseFunc()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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