问题描述
我都试过4种方法,所有这些都没有工作。在其他地方我的code ++工程,相机移动和正确的物体运动正常。
I have tried 4 methods, all which didn't work. Everywhere else my code works, the camera moves properly and the objects move properly.
我使用SharpDX和我的投影矩阵是透视投影矩阵。
I am using SharpDX and my projection matrix is a perspective projection matrix.
我想获得以便在该点将光标定位到一个三维点的屏幕位置。
I am trying to get the screen position of a 3d point in order to position the cursor at that point.
这些都是4种方法我都试过:
These are the 4 methods I have tried:
public Vector2 WorldPosToScreenPos1(Vector3 p)
{
Vector3.Transform(ref p, ref View, out p);
Vector3.Transform(ref p, ref Projection, out p);
return new Vector2(
RenderForm.ClientSize.Width * (p.X + 1) / 2,
RenderForm.ClientSize.Height * (1 - (p.Y + 1) / 2)
);
}
public Vector2 WorldPosToScreenPos2(Vector3 p)
{
Vector3.Transform(ref p, ref View, out p);
Vector3.Transform(ref p, ref Projection, out p);
p.X /= p.Z;
p.Y /= p.Z;
p.X = (p.X + 1) * RenderForm.ClientSize.Width / 2;
p.Y = (p.Y + 1) * RenderForm.ClientSize.Height / 2;
return new Vector2(p.X, p.Y);
}
public Vector2 WorldPosToScreenPos3(Vector3 p)
{
Viewport viewport = new Viewport(0, 0, RenderForm.ClientSize.Width, RenderForm.ClientSize.Height);
viewport.Project(ref p, ref ViewProj, out p);
return new Vector2(p.X, p.Y);
}
public Vector2 WorldPosToScreenPos4(Vector3 p)
{
Vector3.Project(p, 0, 0, RenderForm.ClientSize.Width, RenderForm.ClientSize.Height, 1, 10000, ViewProj);
return new Vector2(p.X, p.Y);
}
在所有的四种方法(其中所有给出不同的结果)的二维位置,我得到的是关闭的,往往幅度较大。我究竟做错了什么?
In all four methods (which all give different results) the 2d position I get is off, often by a big margin. What am I doing wrong?
推荐答案
原来的第4方法是正确的,所有它有缺失的是检索由Vector3.Project()的返回值。
It turns out the 4th method was right, all it had missing was retrieving the value returned by Vector3.Project().
3其他方法还是给了显著不同的结果,我仍然不知道为什么他们没有工作。如果有人知道,我AP preciate就知道了。
The 3 other methods still gave significantly different results and I still don't know why they didn't work. If someone knows, I'd appreciate to know.
这篇关于投影3D点到2D屏幕位置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!