I'm using the cool example from the managed directx wiki.
http://pluralsight.com/wiki/default.aspx/Craig.DirectX/MeshBasicsTutorial.html
I created an object that took the mesh name and the device as arguments and have their own render methods. It works perfectly if I only make and render one Mesh but if I render two the materials seem to disapear, every surface of the mesh is totally black except for where there are textures, those parts render fine. What gives
Here's the bit of the constructor which has to do with the mesh:
ExtendedMaterial[] exMaterials;
mesh = Mesh.FromFile(Path.Combine(artPath, name), MeshFlags.SystemMemory,
device, out exMaterials);
if (textures != null)
{
DisposeTextures();
}
textures = new Texture[exMaterials.Length];
materials = new Material[exMaterials.Length];
for (int i = 0; i < exMaterials.Length; ++i)
{
if (exMaterials[ i ].TextureFilename != null)
{
string texturePath = Path.Combine(artPath,
exMaterials[ i ].TextureFilename);
textures[ i ] = TextureLoader.FromFile(device, texturePath);
}
materials[ i ] = exMaterials[ i ].Material3D;
materials[ i ].Ambient = materials[ i ].Diffuse;
}
and here's the render method:
public void RenderMesh()
{
for (int i = 0; i < materials.Length; ++i)
{
if (textures[ i ] != null)
{
someDevice.SetTexture(0, textures[ i ]);
}
device.Material = materials[ i ];
mesh.DrawSubset(i);
}
}

in managed directx, Materials are black if I create more than one mesh
fawkes
Renderstates are the current state of the rendering process - think of them as global varaibles that the GPU understands. e.g from now on draw in wireframe, from now on use this material, from now on blend textures like this. Notice they are always 'from now on'.
So in your previous code sample, if you unroll the loops and calls to 'render' you get this:
if (tX !=null) settexture tX
drawsubset mX
if (tY != null) settexture tY
drawsubset mY
if (tZ != null) settexture tZ
drawsubset mZ
etc many times.
if tY== null then you do not call set texture. Therefore mY gets drawn with the texture that was last set i.e. setTexture tX. But mY was never expected to be drawn with a texture so its texture coordiates are wrong/undefined/not set/zeroed (one or more I don't know). By taking away the if statement you called setTexture null, which says 'from this point forward don't bother looking up the texture coordinates'.
Does that make better sense
MonkWebs
By checking for null before running that line of code you were not turning the texture 'off' for the parts of the mesh that didn't have textures. If a mesh subset is texture free, odds are its texture coordinates are not set (i.e. they are 0) so the texture lookup ends up sampling some odd bit of your texture for every vertex..... black bits on the mesh.
SetTexture is like any renderstate, it stays in effect from that point on until you cahnge or disable it.
bvrkchowdary
IanMixxxqqq
What exactly is a renderstate You can point me to an article about it if you like.
Tall Dude
Mail me your project (no binaries) zman@thezbuffer.com, I can't see anything obvious in your code and its always easier in the debugger anyway.
djdekker
Chen_mcc
FA65
public GameObject(string color, Device device)
{
gameVertices = CreateVertexBuffer(device);
SetupMaterials(color, device);
}
both primitives have the same material. Which material it chooses seems to depend on which object is instatiated last.
link to article in question.
http://pluralsight.com/wiki/default.aspx/Craig.DirectX/MaterialsBasicsTutorial.html