2013-03-15 95 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

相關問題