# Applying Alpha to existing material

## Recommended Posts

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[i] ); Device->SetTexture(0, Textures[i]); 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.

##### Share on other sites
Quote:
 Original post by andrew_wwCan 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.

##### Share on other sites

Thanks very much. I put your code within the loop and all seems o be working.

##### Share on other sites
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

## 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

• ### Forum Statistics

• Total Topics
628383
• Total Posts
2982384

• 10
• 9
• 15
• 24
• 11