2013-03-15 98 views
0

我试图在另一个类中绘制模型。 我试图在另一个班级中绘制模型。然而,我的模型根本不会得到我所做的代码的渲染。我可以在另一个班级中绘制模型吗?

这里的Game1代码:注:这些代码很好的工作的Game1)

private Vector3 position = new Vector3(0, 0, 0); 
private Matrix world; 
private Matrix view; 
private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f/600f, 0.1f, 100f); 
private Model car; 
SpriteBatch spriteBatch; 
GraphicsDeviceManager graphics; 
public Game1() 
{ 

    graphics = new GraphicsDeviceManager(this); 
    Content.RootDirectory = "Content"; 
    Vector3 transformedReference = Vector3.Transform(new Vector3(0, 5, 15), Matrix.CreateRotationY(0f)); 
    view = Matrix.CreateLookAt(position + transformedReference, position, Vector3.Up); 
} 
protected override void Initialize() 
{ 
    Components.Add(new Car(this, view, projection)); 
    world = Matrix.CreateTranslation(position); 
    Vector3 transformedReference = Vector3.Transform(new Vector3(0, 5, 15), Matrix.CreateRotationY(0f)); 
    view = Matrix.CreateLookAt(position + transformedReference, position, Vector3.Up); 
    base.Initialize(); 
} 
protected override void LoadContent() 
{ 
    spriteBatch = new SpriteBatch(GraphicsDevice); 
    car = Content.Load <Model> ("car\\car"); 
} 
public void DrawModel(Model model, Matrix world, Matrix view, Matrix projection) 
{ 

    foreach(ModelMesh mesh in model.Meshes) 
    { 
     foreach(BasicEffect effect in mesh.Effects) 
     { 

      effect.EnableDefaultLighting(); 
      effect.PreferPerPixelLighting = true; 
      effect.World = mesh.ParentBone.Transform * world; 
      effect.View = view; 
      effect.Projection = projection; 
     } 

     mesh.Draw(); 
    } 
} 
protected override void Update(GameTime gameTime) 
{ 
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
     this.Exit(); 
    world = Matrix.CreateTranslation(position); 
    base.Update(gameTime); 
} 
protected override void Draw(GameTime gameTime) 
{ 
    GraphicsDevice.Clear(Color.CornflowerBlue); 

    DrawModel(car, world, view, projection); 
    base.Draw(gameTime); 
} 

,在这里我的代码另一个类:注:这些代码没有按不行,或者模型不能在Game1图形上渲染)

  public class Car: DrawableGameComponent 
{ 
    public Model CarModel 
    { 
     get; 
     set; 
    } 
    private Vector3 position = new Vector3(0, 0, 0); 
    private Matrix World = Matrix.CreateTranslation(new Vector3(0, 0, 0)); 
    public Matrix Camera 
    { 
     get; 
     set; 
    } 
    public Matrix Projection 
    { 
     get; 
     set; 
    } 
    public Game1 GameParent 
    { 
     get; 
     set; 
    } 
    public Car(Game1 game, Matrix view, Matrix projection): base(game) 
    { 
     view = Camera; 
     Projection = projection; 
     Camera = view; 
     GameParent = game; 

     World = Matrix.CreateTranslation(position); 
     base.Initialize(); 
    } 

    public static void DrawModel(Model model, Matrix world, Matrix view, Matrix projection) 
    { 
     foreach(ModelMesh mesh in model.Meshes) 
     { 
      foreach(BasicEffect effect in mesh.Effects) 
      { 

       effect.EnableDefaultLighting(); 
       effect.PreferPerPixelLighting = true; 
       effect.World = mesh.ParentBone.Transform * world; 
       effect.View = view; 
       effect.Projection = projection; 
      } 

      mesh.Draw(); 
     } 
    } 
    protected override void LoadContent() 
    { 
     CarModel = GameParent.Content.Load <Model> ("car\\car"); 
     base.LoadContent(); 
    } 
    public override void Update(GameTime gameTime) 
    { 
     base.Update(gameTime); 
    } 
    public override void Draw(GameTime gameTime) 
    { 
     DrawModel(CarModel, World, Camera, Projection); //DOESN'T WORK WELL!! 
     base.Draw(gameTime); 
    } 
} 

好的,我的观点只是w蚂蚁在另一个班级绘制3D模型,,

好吧,我该怎么做才能解决这个问题?

我希望你不介意帮我,你能明白我的意思..

