Sign in to follow this  
prometheus999

[.net] Materials go black if I display more than one mesh

Recommended Posts

prometheus999    133
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); } }

Share this post


Link to post
Share on other sites
prometheus999    133
Okay, here's an update on my progress. I used the example code in for materials in the directx wiki. Then I try to encapsulate this into an object so all I have to do is transform and then render the primitive. If I make the SetUpMaterials method a method of this new object and call it durning the constructor like so:

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

Share this post


Link to post
Share on other sites
prometheus999    133
Problem solved. But I'm not sure why this makes a difference. I take out the if (textures[i] != null) statement and always execute device.SetTexture(0, textures[i]); I can render as many meshes as I like. But I think this means that there would be problems if I wanted to render a mesh without a texture.

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