Jump to content
  • Advertisement
Sign in to follow this  
WedgeMan

Dynamically Choosing Vertex Format

This topic is 2570 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

Hi All,

So this questions is more of a C++ architecture question but it's directly related to DirectX11.

In all the tutorials, they have you define a struct for your vertex format along the lines of:



struct VertexFormat {
XMFLOAT3 position;
XMFLOAT3 color;
};




And then when you create your Vertex Buffer you need to use that struct to fill out the ByteWidth of the D3D11_BUFFER_DESC and create an array of VertexFormat objects like so:


D3D11_BUFFER_DESC vertexBufferDesc;
vertexBufferDesc.ByteWidth = sizeof(VertexFormat);




VertexFormat* vertices = new VertexFormat[numVertices];





So this is all good and fine when following a tutorial, but eventually we'll want to make something a bit more modular and dynamic.

Depending on the Material used, VertexBuffers are going to use a different VertexFormat struct. Some will have normals, uv's for various textures etc.


My question is really how to make a generic function that does the work but will operate on a variety of VertexFormat objects. These VertexFormat objects wouldn't be known until runtime so I don't think templates would work.

If I were to write this in a dynamic language I would do something like this: (C++ syntax still)


//Note this function for illustration purposes only. I know it doesn't actually do anything.
void AbstractMaterial::CreateVertexBuffer() {
D3D11_BUFFER_DESC vertexBufferDesc;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER];
vertexBufferDesc.ByteWidth = sizeof(myDerivedVertexFormat); //TODO: myDerivedVertexFormat is a variable that contains the TYPE of VertexFormat struct


vertices = new myDerivedVertexFormat[numVertices]; //Fill in vertices from raw data...

D3D11_SUBRESOURCE_DATA vertexBufferData;
vertexBufferData.pSysMem = vertices;
vertexBufferData.SysMemPitch = 0;
vertexBufferData.SysMemSlicePitch = 0;

//Finish creating vertex buffer }




Seems to me like the limitation of Run Time Type Checking in C++ will prevent this from happening. The only thing I can really think of is manually reimplementing the function in every derived material... but come on, that sounds terrible!

There must be a better way?
Is there?

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Your example isn't a realistic one. In practice it is very rare that you will write code to allocate memory for vertices and manually fill in the vertex data. Usually you will be loading in vertex data that's been exported from a modeling package, in which case you just need enough info to create the input layout and then you can just create the vertex buffer from the raw block of vertex data. Typically this data will somehow be embedded in the data you load from file. So ultimately you don't actually need to know or care about the format of the data inside the raw vertex buffer, you just need to create the input layout and provide the proper parameters when creating the vertex buffer. This means you can just use a simple void* pointer for your vertex data.

For the cases where you do manually fill out a vertex buffer (for instance, making a debug sphere or cube), then you can just use a pre-defined structure.

Share this post


Link to post
Share on other sites
Ahhhh, that's really the key, I was under the impression it had to be an array of structs. Which now of course I see it was a struct just for convenience. I can just specify the "format" the memory is in each derived material and not have to worry about storing it in a struct at all.

Thanks for clearing that up!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!