Translating mouse location to Object coordinates

2019-09-03 10:15发布

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?

标签: webgl glge
1条回答
劫难
2楼-- · 2019-09-03 10:49

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 .

查看更多
登录 后发表回答