Applying Alpha to existing material

Started by
2 comments, last by programmer_tom 16 years, 3 months ago
Hello, I'm trying to follow a tutorial on alpha (blending) and I'm having trouble applying the techniques to my own project. The trouble is the example creates a new material which is then applied to the object. My project uses an x file which already has a material applied to it. Can this existing material be modified within directx ? This is the code that is drawing the x fie object: for(int i = 0; i < Mtrls.size(); i++) { Device->SetMaterial( &Mtrls ); Device->SetTexture(0, Textures); Mesh->DrawSubset(i); } and this is code that is from the tutorial: TeapotMtrl = d3d::RED_MTRL; TeapotMtrl.Diffuse.a = 0.5f; // set alpha to 50% opacity Code that draws it: Device->SetMaterial(&TeapotMtrl); Is what I'm trying to do actually possible? Should I create a new material which has no color then run the SetMaterial() function twice? Thanks again.
Advertisement
Quote:
Original post by andrew_ww
Can this existing material be modified within directx?


Sure, why not? For example, to modify the first material of the mesh you would write:
Mtrls[0].Diffuse.a = 0.5f;
Of course, you'll probably want to do this for all the mesh's materials.

Thanks very much. I put your code within the loop and all seems o be working.
Unless all the materials are the same in the mesh, you want to call SetMaterial() before each DrawSubset() call (same thing goes for textures). How you set the material values is up to you.

Note that if you're using the pipeline to draw, i believe you need to set the ambient.a as well as the diffuse.a for transparency to work (and of course have the render states etc. set up right).

Also note that your transparent subsets are gonna be rendered in the order they appear in the .x file unless you manually sort them back to front (which will result in incorrect ordered alpha blending in some cases).

-programmer_tom

This topic is closed to new replies.

Advertisement