+0

当然可以,要做到这一点的一种方法是在每个模型(或模型列表底部的方法调用平局的功能! )。我没有把这个作为答案,因为我没有时间做一个工作的例子 – Sayse 2013-03-15 07:54:20

+0

我想你应该问你的问题在http://gamedev.stackexchange.com/ – 2013-03-15 07:57:52

+0

@Sayse:请给我的例子,如果你没有你可以给我链接,这个问题的相关链接 – Ricks 2013-03-15 08:12:34

回答

2

这是很难看到什么是你的代码错误,因为它包含了一个两个版本。清理一下。这里有一些提示:

你不想使用DrawableGameComponent来获取单个对象,以我的经验。坚持写你自己的课程,并把它们放入集合或类似的东西。这样,你就不必处理XNA发布你的更新的巫术,并为你画画。更好地控制自己。

您不希望您的CAR处理视图和投影矩阵。留下你的游戏类(现在应该有一个相机类),并将它们传递给你的Car.Draw方法。我看到你在构造函数中传递它,但是Matrixes是值类型的,就我所能记得的,所以在代码的其他部分对视图的更改不会传播到你的汽车。

所以更改Car.Draw到:

public void Draw(Matrix view, Matrix projection) 
{ 
    DrawModel(view, projection); 
} 

正如你可能从我的变化去画,你也应该让DrawModel正常的方法(消除静电),使其不必接受模型和世界。

你的车应该有一个四元数或类似的旋转。 Car.World可以写成:

Matrix World = Matrix.Identity; 

//In update: 
World = Matrix.FromQuaternion(Rotation) * Matrix.CreateTranslation(Position); 

让Car的构造函数以Model为参数。这样你也可以抛弃“GameParent”和LoadContent-Method。

在你游戏级:

沟静态DrawModel方法。 沟领域世界和汽车。他们现在属于Car-class。

做一个字段(类级变量不是一个属性)为您的爱车:

Car MyCar = null; 

在Game1.LoadContent:

MyCar = new Car(Content.Load<Model>("car//car")); 

在的Game1。更新:

MyCar.Update(gameTime); 

在Game1.Draw:

GraphicsDevice.Clear(Color.CornFlowerBlue); 
MyCar.Draw(View, Projection); 

编辑;

对于一个游戏引擎,你通常会具有一些“零件”那你现在缺少:

  • 游戏状态系统(菜单是一个类型的国家,NormalLevel和BossLeve有其他的例子)
  • 相机服务(所以你可以从任何地方得到当前的视图/投影矩阵) - 定时服务(所以你可以从任何地方获得(浮动)gameTime.ElapsedGametime.TotalSeconds)
  • 输入服务(所以你可以得到每更新输入值来自任何地方)

一个摄像头的系统可以是简单的:

public interface ICamera 
{ 
    Vector3 Position { get; } 
    Matrix View { get; } 
    Matrix Projection { get; } 

    void Update(float deltaTime); 
    void Target(Vector3 targetPosition); 
} 

public class CameraService 
{ 
    public static ICamera ActiveCamera { get; private set; } 

    public static void ActivateCamera(ICamera camera) 
    { 
     if (ActiveCamera != null) 
      camera.Target(ActiveCamera.Target); 

     ActiveCamera = camera; 
    } 

    public static Update(float deltaTime) 
    { 
     if (ActiveCamera != null) 
      ActiveCamera.Update(deltaTime); 
    } 
} 

public class BasicCamera: ICamera 
{ 
    public Vector3 Position { get; protected set; } 
    public Matrix View { get; protected set; } 
    public Matrix Projection { get; protected set; } 

    public void Target(Vector3 targetPosition) 
    { 
     View = Matrix.CreateLookAt(Position, targetPosition, something something); 
    } 

    public BasicCamera(Vector3 position, Vector3 target) 
    { 
     //Set shit up 
    } 
} 
+0

好吧,,,这是工作很好..但如果有很多实例呢?我应该画每个实例吗? 我如何删除实例,如果不再使用,就像List.Remove() – Ricks 2013-03-15 08:59:10

+0

你应该有一个“汽车管理器”,保持汽车的集合,并通过循环来更新和绘制。它应该照顾到删除和添加汽车到你的游戏。 – 2013-03-15 09:51:32

+0

好吧,我明白了。当我被困住时,你真的给了我一个想法。 感谢您的回答。 – Ricks 2013-03-15 10:08:49

相关问题