How to create a customVertex format/class?

Started by
4 comments, last by Armadon 18 years, 8 months ago
Hi, I'm using Managed DirectX(June update, not aug) in VC2005 Beta 2, and it works fine. I'm just wondering how I would go about creating a customVertex class? Because I need to be able to specify multiple texture coord sets, none of build in methods support this. But I can't find any documentation or tutorials on how to actually do this? Anyone able to write me a very short x,y,z type example? :) - Also, was wondering about vertex buffers. Can I dynamically increase their size, or do I have to employ a locking system to my custom mesh class and create a new vertex buffer after editing?(Editing only ever adds tris/verts, no deleation).
Advertisement
Hi there ScopeDynamo,
How are you doing?

[The problem]
I am not sure I know what you really mean but let me have a bash.
You are wanting to stipulate to direct3d that you want to give it 2 different texture coordinates per vertex.

[The possible set of solutions]
DirectX gives you a list of structures you can use in the CustomVertex class.
e.g.
[source lang = c#]			CustomVertex.PositionColored[] triangle = new CustomVertex.PositionColored[3];			triangle[0] = new CustomVertex.PositionColored(-1.0f, 0.0f, 0.0f, Color.Red.ToArgb());			triangle[1] = new CustomVertex.PositionColored(-1.0f, 2.0f, 0.0f, Color.Blue.ToArgb());			triangle[2] = new CustomVertex.PositionColored( 1.0f, 0.0f, 0.0f, Color.Yellow.ToArgb());			vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, window.D3DDevice, Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Managed);			vb.SetData(triangle, 0, LockFlags.None);


If you want to define your own type of Vertex format you can use the VertexDeclaration class and do it.

e.g.
[source lang = c#]VertexElement[] elements = new VertexElement[]{    new VertexElement(0, 0, DeclarationType.Float3,                            DeclarationMethod.Default,                            DeclarationUsage.Position, 0),                                new VertexElement(0, 12, DeclarationType.Float3,                             DeclarationMethod.Default,                             DeclarationUsage.Normal, 0),                                new VertexElement(0, 24, DeclarationType.Float2,                             DeclarationMethod.Default,                             DeclarationUsage.TextureCoordinate, 0),    new VertexElement(0, 36, DeclarationType.Float2,							 DeclarationMethod.Default,							 DeclarationUsage.TextureCoordinate, 1),                                VertexElement.VertexDeclarationEnd };VertexDeclaration decl = new VertexDeclaration(device, elements);


That will give you a new VertexDeclaration with 2 texture coordinates
You will have to clone the mesh with the new vertex format though...

e.g.
[source lang = c#]Mesh tempMesh = mesh.Clone(MeshFlags.Managed, elements, device);mesh.Dispose();mesh = tempMesh;


[The Problem] (For your Vertex Buffer Question)
Also, was wondering about vertex buffers. Can I dynamically increase their size, or do I have to employ a locking system to my custom mesh class and create a new vertex buffer after editing?(Editing only ever adds tris/verts, no deleation).

[The set of possible solutions]
When you load a mesh, a vertex buffer gets created for that mesh so that you can render the mesh. This buffer is a static buffer.
What you could do is either lock it. Retrieve the values and place it in another vertex buffer.


Hope this helps.
Keep cool.
Sorry, I'm not fermiliar enough with managed DX to help you out with the vertex structure, however, vertex buffers (and index buffers for that matter) are created with a certain size. There is no way to change the size after creation. What you could do is create a new bigger one, copy between the two, and then release the smaller one.
A simpler option would be to create a very big VB, and only use a part of it. You can just use the first X vertices, and have the rest filled with junk. If you need to add more vertices, just lock between verts Y and Z and copy in the new data, you don't need to lock the whole buffer.
Seem like the best solution there, IMO.
Sirob Yes.» - status: Work-O-Rama.
Here's a quick tutorial on defining your own FVF vertex structure:
Click!
Stay Casual,KenDrunken Hyena
Hi,

Thanks for the help. I pretty much understand it all, but one thing I'm not sure of is concerning the CustomVertex Tutorial in the last post.
I 'got it', and have impleneted a custom vertex class, but I still can't find any information of what I need to call the texture coord sets. Are they simply an array each named U,V or a multi-dimensional one? Do I need to use a int counter of some kind to indicate how many sets the format contains?

Here's my code(Mostly 'borrowed' from the last tutorial)

namespace CustomVertex    {        public struct TransformedVivid1        {            public float X;            public float Y:            public float Z;            public float Rhw;            public UInt32 Colour;                      public static readonly D3D.VertexFormats Format = D3D.VertexFormats.Transformed | D3D.VertexFormats.Diffuse;            public static readonly int StrideSize=DX.DXHelp.GetTypeSize(typeof(TransformedColored));            public TransformedVivid1(float p_x,float p_y,float p_z,float p_rhw,uint p_color)            {                X=p_x;                Y=p_y;                Z=p_z;                Rhw=0;                ColorOperator = p_color;            }        }


Also, is there a list of the enums Microsoft.DirectX.Direct3D.VertexFormats; contains? I'm guessing I have to OR in a texture set flag of some kind?
-

Hi there ScopeDynamo,

[The problem]
How to use the VertexFormats to define a VERTEX with a color, a normal and 2 texture coordinates?

[The solution]
[source lang = C#]public struct VERTEX{	float x, y, z;        float nx, ny, nz;	float u, v; //first set of texture coordinates	float u1, v1; //second set of texture coordinates	UInt32 color;public static readonly VertexFormats Format = VertexFormats.Position | VertexFormats.Normal | VertexFormats.Texture0 | VertexFormats.Texture1;}


Something like this will work,
You can call the variables whatever you like. Note that the VertexFormats Enumeration gives you a whole list of enums you can use.

The VertexFormats Documentation

The general rule is to tell direct3d how your data is arranged and just keep to the constraints.. such as:
Vertex format includes a vertex normal vector. This value cannot be used with the Transformed flag.


I hope this helps a bit.
Keep cool.

This topic is closed to new replies.

Advertisement