1

I'm creating a bounding box and for debugging would like to see where it is. It is define as four corners and I simply want to draw line in between these 4 corners.

I managed to Google how to do this:

    public void Draw(GraphicsDevice graphicsDevice)
    {
        int num = mCorners.Length;
        VertexPositionColor[] primitiveList = new VertexPositionColor[num];

        for (int i = 0; i < num; ++i)
        {
            primitiveList[i] = new VertexPositionColor(new Vector3(mCorners[i], 0), Color.White);
        }

        short[] triangleStripIndices = new short[] { 0, 1, 2, 3, };
        graphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, primitiveList, 0, 4, triangleStripIndices, 0, 3);
    }

However, when this code is ran the app just closes. It is in debug mode but there is no stack trace, crash message, error log etc. It just closes, making it very hard to debug.

I've found a similar question that doesn't have an answer: XNA 4.0 app closes suddenly when it hits a method without throwing any exceptions. The suggestion was is it being initialised properly, and yes mine is. The GraphicsDevice is passed around as a parameter and not obtained statically.

Does anybody know what may be causing this?

Thanks,

4

2 回答 2

0

嗨,卢克,试着像这样把你的代码放在 Dispatcher 中

   this.Dispatcher.BeginInvoke(new System.Action(delegate()
            {
int num = mCorners.Length;
        VertexPositionColor[] primitiveList = new VertexPositionColor[num];

        for (int i = 0; i < num; ++i)
        {
            primitiveList[i] = new VertexPositionColor(new Vector3(mCorners[i], 0), Color.White);
        }

        short[] triangleStripIndices = new short[] { 0, 1, 2, 3, };
        graphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, primitiveList, 0, 4, triangleStripIndices, 0, 3);


            }));

当 UI 需要更改并且过程仍在工作时,系统似乎崩溃了。它会引发未处理的异常。

于 2012-06-07T18:19:20.970 回答
0

Ok, figured it out. Code is as follows:

    private BasicEffect mBasicEffect;

    public void Draw(GraphicsDevice graphicsDevice)
    {
        // If we haven't set this up yet then do so now
        if (mBasicEffect == null)
        {
            CreateBasicEffect(graphicsDevice);
        }

        foreach (EffectPass pass in mBasicEffect.CurrentTechnique.Passes)
        {
            pass.Apply();

            int num = mCorners.Length + 1;
            short[] triangleStripIndices = new short[num];
            VertexPositionColor[] primitiveList = new VertexPositionColor[num];

            for (int i = 0; i < num; ++i)
            {
                int index = i % mCorners.Length;
                Vector2 vec = mCorners[index];
                primitiveList[index] = new VertexPositionColor(new Vector3(vec, 0), Color.White);
                triangleStripIndices[i] = (short)i;
            }

            graphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, primitiveList, 0, 5, triangleStripIndices, 0, 4);
        }
    }

    private void CreateBasicEffect(GraphicsDevice device)
    {
        mBasicEffect = new BasicEffect(device);
        mBasicEffect.VertexColorEnabled = true;

        Matrix viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 1), Vector3.Zero, Vector3.Up);
        Matrix worldMatrix = Matrix.CreateTranslation(0, 0, 0);
        Matrix projectionMatrix = Matrix.CreateOrthographicOffCenter(0, device.Viewport.Width, device.Viewport.Height, 0, 1, 1000);

        mBasicEffect.World = worldMatrix;
        mBasicEffect.View = viewMatrix;
        mBasicEffect.Projection = projectionMatrix;
    }
于 2012-06-08T15:30:47.923 回答