我的上一个问题被搁置了,因为我只是试图提供我的所有源代码(实际上这是一个非常小的例子)。我的问题是我能够绘制一个四边形并定位我的相机并面向{0,0,0}。无论出于何种原因,当我绘制我的模型时应该是站在正中心的士兵,我看不到它。在调试时我看到模型正确加载,有骨骼/网格等,所以我只是不确定为什么我看不到它。
我的相机设置如下:
public class CameraManager : GameComponent
{
private GraphicsDeviceManager fGraphicsDeviceManager;
public Matrix ViewMatrix { get; private set; }
public Matrix ProjectionMatrix { get; private set; }
public Vector3 CameraPosition { get; set; }
public Vector3 CameraTarget { get; set; }
public float AspectRatio { get; set; }
public float NearClip { get; set; }
public float FarClip { get; set; }
public float ViewAngle { get; set; }
public CameraManager(Game pGame, GraphicsDeviceManager pGraphicsDeviceManager) : base(pGame)
{
fGraphicsDeviceManager = pGraphicsDeviceManager;
CameraPosition = new Vector3(500, 300, 100);
CameraTarget = Vector3.Zero;
Viewport vViewPort = pGraphicsDeviceManager.GraphicsDevice.Viewport;
AspectRatio = (float)vViewPort.Width / (float)vViewPort.Height;
NearClip = 1.0f;
FarClip = 2000.0f;
ViewAngle = MathHelper.PiOver4;
}
public override void Update(GameTime pGameTime)
{
ViewMatrix = Matrix.CreateLookAt(CameraPosition, CameraTarget, Vector3.Up);
ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(ViewAngle, AspectRatio, NearClip, FarClip);
base.Update(pGameTime);
}
}
请注意,相机不会改变,它只是在看零的世界中设置静态。
现在是模型类:
public class Soldier : DrawableGameComponent
{
Game1 fGame;
Model fModel;
public Vector3 Forward { get; set; }
public Vector3 Position { get; set; }
public Soldier(Game1 pGame)
: base(pGame)
{
fGame = pGame;
Forward = Vector3.Forward;
Position = Vector3.Zero;
}
protected override void LoadContent()
{
fModel = fGame.Content.Load<Model>(@"Models\Human\Soldier\MP_US_Support");
//fModel = fGame.Content.Load<Model>(@"Models\Vehicles\Planes\model_plane");
}
public override void Update(GameTime pGameTime)
{
base.Update(pGameTime);
}
public override void Draw(GameTime pGameTime)
{
foreach (ModelMesh vMesh in fModel.Meshes)
{
foreach (BasicEffect vBasicEffect in vMesh.Effects)
{
vBasicEffect.Projection = fGame.fCameraManager.ViewMatrix;
vBasicEffect.View = fGame.fCameraManager.ProjectionMatrix;
vBasicEffect.World = Matrix.Identity;
}
vMesh.Draw();
}
base.Draw(pGameTime);
}
}
同样,模型加载正常(我认为)。我将投影/视图设置为相机的ViewMatrix / ProjectionMatrix。
最后在我的Game1.cs中,我将它们添加为组件:
protected override void Initialize()
{
//Initialize Camera
fCameraManager = new CameraManager(this,fGraphicsDeviceManager);
this.Components.Add(fCameraManager);
//Initialize Map
fMap = new FlatMap(this);
this.Components.Add(fMap);
//Initialize a Soldier
fSoldier = new Soldier(this);
this.Components.Add(fSoldier);
this.IsMouseVisible = true;
base.Initialize();
}
如果有人对完整的源代码感兴趣,可以在这里找到: http://www.filedropper.com/worldexplorergame(26mb由于模型,否则源代码的数量非常小)。
答案 0 :(得分:1)
您已交换投影和视图转换:
vBasicEffect.Projection = fGame.fCameraManager.ProjectionMatrix;
vBasicEffect.View = fGame.fCameraManager.ViewMatrix;
不过,你的模型看起来有点太详细了。如果你想拥有多名士兵并希望保持实时性,你应该考虑简化模型。