C# 视场与坐标变换
在我的程序中,我有一个输入和一个输出。输入是一个二维位置,范围从(0,0)到(240360)。我的输出是用XNA生成的3D世界 但是,我还没有弄清楚如何将点从二维栅格转换为三维栅格。我希望能够平移这些点,以便(0,0)在XNA中显示相机可以看到的左上角工作中的点。同样,我希望(240360)的点出现在相机可以看到的右下角。Z值将为零(它会改变,但这超出了这个问题的范围) 我怎样才能知道我相机的视角在哪里? 下面是我在3D世界中绘制对象的方式C# 视场与坐标变换,c#,graphics,xna,C#,Graphics,Xna,在我的程序中,我有一个输入和一个输出。输入是一个二维位置,范围从(0,0)到(240360)。我的输出是用XNA生成的3D世界 但是,我还没有弄清楚如何将点从二维栅格转换为三维栅格。我希望能够平移这些点,以便(0,0)在XNA中显示相机可以看到的左上角工作中的点。同样,我希望(240360)的点出现在相机可以看到的右下角。Z值将为零(它会改变,但这超出了这个问题的范围) 我怎样才能知道我相机的视角在哪里? 下面是我在3D世界中绘制对象的方式 Vector3 camPos = ne
Vector3 camPos = new Vector3(0.0f, 0.0f, 500.0f);
Matrix view = Matrix.CreateLookAt(camPos, Vector3.Zero, Vector3.Up);
Matrix proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.DisplayMode.AspectRatio, 1, 10000);
Matrix ballWorld = Matrix.Identity;
//rotate it so that it is always facing foward
ballWorld.Forward = direction;
ballWorld.Up = Vector3.Up;
ballWorld.Right = Vector3.Cross(direction, Vector3.Up);
foreach (SphereObject so in blobs) {
Matrix sphereWorld = ballWorld;
sphereWorld *= Matrix.CreateTranslation(so.Position);
sphereWorld *= Matrix.CreateScale(so.Scale);
so.SphereModel.Draw(sphereWorld, view, proj);
}
在这里,我从二维平面获取点,并创建SphereObject
public void CreateNewBlob(System.Drawing.Point p) {
Random rand = new Random();
SphereObject so = new SphereObject(model, new Vector3(p.X, p.Y, 0));
so.SetColor(new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble()));
blobs.Add(so);
}
如何将2D点映射到视口上,然后从该点取消投影光线,并使用它根据远剪裁平面查找光线上最远的可见点
float farplane = 1000;
Matrix proj = Matrix.CreateLookAt(Vector3.Zero, Vector3.Forward, Vector3.Up);
Matrix view = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, this.GraphicsDevice.Viewport.AspectRatio, 1, farplane);
Vector2 Coord = new Vector2(10,10); //between 0,0 and 240,360
Coord.X = Coord.X * (GraphicsDevice.Viewport.Width / 240);
Coord.Y = Coord.Y * (GraphicsDevice.Viewport.Height / 360);
Ray r = ProjectRayFromPointOnScreen((int)Coord.X, (int)Coord.Y, proj, view);
Vector3 ballpos = r.Position + (r.Direction *= (farplane - 1)); //Move just inside the far plane so its not clipped by accident
public Ray ProjectRayFromPointOnScreen(int x, int y, Matrix ProjectionMatrix, Matrix ViewMatrix)
{
Vector3 nearsource = new Vector3(x, y, 0);
Vector3 farsource = new Vector3(x, y, 1);
/* The Unproject method: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.viewport.unproject.aspx */
GraphicsDevice graphics = this.GraphicsDevice;
Vector3 startpoint = graphics.Viewport.Unproject(nearsource,
ProjectionMatrix,
ViewMatrix,
Matrix.Identity);
Vector3 endpoint = graphics.Viewport.Unproject(farsource,
ProjectionMatrix,
ViewMatrix,
Matrix.Identity);
Vector3 direction = endpoint - startpoint;
direction.Normalize();
Ray r = new Ray(startpoint, direction);
return r;
}
这似乎是可行的,但我不确定你想在你的应用程序中达到什么效果。
祝你好运 你确定你不想使用