Jump to content
  • Advertisement
Sign in to follow this  
prometheus999

Managed Direct3D: Materials just won't behave

This topic is 4788 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

I used the example code in for materials in a cool 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 rather than each of them having their own material, which is what I want. Which material is used seems to depend on which object is instatiated last. I think the problem is with the SetupMaterials method of my GameObject class: protected void SetupMaterials() { Material mat = new Material(); // Set the properties of the material // The object itself will be blue mat.Diffuse = Color.Blue; // We want it to look slightly dull, so maybe a grey // wide highlight mat.Specular = Color.LightGray; mat.SpecularSharpness = 15.0F; device.Material = mat; // Very important - without this there is no specularity device.RenderState.SpecularEnable = true; } 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
Advertisement
I don't know much about Managed D3D, but I think the problem is with the device.Material = mat. Are you doing this once before calling Draw*Primitive() or only at material init time? You need to set the right material in the device just before you make the draw call, if you do that at material init, the device will always use the one you initialized last :)

Share this post


Link to post
Share on other sites
That's exactly what was happening. I was able to fix the problem. But That doesn't seem to be the problem with my tweakage to the wiki's mesh example.

http://pluralsight.com/wiki/default.aspx/Craig.DirectX/MeshBasicsTutorial.html

I make the textures and materials members of a GameObject class and get a reference to the device. It displays one mesh just fine, even if I make more than one GameObject object it displays fine. But the meshes go totally black save where there are textures if I call more than one object's render method.

Is there anything wrong with this method?

public void RenderMesh()
{
for (int i = 0; i < materials.Length; ++i)
{
if (textures != null)
{
dev.SetTexture(0, textures);
}


dev.Material = materials;

mesh.DrawSubset(i);
}
}

Share this post


Link to post
Share on other sites
I changed the render method to this:


public void RenderMesh()
{
for (int i = 0; i < materials.Length; ++i)
{
if (textures != null)
{
dev.SetTexture(0, textures);
}
if (keepGoing++ < 20) //press okay two times
MessageBox.Show("are we working here?");
dev.Material = materials;

mesh.DrawSubset(i);
}
}

The first couple of times I press okay I see nothing, then I see one of the models with materials intact. After 5 or more okay presses, it varies, niether one has it's materials.

urgh.

Share this post


Link to post
Share on other sites
Problem solved. But I'm not sure why this makes a difference. I take out the if (textures != null) statement and always execute device.SetTexture(0, textures); 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
In general, using a texture or not depends on the pixel shader you set, or if you are using the fixed function pipeline, the texture stage states. For example, if you don't want to use a texture, but only the material color, you'll something like:

device.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1)
device.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_DIFFUSE)

And for textured meshes (vertex color * texture):

device.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE)
device.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE)
device.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE)

It probably doesn't compile with managed DX, but there is a way to do that. Hard to say why it doesn't work with the "if"..

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!