2015-10-05 45 views
2

我一直試圖在xna中繪製一個簡單的立方體,但它顯示爲完全黑色。我曾嘗試過多種不同的FBX模型。使用管道中模型的設置。我也要儘可能地應用基本的閃電。它仍然顯得黑色。模型在XNA中顯示爲黑色

我的代碼:

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using System; 

namespace Game1 
{ 

public class Game1 : Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    BasicEffect effect; 
    Texture2D floor; 
    Model model; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
    } 

    protected override void Initialize() 
    { 
     effect = new BasicEffect(graphics.GraphicsDevice); 

     base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     floor = Content.Load<Texture2D>("floor"); 
     model = Content.Load<Model>("cube"); 
    } 

    protected override void UnloadContent() 
    { 
    } 

    protected override void Update(GameTime gameTime) 
    { 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) 
      Exit(); 


     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 
     var cameraPosition = new Vector3((float)Math.Cos((double)gameTime.TotalGameTime.TotalMilliseconds/1000)*20, 40, (float)Math.Cos((double)gameTime.TotalGameTime.TotalMilliseconds/1000) * 20); 
     var cameraLookAtVector = Vector3.Zero; 
     var cameraUpVector = Vector3.UnitZ; 

     effect.View = Matrix.CreateLookAt(
      cameraPosition, cameraLookAtVector, cameraUpVector); 

     float aspectRatio = 
      graphics.PreferredBackBufferWidth/(float)graphics.PreferredBackBufferHeight; 
     float fieldOfView = Microsoft.Xna.Framework.MathHelper.PiOver4; 
     float nearClipPlane = 1; 
     float farClipPlane = 200; 

     effect.Projection = Matrix.CreatePerspectiveFieldOfView(
      fieldOfView, aspectRatio, nearClipPlane, farClipPlane); 

     effect.TextureEnabled = true; 
     effect.Texture = floor; 

     drawModel(model, effect.World, effect.View, effect.Projection); 

     foreach (var pass in effect.CurrentTechnique.Passes) 
     { 
      pass.Apply(); 

      drawQuad(new Vector3[] { 
       new Vector3(20,-20,0), 
       new Vector3(-20,-20,0), 
       new Vector3(-20,20,0), 
       new Vector3(20,20,0), 
      },2f); 
     } 


     base.Draw(gameTime); 
    } 

    public void drawQuad(Vector3[] p, float tiling) 
    { 
     VertexPositionNormalTexture[] verts = new VertexPositionNormalTexture[6]; 

     verts[0].Position = p[0]; 
     verts[1].Position = p[1]; 
     verts[2].Position = p[3]; 

     verts[3].Position = p[1]; 
     verts[4].Position = p[2]; 
     verts[5].Position = p[3]; 

     verts[0].Normal = p[0]; 
     verts[1].Normal = p[1]; 
     verts[2].Normal = p[3]; 

     verts[3].Normal = p[1]; 
     verts[4].Normal = p[2]; 
     verts[5].Normal = p[3]; 

     verts[0].TextureCoordinate = new Vector2(0, 0); 
     verts[1].TextureCoordinate = new Vector2(0, tiling); 
     verts[2].TextureCoordinate = new Vector2(tiling, 0); 

     verts[3].TextureCoordinate = verts[1].TextureCoordinate; 
     verts[4].TextureCoordinate = new Vector2(tiling, tiling); 
     verts[5].TextureCoordinate = verts[2].TextureCoordinate; 

     graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, verts, 0, 2); 
    } 

    public void drawModel(Model model, Matrix world, Matrix view, Matrix projection) 
    { 
     foreach (var mesh in model.Meshes) 
     { 
      foreach (BasicEffect effect in mesh.Effects) 
      { 
        effect.EnableDefaultLighting(); 
        effect.DiffuseColor = Color.White.ToVector3(); 
        effect.World = world; 
        effect.View = view; 
        effect.Projection = projection; 
      } 

      mesh.Draw(); 
     } 
    } 
} 

}

回答

1

FBX不會在模型文件本身嵌入紋理,它只是存儲路徑紋理文件。此路徑通常是相對於導出位置(但取決於工具)。

嘗試將紋理文件放置在與導出位置相同的相對路徑中,但放置在可執行文件的輸出目錄中。否則,FBX文件是人類可讀的IIRC,所以你應該能夠確定它在哪裏尋找紋理,並把它放在那裏。

+0

好的,我之前沒有使用紋理文件。這是必須的嗎?無論如何。我創建了同一個立方體並對其應用了紋理。紋理明確地處於正確的路徑中。當我在管道中構建內容時,如果它不在正確的路徑中,它將返回一個錯誤。所以內容構建是成功的,但它仍然是完全黑色的。這真的很奇怪。另外,monogame似乎不支持acii fbx文件,它們需要是二進制文件。 – Voxed

+0

@ceoepts是的,XNA支持很少的「材料」。你需要包含紋理文件。紋理是否也編譯成XNB?你包括它嗎? – BradleyDotNET

+0

是的,我包括monogame管道中的所有內容。我可以看到bin文件夾中的所有xnb。我真的不知道要測試什麼:/ – Voxed