0

我试图在 XNA 中绘制一个正方形,然后根据用户输入简单地用键盘移动它。不幸的是,我什至无法让正方形显示 - 我很肯定我在这里遗漏了一些东西,但我无法弄清楚它到底是什么。我计划创建一个可以在我的游戏中使用的原始形状的复杂层次结构。类的主要组件Shape被初始化,派生类(在这种情况下Rect)利用这些组件进一步使用,并配置它们。

我在这里做错了什么?

代码

形状.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();
    }

矩形文件

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

1 回答 1

1

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

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

这是对我来说非常有用的代码——我所做的只是添加一个投影矩阵、重新排序顶点、更改索引并修复图元计数。

http://pastebin.com/tMKCxBLL

希望这可以帮助!

于 2012-06-18T14:32:19.410 回答