Jump to content
  • Advertisement
Sign in to follow this  
Khaos Dragon

[MDX 2.0] Vertex Buffers

This topic is 4604 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am wondering if anybody has any examples of how to use vertex,index, and graphics buffers in the new MDX 2.0 Beta. In all the examples it seems as if I am forced to use the old VertexFormat (modeled after the old flexible vertex format macros) rather than custom vertex declarations. If I use a vertex declaration instead of a vertex format, is there anyway to do anything?

Share this post


Link to post
Share on other sites
Advertisement
I assume that you know the basic works of these items so I don't have to go into that. MDX 2.0 uses generics and GraphicsBuffers.

To create a custom vertex create a structure.

struct Vertex
{
Vector4 position;
Vector3 normal;
Vector2 texture;

public Vertex(float x, float y, float z, float nx, float ny, float nz, float tu, float tv)
{
this.position = new Vector4(x, y, z, 1);
this.normal = new Vector3(nx, ny, nz);
this.texture = new Vector2(tu, tv);
}

public static VertexFormats Format
{
get
{
return VertexFormats.PositionW | VertexFormats.Normal | VertexFormats.Texture1;
}
}

}

After that you should create a vertex declaration which takes an array of vertex elements. It look like this in this case.

VertexElement[] elements = new VertexElement[]
{
new VertexElement(0, 0, DeclarationType.Float4,
DeclarationMethod.Default,
DeclarationUsage.Position, 0),

new VertexElement(0, 16, DeclarationType.Float3,
DeclarationMethod.Default,
DeclarationUsage.Normal, 0),

new VertexElement(0, 28, DeclarationType.Float2,
DeclarationMethod.Default,
DeclarationUsage.TextureCoordinate, 0),

VertexElement.VertexDeclarationEnd
};

In a vertex element the parameters are like this:
1. Forgot what it is :)
2. The byte offset, every float is 4 bytes so a float4 is 16 bytes. The next element will have an offset of 16 bytes.
3. The method should mostly be default.
4. The usage should indicate what the data is used for.
5. The stream number. For example if you use multiple texture coordinates the first one will have stream number 0 and the second one stream number 1.

Don't forget to end the element list with a VertexElement.VertexDeclarationEnd!

Finally creating the declaration is easy:

VertexDeclaration decl = new VertexDeclaration(device, elements);

Next is the vertexbuffer which uses generics. Between the brackets is the type of vertex you want to create in this case it is the custom vertex format we just created.

VertexBuffer.CreateGeneric<Vertex> = VertexBuffer.CreateGeneric<Vertex>(device, 4, Usage.None, Vertex.Format, Pool.Managed, null);

To fill this buffer lock it to get a graphics buffer.

GraphicsBuffer<Vertex> data = vertexBuffer.Lock<Vertex>(0, 0, LockFlags.Discard);

Fill it with 4 new vertices.
data[0] = new Vertex(-500f, 0f, -500f, 0f, 1f, 0f, 0f, 1f);

data[1] = new Vertex(-500f, 0f, 500f, 0f, 1f, 0f, 0f, 0f);

data[2] = new Vertex(500f, 0f, -500f, 0f, 1f, 0f, 1f, 1f);

data[3] = new Vertex(500f, 0f, 500f, 0f, 1f, 0f, 1f, 0f);

Unlock the buffer to save changes:
this.vertexBuffer.Unlock();

An indexbuffer is created in the same way, instead of the vertex format you use an int.

To use this in rendering set the vertex and index buffer as usual, but don't forget to set the device.VertexDeclaration to the declaration you just made.

Have fun, hopes this helps!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!