Jump to content

  • Log In with Google      Sign In   
  • Create Account


Dynamically create VertexType struct?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 AverageJoeSSU   Members   -  Reputation: 516

Like
0Likes
Like

Posted 13 February 2013 - 01:24 PM

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


Sponsor:

#2 btower   Members   -  Reputation: 906

Like
0Likes
Like

Posted 13 February 2013 - 02:07 PM

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.

Edited by btower, 13 February 2013 - 02:08 PM.


#3 AverageJoeSSU   Members   -  Reputation: 516

Like
0Likes
Like

Posted 13 February 2013 - 03:13 PM

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


#4 AverageJoeSSU   Members   -  Reputation: 516

Like
0Likes
Like

Posted 13 February 2013 - 03:24 PM

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


#5 btower   Members   -  Reputation: 906

Like
0Likes
Like

Posted 13 February 2013 - 03:30 PM

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;
}

Edited by btower, 13 February 2013 - 03:36 PM.


#6 AverageJoeSSU   Members   -  Reputation: 516

Like
0Likes
Like

Posted 13 February 2013 - 03:45 PM

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


#7 AverageJoeSSU   Members   -  Reputation: 516

Like
0Likes
Like

Posted 21 February 2013 - 05:35 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS