我试图了解投影向量的值范围是什么。这似乎与 MSDN 所说的不一致。
下面的代码输出一个彩色三角形。三角形完全可见。我希望在预测顶点之后,以下情况将是正确的:
X 和 Y 在 -1 到 1 的范围内。Z 在 0 到 1 的范围内。
我从这里得到了这些信息:http: //msdn.microsoft.com/en-us/library/bb195665.aspx
但是,当我使用视图和投影矩阵的乘积手动转换三角形的顶点时,它们具有以下值:
{X:1,8 Y:0 Z:3,991996} {X:0 Y:3 Z:3,991996} {X:-1,8 Y:0 Z:3,991996}
为什么这些值超出了可见范围,而三角形却完全可见?
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using Microsoft.Xna.Framework.Input;
namespace WasMachtDieProjektion
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
Matrix _view;
Matrix _projection;
VertexPositionColor[] _verticies;
Vector3[] _vectors;
BasicEffect _effect;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void LoadContent()
{
_vectors = new Vector3[] {
new Vector3(-3,0,0),
new Vector3(0,3,0),
new Vector3(3,0,0)
};
_verticies = new VertexPositionColor[] {
new VertexPositionColor(_vectors[0], Color.AliceBlue),
new VertexPositionColor(_vectors[1], Color.Yellow),
new VertexPositionColor(_vectors[2], Color.Red)
};
_effect = new BasicEffect(graphics.GraphicsDevice);
_effect.VertexColorEnabled = true;
GraphicsDevice.RasterizerState = new RasterizerState() { CullMode = CullMode.None };
}
protected override void Update(GameTime gameTime)
{
_view = Matrix.CreateLookAt(new Vector3(0, 0, -4f), new Vector3(0, 0, 0), Vector3.Up);
_projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, GraphicsDevice.Viewport.AspectRatio, 0.01f, 20f);
Vector3[] transformed = new Vector3[3];
Matrix trans = _view * _projection;
Vector3.Transform(_vectors, ref trans, transformed);
foreach (var v in transformed)
{
Console.WriteLine(v);
}
Console.WriteLine("---");
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_effect.View = _view;
_effect.Projection = _projection;
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, _verticies, 0, 1);
}
base.Draw(gameTime);
}
}
}