XNA - 如何在世界空间中绘制线条?

时间:2013-03-03 23:16:39

标签: 3d xna lines perspective

我正在研究A *算法,我希望能够在路径查找图中的节点之间绘制线条,尤其是那些通向出口的线条。我正在工作的环境是3D。我只是无法弄清楚为什么我的代码没有显示行,所以我简化它以便它只渲染一行。现在我可以看到一条线,但它在屏幕空间中,而不是世界空间。有没有一种简单的方法在XNA中以世界坐标绘制线条?

以下是代码:

        _lineVtxList = new VertexPositionColor[2];
        _lineListIndices = new short[2];

        _lineListIndices[0] = 0;
        _lineListIndices[1] = 1;
        _lineVtxList[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.MediumPurple);
        _lineVtxList[1] = new VertexPositionColor(new Vector3(100, 0, 0), Color.MediumPurple);
        numLines = 1;
....
            BasicEffect basicEffect = new BasicEffect(g);
            basicEffect.VertexColorEnabled = true;
            basicEffect.CurrentTechnique.Passes[0].Apply();
            basicEffect.World = Matrix.CreateTranslation(new Vector3(0, 0, 0));
            basicEffect.Projection = projMat;
            basicEffect.View = viewMat;
            if (_lineListIndices != null && _lineVtxList != null)
            {

            //    g.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, _lineVtxList, 0, 1);

                g.DrawUserIndexedPrimitives<VertexPositionColor>(
                        PrimitiveType.LineList,
                        _lineVtxList,
                        0,  // vertex buffer offset to add to each element of the index buffer
                        _lineVtxList.Length,  // number of vertices in pointList
                        _lineListIndices,  // the index buffer
                        0,          // first index element to read
                        numLines   // number of primitives to draw
                        );
            }

矩阵projMat和viewMat是我用来渲染的相同视图和投影矩阵 场景中的其他一切。我是否将它们分配给basicEffect似乎并不重要。这是场景的样子:

enter image description here

1 个答案:

答案 0 :(得分:1)

您没有开始或结束BasicEffect,因此投影和查看矩阵无法应用于DrawUserIndexedPrimitives。尝试使用以下内容进行DrawUserIndexedPrimitives来电:

if (_lineListIndices != null && _lineVtxList != null)
{
    basicEffect.Begin();
    foreach (EffectPass pass in basicEffect.CurrentTechnique.Passses)
    {
        pass.Begin();
        g.DrawUserIndexedPrimitives<VertexPositionColor>(...); // The way you have it
        pass.End();
    }
    basicEffect.End();
}