3

我们正在尝试对齐我们的相机,以便看到尽可能接近的矩形(平行六面体的面,因为我们在 3D 中)。此处考虑的视口的纵横比与要对齐的矩形不匹配。

我们所做的:

在我们的场景中就是这个矩形。摄像头位于其前方。下面是摄像头的配置:

projection = Matrix.CreatePerspectiveFieldOfView(
    Fov, AspectRatio, NearPlane, FarPlane
);

view = Matrix.CreateLookAt(Position, Target, this.Up);

float Fov = PI/4;

float AspectRatio = device.Viewport.AspectRatio;

Vector3 Target设置为矩形的中心。

Vector3 Position是我们正在寻找的价值。

然后初始化相机: oat width = (float)GraphicsDevice.Viewport.Width; 浮动高度 = (float)GraphicsDevice.Viewport.Height;

float dist = (width / 2) + (height / 2);

float alpha = 0;
float beta = 0;
float gamma = 0;

alpha = (float)Math.Sqrt(Math.Pow(width / 2, 2) + Math.Pow(height / 2, 2));
beta = height / ((float)Math.Cos(MathHelper.ToRadians(70.3f)) * 2);
gamma = (float)Math.Sqrt(beta * beta - alpha * alpha);


position = new Vector3(width / 2, -height / 2, gamma);
target = new Vector3(width / 2, -height / 2, dist / 2);

camera = new Camera(GraphicsDevice.Viewport)
{
    Position = position,
    Target = target,
    Near = 1f,
    Far = 10000f
};

VS 解决方案

更精确:为了创建视图,我们使用Matrix.CreateLookAt和 进行投影Matrix.CreatePerspectiveFieldOfView。我们也不确定这些是否是完美的选择,但听起来很像。

我们得到什么:

在一些基本的三角函数之后,我们得到了一个在高度 > 宽度时工作的相机位置。作为: 在此处输入图像描述 在此处输入图像描述

但是当宽度>高度时它不会: 在此处输入图像描述

你...

...知道我们做错了什么吗?您有其他或更好的方法来实现这一目标吗?

4

1 回答 1

0

找到金字塔的高度是行不通的。只需使用 Tan,因为Fov = PI/4代码应如下所示:

float tanGamma = (height / 2) * ((float)Math.Tan(MathHelper.ToRadians(67.5f)));
于 2012-09-03T07:50:31.740 回答