• Advertisement
Sign in to follow this  

Different textures for one mesh

This topic is 3610 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, I am new in DirectX programming. I use MDX with C#. Creating meshes (box) with one texture is easy. But how can I set different textures for every side of the box? For example: I have a cube and want to set a colored texture for every side. So I need 6 textures. How do I tell MDX where to put the textures on the cube? Thank you for your help and sorry for my English :)

Share this post


Link to post
Share on other sites
Advertisement
you need to create different subset, each for a different texture.
Take a look at this . It's C++ but it shouldn't be a problem to translate.

At render time, all you have to do is to cylce through the mesh's subsets and set its texture... something like this (warning: pseudo-code ahead)

for(uint i=0; i!= numTextures;++i)
{
device.SetTexture(pTextureList);
mesh->DrawSubset(i);
}



in your case numTextures is 6.

Hope this helps :)


Share this post


Link to post
Share on other sites
@b3rs3rk
Your idea sounds very interesting :)
I have some problems in translating the C++ code to managed directx in C# because I cannot find some methods.

I create my mesh with:

texturedBox = Mesh.Box(device, x, y, z);
Mesh tempBox = texturedBox.Clone(texturedBox.Options.Value,VertexFormats.PositionNormal | VertexFormats.Texture1, device);
texturedBox.Dispose();
texturedBox = tempBox;



I can't find the "GetFaceCount" method in "texturedBox". Is it possible that it does not exist in MDX?

[Edited by - benben83 on April 7, 2008 6:46:49 AM]

Share this post


Link to post
Share on other sites
I don't remeber the c# syntax but i'm pretty sure there's a method to get the number of indices.... assuming you're using triangle lists, you should have that
faceCount = numIndices / 3 :)

Share this post


Link to post
Share on other sites
Okay I have found it ;)

But there still seems to be a problem...

I create the subset (only for testing):

int[] abuffer = texturedBox.LockAttributeBufferArray(LockFlags.None);

for (int i = 0; i < texturedBox.NumberFaces; i++)
{
abuffer = i + 1;
}

texturedBox.UnlockAttributeBuffer();





The new part of the render method:

int tempIndex = 0;

for (int k = 1; k <= box.TexturedBox.NumberFaces; k++)
{
Texture texture = textures.GetTexture(tempIndex);
device.SetTexture(0, texture);
box.TexturedBox.DrawSubset(k);
tempIndex = (tempIndex + 1) % 5;
}





I use tempIndex only for testing the subsets. I have a list of five textures in my "Textures"-Object. My problem is that the box is invisible. No texture is rendered. If a use box.TexturedBox.DrawSubset(0) the whole box gets one texture.

I use following code for mapping the texture on the box. I think here is my mistake:

//left
verts[0].Tu = 0f; verts[0].Tv = 0f;
verts[1].Tu = 1f; verts[1].Tv = 0f;
verts[2].Tu = 1f; verts[2].Tv = 1f;
verts[3].Tu = 0f; verts[3].Tv = 1f;

//right
verts[8].Tu = 0f; verts[8].Tv = 0f;
verts[9].Tu = 1f; verts[9].Tv = 0f;
verts[10].Tu = 1f; verts[10].Tv = 1f;
verts[11].Tu = 0f; verts[11].Tv = 1f;

//top
verts[4].Tu = 0f; verts[4].Tv = 0f;
verts[5].Tu = 1f; verts[5].Tv = 0f;
verts[6].Tu = 1f; verts[6].Tv = 1f;
verts[7].Tu = 0f; verts[7].Tv = 1f;

//bottom
verts[12].Tu = 0f; verts[12].Tv = 0f;
verts[13].Tu = 1f; verts[13].Tv = 0f;
verts[14].Tu = 1f; verts[14].Tv = 1f;
verts[15].Tu = 0f; verts[15].Tv = 1f;

//front
verts[20].Tu = 0f; verts[20].Tv = 0f;
verts[21].Tu = 1f; verts[21].Tv = 0f;
verts[22].Tu = 1f; verts[22].Tv = 1f;
verts[23].Tu = 0f; verts[23].Tv = 1f;

//back
verts[16].Tu = 0f; verts[16].Tv = 0f;
verts[17].Tu = 1f; verts[17].Tv = 0f;
verts[18].Tu = 1f; verts[18].Tv = 1f;
verts[19].Tu = 0f; verts[19].Tv = 1f;





[Edit: The box has 12 faces and I put every face in an own attribute group.]

[Edit2: I found the problem. I have to use texturedBox.UnlockAttributeBuffer(aBuffer) instead of using texturedBox.UnlockAttributeBuffer() without an parameter :)]

[Edited by - benben83 on April 7, 2008 7:07:56 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement