使用DrawUserPrimitives的C#DirectX Draw行工作正常,但DrawPrimitives不起作用

时间:2012-06-08 06:56:56

标签: c# drawing directx lines

我在C#中遇到了DirectX的问题。我想画几行。首先我用DrawUserPrimitives完成它,一切都很好。但后来我切换到了vertexBuffer因为我想进行旋转和其他相机操作。我在窗口看不到任何东西。

有部分代码填充vertexBuffer并在平面上绘制顶点。

vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 
   8 * (CurrentPanel.ElementsCount() + 1), m_device, Usage.None,
   CustomVertex.PositionColored.Format, Pool.Default);
CustomVertex.PositionColored[] verts =
   (CustomVertex.PositionColored[])vertexBuffer.Lock(0, 0);
//this function returns array of verts based on given points.
verts = CurrentPanel.GetLines(); 
vertexBuffer.Unlock();
m_device.Clear(ClearFlags.Target, System.Drawing.Color.FromArgb(255, 255, 255).ToArgb(), 1.0f, 0);
m_device.BeginScene(); //m_device is my DirectX.Device
SetupViewport(); //Set all of matrixes...
m_device.SetStreamSource(0, vertexBuffer, 0);
m_device.VertexFormat = CustomVertex.PositionColored.Format;
//m_device.DrawUserPrimitives(PrimitiveType.LineList, CurrentPanel.ElementsCount() * 4, verts); // <- WHEN I USE THIS ALL IS OK
m_device.DrawPrimitives(PrimitiveType.LineList, 0, 4*(CurrentPanel.ElementsCount()+1)); //<-DO NOT WORK
m_device.EndScene();
m_device.Present(); 

我想补充一点,此代码基于Microsoft DirectX示例。

1 个答案:

答案 0 :(得分:0)

您没有将顶点写入VertexBuffer。

您创建了一个名为verts的变量,并使用它来引用从Lock方法返回的数组,然后将verts引用的数组替换为返回值CurrentPanel.GetLines()。 但你实际上并没有向VertexBuffer写任何东西。