# MDX for .NET 2.0 (How to use GraphicsBuffer)

## Recommended Posts

Hi, Here is the peace of code I try to make work :
    // create the vertex and index buffer
VertexBuffer  ^ vertexBuffer  = gcnew VertexBuffer(m_D3DDevice, 4 * sizeof(CustomVertex::PositionColored), Usage::None, CustomVertex::PositionColored::Format, Pool::Managed, nullptr);
IndexBuffer   ^ indexBuffer   = gcnew IndexBuffer(m_D3DDevice, 6 * sizeof(short), Usage::None, Pool::Managed, true, nullptr);

// try to fill the vertex buffer >_<
// first, I lock it to a GraphicsBuffer (no doc on that one :/)
GraphicsBuffer ^ lockedBuffer = vertexBuffer->Lock(0, 0, LockFlags::None);

// the lockedBuffer->Write() needs a cli::array<unsigned char, 1> ^ as a parameter, so I create on, big enough for all the vertices
cli::array<unsigned char> ^ vertices = gcnew cli::array<unsigned char> (4 * sizeof(CustomVertex::PositionColored));

// and here is the problem : How do I fill this cli::array now ????
// ....

// write the content into the buffer and unlock
lockedBuffer->Write(vertices);
vertexBuffer->Unlock();

// do the same for the indices (if I can do it for the vertices, it's the same)

// set my buffers and draw
m_D3DDevice->VertexFormat = CustomVertex::PositionColored::Format;
m_D3DDevice->Indices      = indexBuffer;
m_D3DDevice->SetStreamSource(0, vertexBuffer, 0);
m_D3DDevice->DrawIndexedPrimitives(PrimitiveType::TriangleList, 0, 0, 4, 0, 2);


So, my problem is : 1) is it the correct method to fill a buffer with managed DirectX, C++, and with the 2.0 runtime ? 2) if it's the correct method, then, how do I fill this cli::array ??? It's unsigned char, because the GraphicsBuffer::Write() method requires unsigned chars !! So how can I add my vertices ? Thx in advance for any answer / pointer to a good tutorial / whatever could help me ^^ (And i'm using managed C++, not C#. And .NET 2.0, not 1.X !) [Edited by - paic on March 16, 2006 8:36:45 AM]

##### Share on other sites
The graphics buffer should be a generic, in MDX2.0. I'm assuming you're referencing version 2.0 of MDX, though you didn't mention.

I use:

GraphicsBuffer<MyVertexStruct> gBuffer;
gBuffer = vBuffer.Lock<MyVertexStruct>(....);

and then write each vertex using: gBuffer[i] = whatever;

Also note, there seem to be two versions of the GraphicsBuffer. You need the one that is in the Generics namespace.

Hope this helps.

##### Share on other sites
O_o
Thx 1000 times !
And do you mind telling me where did you find this information ????? (I never thought there would be 2 versions of GraphicsBuffer !)

##### Share on other sites
I got tipped off by someone, though I can't quite remember who it was. One of the regulars here - so I bet he's reading this. Thanks again :).

Anyhow, I understood this feature was added in one of the later releases of MDX2.0, so someone who's been following the release info would see that they added it.

Also, if you're interested, the non-generic GraphicsBuffer should have a GetBuffer<>() method, that returns a generic buffer with the type you give it, so that's an option too (I was using this before I found out about the newer type).

Hope this helps :).

##### Share on other sites
It's still prerelease I think so they haven't fully documented it. Remember the generic version of GraphicsBuffer is in DirectX::Generic.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628320
• Total Posts
2982072

• 21
• 9
• 9
• 13
• 11