C#XNA 3D模型无法渲染

时间:2013-01-28 21:10:22

标签: c# 3d xna rendering

该模型是.fbx 我也尝试了很多模型,似乎没有什么工作我想知道是否有人可以帮助我,以下代码是我所有感谢!

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _3D
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Model model;
        Matrix[] transforms;

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

        protected override void Initialize()
        {

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            model = Content.Load<Model>("3d/screwdriver");
            transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);
        }
        protected override void UnloadContent()
        {
        }


        protected override void Update(GameTime gameTime)
        {

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            base.Update(gameTime);
        }    

        // The draw method is one of the reasons I think nothing is working,

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            Matrix view = Matrix.CreateLookAt(
                new Vector3(200, 300, 900),
                new Vector3(0, 50, 0),
                Vector3.Up);
            Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 10000.0f);
            Matrix baseWorld = Matrix.CreateScale(0.4f) * Matrix.CreateRotationY(MathHelper.ToRadians(180));
            foreach (ModelMesh mesh in model.Meshes)
            {
                Matrix localWorld = transforms[mesh.ParentBone.Index] * baseWorld;
                foreach (ModelMeshPart part in mesh.MeshParts)
                { 
                BasicEffect e = (BasicEffect)part.Effect;
                e.World = localWorld;
                e.View = view;
                e.Projection = projection;
                e.EnableDefaultLighting();

                }
                mesh.Draw();
            }
            base.Draw(gameTime);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我没有看到您的代码出现任何问题,因此问题可能出在模型上。

可能是由相关模型文件引起的。 Blender上的股票FBX Exporter存在一个已知问题(我不知道您使用哪种建模软件制作模型,但事情仍然相关),这使得模型的导出尺寸比其大100倍搅拌机。结果是模型非常大,它不仅将摄像机封装在自身内部,而且还非常大,以至于它的多边形超出了观看的截头体。结果:模型确实已渲染,但除非您移动相机直到意识到缩放问题,否则它将永远不会出现在屏幕上。

我这样说是因为我自己处理了这个问题。如果您确实使用Blender,则应使用此页面中的导出器: http://blog.diabolicalgame.co.uk/2011/07/exporting-animated-models-from-blender.html