• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AverageJoeSSU

Dynamically create VertexType struct?

6 posts in this topic

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

 

 

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0