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

3d XNA-如何在世界空间中绘制线条?,3d,xna,lines,perspective,3d,Xna,Lines,Perspective,我正在研究A*算法,我希望能够在路径查找图中的节点之间画线,特别是那些通向出口的节点。我工作的环境是3D的。我就是不明白为什么我的代码没有显示这些行,所以我简化了代码,使它只显示一行。现在我可以看到一条线,但它在屏幕空间,而不是世界空间。有没有一种简单的方法可以在XNA的世界坐标系中画线 代码如下: _lineVtxList = new VertexPositionColor[2]; _lineListIndices = new short[2];

我正在研究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
                        );
            }
\u lineVtxList=新的顶点曝光颜色[2];
_LineListIndexes=新短[2];
_LineListIndexes[0]=0;
_LineListIndexes[1]=1;
_lineVtxList[0]=新的VertexPositionColor(新向量3(0,0,0),Color.MediumPurple);
_lineVtxList[1]=新的VertexPositionColor(新矢量3(100,0,0),Color.MediumPurple);
numLines=1;
....
BasicFect BasicFect=新的BasicFect(g);
basicEffect.VertexColorEnabled=真;
BasicFect.CurrentTechnical.Passes[0]。Apply();
basicEffect.World=Matrix.CreateTranslation(新向量3(0,0,0));
基本效果投影=projMat;
basicEffect.View=viewMat;
如果(_lineListIndexes!=null&&u lineVtxList!=null)
{
//g.DrawUserPrimitives(PrimitiveType.LineList,_lineVtxList,0,1);
g、 DrawUserIndexedPrimitions(
PrimitiveType.LineList,
_lineVtxList,
0,//要添加到索引缓冲区的每个元素的顶点缓冲区偏移量
_lineVtxList.Length,//点列表中的顶点数
_LineListIndexes,//索引缓冲区
0,//要读取的第一个索引元素
numLines//要绘制的基本体的数量
);
}
矩阵projMat和viewMat是我用于渲染的相同视图和投影矩阵 现场的其他一切。我是否将它们指定为BasicFect似乎并不重要。下面是场景的样子:


您没有开始或结束您的
基本效果
,因此投影和视图矩阵将不会应用于
DrawUserIndexedPrimitives
。尝试将您的
DrawUserIndexedPrimitions
调用与以下内容进行比较:

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();
}
if(_linelistIndexes!=null&&u lineVtxList!=null)
{
基本效果。开始();
foreach(在basicEffect.currentTechnical.Passses中传递)
{
pass.Begin();
g、 DrawUserIndexedPrimitives(…);//您拥有它的方式
pass.End();
}
基本效果结束();
}

谢谢您的回复,但我使用的是XNA 4.0,这个版本的BasicFect没有这个方法。结果表明,Begin()和End()不再是BasicFect的一部分,您只需键入:pass.apply(),并且不必担心结束任何内容。谢谢你的回复,这让我走上了正确的道路!我编辑了您的回复以反映v4.0,因此可以忽略我以前的评论。看起来您已将其重新编辑。这可能在XNA3.0中有效,但在4.0中无效。在4.0中,您只需调用pass.Begin(),但不需要basicfect.Begin()或basicfect.End()@NickLokarno-Odd。。。我没碰你的编辑。您的编辑可能未通过审核流程获得批准,否则会有“编辑人”标记。似乎很奇怪:/