如何在XNA中绘制和移动方块?

时间:2012-06-18 14:22:57

标签: c# graphics xna

我正在尝试在XNA中绘制一个正方形,并根据用户输入简单地用键盘移动它。不幸的是,我甚至没有能够让广场显示出来 - 我很肯定我在这里遗漏了一些东西,但我无法确切地知道它是什么。我打算创建一个复杂的原始形状层次结构,我可以在游戏中使用它。 Shape类的主要组件已初始化,派生类(在本例中为Rect)利用这些组件进一步使用,并对其进行配置。

我在这里做错了什么?

代码

Shape.cs

public abstract class Shape
    {
        public Vector3 Center;
        public Color[] Colors;
        public bool SetColorsOnUpdate;

        public virtual Rectangle BoundingBox { get; set; }
        public float Radius { get { return mRadius; } }

        protected VertexBuffer mVertexBuf;
        protected IndexBuffer mIndexBuf;
        protected float mRadius;
        protected BasicEffect mShader;

        public Shape(Vector3 center, Color[] colors, int numVertices, float radius)
        {
            Colors = colors;
            Center = center;
            mVertexBuf = new VertexBuffer(Graphics.MainDevice, typeof(VertexPositionColor), numVertices, BufferUsage.WriteOnly);
            mIndexBuf = new IndexBuffer(Graphics.MainDevice, IndexElementSize.SixteenBits, numVertices, BufferUsage.WriteOnly);
            SetColorsOnUpdate = true;
            mShader = new BasicEffect(Graphics.MainDevice);
            mRadius = radius;
        }

        public abstract void Update();
        public abstract void Draw();
    }

Rect.cs

public class Rect : Shape
{
    public override Rectangle BoundingBox
    {
        get
        { 
            int x = (int)Center.X, y = (int)Center.Y;
            int diameter = (int)mRadius * 2;

            return new Rectangle(
                x, y,
                x + diameter, 
                y + diameter
            );
        }
    }

    const float TestRectZCoordinate = 0;
    const int NumVertices = 4;

    public Rect(Vector3 center, Color[] colors, float radius)
        : base(center, colors, NumVertices, radius)
    {
        if (colors.Length < NumVertices)
            throw new IndexOutOfRangeException(string.Format("Color array passed to Rect constructor MUST have an element index size of 4. Current length passed is {0}", colors.Length));

        mShader.VertexColorEnabled = true;

        mVertexBuf.SetData<VertexPositionColor>(
                new VertexPositionColor[]
                {
                    new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[0]),
                    new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[1]),
                    new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[2]),
                    new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[3])
                }
            );

        mIndexBuf.SetData<short>(new short[] { 0, 1, 2, 3 });
    }

    public override void Update()
    {
        //TODO
    }

    public override void Draw()
    {
        mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up);
        mShader.CurrentTechnique.Passes[0].Apply();

        Graphics.MainDevice.SetVertexBuffer(mVertexBuf);
        Graphics.MainDevice.Indices = mIndexBuf;

        Graphics.MainDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, NumVertices, 0, 1);
    }
}

更新

这是最新的Rect.Draw()实现。

public override void Draw()
    {
        Viewport viewport = Graphics.MainDevice.Viewport;

        mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up);
        mShader.View = Matrix.CreateLookAt(new Vector3(viewport.Width / 2, viewport.Height / 2, -5f), Center, Vector3.Up);
        mShader.CurrentTechnique.Passes[0].Apply();

        Graphics.MainDevice.SetVertexBuffer(mVertexBuf);
        Graphics.MainDevice.Indices = mIndexBuf;

        Graphics.MainDevice.DrawIndexedPrimitives(PrimitiveType.LineStrip, 0, 0, NumVertices, 0, 2);
    }

1 个答案:

答案 0 :(得分:1)

啊 - 我想我至少看到了你的一个问题。您正在绘制TriangleList(请参阅PrimitiveType),您需要6个索引,而不是4个。

知道了。看起来你的顶点/索引顺序是错误的。我也没有看到投影矩阵。

这里的代码对我来说非常有用 - 我所做的就是添加投影矩阵,重新排序顶点,更改索引并修复原始数。

http://pastebin.com/tMKCxBLL

希望这有帮助!