Translating mouse location to Object coordinates

北城余情 提交于 2020-01-06 08:23:11

问题


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.

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

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


回答1:


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.

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.

Pseudo-code:

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

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 .



来源:https://stackoverflow.com/questions/13023244/translating-mouse-location-to-object-coordinates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!