开发者

directx 9 mouse over an object

ok when i used开发者_如何转开发 to draw stuff using gdi+ i used to know the coordinates for objects but now when im using meshes in directx 9 i have no idea how to get the coordinates for the object so i can find if the mouse over an object. any idea how to find the coordinates ?


You need to cast the mouse position into the world and convert it to world-space coordinates, which then are tested against the various objects. You may be able to find a library to do this for you, I know OpenGL supports picking and most wrappers offer enhanced functions for that, but the principle is:

Find the mouse coordinates in the window. Using those coordinates, cast a ray (whether you actually use a ray in the system or simply do similar math isn't a big deal here) into the world. You'll use the current view matrix ("camera" angle and position) to calculate the direction and origin of the ray. Using that ray, test against your objects, their bounding boxes or geometry (whichever you choose) to find the object. Using the intersection coordinates, find the object that is at that location.

You can also use the depth buffer for this very easily, if your scene is relatively static. Simply render with a depth texture set as the Z buffer, then use the depth, mouse position and view matrix to find the point of intersection.

It may be possible to do this in reverse, that is, map each object to the appropriate screen coordinates, but you will likely run into issues with depth sorting and overlapping areas. Also, it can be unnecessarily slow mapping every object to window areas each frame.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