2013-03-03 120 views
0

我正在研究一个A *算法,我希望能够在路径查找图中的节点之间画线,特别是那些通往出口的线。我工作的环境是3D。我只是不知道为什么我的代码不显示线条,所以我简化了它,以便它只渲染一行。现在我可以看到一条线,但它在屏幕空间中,而不是世界空间。有没有简单的方法在XNA的世界坐标中画线?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

你是不是开始或结束您的BasicEffect使投影和视图matricies不会被应用到DrawUserIndexedPrimitives。试着用这个encompasing您的通话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(); 
} 
+1

谢谢你的回应,但我使用XNA 4.0,这个版本BasicEffect的不具有法。 – NickLokarno 2013-03-04 01:31:50

+2

事实证明Begin()和End()不再是BasicEffect的一部分,而只需键入:pass.apply(),不用担心会结束任何事情。感谢您的回复,但这使我走上了正轨!我编辑你的回复以反映v4.0,所以我以前的评论可以忽略。 – NickLokarno 2013-03-04 02:07:12

+0

看起来像你编辑回来。这可能在XNA 3.0中有效,但不在4.0中。在4.0中,您只需调用pass.Begin(),但不需要basicEffect.Begin()或basicEffect.End() – NickLokarno 2013-04-05 23:25:24