Dynamically create VertexType struct?

Started by
5 comments, last by AverageJoeSSU 11 years, 2 months ago

Hey all,

I am using assimp to import my models, and would like to be able to dynamically create the cpu side vertex struct.

I see examples of dynamically creating the vertex buffers and input layout, but that isnt enough correct?

i need the CPU side object to represent the data, in an array.

Perhaps I'm going about this all wrong.

I was thinking i could make a struct with a union of the possible datatypes, but that will waste space. HMMMMM

any links would be appriciated.

-J

------------------------------

redwoodpixel.com

Advertisement

Dynamically generating an input layout based on what you need doesn't need to be complex at all. See below for a quick example.

The exciting part is ensuring a specific input layout is only loaded once and used across multiple meshes. I'm not sure if that's what you were asking?


D3D11_INPUT_ELEMENT_DESC dynamicLayout[MAX_ELEMENTS_YOU_SUPPORT];
int lastIndex = 0;
aiMesh * mesh; // <- assumed that your assimp mesh is in here
// repeat the sample below for normal, texcoord, tangents, etcetera..
if (mesh->HasPosition())
{
 dynamicLayout[lastIndex].SemanticName = "POSITION";
 dynamicLayout[lastIndex].SemanticIndex = 0;
 dynamicLayout[lastIndex].Format = DXGI_FORMAT_R32G32B32_FLOAT;
 dynamicLayout[lastIndex].InputSlot = 0;
 dynamicLayout[lastIndex].AlignedByteOffset = 0;
 dynamicLayout[lastIndex].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
 dynamicLayout[lastIndex].InstanceDataStepRate = 0;
 ++lastIndex;
}
ID3D11InputLayout * layout; // output is stored in here, you need this later on when rendering
HRESULT result = d3ddevice->CreateInputLayout(dynamicLayout, lastIndex, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &layout);
if (FAILED(result))
{
 //error handling
}
// after the input layout is created, you don't need the dynamicLayout array anymore, so it can go out-of-scope or you can delete it if it was dynamically allocated.

Dynamically generating an input layout based on what you need doesn't need to be complex at all. See below for a quick example.

The exciting part is ensuring a specific input layout is only loaded once and used across multiple meshes. I'm not sure if that's what you were asking?

// after the input layout is created, you don't need the dynamicLayout array anymore, so it can go out-of-scope or you can delete it if it was dynamically allocated.

this part i have down... how would that path look with this part of the process though....


HRESULT SMesh::CreateVB(ID3D11DeviceContext* dc, ID3D11Device* device, int numVerts, SVertex* verts)
{
	HRESULT hr;
	
	ZeroMemory( &bufferDesc, sizeof(bufferDesc) );
    bufferDesc.Usage            = D3D11_USAGE_DEFAULT;
    bufferDesc.ByteWidth        = sizeof(SVertex) * numVerts;
    bufferDesc.BindFlags        = D3D11_BIND_VERTEX_BUFFER;
    bufferDesc.CPUAccessFlags   = 0;
    bufferDesc.MiscFlags        = 0;

	vertexCount = numVerts;

	ZeroMemory( &InitData, sizeof(InitData) );
    InitData.pSysMem = verts;
	InitData.SysMemPitch = 0;
	InitData.SysMemSlicePitch = 0;
	hr = device->CreateBuffer( &bufferDesc, &InitData, &vertexBuffer[0] );


	return hr;
}

particularly SVertex. or does this not have to map 1 to 1 with a layout?

------------------------------

redwoodpixel.com

OH!

I think i get it....

If you have a Vertex Buffer PER type of data, then you can just say "Do i have normals? yes? create normal VB."

Then your input layout would just do what you have posted....

But how would this then look in the shader... i guess you would have to enable the right shader that takes the inputs you supply, yes?

------------------------------

redwoodpixel.com

It does have to map 1 to 1 with the layout, indeed. But the pSysMem pointer doesn't have to be an array (or pointer) of type SVertex. You can create a byte array and fill it using the same method I used in the aforementioned example. You can use assimp's aiMesh to check if the layout has the position, normal, and so on and add them to the array of bytes in the same order as you did in the input layout.

I hope that suits your needs. Here is quick&dirty example (don't use it directly, it's just to show how it would work, I didnt try to compile it):


HRESULT SMesh::CreateVB(ID3D11DeviceContext* dc, ID3D11Device* device, aiMesh * mesh)
{
 HRESULT hr;
 
 int singleVertexSize = 3*3; // in bytes (x,y,z = 3 floats); always has position, so no need to check that
 if (mesh->HasNormals()) singleVertexSize += 3*3; // x,y,z = 3 floats
 if (mesh->HasTexCoords()) singleVertexSize += 2*3; // u,v = 2 floats
 ZeroMemory( &bufferDesc, sizeof(bufferDesc) );
    bufferDesc.Usage            = D3D11_USAGE_DEFAULT;
    bufferDesc.ByteWidth        = singleVertexSize * mesh->mNumVertices;
    bufferDesc.BindFlags        = D3D11_BIND_VERTEX_BUFFER;
    bufferDesc.CPUAccessFlags   = 0;
    bufferDesc.MiscFlags        = 0;
 vertexCount = mesh->mNumVertices;
 char * pmem = new char[singleVertexSize];
 float * fp;
 for (int i = 0; i < mesh->mNumVertices; ++i)
 {
  fp = (float*)pmem;
  *fp = mesh->mVertices[i].x;
  *(fp+1) = mesh->mVertices[i].y;
  *(fp+2) = mesh->mVertices[i].z;
  if (mesh->HasNormals())
  {
   *(fp+3) = mesh->mNormals[i].x;
   *(fp+4) = mesh->mNormals[i].y;
   *(fp+5) = mesh->mNormals[i].z;
  }
  if (mesh->HasTexCoords())
  {
   *(fp+6) = mesh->mTexCoords[i].u;
   *(fp+7) = mesh->mTexCoords[i].v;
  }
  pmem += singleVertexSize;
 }
 
 //ZeroMemory( &InitData, sizeof(InitData) );   not really needed, you fill all members below explicitly..
 InitData.pSysMem = pmem;
 InitData.SysMemPitch = 0;
 InitData.SysMemSlicePitch = 0;
 hr = device->CreateBuffer( &bufferDesc, &InitData, &vertexBuffer[0] );
 delete [] pmem;
 return hr;
}

Ah yes i see...

So i suspect that those IF checks are more important when you want to apply a "material" to a given mesh.

for example if your material/shaders need normals then this "mesh->HasNormals()" almost becomes an assert.

------------------------------

redwoodpixel.com

For anyone that looks this up, pmem points to the end of the array after looping, and the size of pmem should be sizeOfVertex * numberOfVertices.

that and using sizeof(float) is all i could find wrong with the posted code.

------------------------------

redwoodpixel.com

This topic is closed to new replies.

Advertisement