我正在尝试使用可移动相机在屏幕上正确绘制一些3D模型,但遇到两个问题。 第一个问题立即可见:模型应该是立方体,但绘制为矩形棱柱。就像它们被挤压/压缩一样,但我找不到原因。
第二个问题出现在俯视“立方体”时。尽管它们应该只是旋转,但它们似乎在旋转时以一种奇怪的方式伸展。
Still looking down, but rotated CW 90 degrees
这是我的相机代码:
public class Camera
{
private Driver Driver;
private int ScreenWidth { get { return Driver.GraphicsDevice.Viewport.Width; } }
private int ScreenHeight { get { return Driver.GraphicsDevice.Viewport.Height; } }
public Matrix ViewMatrix { get; private set; }
public Matrix ProjectionMatrix { get; private set; }
private const float NearClippingPlane = 0.1f;
private const float FarClippingPlane = 200.0f;
public Vector3 Position { get; private set; }
private Vector3 _direction; // Do not use _direction!
private Vector3 Direction
{
get { return _direction; }
set
{
if (value == Vector3.Zero) { }
else
{
var normalizedTemp = value;
normalizedTemp.Normalize();
if (normalizedTemp.Y > -.9999f) _direction = normalizedTemp;
}
}
}
private Vector3 Up;
private const float MovementSpeed = 10f;
private const float PanSpeed = 10f;
private MouseState previousMouseState;
private Vector3 HorizontalPlaneDirection
{
get
{
var direction = new Vector3(Direction.X, 0, Direction.Z);
if (direction != Vector3.Zero)
direction.Normalize();
return direction;
}
}
public Camera(Driver game, Vector3 position, Vector3 target, Vector3 up)
{
Driver = game;
// Build camera view matrix
Position = position;
Direction = target - position;
Up = up;
CreateLookAt();
ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), ScreenWidth / ScreenHeight, NearClippingPlane, FarClippingPlane);
}
public void Initialize()
{
// Set mouse position and do initial get state
Mouse.SetPosition(ScreenWidth / 2, ScreenHeight / 2);
previousMouseState = Mouse.GetState();
}
public void Update(GameTime gameTime, KeyboardState keyboard, MouseState mouse)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
float moveSpeed = MovementSpeed * elapsed;
float panSpeed = PanSpeed * elapsed;
if (keyboard.IsKeyDown(Keys.W))
Position += HorizontalPlaneDirection * moveSpeed;
if (keyboard.IsKeyDown(Keys.S))
Position -= HorizontalPlaneDirection * moveSpeed;
if (keyboard.IsKeyDown(Keys.A))
Position += Vector3.Cross(Up, HorizontalPlaneDirection) * moveSpeed;
if (keyboard.IsKeyDown(Keys.D))
Position -= Vector3.Cross(Up, HorizontalPlaneDirection) * moveSpeed;
if (keyboard.IsKeyDown(Keys.Space))
Position += Up * moveSpeed;
if (keyboard.IsKeyDown(Keys.LeftShift))
Position -= Up * moveSpeed;
// Rotation in the world
Direction = Vector3.Transform(Direction, Matrix.CreateFromAxisAngle(Up, -MathHelper.ToRadians(1) * (mouse.X - previousMouseState.X) * panSpeed));
Direction = Vector3.Transform(Direction, Matrix.CreateFromAxisAngle(Vector3.Cross(Up, HorizontalPlaneDirection), MathHelper.ToRadians(1) * (mouse.Y - previousMouseState.Y) * panSpeed));
Driver.ResetMouseState();
// Reset prevMouseState
previousMouseState = Mouse.GetState();
CreateLookAt();
}
private void CreateLookAt()
{
ViewMatrix = Matrix.CreateLookAt(Position, Position + Direction, Up);
}
}
以及绘制模型的代码:
public void Draw(Camera Camera)
{
foreach (var mesh in Model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
//effect.EnableDefaultLighting();
//effect.PreferPerPixelLighting = true;
effect.World = GetWorldMatrix();
effect.View = Camera.ViewMatrix;
effect.Projection = Camera.ProjectionMatrix;
}
mesh.Draw();
}
}
我知道代码位很长,可能很难通过,但是我非常感谢您的帮助。谢谢!
答案 0 :(得分:0)
嗯,我很傻。解决方案很简单-问题在于投影矩阵。
在我的代码中,当我设置ProjectionMatrix
时,将其长宽比设置为ScreenWidth / ScreenHeight
。但是,由于这两个都是整数,因此结果也是也是整数。因此,不是将ScreenWidth / ScreenHeight
的非整数结果设为16:9的适当宽高比,而是将其强制转换为整数,而我的宽高比最终变为ScreenWidth / ScreenHeight = 1920 / 1080 = (int)1.7778 = 1
。
显然,解决方案很简单。将浮标放置到除数上,您将得到正确的答案。 ScreenWidth / (float)ScreenHeight = 1920 / 1080.0f = 1.7778f
。
我希望我的挣扎之日能够帮助其他人解决这个问题。另外,如果在XNA中创建3D相机时遇到其他问题,建议您查看this代码示例。它是为手机设计的(出于某种原因?),它是一个DLL,这意味着您将无法运行它(您必须添加一个可执行项目,引用该DLL并以任何方式重建该可执行项目。您可以对DLL进行编辑),但是它仍然是一个很好的起点/参考点。