I’ve got problem with DirectX in C#. I want to draw some lines. Firstly I’ve done it with DrawUserPrimitives and all is fine. But then I switched to vertexBuffer because I want to make rotations and other camera action. And I can’t see nothing in the window.
There is part of code that fills vertexBuffer and draw verts on a plane.
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();
I would like to add that this code is based on Microsoft DirectX samples.
You did not write the vertices to the VertexBuffer.
You created a variable named
vertsand used it to reference the Array that is returned from theLockmethod, then you replaced the array referenced byvertsto be the return value fromCurrentPanel.GetLines().But you didn’t actually write anything to the VertexBuffer.