Dynamic mesh creation with multiple textures

Hi... you may remember me from prior threads like "How do I turn on my computer" and "Eek!  There's a mouse on my desk!"

I am attempting to create a mesh that is two sub meshes - one for each side of a playing card - because each side uses a different texture.  That is, I have a deck texture with the various possible backs, and a face texture.

It seems like this is something very doable using the SetAttributeTable, but after trying literally a dozen different ways I cannot get it to work as expected

I created 2 AttributeRange structs with the data about my two meshes (2 triangles to a side - nothing fancy) and called the SetAttributeTable function.  It did not fail, and if I call GetAttributeTable the data is there as I specified, but it doesn't affect the mesh !   The mesh draws correctly, but it is only 1 submesh.    I am renderring it as shown below.

device.SetTexture(0, m_texFace);
m_meshCard.DrawSubset(0);
device.SetTexture(0, m_texDeck);
m_meshCard.DrawSubset(1);

The DrawSubset(0) is doing all the work and thus only the first texture is used drawing the card with the wrong texture.  I can comment out the last two lines and it acts the same.

I tried OptimizeInPlace after setting the attributes.  I tried LockAttributeTable and LockAttributeBuffer.  I tried modifying all the data in the attribute range struct as well.  Nothing seems to affect the created mesh!

Here is the code I used for the attribute table.

 short[] nIndices = {0, 1, 2, /* Front face - left */ 1, 3, 2, /* Front face - right */
   4, 5, 6, /* Back face - left */  5, 7, 6 /* Back face - right */};

 m_meshCard = new Mesh(4, 8, MeshFlags.Managed, CustomVertex.PositionNormalTextured.Format, device);

 // Because we are using two different textures fo this mesh - deck and face - we have to
 // have two subsets in mesh.
 AttributeRange[] attrCard = new AttributeRange[2];

 // Represents the front of the card   SubMesh 0
 attrCard[0].AttributeId = 0;
 attrCard[0].FaceCount   = 2;
 attrCard[0].FaceStart   = 0;
 attrCard[0].VertexCount = 6;
 attrCard[0].VertexStart = 0;

 // Represents the back of the card    SubMesh 1
 attrCard[1].AttributeId = 1;
 attrCard[1].FaceCount   = 2;
 attrCard[1].FaceStart   = 2;
 attrCard[1].VertexCount = 6;
 attrCard[1].VertexStart = 6;

 m_meshCard.SetAttributeTable(attrCard);

 // Code for setting the vetices and index buffer left out

Any ideas what piece of the puzzle I am missing   Thanks in advance!!

Sorry for the novel, but I wanted to show that I made real attempts to solve this on my own.




Answer this question

Dynamic mesh creation with multiple textures

  • bjoseph954

    Thanks for the help. It is not quite the same for C# using Managed DirectX, but I managed to find it using your information!

    Here is the code that worked for me:

    // First two faces are submesh 0 and second two faces are submesh 1
    int[] nAttributes = {0, 0, 1, 1};

    m_meshCard.LockAttributeBufferArray(LockFlags.Discard);
    m_meshCard.UnlockAttributeBuffer(nAttributes);

    Of course this is within a try/catch to handle exceptions.



  • cGeorge

     

    I did the same mistake :-)

    It's easier then what you expect...

    First, you don't have to ''Set'' anything...apparently the Attrib buffer allways exist

    (if you create your mesh with more then one material I suppose)

    And then the AttribRange is for something else...not really what you need

    You don't need to optimize either...

    So you do exactly like you do for the pVB and pIB (vertex buffer and index buffer)

    You use your mesh pointer, you lock the pAB (attribute buffer)

    You copy a table of DWORD into it, and you call Unlock...

    The DWORD TableAttrib[1000]; should contain as many entry has you have Faces

    You copy a pMesh->GetNumberOfFaces() number of faces into it...

    In your case you set 0 for the faces on one side and 1 for the faces on the other side

    TableAttrib[0]=0;
    TableAttrib[1]=0;
    TableAttrib[2]=0;
    TableAttrib[3]=0;
    TableAttrib[4]=0;
    TableAttrib[5]=1;
    TableAttribDevil=1;
    TableAttrib[7]=1;
    TableAttribMusic=1;

    Each DWORD value is for 1 faces...in the pIB you had 3 index value by faces
    here it's ONE by Faces...

    HRESULT LockAttributeBuffer(
      DWORD Flags,
      DWORD ** ppData
    );

    It's totally easy :-)


  • Terkamian

    Since the AttributeRange is not working for me, I created two meshes - one for the front of the card and one for the back. This works, but is certainly not the proper solution!

    Can anyone think of why setting the attributes of the mesh would not work

    Thanks.



  • rabram

    I'm glad I could help :-)

    I will learn C# someday :-)


  • JBDevoArch

    Thank you. I look forward to trying it tonight, if I can find some free time!

  • Dynamic mesh creation with multiple textures