将鼠标位置转换为对象坐标 [英] Translating mouse location to Object coordinates

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

问题描述

我正在尝试使用 Collada 对象将基本的鼠标集成到一个小演示中.我能够获得正确的鼠标 x 和 y,但我不知道如何将它们转换为用于对象的坐标.

I'm trying to get basic mouse integration into a small demo using a Collada object. I am able to get the correct mouse x and y, but I don't know how to translate them into coordinates to be used for the object.

我希望 GLGE 函数 duck.setLoc(mousepos.x, mousepos.y) 能够从像素转换,但事实并非如此.

I was hoping the GLGE function duck.setLoc(mousepos.x, mousepos.y) would convert from pixels, but that is not the case.

我需要做什么才能将 2D mousepos 像素转换为 3D 点?

What do I have to do to translate the 2D mousepos pixels into a 3D point?

推荐答案

如果您有屏幕坐标 (x0, y0) 的点,您可以取消投影它们并在 3d 中计算它的坐标,例如,z-coord设置为靠近剪辑窗格.就像你对 (x,y, near-z) 用投影矩阵得到他们的屏幕坐标一样,现在你做反向处理.

If you have point with screen coordinates (x0, y0), you can un-project them and calculate it's coordinates in 3d with, for example, z-coord set to near clipping pane. Like you would for (x,y, near-z) get their screen coords with projective matrix, now you do reverse process.

但是如果你想在 3d 中得到交点的坐标,那么你可以使用上面提到的这个样本点和相机的原点在场景中投射一条光线并计算交点.

But if you want get intersection point's coords in 3d, then you could use this sample point mentioned above and camera's origin point to cast a ray in the scene and calculate intersection.

伪代码:

ray.origin = camera.position;  // vec3
ray.direction = samplePoint.position - camera.position; //vec3

check_intersections_with_triangles_in_scene( scene, ray ); // retrieve 3d coord of intersection

例如,如果您使用的是 Three.js,那么所有这些功能都已内置于其中,因此轻而易举.检查这个演示:http://threejs.org/examples/webgl_interactive_draggablecubes.html.

If you're using Three.js, for example, all these functions are already built in it, so it's a piece of cake to do. Check this demo: http://threejs.org/examples/webgl_interactive_draggablecubes.html .

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

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