xna动画问题

时间:2013-02-12 04:11:40

标签: 3d xna

我想绘制3轴X Y Z和模型 看看它在空间中的位置以及它是如何移动的

当我用简单的旋转绘制立方体时 一切都好吗

但是当我添加轴绘图方法

rendring不行

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 WindowsGame2
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
    //Some declaration 
    float x = 30f;
    float y = 0f;
    float z = 30f;

    //GraphicsDevice device;
    VertexBuffer vertexBuffer;
    VertexPositionColor[] lines;

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Model model;
    Matrix view;
    Matrix progection;
    float xTans = 0.0f;
    float yTans = 0.0f;
    float zTans = 0.0f;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {

        //device = graphics.GraphicsDevice;
        base.Initialize();
        this.IsMouseVisible = true;
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.

        spriteBatch = new SpriteBatch(GraphicsDevice);
        model = Content.Load<Model>("cube");

        DrawLines();

        createCamera();

    }

    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);

        //update camera
    }

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

        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (BasicEffect fx in mesh.Effects)
            {
                fx.View = view;

                //fx.World = Matrix.CreateRotationX(xTans);
                fx.World = Matrix.CreateRotationY(yTans);
                //fx.World = Matrix.CreateRotationZ(zTans);

                fx.Projection = progection;
                fx.EnableDefaultLighting();

            }
            mesh.Draw();

        }
        //xTans += 0.01f;
        yTans += 0.01f;
        //zTans += 0.01f;

        BasicEffect effect = new BasicEffect(graphics.GraphicsDevice);
        effect.VertexColorEnabled = true;

        effect.View = view;
        effect.Projection = progection;

        effect.World = Matrix.Identity;

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            // Begin Drawing
            pass.Apply();
            // Set the vertex buffer to graphic device
            graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer, 0);
            // Draw the axes with LineList Type
            graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
            PrimitiveType.LineList, lines, 0, 3);
            //pass.Apply();
        }
        base.Draw(gameTime);
    }

    protected void DrawLines()
    {
        lines = new VertexPositionColor[6];

        //Setting Size and Width and Color of the X axis
        lines[0] = new VertexPositionColor(new Vector3(50, 0, 0), Color.Red);
        lines[1] = new VertexPositionColor(new Vector3(-50, 0, 0), Color.Red);

        //Setting Size and Width and Color of the Y axis
        lines[2] = new VertexPositionColor(new Vector3(0, 50, 0), Color.Green);
        lines[3] = new VertexPositionColor(new Vector3(0, -50, 0), Color.Green);

        //Setting Size and Width and Color of the Z axis
        lines[4] = new VertexPositionColor(new Vector3(0, 0, 50), Color.Blue);
        lines[5] = new VertexPositionColor(new Vector3(0, 0, -50), Color.Blue);

        vertexBuffer = new VertexBuffer(graphics.GraphicsDevice,
            VertexPositionColor.VertexDeclaration, 6, BufferUsage.None);
        vertexBuffer.SetData<VertexPositionColor>(lines);
    }

    protected void createCamera()
    {
        view = Matrix.CreateLookAt(
            new Vector3(0f, 60.0f, 60f), 
            Vector3.Zero, 
            Vector3.Up);
        progection = Matrix.CreatePerspectiveFieldOfView(
            MathHelper.ToRadians(8.0f), 
            graphics.GraphicsDevice.Viewport.AspectRatio, 
            10.0f, 
            1000.0f);
    }        
} //end game class
} //end namespace

绘制轴的函数是drawLines()

在方法draw()

我有这个真正绘制轴

BasicEffect effect = new BasicEffect(graphics.GraphicsDevice);
effect.VertexColorEnabled = true;

effect.View = view;
effect.Projection = progection;

effect.World = Matrix.Identity;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
    // Begin Drawing
    pass.Apply();
    // Set the vertex buffer to graphic device
    graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer, 0);
    // Draw the axes with LineList Type
    graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
    PrimitiveType.LineList, lines, 0, 3);
    //pass.Apply();
}

我不知道为什么轮换的速度很慢

1 个答案:

答案 0 :(得分:1)

有好消息,有坏消息。好消息是您的项目可以顺利运行。你发布了足够的代码来构建你的项目,只要构建器有像我一样的fbx模型。这是必要的变化。

// take this out of your Draw() method.
BasicEffect effect = new BasicEffect(graphics.GraphicsDevice);

// put this in your class variables
BasicEffect effect;

//and put this at the end of LoadContent()
effect = new BasicEffect(graphics.GraphicsDevice);

帖子末尾的解释。

坏消息是你有大量的代码是坏的。它将编译,它将运行,但它不会运行良好。我认为你在编写XNA项目方面有些新意,这意味着你可能对游戏程序的组件缺乏了解。

虽然我会尝试解释你做错的一些事情,但你的帖子对于SO来说并不是一个好的帖子。这是简单的不良做法的结果,而不是技术难题。在gamedev.stackexchange.com处,有时会接受这样的问题,因为它们是如何(不)用特定技术编程游戏的好例子。当我完成回答时,我要把你的帖子标记为移动到那里;国防部可能会认为它不值得。

无论如何,这是我的最大努力:

您可以在加载内容方法中调用此函数。

protected override void LoadContent()
    ...
    DrawLines();
    ...
}

正如默认模板注释告诉您的那样,加载内容方法用于加载内容。不是为了画画。它只被调用一次,所以无论如何从它绘制一个帧是愚蠢的。但这并不是你正在用这种方法做的。你在那里宣布一些原始数据。没关系,但最后两行是多余的:

protected void DrawLines()
{
    lines = new VertexPositionColor[6];
    lines[0] = new VertexPositionColor(new Vector3(50, 0, 0), Color.Red);
    // ...

    vertexBuffer = new VertexBuffer(graphics.GraphicsDevice,
        VertexPositionColor.VertexDeclaration, 6, BufferUsage.None);
    vertexBuffer.SetData<VertexPositionColor>(lines);
    ...

您甚至不使用该顶点缓冲区。您使用DrawUserPrimitives<T>(),它为您设置顶点缓冲区。这意味着您也不需要使用draw方法中的这一行。

graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer, 0);

在开始效果传递后设置顶点数据。

这是一个坏主意。在图形硬件开始工作后中断它。这通常是游戏性能的瓶颈。在你的情况下,这不是一个坏主意。

您每帧都会创建一个新效果。

正如我上面所写,这是你唯一需要改变的地方。尽管它的名称,BasicEffect是一个复杂的OO容器,用于处理大量可配置的着色器片段,以及绘制具有多个纹理和材质和效果的复杂几何体所需的所有其他内容。并且您尝试每秒分配其中一个对象60次。

我的建议如下:阅读更多blog posts,了解更多example code,并学会使用a memory profiler