XNA模型未显示在屏幕上

时间:2014-08-10 07:01:49

标签: c# windows model xna blender

我正在尝试显示我在Blender中创建的模型,现在我已将其导出到fbx中,当我将其渲染到XNA时,它工作不正常,然后我从互联网下载模型以确认我的模型是正确的或者不是,但是当我从互联网上下载模型时,它仍然没有正常工作。

 public class Game1 : Microsoft.Xna.Framework.Game
 {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Model myModel;
    Vector3 modelPosition = Vector3.Zero,
            cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f);

    private float GetMaxMeshRadius(Model m)
    {
        float radius = 0.0f;
        foreach (ModelMesh mm in m.Meshes)
        {
            if (mm.BoundingSphere.Radius > radius) 
            {
                radius = mm.BoundingSphere.Radius; 
            }
        }
        return radius;
    }

    private void DrawModelViaMeshes(Model m, float radius, Matrix proj, Matrix view)
    {
        Matrix world = Matrix.CreateScale(1.0f / radius);
        foreach (ModelMesh mm in myModel.Meshes)
        {
            foreach (Effect e in mm.Effects)
            {
                IEffectMatrices iem = e as IEffectMatrices;
                if (iem != null)
                {
                    iem.World = GetParentTransform(m, mm.ParentBone) * world;
                    iem.Projection = proj;
                    iem.View = view;
                }
            }
            mm.Draw();
        }
    }

    private Matrix GetParentTransform(Model m, ModelBone mb)
    {
     return (mb == m.Root)?mb.Transform : mb.Transform*GetParentTransform(m,mb.Parent);        
    } 

    private void DrawModel(Model m, float radius, Matrix proj, Matrix view)
    {
        m.Draw(Matrix.CreateScale(1.0f / radius), view, proj); 
    }

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

    /// <summary>
   /// Allows the game to perform any initialization it needs to before starting to run  
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        myModel = Content.Load<Model>("Models\\lumberJack");
        // TODO: use this.Content to load your game content here
    }


    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>


    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here

        float radius = GetMaxMeshRadius(myModel);
        Matrix proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                                GraphicsDevice.Viewport.AspectRatio,
                                1.0f, 100.0f);
        Matrix view = Matrix.CreateLookAt(new Vector3(2, 3, 4),
                        Vector3.Zero, Vector3.Up);

        ////I have tried 3 different mathods to draw my model but still no one is   
        working non display my Model on screen

        DrawModel(myModel, radius, proj, view);

        DrawModelViaMeshes(myModel, radius, proj, view);
        DrawModelViaVertexBuffer(myModel,radius,proj,view);
        base.Draw(gameTime);
    }

 private void DrawModelViaVertexBuffer(Model m, float radius, Matrix proj, Matrix view) 
    {
        Matrix world = Matrix.CreateScale(1.0f /radius);

        foreach (ModelMesh mm in m.Meshes) 
        {

        foreach(ModelMeshPart mmp in mm.MeshParts)
        {
           IEffectMatrices iem = mmp.Effect as IEffectMatrices;
           if ((mmp.Effect != null) && (iem != null))
           {
            iem.World = GetParentTransform(m, mm.ParentBone) * world; 
            iem.Projection = proj;
            iem.View = view;   
            GraphicsDevice.SetVertexBuffer(mmp.VertexBuffer,mmp.VertexOffset);  
            GraphicsDevice.Indices = mmp.IndexBuffer;
            foreach (EffectPass ep in mmp.Effect.CurrentTechnique.Passes)
            {
            ep.Apply();
            GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,   
                      0, 0, mmp.NumVertices, mmp.StartIndex, mmp.PrimitiveCount);
                    }
                }

            }
            mm.Draw();
        } 
    }
  }
 }

0 个答案:

没有答案